Compare commits

...

4 Commits

11 changed files with 418 additions and 44 deletions
+149 -8
View File
@@ -35,6 +35,7 @@ import { versionApi } from '@/services/api';
import { triggerHeaderRefresh } from '@/hooks/useHeaderRefresh';
import { LANGUAGE_LABEL_KEYS, LANGUAGE_ORDER } from '@/utils/constants';
import { isSupportedLanguage } from '@/utils/language';
import type { Theme } from '@/types';
const sidebarIcons: Record<string, ReactNode> = {
dashboard: <IconLayoutDashboard size={18} />,
@@ -117,6 +118,12 @@ const headerIcons = {
<path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9z" />
</svg>
),
whiteTheme: (
<svg {...headerIconProps}>
<circle cx="12" cy="12" r="7" />
<circle cx="12" cy="12" r="3" fill="currentColor" stroke="none" />
</svg>
),
autoTheme: (
<svg {...headerIconProps}>
<defs>
@@ -145,6 +152,39 @@ const headerIcons = {
),
};
const THEME_CARDS: Array<{
key: Theme;
labelKey: string;
colors: { bg: string; card: string; border: string; text: string; textMuted: string };
}> = [
{
key: 'auto',
labelKey: 'theme.auto',
colors: {
bg: 'linear-gradient(135deg, #faf9f5 0 50%, #151412 50% 100%)',
card: 'linear-gradient(135deg, #f0eee8 0 50%, #1d1b18 50% 100%)',
border: '#bdb6ae',
text: '#2d2a26',
textMuted: 'linear-gradient(135deg, #a29c95 0 50%, #9c958d 50% 100%)',
},
},
{
key: 'white',
labelKey: 'theme.white',
colors: { bg: '#ffffff', card: '#ffffff', border: '#e5e5e5', text: '#2d2a26', textMuted: '#a29c95' },
},
{
key: 'light',
labelKey: 'theme.light',
colors: { bg: '#faf9f5', card: '#f0eee8', border: '#e3e1db', text: '#2d2a26', textMuted: '#a29c95' },
},
{
key: 'dark',
labelKey: 'theme.dark',
colors: { bg: '#151412', card: '#1d1b18', border: '#3a3530', text: '#f6f4f1', textMuted: '#9c958d' },
},
];
const parseVersionSegments = (version?: string | null) => {
if (!version) return null;
const cleaned = version.trim().replace(/^v/i, '');
@@ -186,7 +226,7 @@ export function MainLayout() {
const clearCache = useConfigStore((state) => state.clearCache);
const theme = useThemeStore((state) => state.theme);
const cycleTheme = useThemeStore((state) => state.cycleTheme);
const setTheme = useThemeStore((state) => state.setTheme);
const language = useLanguageStore((state) => state.language);
const setLanguage = useLanguageStore((state) => state.setLanguage);
@@ -194,9 +234,11 @@ export function MainLayout() {
const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
const [checkingVersion, setCheckingVersion] = useState(false);
const [languageMenuOpen, setLanguageMenuOpen] = useState(false);
const [themeMenuOpen, setThemeMenuOpen] = useState(false);
const [brandExpanded, setBrandExpanded] = useState(true);
const contentRef = useRef<HTMLDivElement | null>(null);
const languageMenuRef = useRef<HTMLDivElement | null>(null);
const themeMenuRef = useRef<HTMLDivElement | null>(null);
const brandCollapseTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const headerRef = useRef<HTMLElement | null>(null);
@@ -304,6 +346,32 @@ export function MainLayout() {
};
}, [languageMenuOpen]);
useEffect(() => {
if (!themeMenuOpen) {
return;
}
const handlePointerDown = (event: MouseEvent) => {
if (!themeMenuRef.current?.contains(event.target as Node)) {
setThemeMenuOpen(false);
}
};
const handleEscape = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
setThemeMenuOpen(false);
}
};
document.addEventListener('mousedown', handlePointerDown);
document.addEventListener('keydown', handleEscape);
return () => {
document.removeEventListener('mousedown', handlePointerDown);
document.removeEventListener('keydown', handleEscape);
};
}, [themeMenuOpen]);
const handleBrandClick = useCallback(() => {
if (!brandExpanded) {
setBrandExpanded(true);
@@ -319,8 +387,22 @@ export function MainLayout() {
const toggleLanguageMenu = useCallback(() => {
setLanguageMenuOpen((prev) => !prev);
setThemeMenuOpen(false);
}, []);
const toggleThemeMenu = useCallback(() => {
setThemeMenuOpen((prev) => !prev);
setLanguageMenuOpen(false);
}, []);
const handleThemeSelect = useCallback(
(nextTheme: Theme) => {
setTheme(nextTheme);
setThemeMenuOpen(false);
},
[setTheme]
);
const handleLanguageSelect = useCallback(
(nextLanguage: string) => {
if (!isSupportedLanguage(nextLanguage)) {
@@ -566,13 +648,72 @@ export function MainLayout() {
</div>
)}
</div>
<Button variant="ghost" size="sm" onClick={cycleTheme} title={t('theme.switch')}>
{theme === 'auto'
? headerIcons.autoTheme
: theme === 'dark'
? headerIcons.moon
: headerIcons.sun}
</Button>
<div className={`theme-menu ${themeMenuOpen ? 'open' : ''}`} ref={themeMenuRef}>
<Button
variant="ghost"
size="sm"
onClick={toggleThemeMenu}
title={t('theme.switch')}
aria-label={t('theme.switch')}
aria-haspopup="menu"
aria-expanded={themeMenuOpen}
>
{theme === 'auto'
? headerIcons.autoTheme
: theme === 'dark'
? headerIcons.moon
: theme === 'white'
? headerIcons.whiteTheme
: headerIcons.sun}
</Button>
{themeMenuOpen && (
<div className="notification entering theme-menu-popover" role="menu" aria-label={t('theme.switch')}>
{THEME_CARDS.map((tc) => (
<button
key={tc.key}
type="button"
className={`theme-card ${theme === tc.key ? 'active' : ''}`}
onClick={() => handleThemeSelect(tc.key)}
role="menuitemradio"
aria-checked={theme === tc.key}
>
<div
className="theme-card-preview"
style={{ background: tc.colors.bg, border: `1px solid ${tc.colors.border}` }}
>
<div
className="theme-card-header"
style={{
background: tc.colors.card,
borderBottom: `1px solid ${tc.colors.border}`,
}}
/>
<div className="theme-card-body">
<div
className="theme-card-sidebar"
style={{
background: tc.colors.card,
borderRight: `1px solid ${tc.colors.border}`,
}}
/>
<div className="theme-card-content" style={{ background: tc.colors.bg }}>
<div
className="theme-card-line"
style={{ background: tc.colors.textMuted }}
/>
<div
className="theme-card-line short"
style={{ background: tc.colors.textMuted }}
/>
</div>
</div>
</div>
<span className="theme-card-label">{t(tc.labelKey)}</span>
</button>
))}
</div>
)}
</div>
<Button variant="ghost" size="sm" onClick={logout} title={t('header.logout')}>
{headerIcons.logout}
</Button>
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useRef, useState, type ChangeEvent, type RefObject } from 'react';
import { useTranslation } from 'react-i18next';
import { authFilesApi } from '@/services/api';
import { authFilesApi, isAuthFileInvalidJsonObjectError } from '@/services/api';
import { apiClient } from '@/services/api/client';
import { useNotificationStore } from '@/stores';
import type { AuthFileItem } from '@/types';
@@ -60,6 +60,16 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
const fileInputRef = useRef<HTMLInputElement | null>(null);
const selectionCount = selectedFiles.size;
const resolveStatusUpdateErrorMessage = useCallback(
(err: unknown) => {
if (isAuthFileInvalidJsonObjectError(err)) {
return t('auth_files.prefix_proxy_invalid_json');
}
return err instanceof Error ? err.message : '';
},
[t]
);
const toggleSelect = useCallback((name: string) => {
setSelectedFiles((prev) => {
const next = new Set(prev);
@@ -337,10 +347,9 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
setFiles((prev) => prev.map((f) => (f.name === name ? { ...f, disabled: nextDisabled } : f)));
try {
const res = await authFilesApi.setStatus(name, nextDisabled);
setFiles((prev) =>
prev.map((f) => (f.name === name ? { ...f, disabled: res.disabled } : f))
);
await authFilesApi.setStatus(name, nextDisabled);
await loadFiles();
void refreshKeyStats().catch(() => {});
showNotification(
enabled
? t('auth_files.status_enabled_success', { name })
@@ -348,7 +357,7 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
'success'
);
} catch (err: unknown) {
const errorMessage = err instanceof Error ? err.message : '';
const errorMessage = resolveStatusUpdateErrorMessage(err);
setFiles((prev) =>
prev.map((f) => (f.name === name ? { ...f, disabled: previousDisabled } : f))
);
@@ -362,7 +371,7 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
});
}
},
[showNotification, t]
[loadFiles, refreshKeyStats, resolveStatusUpdateErrorMessage, showNotification, t]
);
const batchSetStatus = useCallback(
@@ -372,6 +381,9 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
const targetNames = new Set(uniqueNames);
const nextDisabled = !enabled;
const previousDisabled = new Map(
files.map((file) => [file.name, file.disabled === true] as const)
);
setFiles((prev) =>
prev.map((file) =>
@@ -385,31 +397,26 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
let successCount = 0;
let failCount = 0;
const failedNames = new Set<string>();
const confirmedDisabled = new Map<string, boolean>();
results.forEach((result, index) => {
const name = uniqueNames[index];
results.forEach((result) => {
if (result.status === 'fulfilled') {
successCount++;
confirmedDisabled.set(name, result.value.disabled);
} else {
failCount++;
failedNames.add(name);
}
});
setFiles((prev) =>
prev.map((file) => {
if (failedNames.has(file.name)) {
return { ...file, disabled: !nextDisabled };
}
if (confirmedDisabled.has(file.name)) {
return { ...file, disabled: confirmedDisabled.get(file.name) };
}
return file;
})
);
if (successCount > 0) {
await loadFiles();
void refreshKeyStats().catch(() => {});
} else {
setFiles((prev) =>
prev.map((file) => {
if (!targetNames.has(file.name)) return file;
return { ...file, disabled: previousDisabled.get(file.name) === true };
})
);
}
if (failCount === 0) {
showNotification(t('auth_files.batch_status_success', { count: successCount }), 'success');
@@ -422,7 +429,7 @@ export function useAuthFilesData(options: UseAuthFilesDataOptions): UseAuthFiles
deselectAll();
},
[deselectAll, showNotification, t]
[deselectAll, files, loadFiles, refreshKeyStats, showNotification, t]
);
const batchDelete = useCallback(
@@ -260,8 +260,7 @@ export function useAuthFilesPrefixProxyEditor(
});
try {
const file = new File([payload], name, { type: 'application/json' });
await authFilesApi.upload(file);
await authFilesApi.saveText(name, payload);
showNotification(t('auth_files.prefix_proxy_saved_success', { name }), 'success');
await loadFiles();
await loadKeyStats();
+1
View File
@@ -1400,6 +1400,7 @@
"theme": {
"switch": "Theme",
"light": "Light",
"white": "Pure White",
"dark": "Dark",
"switch_to_light": "Switch to light mode",
"switch_to_dark": "Switch to dark mode",
+1
View File
@@ -1405,6 +1405,7 @@
"theme": {
"switch": "Тема",
"light": "Светлая",
"white": "Чисто-белая",
"dark": "Тёмная",
"switch_to_light": "Переключиться на светлую тему",
"switch_to_dark": "Переключиться на тёмную тему",
+1
View File
@@ -1400,6 +1400,7 @@
"theme": {
"switch": "主题",
"light": "亮色",
"white": "纯白",
"dark": "暗色",
"switch_to_light": "切换到亮色模式",
"switch_to_dark": "切换到暗色模式",
+49 -2
View File
@@ -9,12 +9,39 @@ import type { OAuthModelAliasEntry } from '@/types';
type StatusError = { status?: number };
type AuthFileStatusResponse = { status: string; disabled: boolean };
export const AUTH_FILE_INVALID_JSON_OBJECT_ERROR = 'AUTH_FILE_INVALID_JSON_OBJECT';
const getStatusCode = (err: unknown): number | undefined => {
if (!err || typeof err !== 'object') return undefined;
if ('status' in err) return (err as StatusError).status;
return undefined;
};
const parseAuthFileJsonObject = (rawText: string): Record<string, unknown> => {
const trimmed = rawText.trim();
let parsed: unknown;
try {
parsed = JSON.parse(trimmed) as unknown;
} catch {
throw new Error(AUTH_FILE_INVALID_JSON_OBJECT_ERROR);
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error(AUTH_FILE_INVALID_JSON_OBJECT_ERROR);
}
return { ...(parsed as Record<string, unknown>) };
};
const saveAuthFileText = async (name: string, text: string) => {
const file = new File([text], name, { type: 'application/json' });
await authFilesApi.upload(file);
};
export const isAuthFileInvalidJsonObjectError = (err: unknown): boolean =>
err instanceof Error && err.message === AUTH_FILE_INVALID_JSON_OBJECT_ERROR;
const normalizeOauthExcludedModels = (payload: unknown): Record<string, string[]> => {
if (!payload || typeof payload !== 'object') return {};
@@ -105,8 +132,18 @@ const OAUTH_MODEL_ALIAS_ENDPOINT = '/oauth-model-alias';
export const authFilesApi = {
list: () => apiClient.get<AuthFilesResponse>('/auth-files'),
setStatus: (name: string, disabled: boolean) =>
apiClient.patch<AuthFileStatusResponse>('/auth-files/status', { name, disabled }),
async setStatus(name: string, disabled: boolean): Promise<AuthFileStatusResponse> {
const json = await authFilesApi.downloadJsonObject(name);
if (disabled) {
json.disabled = true;
} else {
delete json.disabled;
}
await authFilesApi.saveJsonObject(name, json);
return { status: disabled ? 'disabled' : 'enabled', disabled };
},
upload: (file: File) => {
const formData = new FormData();
@@ -126,6 +163,16 @@ export const authFilesApi = {
return blob.text();
},
async downloadJsonObject(name: string): Promise<Record<string, unknown>> {
const rawText = await authFilesApi.downloadText(name);
return parseAuthFileJsonObject(rawText);
},
saveText: (name: string, text: string) => saveAuthFileText(name, text),
saveJsonObject: (name: string, json: Record<string, unknown>) =>
saveAuthFileText(name, JSON.stringify(json)),
// OAuth 排除模型
async getOauthExcludedModels(): Promise<Record<string, string[]>> {
const data = await apiClient.get('/oauth-excluded-models');
+25 -6
View File
@@ -25,12 +25,28 @@ const getSystemTheme = (): ResolvedTheme => {
return 'light';
};
const applyTheme = (resolved: ResolvedTheme) => {
const resolveTheme = (theme: Theme): ResolvedTheme | 'white' => {
if (theme === 'auto') {
return getSystemTheme();
}
if (theme === 'white') {
return 'white';
}
return theme;
};
const applyTheme = (resolved: ResolvedTheme | 'white') => {
if (resolved === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
} else {
document.documentElement.removeAttribute('data-theme');
return;
}
if (resolved === 'white') {
document.documentElement.setAttribute('data-theme', 'white');
return;
}
document.documentElement.removeAttribute('data-theme');
};
export const useThemeStore = create<ThemeState>()(
@@ -40,14 +56,17 @@ export const useThemeStore = create<ThemeState>()(
resolvedTheme: 'light',
setTheme: (theme) => {
const resolved: ResolvedTheme = theme === 'auto' ? getSystemTheme() : theme;
const resolved = resolveTheme(theme);
applyTheme(resolved);
set({ theme, resolvedTheme: resolved });
set({
theme,
resolvedTheme: resolved === 'white' ? 'light' : resolved,
});
},
cycleTheme: () => {
const { theme, setTheme } = get();
const order: Theme[] = ['light', 'dark', 'auto'];
const order: Theme[] = ['light', 'white', 'dark', 'auto'];
const currentIndex = order.indexOf(theme);
const nextTheme = order[(currentIndex + 1) % order.length];
setTheme(nextTheme);
+105
View File
@@ -251,6 +251,111 @@
}
}
.theme-menu {
position: relative;
display: inline-flex;
align-items: center;
.theme-menu-popover {
position: absolute;
top: calc(100% + 8px);
right: 0;
z-index: $z-dropdown;
padding: $spacing-sm $spacing-sm $spacing-xs;
display: flex;
gap: $spacing-xs;
width: max-content;
max-width: calc(100vw - 16px);
}
.theme-card {
border: 2px solid transparent;
border-radius: $radius-md;
background: transparent;
cursor: pointer;
padding: 6px 6px 4px;
display: flex;
flex-direction: column;
align-items: center;
gap: 4px;
transition: border-color $transition-fast, background-color $transition-fast;
&:hover {
background: var(--bg-secondary);
}
&:focus-visible {
outline: none;
box-shadow: 0 0 0 2px rgba($primary-color, 0.22);
}
&.active {
border-color: var(--primary-color);
}
}
.theme-card-preview {
width: 72px;
height: 52px;
border-radius: $radius-sm;
overflow: hidden;
display: flex;
flex-direction: column;
}
.theme-card-header {
height: 10px;
flex-shrink: 0;
}
.theme-card-body {
flex: 1;
display: flex;
min-height: 0;
}
.theme-card-sidebar {
width: 16px;
flex-shrink: 0;
}
.theme-card-content {
flex: 1;
padding: 5px 8px;
display: flex;
flex-direction: column;
gap: 4px;
justify-content: center;
}
.theme-card-line {
height: 3px;
border-radius: 1px;
&.short {
width: 60%;
}
}
.theme-card-label {
font-size: 11px;
color: var(--text-primary);
font-weight: 500;
white-space: nowrap;
}
@media (max-width: $breakpoint-mobile) {
.theme-menu-popover {
right: auto;
left: 50%;
transform: translateX(-50%);
display: grid;
grid-template-columns: repeat(2, max-content);
justify-content: center;
}
}
}
svg {
display: block;
}
+53
View File
@@ -56,6 +56,59 @@
--accent-tertiary: var(--bg-tertiary);
}
// 纯白主题
[data-theme='white'] {
--bg-secondary: #ffffff;
--bg-primary: #ffffff;
--bg-tertiary: #f6f6f6;
--bg-hover: var(--bg-tertiary);
--bg-quinary: #ffffff;
--bg-error-light: rgba(198, 87, 70, 0.08);
--text-primary: #2d2a26;
--text-secondary: #6d6760;
--text-tertiary: #a29c95;
--text-quaternary: #c0bab3;
--text-muted: var(--text-tertiary);
--border-color: #e5e5e5;
--border-secondary: var(--border-color);
--border-primary: #d9d9d9;
--border-hover: #cccccc;
--primary-color: #8b8680;
--primary-hover: #7f7a74;
--primary-active: #726d67;
--primary-contrast: #ffffff;
--success-color: #10b981;
--warning-color: #c65746;
--error-color: #c65746;
--danger-color: var(--error-color);
--info-color: var(--primary-color);
--warning-bg: rgba(198, 87, 70, 0.12);
--warning-border: rgba(198, 87, 70, 0.35);
--warning-text: var(--warning-color);
--success-badge-bg: #d1fae5;
--success-badge-text: #065f46;
--success-badge-border: #6ee7b7;
--failure-badge-bg: rgba(198, 87, 70, 0.14);
--failure-badge-text: #8a3a30;
--failure-badge-border: rgba(198, 87, 70, 0.35);
--count-badge-bg: rgba(139, 134, 128, 0.18);
--count-badge-text: var(--primary-active);
--shadow: 0 1px 2px 0 rgb(0 0 0 / 0.08);
--shadow-lg: 0 10px 18px -3px rgb(0 0 0 / 0.1);
--radius-md: 8px;
--accent-tertiary: var(--bg-tertiary);
}
// 深色主题(#191919
[data-theme='dark'] {
// 极简暖灰:深色模式(提升对比度与层级)
+1 -1
View File
@@ -2,7 +2,7 @@
* 通用类型定义
*/
export type Theme = 'light' | 'dark' | 'auto';
export type Theme = 'light' | 'white' | 'dark' | 'auto';
export type Language = 'zh-CN' | 'en' | 'ru';