From 6a9a048e12c25429dfd9482eb57bfc8652d1aeb2 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 3 Dec 2026 00:18:38 +0400 Subject: [PATCH] refac --- src/app.d.ts | 1 + .../components/admin/Settings/Models.svelte | 4 +- .../chat/Settings/ArchivedChats.svelte | 4 +- src/lib/components/chat/SettingsModal.svelte | 3 + src/lib/utils/settings-routes.ts | 59 +++++++++++++++++ src/routes/(app)/+layout.svelte | 64 +++++++++++++++++-- .../(app)/settings/[...path]/+page.svelte | 15 +++++ 7 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 src/lib/utils/settings-routes.ts create mode 100644 src/routes/(app)/settings/[...path]/+page.svelte diff --git a/src/app.d.ts b/src/app.d.ts index 789e1e7347..fcf7a55159 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -36,6 +36,7 @@ declare global { inbox?: boolean; inboxUserId?: string; tab?: string; + settings?: boolean; } // interface Error {} // interface Locals {} diff --git a/src/lib/components/admin/Settings/Models.svelte b/src/lib/components/admin/Settings/Models.svelte index aa30f34540..f637e440de 100644 --- a/src/lib/components/admin/Settings/Models.svelte +++ b/src/lib/components/admin/Settings/Models.svelte @@ -669,8 +669,8 @@ } if (isPresetModel(model)) { - showSettings.set(false); await goto(`/workspace/models/edit?id=${encodeURIComponent(model.id)}`); + showSettings.set(false); return; } @@ -685,8 +685,8 @@ id: `${model.id}-clone`, name: `${model.name} (Clone)` }); - showSettings.set(false); await goto('/workspace/models/create'); + showSettings.set(false); }; const exportModelHandler = async (model) => { diff --git a/src/lib/components/chat/Settings/ArchivedChats.svelte b/src/lib/components/chat/Settings/ArchivedChats.svelte index 7cd8341dbe..2c8fe64c45 100644 --- a/src/lib/components/chat/Settings/ArchivedChats.svelte +++ b/src/lib/components/chat/Settings/ArchivedChats.svelte @@ -379,10 +379,10 @@ class="block truncate text-base-800 hover:text-base-1000 dark:text-base-200 dark:hover:text-base-0" href={`/c/${chat.id}`} title={chat?.title} - on:click={(event) => { + on:click={async (event) => { event.preventDefault(); + await goto(`/c/${chat.id}`); showSettings.set(false); - goto(`/c/${chat.id}`); }} > {chat?.title} diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index 2ce1816f2c..bf8fa5a5d5 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -34,6 +34,8 @@ const i18n: Writable = getContext('i18n'); export let show: boolean | string | SettingsModalRequest = false; + // Called with the open tab whenever it changes, so the page address can follow it. + export let onTabChange: (tab: string) => void = () => {}; let modalShow = false; let lastShow: boolean | string | SettingsModalRequest = false; let tabState: Record | null = null; @@ -628,6 +630,7 @@ $: if (modalShow && selectedTab) { scrollToSelectedTab(); + onTabChange(selectedTab); } $: if ($config && $user) { diff --git a/src/lib/utils/settings-routes.ts b/src/lib/utils/settings-routes.ts new file mode 100644 index 0000000000..4412007ae7 --- /dev/null +++ b/src/lib/utils/settings-routes.ts @@ -0,0 +1,59 @@ +// Readable addresses for the settings tabs: /settings/ and /settings/admin/. +const SETTINGS_SLUGS: Record = { + general: 'preferences', + shortcuts: 'keyboard', + audio: 'audio', + account: 'profile', + personalization: 'personalization', + notifications: 'notifications', + archived_chats: 'archived-chats', + data_controls: 'data-controls', + usage: 'usage', + about: 'about', + 'admin:general': 'admin/general', + 'admin:authentication': 'admin/authentication', + 'admin:features': 'admin/features', + 'admin:tasks': 'admin/tasks', + 'admin:interface': 'admin/interface', + 'admin:analytics': 'admin/analytics', + 'admin:db': 'admin/database', + 'admin:connections': 'admin/connections', + 'admin:models': 'admin/models', + 'admin:subagents': 'admin/sub-agents', + 'admin:integrations': 'admin/integrations', + 'admin:web': 'admin/web-search', + 'admin:documents': 'admin/documents', + 'admin:images': 'admin/images', + 'admin:audio': 'admin/audio', + 'admin:code-execution': 'admin/code-execution', + 'admin:pipelines': 'admin/pipelines' +}; + +// Older admin addresses that point at tabs that were merged or renamed. +const SETTINGS_ALIASES: Record = { + 'admin/apps': 'admin:integrations', + 'admin/videos': 'admin:images', + 'admin/db': 'admin:db', + 'admin/web': 'admin:web', + 'admin/subagents': 'admin:subagents' +}; + +export const settingsPath = (tab: string) => + `/settings/${SETTINGS_SLUGS[tab] ?? SETTINGS_SLUGS.general}`; + +/** The tab (and model, for /settings/admin/models/) a settings address points at. */ +export const parseSettingsPath = (pathname: string): { tab: string; modelId?: string } | null => { + const match = pathname.match(/^\/settings(?:\/(.*?))?\/?$/); + if (!match) return null; + const rest = match[1] ?? ''; + if (!rest) return { tab: 'general' }; + + const model = rest.match(/^admin\/models\/(.+)$/); + if (model) return { tab: 'admin:models', modelId: decodeURIComponent(model[1]) }; + + const tab = + Object.keys(SETTINGS_SLUGS).find((id) => SETTINGS_SLUGS[id] === rest) ?? + SETTINGS_ALIASES[rest] ?? + Object.keys(SETTINGS_SLUGS).find((id) => id === rest || id === rest.replace('/', ':')); + return { tab: tab ?? 'general' }; +}; diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index 6d70ff713b..5bf121f95d 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -3,7 +3,7 @@ import { onMount, tick, getContext, setContext } from 'svelte'; import type { AppLayoutContext } from '$lib/types'; - import { goto } from '$app/navigation'; + import { afterNavigate, beforeNavigate, goto, pushState, replaceState } from '$app/navigation'; import { page } from '$app/stores'; import { fade } from 'svelte/transition'; @@ -60,6 +60,7 @@ import Spinner from '$lib/components/common/Spinner.svelte'; import { loadKeybindings, matchKeybinding, Shortcut } from '$lib/shortcuts'; import { canUseTemporaryChats } from '$lib/utils/settings-access'; + import { parseSettingsPath, settingsPath } from '$lib/utils/settings-routes'; const i18n = getContext('i18n'); const getUserSettings = getContext<(token: string) => Promise>('getUserSettings'); @@ -203,22 +204,75 @@ } handledSettingsUrl = urlKey; - showSettings.set( + const tab = requestedSettings.startsWith('admin:') && $user?.role !== 'admin' ? 'general' - : requestedSettings - ); + : requestedSettings; + const modelId = $page.url.searchParams.get('model'); + // Strip the params before opening: opening adds the settings address to history, and + // this replace would otherwise overwrite it. const params = new URLSearchParams($page.url.searchParams); params.delete('settings'); + params.delete('model'); const query = params.toString(); await goto(`${$page.url.pathname}${query ? `?${query}` : ''}${$page.url.hash}`, { replaceState: true, noScroll: true, keepFocus: true }); + + showSettings.set(tab === 'admin:models' && modelId ? { tab, state: { id: modelId } } : tab); }; + // Settings has its own address. Opening it adds one history entry, switching tabs replaces + // it, and closing steps back off it, so Back closes settings in one step. + let settingsEntry = false; + let settingsOpen = false; + let stepBackPending = false; + let leavingSettings = false; + + const syncSettingsUrl = (tab: string) => { + const path = settingsPath(tab); + if (settingsEntry) { + if (location.pathname !== path) replaceState(path, { ...$page.state, settings: true }); + } else { + pushState(path, { ...$page.state, settings: true }); + } + }; + + $: onSettingsEntry(!!$page.state.settings); + const onSettingsEntry = (entry: boolean) => { + if (entry === settingsEntry) return; + settingsEntry = entry; + if (!entry) { + // Left the settings entry: Back, or the step back taken when settings closed. + if (stepBackPending) stepBackPending = false; + else if ($showSettings) showSettings.set(false); + } else if (!$showSettings) { + // Forward (or Back) onto a settings entry reopens it on that tab. + showSettings.set(parseSettingsPath(location.pathname)?.tab ?? 'general'); + } + }; + + $: onSettingsOpen(!!$showSettings); + const onSettingsOpen = (open: boolean) => { + if (open === settingsOpen) return; + settingsOpen = open; + if (!open && settingsEntry && !leavingSettings) { + stepBackPending = true; + history.back(); + } + }; + + // Following a link out of settings keeps the settings entry behind it in history. + beforeNavigate((navigation) => { + if (navigation.type !== 'popstate' && settingsEntry) leavingSettings = true; + }); + afterNavigate(() => { + leavingSettings = false; + }); + const gotoAuth = async () => { const currentUrl = `${$page.url.pathname}${$page.url.search}`; await goto(`/auth?redirect=${encodeURIComponent(currentUrl)}`); @@ -468,7 +522,7 @@ {#if settingsRequested} - + {/if} diff --git a/src/routes/(app)/settings/[...path]/+page.svelte b/src/routes/(app)/settings/[...path]/+page.svelte new file mode 100644 index 0000000000..a508de8500 --- /dev/null +++ b/src/routes/(app)/settings/[...path]/+page.svelte @@ -0,0 +1,15 @@ +