import { IAppState } from '../store' ; import { IEntriesState , IGetEntriesParams } from '../store/entries' ; export const getAuthHeader = ( getState ) => { const { token } = getState ( ) . auth ; return { headers : { Authorization : token , } , } ; } ; export const shortenLongName = ( name , maxChars ) => name . length > maxChars ? `${ name . slice ( 0 , maxChars ) }...` : name ; export const capitalizeFirstLetter = ( text ) : string => text [ 0 ] . toUpperCase ( ) + text . slice ( 1 ) ; const beautifySort = ( sort ) => capitalizeFirstLetter ( sort ) ; const beautifyType = ( type ) => `${ capitalizeFirstLetter ( type . replace ( 'supergroup' , 'group' ) ) }s` ; const beautifyCat = ( category ) => capitalizeFirstLetter ( category . replace ( '-' , '/' ) ) ; export const wait = ( ms ) => new Promise ( resolve => { setTimeout ( resolve , ms ) ; } ) ; export const getOpenLink = ( username ) => ( e ) => { e . stopPropagation ( ) ; window . open ( `https://t.me/${ username }` , '_blank' ) ; window . focus ( ) ; } ; export const hasAd = ( text ) => /((http(s)?(\:\/\/))?(www\.)?([\w\-\.\/])*(\.[a-zA-Z]{2,3}\/?))[^\s\b\n|]*[^.,;:\?\!\@\^\$ -]/gi . test ( text ) || /(@\w+)/gi . test ( text ) ; export const areParamsEqual = ( params , state ) : boolean => params . limit <= state . limit && state . data [ params . type ] [ params . sort ] . length && ( params . category || state . category ? params . category === state . category : true ) ; export const getParamsFromQueries = ( { category , sort , type , search , } ) : IGetEntriesParams [ ] => { const searchParam = search && { search } ; if ( category && sort && type ) { return [ { category , limit : 18 , sort , type } ] ; } if ( sort && type ) { return [ { limit : 18 , sort , type } ] ; } if ( type && ! category ) { return [ { limit : 9 , sort : 'top' , type } , { limit : 9 , sort : 'hot' , type } , { limit : 9 , sort : 'new' , type } , ] ; } if ( category && ! type ) { return [ { category , limit : 9 , sort : 'top' , type : 'channel' } , { category , limit : 9 , sort : 'top' , type : 'bot' } , { category , limit : 9 , sort : 'top' , type : 'supergroup' } , ] ; } if ( category && type ) { return [ { category , limit : 9 , sort : 'top' , type } , { category , limit : 9 , sort : 'hot' , type } , { category , limit : 9 , sort : 'new' , type } , ] ; } if ( sort ) { return [ { ... searchParam , limit : 9 , sort , type : 'channel' } , { ... searchParam , limit : 9 , sort , type : 'bot' } , { ... searchParam , limit : 9 , sort , type : 'supergroup' } , ] ; } return [ ] ; } ; export const getViewMoreLinkURL = ( { category , sort , type , } ) : string => { switch ( true ) { case ! ! category && ! ! sort && ! ! type : return `${ type }/${ category }/${ sort }` ; case ! ! sort && ! ! type : return `${ type }/${ sort }` ; case ! ! type && ! category : return type ; case ! ! category && ! type : return category ; case ! ! category && ! ! type : return `${ type }/${ category }` ; case ! ! sort : return sort ; default : return '' ; } } ; export const getPageTitle = ( { category , sort , type , } ) : string => { switch ( true ) { case ! ! category && ! ! sort && ! ! type : return `${ beautifySort ( sort ) } Telegram ${ beautifyCat ( category ) } ${ beautifyType ( type ) }` ; case ! ! sort && ! ! type : return `${ beautifySort ( sort ) } Telegram ${ beautifyType ( type ) }` ; case ! ! type && ! category : return `Top Telegram ${ beautifyType ( type ) }` ; case ! ! category && ! type : return `Top Telegram ${ beautifyCat ( category ) } Channels, Bots and Groups` ; case ! ! category && ! ! type : return `Top Telegram ${ beautifyCat ( category ) } ${ beautifyType ( type ) }` ; case ! ! sort : return `${ beautifySort ( sort ) } Telegram Channels, Bots and Groups` ; default : return 'Top Telegram Channels, Bots and Groups' ; } } ;