mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(webui): perfect-design app shell, command palette and theme boot
This commit is contained in:
@@ -5,25 +5,25 @@ import { buildHash, parseGlobalParams, parseHashState, parsePageParams } from '.
|
||||
|
||||
describe('parseHashState', () => {
|
||||
it('splits a page and query string', () => {
|
||||
const { page, query } = parseHashState('#spend?range=30d&by=model')
|
||||
expect(page).toBe('spend')
|
||||
const { page, query } = parseHashState('#usage?range=30d&by=model')
|
||||
expect(page).toBe('usage')
|
||||
expect(query.get('range')).toBe('30d')
|
||||
expect(query.get('by')).toBe('model')
|
||||
})
|
||||
|
||||
it('parses a bare page with no query to an empty query', () => {
|
||||
const { page, query } = parseHashState('#consumers')
|
||||
expect(page).toBe('consumers')
|
||||
const { page, query } = parseHashState('#users')
|
||||
expect(page).toBe('users')
|
||||
expect(Array.from(query.keys())).toEqual([])
|
||||
})
|
||||
|
||||
it('falls back to DEFAULT_PAGE ("home") for an empty hash', () => {
|
||||
expect(parseHashState('').page).toBe('home')
|
||||
it('falls back to DEFAULT_PAGE ("overview") for an empty hash', () => {
|
||||
expect(parseHashState('').page).toBe('overview')
|
||||
})
|
||||
|
||||
it('falls back to DEFAULT_PAGE for an unrecognized, non-legacy page, and still parses its query', () => {
|
||||
const { page, query } = parseHashState('#bogus?range=30d')
|
||||
expect(page).toBe('home')
|
||||
expect(page).toBe('overview')
|
||||
expect(query.get('range')).toBe('30d')
|
||||
})
|
||||
|
||||
@@ -40,17 +40,27 @@ describe('parseHashState', () => {
|
||||
describe('legacy tab-name mapping', () => {
|
||||
it.each([
|
||||
['providers', 'models'],
|
||||
['usage', 'consumers'],
|
||||
['charts', 'spend'],
|
||||
['events', 'reliability'],
|
||||
['charts', 'usage'],
|
||||
['events', 'events'],
|
||||
['targets', 'targets'],
|
||||
] as const)('#%s maps to #%s', (legacy, mapped) => {
|
||||
expect(parseHashState(`#${legacy}`).page).toBe(mapped)
|
||||
})
|
||||
|
||||
it('legacy ids map to new ids (home→overview, spend→usage, consumers→users)', () => {
|
||||
expect(parseHashState('#home').page).toBe('overview')
|
||||
expect(parseHashState('#spend').page).toBe('usage')
|
||||
expect(parseHashState('#consumers').page).toBe('users')
|
||||
})
|
||||
|
||||
it('a new id wins over a legacy alias of the same name (usage, events)', () => {
|
||||
expect(parseHashState('#usage').page).toBe('usage')
|
||||
expect(parseHashState('#events').page).toBe('events')
|
||||
})
|
||||
|
||||
it('drops the old hash\'s query params entirely, regardless of shape', () => {
|
||||
const { page, query } = parseHashState('#charts?tab=models&metric=cost&scope=user%3Aalice')
|
||||
expect(page).toBe('spend')
|
||||
expect(page).toBe('usage')
|
||||
expect(Array.from(query.keys())).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -58,20 +68,20 @@ describe('parseHashState', () => {
|
||||
|
||||
describe('buildHash', () => {
|
||||
it('omits the query suffix entirely when params is empty', () => {
|
||||
expect(buildHash('home', {})).toBe('#home')
|
||||
expect(buildHash('overview', {})).toBe('#overview')
|
||||
})
|
||||
|
||||
it('appends a query string for non-empty params', () => {
|
||||
expect(buildHash('spend', { range: '30d' })).toBe('#spend?range=30d')
|
||||
expect(buildHash('usage', { range: '30d' })).toBe('#usage?range=30d')
|
||||
})
|
||||
|
||||
it('joins multiple params with &, in insertion order', () => {
|
||||
const hash = buildHash('spend', { range: '30d', cmp: 'prev', scope: 'user:alice' })
|
||||
expect(hash).toBe('#spend?range=30d&cmp=prev&scope=user%3Aalice')
|
||||
const hash = buildHash('usage', { range: '30d', cmp: 'prev', scope: 'user:alice' })
|
||||
expect(hash).toBe('#usage?range=30d&cmp=prev&scope=user%3Aalice')
|
||||
})
|
||||
|
||||
it('drops a param whose value is an explicit empty string', () => {
|
||||
expect(buildHash('consumers', { q: '' })).toBe('#consumers')
|
||||
expect(buildHash('users', { q: '' })).toBe('#users')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -112,6 +122,22 @@ describe('parseGlobalParams', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('sel param', () => {
|
||||
it('sel survives round trip', () => {
|
||||
const hash = buildHash('models', { range: '7d', sel: 'model:uni%2Fglm-5.3-flash' })
|
||||
const { page, query } = parseHashState(hash)
|
||||
expect(page).toBe('models')
|
||||
expect(parsePageParams(query).sel).toBe('model:uni%2Fglm-5.3-flash')
|
||||
expect(parseGlobalParams(query)).toEqual({ range: '7d' })
|
||||
})
|
||||
|
||||
it('scope param still parsed alongside sel', () => {
|
||||
const { query } = parseHashState('#users?scope=group%3Aeng&sel=user%3Aalice')
|
||||
expect(parseGlobalParams(query).scope).toBe('group:eng')
|
||||
expect(parsePageParams(query)).toEqual({ sel: 'user:alice' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('parsePageParams', () => {
|
||||
it('excludes range/cmp/scope but keeps every other key', () => {
|
||||
const p = parsePageParams(new URLSearchParams('range=30d&cmp=prev&scope=all&by=model&metric=cost'))
|
||||
|
||||
+10
-19
@@ -4,25 +4,17 @@ import { isCmpMode, isRangeKey } from '@/lib/range'
|
||||
import type { CmpMode, RangeKey } from '@/lib/range'
|
||||
|
||||
/**
|
||||
* LEGACY_PAGE_MAP redirects the pre-redesign flat-tab hash names to their
|
||||
* redesign home (redesign-plan.md section 3.1's legacy mapping): a
|
||||
* bookmark or shared link built against the old #providers/#usage/
|
||||
* #charts/#events/#targets shell still lands somewhere sensible instead
|
||||
* of falling back to the default page. `targets` keeps its own name —
|
||||
* the MCP & Agents page is the one page that did not move.
|
||||
*
|
||||
* The OLD hash's query params never carry over (see parseHashState below)
|
||||
* — they were shaped for the old tab (e.g. #charts's `tab=models&metric=
|
||||
* cost`), and none of that maps cleanly onto the new page's own param
|
||||
* names, so a legacy hash lands on a clean instance of its mapped page
|
||||
* rather than misapplying a stale param.
|
||||
* 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<string, PageId> = {
|
||||
home: 'overview',
|
||||
spend: 'usage',
|
||||
consumers: 'users',
|
||||
providers: 'models',
|
||||
usage: 'consumers',
|
||||
charts: 'spend',
|
||||
events: 'reliability',
|
||||
targets: 'targets',
|
||||
charts: 'usage',
|
||||
}
|
||||
|
||||
export interface HashState {
|
||||
@@ -36,9 +28,8 @@ export interface HashState {
|
||||
*
|
||||
* 1. The page segment is a current PageId (lib/pages.ts) — parse the
|
||||
* query normally.
|
||||
* 2. The page segment is a legacy tab name (LEGACY_PAGE_MAP) — map to the
|
||||
* new page, with an EMPTY query (old params dropped, see the map's own
|
||||
* doc comment).
|
||||
* 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).
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { NAV_GROUPS, navCounts } from './nav-groups'
|
||||
import { DEFAULT_PAGE, PAGE_IDS } from './pages'
|
||||
|
||||
describe('NAV_GROUPS', () => {
|
||||
it('every PAGE_ID appears exactly once in NAV_GROUPS', () => {
|
||||
const ids = NAV_GROUPS.flatMap((g) => g.items.map((i) => i.id))
|
||||
expect([...ids].sort()).toEqual([...PAGE_IDS].sort())
|
||||
expect(new Set(ids).size).toBe(ids.length)
|
||||
})
|
||||
|
||||
it('orders groups as Overview/Usage/Reliability | Directory | Events/Config', () => {
|
||||
expect(NAV_GROUPS.map((g) => [g.label, g.items.map((i) => i.id)])).toEqual([
|
||||
[null, ['overview', 'usage', 'reliability']],
|
||||
['Directory', ['users', 'models', 'routers', 'targets']],
|
||||
[null, ['events', 'config']],
|
||||
])
|
||||
})
|
||||
|
||||
it('labels the directory pages as specified', () => {
|
||||
const labels = NAV_GROUPS[1].items.map((i) => i.label)
|
||||
expect(labels).toEqual(['Users & groups', 'Models & providers', 'Smart routers', 'MCP & agents'])
|
||||
})
|
||||
|
||||
it('defaults to overview', () => {
|
||||
expect(DEFAULT_PAGE).toBe('overview')
|
||||
})
|
||||
})
|
||||
|
||||
describe('navCounts', () => {
|
||||
it('is empty before any data has loaded', () => {
|
||||
expect(navCounts(null, null, null)).toEqual({})
|
||||
})
|
||||
|
||||
it('derives the four directory counts', () => {
|
||||
const counts = navCounts(
|
||||
{ providers: [{ models: ['a', 'b'] }, { models: ['c'] }], routers: [{}, {}] },
|
||||
{ users: [{}, {}, {}] },
|
||||
{ mcpServers: [{}], agents: [{}, {}] },
|
||||
)
|
||||
expect(counts).toEqual({ users: 3, models: 3, routers: 2, targets: 3 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,75 @@
|
||||
import {
|
||||
faBolt,
|
||||
faChartLine,
|
||||
faCubes,
|
||||
faGaugeHigh,
|
||||
faHeartPulse,
|
||||
faPlug,
|
||||
faRoute,
|
||||
faSliders,
|
||||
faUsers,
|
||||
} from '@fortawesome/free-solid-svg-icons'
|
||||
import type { IconDefinition } from '@fortawesome/free-solid-svg-icons'
|
||||
|
||||
import type { PageId } from '@/lib/pages'
|
||||
import type { AdminOverviewResponse, AdminTargetsResponse, AdminUsageResponse } from '@/types/api'
|
||||
|
||||
export type NavCountKey = 'users' | 'models' | 'routers' | 'targets'
|
||||
|
||||
export interface NavItem {
|
||||
id: PageId
|
||||
label: string
|
||||
icon: IconDefinition
|
||||
countKey?: NavCountKey
|
||||
}
|
||||
|
||||
export interface NavGroup {
|
||||
label: string | null
|
||||
items: NavItem[]
|
||||
}
|
||||
|
||||
/** Sidebar structure; every PageId appears exactly once. */
|
||||
export const NAV_GROUPS: NavGroup[] = [
|
||||
{
|
||||
label: null,
|
||||
items: [
|
||||
{ id: 'overview', label: 'Overview', icon: faGaugeHigh },
|
||||
{ id: 'usage', label: 'Usage & spend', icon: faChartLine },
|
||||
{ id: 'reliability', label: 'Reliability', icon: faHeartPulse },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'Directory',
|
||||
items: [
|
||||
{ id: 'users', label: 'Users & groups', icon: faUsers, countKey: 'users' },
|
||||
{ id: 'models', label: 'Models & providers', icon: faCubes, countKey: 'models' },
|
||||
{ id: 'routers', label: 'Smart routers', icon: faRoute, countKey: 'routers' },
|
||||
{ id: 'targets', label: 'MCP & agents', icon: faPlug, countKey: 'targets' },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: null,
|
||||
items: [
|
||||
{ id: 'events', label: 'Events', icon: faBolt },
|
||||
{ id: 'config', label: 'Config', icon: faSliders },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
type OverviewCounts = { providers?: Array<{ models: unknown[] }>; routers?: unknown[] } | null
|
||||
type UsageCounts = { users: unknown[] } | null
|
||||
type TargetCounts = { mcpServers: unknown[]; agents: unknown[] } | null
|
||||
|
||||
/** Sidebar badge counts; a key is omitted until its source has loaded. */
|
||||
export function navCounts(
|
||||
overview: OverviewCounts | AdminOverviewResponse,
|
||||
usage: UsageCounts | AdminUsageResponse,
|
||||
targets: TargetCounts | AdminTargetsResponse,
|
||||
): Partial<Record<NavCountKey, number>> {
|
||||
const counts: Partial<Record<NavCountKey, number>> = {}
|
||||
if (usage) counts.users = usage.users.length
|
||||
if (overview?.providers) counts.models = overview.providers.reduce((n, p) => n + p.models.length, 0)
|
||||
if (overview?.routers) counts.routers = overview.routers.length
|
||||
if (targets) counts.targets = targets.mcpServers.length + targets.agents.length
|
||||
return counts
|
||||
}
|
||||
+5
-41
@@ -1,48 +1,12 @@
|
||||
import {
|
||||
faCoins,
|
||||
faCubes,
|
||||
faHeartPulse,
|
||||
faHouse,
|
||||
faPlug,
|
||||
faSliders,
|
||||
faUsers,
|
||||
} from '@fortawesome/free-solid-svg-icons'
|
||||
import type { IconDefinition } from '@fortawesome/free-solid-svg-icons'
|
||||
|
||||
/**
|
||||
* PAGE_IDS is the sidebar redesign's page union (redesign-plan.md section
|
||||
* 3.1) — replaces composables/useTabHash.ts's old flat TAB_VALUES. The
|
||||
* single source of truth for every "which page" check: SidebarNav.vue's
|
||||
* link list, stores/nav.ts's `page` state, and lib/hash-state.ts's
|
||||
* outer "#<page>" segment all import PAGE_IDS/PageId rather than each
|
||||
* redeclaring their own copy.
|
||||
*/
|
||||
export const PAGE_IDS = ['home', 'spend', 'consumers', 'models', 'reliability', 'config', 'targets'] as const
|
||||
/** Page ids of the redesigned shell: the single source for nav, the store and the hash's page segment. */
|
||||
export const PAGE_IDS = ['overview', 'usage', 'reliability', 'users', 'models', 'routers', 'targets', 'events', 'config'] as const
|
||||
|
||||
export type PageId = (typeof PAGE_IDS)[number]
|
||||
|
||||
/** The landing page when the hash names nothing recognized, or is absent (first load). */
|
||||
export const DEFAULT_PAGE: PageId = 'home'
|
||||
/** The landing page when the hash names nothing recognized, or is absent. */
|
||||
export const DEFAULT_PAGE: PageId = 'overview'
|
||||
|
||||
/** One sidebar entry: the page it navigates to, its visible label, and its icon (redesign-plan.md section 3.1's fixed icon list). */
|
||||
export interface PageNavEntry {
|
||||
id: PageId
|
||||
label: string
|
||||
icon: IconDefinition
|
||||
}
|
||||
|
||||
/** PAGE_NAV drives SidebarNav.vue's link list, in display order. */
|
||||
export const PAGE_NAV: readonly PageNavEntry[] = [
|
||||
{ id: 'home', label: 'Home', icon: faHouse },
|
||||
{ id: 'spend', label: 'Spend', icon: faCoins },
|
||||
{ id: 'consumers', label: 'Consumers', icon: faUsers },
|
||||
{ id: 'models', label: 'Models', icon: faCubes },
|
||||
{ id: 'reliability', label: 'Reliability', icon: faHeartPulse },
|
||||
{ id: 'config', label: 'Config', icon: faSliders },
|
||||
{ id: 'targets', label: 'MCP & Agents', icon: faPlug },
|
||||
]
|
||||
|
||||
/** isPageId is a runtime type guard against PAGE_IDS — the only place that string union is checked against untrusted input (a hash, a hand-typed URL). */
|
||||
/** Runtime guard for untrusted input (a hash, a hand-typed URL). */
|
||||
export function isPageId(value: string): value is PageId {
|
||||
return (PAGE_IDS as readonly string[]).includes(value)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { paletteEntities } from './palette-items'
|
||||
import { PANEL_PAGE } from './panel-selection'
|
||||
|
||||
describe('paletteEntities', () => {
|
||||
it('is empty before data loads', () => {
|
||||
expect(paletteEntities(null, null, null)).toEqual([])
|
||||
})
|
||||
|
||||
it('lists each entity kind with a provider-prefixed model id', () => {
|
||||
const items = paletteEntities(
|
||||
{ providers: [{ name: 'uni', models: ['glm-5.3-flash'] }], groups: [{ name: 'eng' }], routers: [{ name: 'auto' }] },
|
||||
{ users: [{ id: 'alice' }] },
|
||||
{ mcpServers: [{ name: 'fetch' }], agents: [{ name: 'coder' }] },
|
||||
)
|
||||
expect(items.map((i) => `${i.kind}:${i.id}`)).toEqual([
|
||||
'user:alice',
|
||||
'group:eng',
|
||||
'provider:uni',
|
||||
'model:uni/glm-5.3-flash',
|
||||
'router:auto',
|
||||
'target:fetch',
|
||||
'target:coder',
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('PANEL_PAGE', () => {
|
||||
it('routes every kind to its directory page', () => {
|
||||
expect(PANEL_PAGE).toEqual({
|
||||
user: 'users',
|
||||
group: 'users',
|
||||
model: 'models',
|
||||
provider: 'models',
|
||||
router: 'routers',
|
||||
target: 'targets',
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { PanelKind } from '@/lib/panel-selection'
|
||||
|
||||
export interface PaletteEntity {
|
||||
kind: PanelKind
|
||||
id: string
|
||||
label: string
|
||||
}
|
||||
|
||||
type OverviewSource = {
|
||||
providers?: Array<{ name: string; models: string[] }>
|
||||
groups?: Array<{ name: string }>
|
||||
routers?: Array<{ name: string }>
|
||||
} | null
|
||||
|
||||
/** Entities the command palette can open a detail panel for; empty until the dashboard has data. */
|
||||
export function paletteEntities(
|
||||
overview: OverviewSource,
|
||||
usage: { users: Array<{ id: string }> } | null,
|
||||
targets: { mcpServers: Array<{ name: string }>; agents: Array<{ name: string }> } | null,
|
||||
): PaletteEntity[] {
|
||||
const out: PaletteEntity[] = []
|
||||
for (const u of usage?.users ?? []) out.push({ kind: 'user', id: u.id, label: u.id })
|
||||
for (const g of overview?.groups ?? []) out.push({ kind: 'group', id: g.name, label: g.name })
|
||||
for (const p of overview?.providers ?? []) {
|
||||
out.push({ kind: 'provider', id: p.name, label: p.name })
|
||||
for (const m of p.models) out.push({ kind: 'model', id: `${p.name}/${m}`, label: `${p.name}/${m}` })
|
||||
}
|
||||
for (const r of overview?.routers ?? []) out.push({ kind: 'router', id: r.name, label: r.name })
|
||||
for (const t of [...(targets?.mcpServers ?? []), ...(targets?.agents ?? [])]) out.push({ kind: 'target', id: t.name, label: t.name })
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { buildHash, parseHashState, parsePageParams } from './hash-state'
|
||||
import { formatSel, parseSel, resolveSel } from './panel-selection'
|
||||
|
||||
describe('parseSel', () => {
|
||||
it('parses kind:id', () => {
|
||||
expect(parseSel('user:alice')).toEqual({ kind: 'user', id: 'alice' })
|
||||
})
|
||||
|
||||
it('rejects unknown kind', () => {
|
||||
expect(parseSel('galaxy:alice')).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects absent, empty and id-less values', () => {
|
||||
expect(parseSel(undefined)).toBeNull()
|
||||
expect(parseSel('')).toBeNull()
|
||||
expect(parseSel('user')).toBeNull()
|
||||
expect(parseSel('user:')).toBeNull()
|
||||
})
|
||||
|
||||
it('splits on the first colon only', () => {
|
||||
expect(parseSel('target:mcp:fetch')).toEqual({ kind: 'target', id: 'mcp:fetch' })
|
||||
})
|
||||
|
||||
it('rejects a malformed percent escape instead of throwing', () => {
|
||||
expect(parseSel('user:%E0%A4%A')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatSel', () => {
|
||||
it('encodes ids with slashes (uni/glm-5.3-flash)', () => {
|
||||
const s = formatSel('model', 'uni/glm-5.3-flash')
|
||||
expect(s).toBe('model:uni%2Fglm-5.3-flash')
|
||||
expect(parseSel(s)).toEqual({ kind: 'model', id: 'uni/glm-5.3-flash' })
|
||||
})
|
||||
|
||||
it('round-trips ids containing colons, percent signs and spaces', () => {
|
||||
for (const id of ['mcp:fetch', '100%', 'a b', 'x&y=z#w']) {
|
||||
expect(parseSel(formatSel('target', id))).toEqual({ kind: 'target', id })
|
||||
}
|
||||
})
|
||||
|
||||
it('survives the hash round trip through buildHash and parseHashState', () => {
|
||||
const id = 'uni/glm-5.3-flash'
|
||||
const hash = buildHash('models', { sel: formatSel('model', id) })
|
||||
const { page, query } = parseHashState(hash)
|
||||
expect(page).toBe('models')
|
||||
expect(parseSel(parsePageParams(query).sel)).toEqual({ kind: 'model', id })
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveSel', () => {
|
||||
const known = new Set(['user:alice'])
|
||||
const exists = (kind: string, id: string) => known.has(`${kind}:${id}`)
|
||||
|
||||
it('keeps a known selection', () => {
|
||||
expect(resolveSel({ kind: 'user', id: 'alice' }, exists)).toEqual({ kind: 'user', id: 'alice' })
|
||||
})
|
||||
|
||||
it('unknown id clears sel', () => {
|
||||
expect(resolveSel({ kind: 'user', id: 'ghost' }, exists)).toBeNull()
|
||||
})
|
||||
|
||||
it('passes null through', () => {
|
||||
expect(resolveSel(null, exists)).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,54 @@
|
||||
import type { PageId } from '@/lib/pages'
|
||||
|
||||
/** Entity kinds a detail panel can open, carried in the hash as `sel=<kind>:<id>`. */
|
||||
export const PANEL_KINDS = ['user', 'group', 'model', 'provider', 'router', 'target'] as const
|
||||
|
||||
export type PanelKind = (typeof PANEL_KINDS)[number]
|
||||
|
||||
export interface PanelSelection {
|
||||
kind: PanelKind
|
||||
id: string
|
||||
}
|
||||
|
||||
function isPanelKind(value: string): value is PanelKind {
|
||||
return (PANEL_KINDS as readonly string[]).includes(value)
|
||||
}
|
||||
|
||||
/** Parses a `sel` value; null for absent, unknown kind, empty id or a malformed escape. Splits on the first colon only. */
|
||||
export function parseSel(v: string | undefined): PanelSelection | null {
|
||||
if (!v) return null
|
||||
const colon = v.indexOf(':')
|
||||
if (colon === -1) return null
|
||||
const kind = v.slice(0, colon)
|
||||
const rawId = v.slice(colon + 1)
|
||||
if (!isPanelKind(kind) || rawId === '') return null
|
||||
try {
|
||||
return { kind, id: decodeURIComponent(rawId) }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** The id is percent-encoded so slashes, colons and `&` survive; the hash layer's own query encoding is applied on top and undone symmetrically. */
|
||||
export function formatSel(kind: PanelKind, id: string): string {
|
||||
return `${kind}:${encodeURIComponent(id)}`
|
||||
}
|
||||
|
||||
/** Returns the selection when its entity exists, else null (the caller then clears `sel` with a replace). */
|
||||
export function resolveSel(
|
||||
sel: PanelSelection | null,
|
||||
existsFn: (kind: PanelKind, id: string) => boolean,
|
||||
): PanelSelection | null {
|
||||
if (!sel) return null
|
||||
return existsFn(sel.kind, sel.id) ? sel : null
|
||||
}
|
||||
|
||||
/** The page whose directory lists each kind; opening a panel from elsewhere navigates here first. */
|
||||
export const PANEL_PAGE: Record<PanelKind, PageId> = {
|
||||
user: 'users',
|
||||
group: 'users',
|
||||
model: 'models',
|
||||
provider: 'models',
|
||||
router: 'routers',
|
||||
target: 'targets',
|
||||
}
|
||||
Reference in New Issue
Block a user