import { URLPathStr } from "polar-shared/src/url/PathToRegexps" ; import { isPresent } from "polar-shared/src/Preconditions" ; import { ILocation , ILocationWithPathAndHash } from "../react/router/ReactRouters" ; export interface RouterLinkObj { readonly pathname : string ; readonly hash ? : string ; readonly search ? : string ; } export type RouterLink = URLPathStr | RouterLinkObj ; function isRouterLinkObj ( routerLink ) { return isPresent ( ( routerLink as any ) . pathname ) ; } function toRouterLinkObj ( routerLink ) { if ( isRouterLinkObj ( routerLink ) ) { return routerLink ; } return { pathname : routerLink , hash : '' , search : '' } ; } namespace ILocations { export function canonicalizeHash ( hash ? ) { if ( ! hash ) { return "#" ; } if ( ! hash . startsWith ( "#" ) ) { return "#" + hash ; } return hash ; } export function toString ( location ) { const hash = canonicalizeHash ( location . hash ) ; return `${ location . pathname }${ hash }` ; } } export namespace ReactRouterLinks { export function isActive ( target , location = document . location ) { const targetObj = toRouterLinkObj ( target ) ; const { pathname , hash } = targetObj ; return location . pathname === pathname ; } }