import { Location } from 'history' ; import { match , matchPath , generatePath } from 'react-router' ; import { isNumber } from '@proton/shared/lib/helpers/validators' ; import { getSearchParams , changeSearchParams } from '@proton/shared/lib/helpers/url' ; import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants' ; import { Sort , Filter , SearchParameters } from '../models/tools' ; import { getHumanLabelID } from './labels' ; import { MAIN_ROUTE_PATH } from '../constants' ; export type MailUrlParams = { labelID : string ; elementID ? : string ; messageID ? : string ; } ; export const getUrlPathname = ( params ) => generatePath ( MAIN_ROUTE_PATH , { ... params , labelID : getHumanLabelID ( params . labelID ) , } as any ) ; export const setParamsInLocation = ( location , params ) : Location => ( { ... location , pathname : getUrlPathname ( params ) , } ) ; export const setParamsInUrl = ( location , params ) : string => getUrlPathname ( params ) + location . search ; export const getParamsFromPathname = ( pathname ) : match < MailUrlParams > => matchPath ( pathname , { path : MAIN_ROUTE_PATH } ) as match < MailUrlParams > ; const stringToPage = ( string ) : number => { if ( string === undefined ) { return 0 ; } const pageNumber = parseInt ( string , 10 ) ; if ( ! Number . isNaN ( pageNumber ) ) { return pageNumber - 1 ; } return 0 ; } ; const stringToSort = ( string , labelID ? ) : Sort => { const isScheduledLabel = labelID && labelID === MAILBOX_LABEL_IDS . SCHEDULED ; switch ( string ) { case '-size' : return { sort : 'Size' , desc : true } ; case 'size' : return { sort : 'Size' , desc : false } ; case 'date' : return { sort : 'Time' , desc : ! ! isScheduledLabel } ; default : return { sort : 'Time' , desc : ! isScheduledLabel } ; } } ; const stringToInt = ( string ) : number | undefined => { if ( string === undefined ) { return undefined ; } return isNumber ( string ) ? parseInt ( string , 10 ) : undefined ; } ; export const sortToString = ( sort ) : string | undefined => sort . sort === 'Time' ? ( sort . desc ? undefined : 'date' ) : sort . desc ? '-size' : 'size' ; const stringToFilter = ( string ) : Filter => { switch ( string ) { case 'read' : return { Unread : 0 } ; case 'unread' : return { Unread : 1 } ; default : return { } ; } } ; export const filterToString = ( filter ) : string | undefined => filter . Unread === undefined ? undefined : filter . Unread === 0 ? 'read' : 'unread' ; export const keywordToString = ( keyword ) : string | undefined => { const trimmed = keyword . trim ( ) ; return trimmed || undefined ; } ; export const pageFromUrl = ( location ) => stringToPage ( getSearchParams ( location . hash ) . page ) ; export const sortFromUrl = ( location , labelID ? ) => stringToSort ( getSearchParams ( location . hash ) . sort , labelID ) ; export const filterFromUrl = ( location ) => stringToFilter ( getSearchParams ( location . hash ) . filter ) ; export const extractSearchParameters = ( location ) : SearchParameters => { const { address , from , to , keyword , begin , end , attachments , wildcard } = getSearchParams ( location . hash ) ; return { address , from , to , keyword , begin : stringToInt ( begin ) , end : stringToInt ( end ) , attachments : stringToInt ( attachments ) , wildcard : stringToInt ( wildcard ) , } ; } ; export const setPageInUrl = ( location , page ) => changeSearchParams ( location . pathname , location . hash , { page : page === 0 ? undefined : String ( page + 1 ) } ) ; export const setSortInUrl = ( location , sort ) => changeSearchParams ( location . pathname , location . hash , { page : undefined , sort : sortToString ( sort ) } ) ; export const setFilterInUrl = ( location , filter ) => changeSearchParams ( location . pathname , location . hash , { page : undefined , filter : filterToString ( filter ) } ) ; export const setKeywordInUrl = ( location , keyword ) => changeSearchParams ( location . pathname , location . hash , { page : undefined , keyword : keywordToString ( keyword ) } ) ;