import { DEFAULT_PAGE, isPageId } from '@/lib/pages' import type { PageId } from '@/lib/pages' import { isCmpMode, isRangeKey } from '@/lib/range' import type { CmpMode, RangeKey } from '@/lib/range' /** * Pre-redesign hash names that no longer exist as page ids, pointed at their * new home. A name that is also a current PageId (usage, events, targets) * is never looked up here: the new id wins. Old query params are dropped, * since they were shaped for the old page. */ const LEGACY_PAGE_MAP: Record = { home: 'overview', spend: 'usage', consumers: 'users', providers: 'models', charts: 'usage', } export interface HashState { page: PageId query: URLSearchParams } /** * parseHashState splits a raw location.hash ("#spend?range=30d&by=model") * into its outer page and the remaining query string. Three cases: * * 1. The page segment is a current PageId (lib/pages.ts) — parse the * query normally. * 2. The page segment is a legacy name (LEGACY_PAGE_MAP) — map to the * new page, with an EMPTY query. * 3. Anything else (absent, empty, or unrecognized) — DEFAULT_PAGE, query * parsed as-is (so a malformed page segment does not also swallow an * otherwise-valid query string). * * Never throws. */ export function parseHashState(hash: string): HashState { const raw = hash.startsWith('#') ? hash.slice(1) : hash const qIndex = raw.indexOf('?') const pagePart = qIndex === -1 ? raw : raw.slice(0, qIndex) const queryPart = qIndex === -1 ? '' : raw.slice(qIndex + 1) if (isPageId(pagePart)) return { page: pagePart, query: new URLSearchParams(queryPart) } const legacy = Object.hasOwn(LEGACY_PAGE_MAP, pagePart) ? LEGACY_PAGE_MAP[pagePart] : undefined if (legacy) return { page: legacy, query: new URLSearchParams() } return { page: DEFAULT_PAGE, query: new URLSearchParams(queryPart) } } /** * buildHash re-serializes a page plus an already-default-stripped param * record (global filters and/or page params merged by the caller) back * into a "#?" string. "Omit defaults" is the CALLER's job — * composables/useHashState.ts's own computed decides what counts as * default for range/cmp/scope; buildHash itself only defends against an * explicitly empty-string value slipping through, dropping it the same * way. No query suffix at all when `params` ends up empty, so the common * "nothing but defaults" case round-trips to a bare "#". */ export function buildHash(page: PageId, params: Record): string { const entries = Object.entries(params).filter(([, v]) => v !== '') if (entries.length === 0) return `#${page}` return `#${page}?${new URLSearchParams(entries).toString()}` } export interface GlobalHashParams { range?: RangeKey cmp?: CmpMode scope?: string } /** SCOPE_PATTERN mirrors the global filter bar's own scope convention (redesign-plan.md section 3.1): "all", or one of the three "{kind}:{id}" forms — an id may contain almost anything, so each kind only checks its own prefix plus a non-empty remainder. Exported so GlobalFilterBar.vue validates its own scope input against the exact same rule this module uses to parse it back out of the hash. */ export const SCOPE_PATTERN = /^(all|group:.+|user:.+|provider:.+)$/ /** * parseGlobalParams validates the three page-independent hash params * (range/cmp/scope) every page's hash shares. Each field is independently * OMITTED (never throws) when absent or not a recognized value — a * hand-edited or stale-version URL with `range=fortnight` degrades to * "use whatever this field already is" (the caller decides the actual * default), rather than failing the whole hash or resetting the other, * valid fields alongside the one bad one. */ export function parseGlobalParams(query: URLSearchParams): GlobalHashParams { const result: GlobalHashParams = {} const range = query.get('range') if (range && isRangeKey(range)) result.range = range const cmp = query.get('cmp') if (cmp && isCmpMode(cmp)) result.cmp = cmp const scope = query.get('scope') if (scope && SCOPE_PATTERN.test(scope)) result.scope = scope return result } /** GLOBAL_PARAM_KEYS names the three query keys parseGlobalParams reads — parsePageParams below excludes exactly these, and nothing else, from a page's own param record. */ const GLOBAL_PARAM_KEYS: ReadonlySet = new Set(['range', 'cmp', 'scope']) /** * parsePageParams returns every hash query param EXCEPT the three global * ones, verbatim, as a plain record. Deliberately opaque (no per-page * validation here — see stores/nav.ts's own doc comment for why): each * page owns interpreting its own param names/values, and a key this * build does not recognize (an older link, or one from a newer page) * simply passes through unused rather than being rejected. */ export function parsePageParams(query: URLSearchParams): Record { const result: Record = {} for (const [key, value] of query.entries()) { if (GLOBAL_PARAM_KEYS.has(key)) continue if (value === '') continue result[key] = value } return result }