import NextLink from 'next/link' ; import { FunctionComponent } from 'react' ; import { ParsedUrlQuery } from 'querystring' ; import { booleanCodec , decodeQuery , encodeQuery , FromCodecMap , LinkFrom , maybeNumberCodec , stringCodec , } from '../../../utils/routes' ; import { LinkProps } from '../../../model/link-props' ; import { removeUndefinedProps } from '../../../utils/json' ; const itemPropsSources = [ 'work' , 'images_search_result' , 'viewer/paginator' , 'manifests_navigation' , 'search_within_result' , ] as const ; type ItemPropsSource = typeof itemPropsSources [ number ] ; const emptyItemProps = { workId : '' , canvas : 1 , page : 1 , manifest : 1 , pageSize : undefined , isOverview : false , resultPosition : undefined , } ; const codecMap = { workId : stringCodec , canvas : maybeNumberCodec , page : maybeNumberCodec , pageSize : maybeNumberCodec , isOverview : booleanCodec , resultPosition : maybeNumberCodec , manifest : maybeNumberCodec , } ; export type ItemProps = FromCodecMap < typeof codecMap > ; const fromQuery = params => { return decodeQuery < ItemProps > ( params , codecMap ) ; } ; const toQuery = props => { return encodeQuery < ItemProps > ( props , codecMap ) ; } ; function toLink ( partialProps , source ) { const pathname = '/item' ; const props = { ... emptyItemProps , ... partialProps , } ; const query = removeUndefinedProps ( toQuery ( props ) ) ; return { href : { pathname , query : { ... query , source } , } , as : { pathname : `/works/${ props . workId }/items` , query : { } , } , } ; } type Props = LinkFrom < ItemProps > & { source : ItemPropsSource ; } ; const WorksLink = ( { children , source , ... props } ) => { return < NextLink { ... toLink ( props , source ) } > { children } < /NextLink>; } ; export default WorksLink ; export { toLink , fromQuery , emptyItemProps } ;