diff --git a/web/src/features/system-settings/maintenance/config.ts b/web/src/features/system-settings/maintenance/config.ts index a95e946..3d01827 100644 --- a/web/src/features/system-settings/maintenance/config.ts +++ b/web/src/features/system-settings/maintenance/config.ts @@ -16,6 +16,8 @@ along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ +import { parseHeaderNavLink } from '@/lib/nav-modules' + export type HeaderNavAccessConfig = { enabled: boolean requireAuth: boolean @@ -28,7 +30,9 @@ export type HeaderNavModulesConfig = { rankings: HeaderNavAccessConfig docs: boolean about: boolean - [key: string]: boolean | HeaderNavAccessConfig + homeLink: string + pricingLink: string + [key: string]: boolean | HeaderNavAccessConfig | string } export type SidebarSectionConfig = { @@ -40,6 +44,8 @@ export type SidebarModulesAdminConfig = Record export const HEADER_NAV_DEFAULT: HeaderNavModulesConfig = { home: true, + homeLink: '', + pricingLink: '', console: true, pricing: { enabled: true, @@ -151,6 +157,10 @@ export function parseHeaderNavModules( } Object.entries(parsed).forEach(([key, raw]) => { + if (key === 'homeLink' || key === 'pricingLink') { + result[key] = parseHeaderNavLink(raw) + return + } if (key === 'pricing') { result.pricing = parseAccessModule(raw, base.pricing) return diff --git a/web/src/features/system-settings/maintenance/header-navigation-section.tsx b/web/src/features/system-settings/maintenance/header-navigation-section.tsx index 43f0085..074b8ba 100644 --- a/web/src/features/system-settings/maintenance/header-navigation-section.tsx +++ b/web/src/features/system-settings/maintenance/header-navigation-section.tsx @@ -27,10 +27,13 @@ import { FormControl, FormDescription, FormField, + FormItem, FormLabel, FormMessage, } from '@/components/ui/form' +import { Input } from '@/components/ui/input' import { Switch } from '@/components/ui/switch' +import { parseHeaderNavLink } from '@/lib/nav-modules' import { SettingsControlChildren, @@ -48,8 +51,17 @@ import { serializeHeaderNavModules, } from './config' +const navigationLinkSchema = z + .string() + .trim() + .refine((value) => value === '' || parseHeaderNavLink(value) !== '', { + error: 'Enter an HTTP(S) URL or a path starting with /.', + }) + const headerNavSchema = z.object({ home: z.boolean(), + homeLink: navigationLinkSchema, + pricingLink: navigationLinkSchema, console: z.boolean(), pricingEnabled: z.boolean(), pricingRequireAuth: z.boolean(), @@ -69,6 +81,8 @@ type HeaderNavigationSectionProps = { const toFormValues = (config: HeaderNavModulesConfig): HeaderNavFormValues => ({ home: config.home === undefined ? HEADER_NAV_DEFAULT.home : Boolean(config.home), + homeLink: config.homeLink, + pricingLink: config.pricingLink, console: config.console === undefined ? HEADER_NAV_DEFAULT.console @@ -118,6 +132,8 @@ export function HeaderNavigationSection({ const payload: HeaderNavModulesConfig = { ...config, home: values.home, + homeLink: values.homeLink, + pricingLink: values.pricingLink, console: values.console, docs: values.docs, about: values.about, @@ -149,7 +165,7 @@ export function HeaderNavigationSection({ } const simpleModules: Array<{ - key: keyof HeaderNavFormValues + key: 'home' | 'console' | 'docs' | 'about' title: string description: string }> = [ @@ -176,8 +192,8 @@ export function HeaderNavigationSection({ ] const accessModules: Array<{ - enabledKey: keyof HeaderNavFormValues - requireAuthKey: keyof HeaderNavFormValues + enabledKey: 'pricingEnabled' | 'rankingsEnabled' + requireAuthKey: 'pricingRequireAuth' | 'rankingsRequireAuth' requireAuthDependsOn: 'pricingEnabled' | 'rankingsEnabled' title: string description: string @@ -208,6 +224,37 @@ export function HeaderNavigationSection({ }, ] + const renderLinkField = ( + name: 'homeLink' | 'pricingLink', + label: string, + placeholder: string + ) => ( + + ( + + {label} + + + + + {t('Leave blank to use the default link.')} + + + + )} + /> + + ) + return (
@@ -220,28 +267,38 @@ export function HeaderNavigationSection({ saveLabel='Save navigation' />
- {simpleModules.map((module) => ( - ( - - - {module.title} - {module.description} - - - - - - - )} - /> - ))} + {simpleModules.map((module) => { + const toggle = ( + ( + + + {module.title} + {module.description} + + + + + + + )} + /> + ) + if (module.key !== 'home') return toggle + + return ( + + {toggle} + {renderLinkField('homeLink', t('Home link'), '/')} + + ) + })}
@@ -291,6 +348,12 @@ export function HeaderNavigationSection({ )} /> + {module.enabledKey === 'pricingEnabled' && + renderLinkField( + 'pricingLink', + t('Model Square link'), + '/pricing' + )} ))}
diff --git a/web/src/hooks/use-top-nav-links.ts b/web/src/hooks/use-top-nav-links.ts index 5a5baa2..f1f54b3 100644 --- a/web/src/hooks/use-top-nav-links.ts +++ b/web/src/hooks/use-top-nav-links.ts @@ -64,7 +64,8 @@ export function useTopNavLinks(): TopNavLink[] { // Home if (modules?.home !== false) { - links.push({ title: t('Home'), href: '/' }) + const href = modules.homeLink || '/' + links.push({ title: t('Home'), href, external: !href.startsWith('/') }) } // Console -> /dashboard (new console path) @@ -76,7 +77,13 @@ export function useTopNavLinks(): TopNavLink[] { const pricing = modules?.pricing if (pricing && typeof pricing === 'object' && pricing.enabled) { const requiresAuth = pricing.requireAuth && !isAuthed - links.push({ title: t('Model Square'), href: '/pricing', requiresAuth }) + const href = requiresAuth ? '/pricing' : modules.pricingLink || '/pricing' + links.push({ + title: t('Model Square'), + href, + requiresAuth, + external: !href.startsWith('/'), + }) } // Rankings diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 2072019..b0cf14e 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "Home link", + "Model Square link": "Model Square link", + "Leave blank to use the default link.": "Leave blank to use the default link.", + "Enter an HTTP(S) URL or a path starting with /.": "Enter an HTTP(S) URL or a path starting with /.", "_copy": "_copy", ",": ", ", ", and": ", and", diff --git a/web/src/i18n/locales/fr.json b/web/src/i18n/locales/fr.json index be558e2..1f77f7b 100644 --- a/web/src/i18n/locales/fr.json +++ b/web/src/i18n/locales/fr.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "Lien de la page d’accueil", + "Model Square link": "Lien du catalogue des modèles", + "Leave blank to use the default link.": "Laissez vide pour utiliser le lien par défaut.", + "Enter an HTTP(S) URL or a path starting with /.": "Saisissez une URL HTTP(S) ou un chemin commençant par /.", "_copy": "_copie", ",": ", ", ", and": ", et", diff --git a/web/src/i18n/locales/ja.json b/web/src/i18n/locales/ja.json index c75ce5f..f59d0fe 100644 --- a/web/src/i18n/locales/ja.json +++ b/web/src/i18n/locales/ja.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "ホームのリンク", + "Model Square link": "モデル一覧のリンク", + "Leave blank to use the default link.": "空欄の場合は既定のリンクを使用します。", + "Enter an HTTP(S) URL or a path starting with /.": "HTTP(S) URL または / で始まるパスを入力してください。", "_copy": "_copy", ",": "、", ", and": "、および", diff --git a/web/src/i18n/locales/ru.json b/web/src/i18n/locales/ru.json index c03dbe8..6711253 100644 --- a/web/src/i18n/locales/ru.json +++ b/web/src/i18n/locales/ru.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "Ссылка на главную страницу", + "Model Square link": "Ссылка на каталог моделей", + "Leave blank to use the default link.": "Оставьте поле пустым, чтобы использовать ссылку по умолчанию.", + "Enter an HTTP(S) URL or a path starting with /.": "Введите URL HTTP(S) или путь, начинающийся с /.", "_copy": "_копировать", ",": ", ", ", and": ", и", diff --git a/web/src/i18n/locales/vi.json b/web/src/i18n/locales/vi.json index 698b73a..29f9af9 100644 --- a/web/src/i18n/locales/vi.json +++ b/web/src/i18n/locales/vi.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "Liên kết trang chủ", + "Model Square link": "Liên kết danh mục mô hình", + "Leave blank to use the default link.": "Để trống để dùng liên kết mặc định.", + "Enter an HTTP(S) URL or a path starting with /.": "Nhập URL HTTP(S) hoặc đường dẫn bắt đầu bằng /.", "_copy": "_bản sao", ",": ", ", ", and": ", và", diff --git a/web/src/i18n/locales/zh-TW.json b/web/src/i18n/locales/zh-TW.json index c5db38c..40c4246 100644 --- a/web/src/i18n/locales/zh-TW.json +++ b/web/src/i18n/locales/zh-TW.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "首頁連結", + "Model Square link": "模型廣場連結", + "Leave blank to use the default link.": "留空以使用預設連結。", + "Enter an HTTP(S) URL or a path starting with /.": "請輸入 HTTP(S) 網址或以 / 開頭的站內路徑。", "_copy": "_複製", ",": ",", ", and": ",和", diff --git a/web/src/i18n/locales/zh.json b/web/src/i18n/locales/zh.json index 6c90e1f..c593915 100644 --- a/web/src/i18n/locales/zh.json +++ b/web/src/i18n/locales/zh.json @@ -3,6 +3,10 @@ "360": "360", "1000": "1000", "10000": "10000", + "Home link": "主页链接", + "Model Square link": "模型广场链接", + "Leave blank to use the default link.": "留空则使用默认链接。", + "Enter an HTTP(S) URL or a path starting with /.": "请输入 HTTP(S) 链接或以 / 开头的站内路径。", "_copy": "_复制", ",": ",", ", and": ",和", diff --git a/web/src/lib/__tests__/navigation-links.test.ts b/web/src/lib/__tests__/navigation-links.test.ts new file mode 100644 index 0000000..68b37d0 --- /dev/null +++ b/web/src/lib/__tests__/navigation-links.test.ts @@ -0,0 +1,69 @@ +/* +Copyright (C) 2023-2026 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ +import { describe, expect, it } from 'vitest' + +import { + parseHeaderNavModules as parseSettings, + serializeHeaderNavModules, +} from '@/features/system-settings/maintenance/config' +import { parseHeaderNavLink, parseHeaderNavModules } from '@/lib/nav-modules' + +describe('top navigation links', () => { + it('preserves both links and access flags through the settings parsers', () => { + const config = { + home: false, + homeLink: 'https://btbyte.cc/', + pricingLink: '/rankings?period=week#models', + pricing: { enabled: true, requireAuth: true }, + } + const settings = parseSettings(JSON.stringify(config)) + expect( + parseHeaderNavModules(serializeHeaderNavModules(settings)) + ).toMatchObject(config) + }) + + it('keeps the link defaults for existing saved settings', () => { + for (const parse of [parseSettings, parseHeaderNavModules]) { + expect(parse('{"home":true,"pricing":false}')).toMatchObject({ + home: true, + homeLink: '', + pricingLink: '', + pricing: { enabled: false, requireAuth: false }, + }) + } + }) + + it.each([ + 'javascript:alert(1)', + 'data:text/plain,test', + '//example.com', + '/\\example.com', + 'https://', + 'https://name:password@example.com', + 'not-a-link', + ])('rejects invalid links from saved settings (%s)', (value) => { + expect(parseHeaderNavLink(value)).toBe('') + expect( + parseHeaderNavModules({ homeLink: value, pricingLink: value }) + ).toMatchObject({ + homeLink: '', + pricingLink: '', + }) + }) +}) diff --git a/web/src/lib/nav-modules.ts b/web/src/lib/nav-modules.ts index bf64919..54bc127 100644 --- a/web/src/lib/nav-modules.ts +++ b/web/src/lib/nav-modules.ts @@ -31,11 +31,15 @@ export type HeaderNavModules = { rankings: ModuleAccess docs: boolean about: boolean - [key: string]: boolean | ModuleAccess + homeLink: string + pricingLink: string + [key: string]: boolean | ModuleAccess | string } const DEFAULT_HEADER_NAV_MODULES: HeaderNavModules = { home: true, + homeLink: '', + pricingLink: '', console: true, pricing: { enabled: true, requireAuth: false }, rankings: { enabled: true, requireAuth: false }, @@ -106,12 +110,30 @@ function parseHeaderNavRecord(raw: unknown): Record | null { } } +export function parseHeaderNavLink(value: unknown): string { + if (typeof value !== 'string') return '' + const link = value.trim() + if (!link || /[\p{White_Space}\p{Cc}\\]/u.test(link)) return '' + if (link.startsWith('/') && !link.startsWith('//')) return link + if (!/^https?:\/\//i.test(link)) return '' + try { + const url = new URL(link) + return url.hostname && !url.username && !url.password ? link : '' + } catch { + return '' + } +} + export function parseHeaderNavModules(raw: unknown): HeaderNavModules { const result = cloneHeaderNavDefaults() const parsed = parseHeaderNavRecord(raw) if (!parsed) return result Object.entries(parsed).forEach(([key, value]) => { + if (key === 'homeLink' || key === 'pricingLink') { + result[key] = parseHeaderNavLink(value) + return + } if (key === 'pricing') { result.pricing = parseAccess(value, result.pricing) return