import { useCallback, useEffect, useState } from 'react' import { LayoutDashboard } from 'lucide-react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import { useAuthStore } from '@/stores/auth-store' import { api } from '@/lib/api' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card' import { Switch } from '@/components/ui/switch' type SidebarModuleConfig = { enabled: boolean [key: string]: boolean } type SidebarModulesConfig = Record type SectionDef = { key: string title: string description: string modules: { key: string; title: string; description: string }[] } export function SidebarModulesCard() { const { t } = useTranslation() const [loading, setLoading] = useState(false) const [config, setConfig] = useState({}) const currentUser = useAuthStore((s) => s.auth.user) const setUser = useAuthStore((s) => s.auth.setUser) const sectionDefs: SectionDef[] = [ { key: 'chat', title: t('Chat Area'), description: t('Playground and chat functions'), modules: [ { key: 'playground', title: t('Playground'), description: t('AI model testing environment'), }, { key: 'chat', title: t('Chat'), description: t('Chat session management'), }, ], }, { key: 'console', title: t('Console Area'), description: t('Data management and log viewing'), modules: [ { key: 'detail', title: t('Dashboard'), description: t('System data statistics'), }, { key: 'token', title: t('Token Management'), description: t('API token management'), }, { key: 'log', title: t('Usage Logs'), description: t('API usage records'), }, { key: 'midjourney', title: t('Drawing Logs'), description: t('Drawing task records'), }, { key: 'task', title: t('Task Logs'), description: t('System task records'), }, ], }, { key: 'personal', title: t('Personal Center Area'), description: t('User personal functions'), modules: [ { key: 'topup', title: t('Wallet Management'), description: t('Balance and top-up management'), }, { key: 'personal', title: t('Personal Settings'), description: t('Personal info settings'), }, ], }, ] const loadConfig = useCallback(async () => { try { const res = await api.get('/api/user/self') if (res.data.success && res.data.data?.sidebar_modules) { const raw = res.data.data.sidebar_modules const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw setConfig(parsed) } else { const defaults: SidebarModulesConfig = {} for (const sec of sectionDefs) { defaults[sec.key] = { enabled: true } for (const mod of sec.modules) defaults[sec.key][mod.key] = true } setConfig(defaults) } } catch { /* ignore */ } // eslint-disable-next-line react-hooks/exhaustive-deps }, []) useEffect(() => { loadConfig() }, [loadConfig]) const toggleSection = (sectionKey: string, val: boolean) => { setConfig((prev) => ({ ...prev, [sectionKey]: { ...prev[sectionKey], enabled: val }, })) } const toggleModule = ( sectionKey: string, moduleKey: string, val: boolean ) => { setConfig((prev) => ({ ...prev, [sectionKey]: { ...prev[sectionKey], [moduleKey]: val }, })) } const handleSave = async () => { setLoading(true) try { const serialized = JSON.stringify(config) const res = await api.put('/api/user/self', { sidebar_modules: serialized, }) if (res.data.success) { // Sync to auth-store so useSidebarConfig re-runs and the sidebar // updates immediately without needing a page refresh. if (currentUser) { setUser({ ...currentUser, sidebar_modules: serialized }) } toast.success(t('Saved successfully')) } else { toast.error(res.data.message || t('Save failed')) } } catch { toast.error(t('Save failed, please retry')) } finally { setLoading(false) } } const handleReset = () => { const defaults: SidebarModulesConfig = {} for (const sec of sectionDefs) { defaults[sec.key] = { enabled: true } for (const mod of sec.modules) defaults[sec.key][mod.key] = true } setConfig(defaults) toast.success(t('Reset to default configuration')) } return (
{t('Sidebar Personal Settings')} {t('Customize sidebar display content')}
{sectionDefs.map((section) => { const sectionEnabled = config[section.key]?.enabled !== false return (

{section.title}

{section.description}

toggleSection(section.key, v)} />
{section.modules.map((mod) => (

{mod.title}

{mod.description}

toggleModule(section.key, mod.key, v) } disabled={!sectionEnabled} />
))}
) })}
) }