routes.ts (2387B)
1 import { decodeProductReference } from './nostr/references'; 2 import { internalHref } from './navigation-url'; 3 import { canonicalLocalId } from './private-handles'; 4 import { normalizePublicQuery, readPublicQuery } from './catalog/query-input'; 5 6 export function searchHref(input: unknown): string | undefined { 7 const query = normalizePublicQuery(input); 8 if (!query.ok) return undefined; 9 const params = new URLSearchParams(); 10 if (query.text !== '') params.set('q', query.text); 11 return internalHref( 12 params.size === 0 ? '/search' : `/search?${params.toString()}` 13 ); 14 } 15 16 export function staticHref(input: unknown): string | undefined { 17 switch (input) { 18 case '/': 19 case '/search': 20 case '/sell': 21 case '/selling': 22 case '/messages': 23 case '/about': 24 case '/privacy': 25 return internalHref(input); 26 default: 27 return undefined; 28 } 29 } 30 31 export function draftHref(input: unknown): string | undefined { 32 const id = canonicalLocalId(input); 33 return id === undefined ? undefined : internalHref(`/selling/drafts/${id}`); 34 } 35 36 export function conversationHref(input: unknown): string | undefined { 37 const id = canonicalLocalId(input); 38 return id === undefined ? undefined : internalHref(`/messages/${id}`); 39 } 40 41 export function safeContextBack(input: unknown): string | undefined { 42 if ( 43 typeof input === 'string' && 44 input.length <= 8192 && 45 input.startsWith('/search?') && 46 !input.includes('#') 47 ) { 48 try { 49 const query = readPublicQuery( 50 new URL(input, 'https://navigation.invalid') 51 ); 52 return query.ok ? searchHref(query.text) : undefined; 53 } catch { 54 return undefined; 55 } 56 } 57 if (input === '/search' || input === '/selling' || input === '/messages') 58 return internalHref(input); 59 if ( 60 typeof input !== 'string' || 61 input.length > '/products/'.length + 2048 || 62 !input.startsWith('/products/') 63 ) 64 return undefined; 65 return productHref(input.slice('/products/'.length)); 66 } 67 68 export function productHref(input: unknown): string | undefined { 69 const reference = decodeProductReference(input); 70 return reference === undefined 71 ? undefined 72 : internalHref(`/products/${reference.naddr}`); 73 } 74 75 export function productEditHref(input: unknown): string | undefined { 76 const href = productHref(input); 77 return href === undefined ? undefined : internalHref(`${href}/edit`); 78 }