|
| 1 | +import { type ReactNode, useEffect } from 'react'; |
| 2 | +import { Link } from '@tanstack/react-router'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { useAuth } from '../providers/AuthProvider'; |
| 5 | +import { useTheme } from '../providers/ThemeProvider'; |
| 6 | +import { useSiteSettings, defaultLogoUrl } from '../providers/SiteSettingsProvider'; |
| 7 | +import { |
| 8 | + ArrowLeft, |
| 9 | + LogOut, |
| 10 | + Languages, |
| 11 | + Sun, |
| 12 | + Moon, |
| 13 | + ShieldCheck, |
| 14 | +} from 'lucide-react'; |
| 15 | + |
| 16 | +interface ChatShellProps { |
| 17 | + children: ReactNode; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * 全屏沉浸式布局:仅一条窄顶栏(返回控制台 + 用户/主题/语言/退出), |
| 22 | + * 主区高度填满视口,不限制宽度、不加内边距。供 /chat 等需要最大化使用空间的页面挂载。 |
| 23 | + */ |
| 24 | +export function ChatShell({ children }: ChatShellProps) { |
| 25 | + const { user, logout, isAPIKeySession } = useAuth(); |
| 26 | + const { t, i18n } = useTranslation(); |
| 27 | + const { theme, toggleTheme } = useTheme(); |
| 28 | + const site = useSiteSettings(); |
| 29 | + const isAdmin = user?.role === 'admin'; |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + document.title = site.site_name || 'AirGate'; |
| 33 | + }, [site.site_name]); |
| 34 | + |
| 35 | + const toggleLanguage = () => { |
| 36 | + const nextLang = i18n.language === 'zh' ? 'en' : 'zh'; |
| 37 | + i18n.changeLanguage(nextLang); |
| 38 | + localStorage.setItem('lang', nextLang); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="flex flex-col h-screen" style={{ height: '100dvh' }}> |
| 43 | + <header className="flex items-center justify-between h-12 px-3 md:px-4 border-b border-border bg-bg shrink-0"> |
| 44 | + <div className="flex items-center gap-2 min-w-0"> |
| 45 | + <Link |
| 46 | + to="/" |
| 47 | + className="flex items-center gap-1.5 h-8 px-2 rounded-[10px] text-text-tertiary hover:text-text hover:bg-bg-hover transition-colors" |
| 48 | + title={t('nav.back_to_console')} |
| 49 | + > |
| 50 | + <ArrowLeft className="w-3.5 h-3.5 shrink-0" /> |
| 51 | + <span className="text-[12px] font-medium hidden sm:inline"> |
| 52 | + {t('nav.back_to_console')} |
| 53 | + </span> |
| 54 | + </Link> |
| 55 | + <div className="w-px h-4 bg-border mx-1" /> |
| 56 | + <img |
| 57 | + src={site.site_logo || defaultLogoUrl} |
| 58 | + alt="" |
| 59 | + className="w-6 h-6 rounded-sm flex-shrink-0 object-cover" |
| 60 | + /> |
| 61 | + <span className="text-[13px] font-semibold text-text tracking-tight truncate"> |
| 62 | + {site.site_name || 'AirGate'} |
| 63 | + </span> |
| 64 | + </div> |
| 65 | + |
| 66 | + <div className="flex items-center gap-1"> |
| 67 | + <button |
| 68 | + onClick={toggleLanguage} |
| 69 | + className="flex items-center justify-center h-8 px-2 rounded-[10px] text-text-tertiary hover:text-text-secondary hover:bg-bg-hover transition-colors gap-1.5" |
| 70 | + title={i18n.language === 'zh' ? 'Switch to English' : '切换为中文'} |
| 71 | + > |
| 72 | + <Languages className="w-3.5 h-3.5" /> |
| 73 | + <span className="text-[10px] font-mono uppercase hidden sm:inline"> |
| 74 | + {i18n.language === 'zh' ? 'EN' : '中文'} |
| 75 | + </span> |
| 76 | + </button> |
| 77 | + <button |
| 78 | + onClick={toggleTheme} |
| 79 | + className="flex items-center justify-center w-8 h-8 rounded-[10px] text-text-tertiary hover:text-text-secondary hover:bg-bg-hover transition-colors" |
| 80 | + title={theme === 'dark' ? '切换亮色模式' : '切换暗色模式'} |
| 81 | + > |
| 82 | + {theme === 'dark' ? <Sun className="w-3.5 h-3.5" /> : <Moon className="w-3.5 h-3.5" />} |
| 83 | + </button> |
| 84 | + |
| 85 | + <div className="w-px h-5 bg-border mx-1.5" /> |
| 86 | + |
| 87 | + <div className="flex items-center gap-2 pl-1"> |
| 88 | + {!isAPIKeySession && ( |
| 89 | + <div className="hidden md:block text-right"> |
| 90 | + <p className="text-xs font-medium text-text leading-tight"> |
| 91 | + {user?.username || user?.email?.split('@')[0]} |
| 92 | + </p> |
| 93 | + <p className="text-[10px] text-text-tertiary leading-tight"> |
| 94 | + {user?.email} |
| 95 | + </p> |
| 96 | + </div> |
| 97 | + )} |
| 98 | + {isAdmin ? ( |
| 99 | + <div className="flex items-center justify-center w-7 h-7 rounded-full bg-primary-subtle text-primary shrink-0"> |
| 100 | + <ShieldCheck className="w-3.5 h-3.5" /> |
| 101 | + </div> |
| 102 | + ) : ( |
| 103 | + <div className="flex items-center justify-center w-7 h-7 rounded-full bg-primary-subtle text-[11px] font-bold text-primary shrink-0"> |
| 104 | + {(user?.username || user?.email || 'U').charAt(0).toUpperCase()} |
| 105 | + </div> |
| 106 | + )} |
| 107 | + <button |
| 108 | + onClick={logout} |
| 109 | + className="flex items-center justify-center w-7 h-7 rounded-[10px] text-text-tertiary hover:text-danger hover:bg-danger-subtle transition-all" |
| 110 | + title={t('common.logout')} |
| 111 | + > |
| 112 | + <LogOut className="w-3.5 h-3.5" /> |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </header> |
| 117 | + |
| 118 | + <main className="flex-1 min-h-0 overflow-hidden"> |
| 119 | + {children} |
| 120 | + </main> |
| 121 | + </div> |
| 122 | + ); |
| 123 | +} |
0 commit comments