mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-27 10:16:34 +00:00
feat(webui): config page
This commit is contained in:
+103
-39
@@ -1,64 +1,128 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted } from 'vue'
|
||||
import { faChevronDown, faChevronRight } from '@fortawesome/free-solid-svg-icons'
|
||||
import { Button, Callout, Card, CardAction, CardContent, CardHeader, CardTitle, SearchInput, YamlViewer } from '@perfect-design/ui'
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
|
||||
import ChangeHelper from '@/components/ChangeHelper.vue'
|
||||
import ConfigTree from '@/components/ConfigTree.vue'
|
||||
import ErrorState from '@/components/ErrorState.vue'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { copyText } from '@/lib/clipboard'
|
||||
import { filterSections, splitConfigSections } from '@/lib/config-sections'
|
||||
import { formatHelperSel, parseHelperSel } from '@/lib/helper-forms'
|
||||
import type { HelperId } from '@/lib/helper-forms'
|
||||
import { loadState } from '@/lib/load-state'
|
||||
import { emitYamlDocument, toYamlValue } from '@/lib/yaml-emit'
|
||||
import type { YamlValue } from '@/lib/yaml-emit'
|
||||
import ChangeHelperPanel from '@/panels/ChangeHelperPanel.vue'
|
||||
import { useConfigStore } from '@/stores/config'
|
||||
import { useConsumersStore } from '@/stores/consumers'
|
||||
import { useDashboardStore } from '@/stores/dashboard'
|
||||
import { useNavStore } from '@/stores/nav'
|
||||
import { useToastsStore } from '@/stores/toasts'
|
||||
import StateView from '@/widgets/StateView.vue'
|
||||
import type { ViewState } from '@/widgets/StateView.vue'
|
||||
|
||||
/**
|
||||
* ConfigPage (redesign-plan.md section 3.4) composes the redacted config
|
||||
* tree (ConfigTree.vue, GET /admin/api/config, fetched on demand) above
|
||||
* the change helper (ChangeHelper.vue's five snippet-building forms) —
|
||||
* group/user names for those forms' target pickers come from the already-
|
||||
* polled dashboard store (overview.groups) and the on-demand consumers
|
||||
* store (users), so this page fetches config once and reuses whatever the
|
||||
* other two stores already have rather than a third redundant call.
|
||||
*/
|
||||
const config = useConfigStore()
|
||||
const dashboard = useDashboardStore()
|
||||
const consumers = useConsumersStore()
|
||||
const dashboard = useDashboardStore()
|
||||
const nav = useNavStore()
|
||||
const toasts = useToastsStore()
|
||||
|
||||
onMounted(() => {
|
||||
void config.ensureConfig()
|
||||
void consumers.ensureConsumers()
|
||||
})
|
||||
|
||||
const groupNames = computed(() => (dashboard.overview?.groups ?? []).map((g) => g.name))
|
||||
// The server redacts secrets before this response; the page renders it as received and never rewrites values.
|
||||
const yamlText = computed(() => (config.data ? emitYamlDocument(toYamlValue(config.data.config) as Record<string, YamlValue | undefined>) : ''))
|
||||
const sections = computed(() => splitConfigSections(yamlText.value))
|
||||
const query = ref('')
|
||||
const shown = computed(() => filterSections(sections.value, query.value))
|
||||
const collapsed = ref<Set<string>>(new Set())
|
||||
|
||||
function toggle(key: string): void {
|
||||
const next = new Set(collapsed.value)
|
||||
if (!next.delete(key)) next.add(key)
|
||||
collapsed.value = next
|
||||
}
|
||||
|
||||
function jump(key: string): void {
|
||||
query.value = ''
|
||||
const next = new Set(collapsed.value)
|
||||
next.delete(key)
|
||||
collapsed.value = next
|
||||
requestAnimationFrame(() => document.getElementById(`cfg-${key}`)?.scrollIntoView({ block: 'start' }))
|
||||
}
|
||||
|
||||
const state = computed<ViewState>(() => {
|
||||
const s = loadState({ loading: config.loading, hasData: config.data !== null, error: config.error })
|
||||
if (s === 'skeleton') return 'loading'
|
||||
if (s === 'error') return 'error'
|
||||
return sections.value.length === 0 ? 'empty' : 'ready'
|
||||
})
|
||||
|
||||
const warnings = computed(() => config.data?.warnings ?? [])
|
||||
const warningsDropped = computed(() => dashboard.overview?.warningsDropped ?? 0)
|
||||
|
||||
async function copyConfig(): Promise<void> {
|
||||
const result = await copyText(yamlText.value)
|
||||
toasts.push(result === 'copied' ? { kind: 'success', message: 'Config copied.' } : { kind: 'error', message: 'Could not copy the config.' })
|
||||
}
|
||||
|
||||
// --- Change helper sheet: lives in the hash as sel=helper:<form> ---
|
||||
const helperForm = computed(() => parseHelperSel(nav.params.sel))
|
||||
const groupNames = computed(() => (consumers.data?.groups ?? []).map((g) => g.name))
|
||||
const userNames = computed(() => (consumers.data?.users ?? []).map((u) => u.name))
|
||||
|
||||
function openHelper(): void {
|
||||
nav.openSheet(formatHelperSel(helperForm.value ?? 'limits'))
|
||||
}
|
||||
function selectForm(id: HelperId): void {
|
||||
nav.swapSheet(formatHelperSel(id))
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="flex flex-col gap-6">
|
||||
<Card v-if="config.error && !config.data">
|
||||
<CardHeader>
|
||||
<div class="flex flex-col gap-4">
|
||||
<Card size="sm">
|
||||
<CardHeader class="items-center">
|
||||
<CardTitle>Config</CardTitle>
|
||||
<CardAction class="flex items-center gap-2 self-center">
|
||||
<span v-if="config.data?.version" class="text-muted-foreground font-mono text-xs">{{ config.data.version }}</span>
|
||||
<Button variant="outline" size="sm" :disabled="!yamlText" @click="copyConfig">Copy config</Button>
|
||||
<Button size="sm" @click="openHelper">Change helper</Button>
|
||||
</CardAction>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ErrorState :message="config.error" :on-retry="config.fetchConfig" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
<!-- Mirrors ConfigTree's own final SnippetBlock shape (a label line above a bordered block) so it causes no layout shift once the real config arrives. -->
|
||||
<Card v-else-if="config.loading && !config.data">
|
||||
<CardContent class="flex flex-col gap-1.5">
|
||||
<Skeleton class="h-3 w-56" />
|
||||
<Skeleton class="h-64 w-full rounded-md" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
<ConfigTree v-else-if="config.data" :config="config.data.config" :warnings="config.data.warnings" />
|
||||
<CardContent class="flex flex-col gap-3">
|
||||
<div v-if="warnings.length > 0" class="flex flex-col gap-2" data-testid="config-warnings">
|
||||
<Callout v-for="(w, i) in warnings" :key="i" type="warning" size="sm" title="Config warning">
|
||||
<span class="font-mono text-xs break-words">{{ w }}</span>
|
||||
</Callout>
|
||||
<p v-if="warningsDropped > 0" class="text-muted-foreground m-0 text-xs">{{ warningsDropped.toLocaleString('en-US') }} more warnings were dropped by the server cap.</p>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Change helper</CardTitle>
|
||||
<CardDescription>Build a paste-ready snippet for middleware.yaml or users.json — nothing here writes to the running config.</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ChangeHelper :group-names="groupNames" :user-names="userNames" />
|
||||
<StateView :state="state" empty-title="No config returned" :error-message="config.error" @retry="config.fetchConfig()">
|
||||
<div class="flex flex-col gap-2">
|
||||
<SearchInput v-model="query" size="sm" class="max-w-sm" placeholder="Filter sections" aria-label="Filter config sections" />
|
||||
<nav class="flex flex-wrap gap-1" aria-label="Config sections">
|
||||
<Button v-for="s in sections" :key="s.key" variant="ghost" size="xs" class="font-mono" @click="jump(s.key)">{{ s.key }}</Button>
|
||||
</nav>
|
||||
<p v-if="shown.length === 0" class="text-muted-foreground m-0 py-3 text-sm">No section matches "{{ query }}".</p>
|
||||
<section v-for="s in shown" :id="`cfg-${s.key}`" :key="s.key" class="border-border flex flex-col border-t first:border-t-0" :aria-label="s.key">
|
||||
<button
|
||||
type="button"
|
||||
class="hover:bg-accent focus-visible:ring-ring flex h-8 w-full items-center gap-2 rounded px-2 text-left focus-visible:ring-2 focus-visible:outline-none"
|
||||
:aria-expanded="!collapsed.has(s.key)"
|
||||
@click="toggle(s.key)"
|
||||
>
|
||||
<FontAwesomeIcon :icon="collapsed.has(s.key) ? faChevronRight : faChevronDown" class="text-muted-foreground size-3" aria-hidden="true" />
|
||||
<span class="font-mono text-sm font-medium">{{ s.key }}</span>
|
||||
<span class="text-muted-foreground ml-auto font-mono text-xs tabular-nums">{{ s.lineCount.toLocaleString('en-US') }} lines</span>
|
||||
</button>
|
||||
<YamlViewer v-if="!collapsed.has(s.key)" :source="s.text" :start-line="s.startLine" variant="plain" size="sm" line-numbers foldable :copy="false" :label="`${s.key} config`" />
|
||||
</section>
|
||||
</div>
|
||||
</StateView>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<ChangeHelperPanel v-if="helperForm" :form="helperForm" :group-names="groupNames" :user-names="userNames" @select="selectForm" @close="nav.closePanel()" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user