feat: 登录持久化修复 + 左下角账号区 + 管理页面(管理员/邀请码)
This commit is contained in:
@@ -4,10 +4,11 @@ import { Sidebar } from './Sidebar';
|
||||
import { Header } from './Header';
|
||||
import { ToastProvider } from '@/components/ui';
|
||||
import { LoginPage } from '@/components/auth/LoginPage';
|
||||
import { isLocalMode } from '@/lib/api';
|
||||
import { getToken } from '@/lib/api';
|
||||
import { isTauriRef } from '@/lib/api';
|
||||
|
||||
export function AppShell() {
|
||||
const [authed, setAuthed] = useState(() => isLocalMode());
|
||||
const [authed, setAuthed] = useState(() => isTauriRef() || !!getToken());
|
||||
const [navOpen, setNavOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { useState } from 'react';
|
||||
import { NavLink } from 'react-router-dom';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { CalendarDays, Rows3, ChartColumn, GraduationCap, Shapes, SlidersHorizontal, ChevronsLeft } from 'lucide-react';
|
||||
import { appInfo, syncStatus } from '@/lib/api';
|
||||
import { CalendarDays, Rows3, ChartColumn, GraduationCap, Shapes, SlidersHorizontal, LogIn, ShieldCheck } from 'lucide-react';
|
||||
import { syncStatus, serverMe, clearToken, isTauriRef, getToken } from '@/lib/api';
|
||||
import { LoginDialog } from '@/components/auth/LoginDialog';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const NAV = [
|
||||
@@ -17,6 +19,7 @@ const NAV = [
|
||||
{
|
||||
section: '配置',
|
||||
items: [
|
||||
{ to: '/admin', label: '管理', icon: ShieldCheck },
|
||||
{ to: '/types', label: '记录类型', icon: Shapes },
|
||||
{ to: '/taxonomy', label: '字典与维度', icon: SlidersHorizontal }
|
||||
]
|
||||
@@ -33,9 +36,21 @@ function LogoMark() {
|
||||
);
|
||||
}
|
||||
|
||||
function baseUrl() {
|
||||
return isTauriRef() ? (localStorage.getItem('fp-server-url') || '') : '';
|
||||
}
|
||||
|
||||
export function Sidebar({ open, onClose }: { open: boolean; onClose: () => void }) {
|
||||
const info = useQuery({ queryKey: ['app-info'], queryFn: appInfo, staleTime: Infinity });
|
||||
const isWeb = !isTauriRef();
|
||||
const sync = useQuery({ queryKey: ['sync-status'], queryFn: syncStatus, refetchInterval: 15000 });
|
||||
const meQ = useQuery({
|
||||
queryKey: ['me'],
|
||||
queryFn: () => serverMe(baseUrl()),
|
||||
enabled: !!getToken(),
|
||||
retry: 0
|
||||
});
|
||||
const me = meQ.data ?? null;
|
||||
const [loginOpen, setLoginOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<aside
|
||||
@@ -52,7 +67,7 @@ export function Sidebar({ open, onClose }: { open: boolean; onClose: () => void
|
||||
<div className="text-[10px] font-medium tracking-[0.18em] text-ink-3">FOOTPRINTS</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<LoginDialog open={loginOpen} onClose={() => setLoginOpen(false)} defaultUrl={localStorage.getItem('fp-server-url') || 'https://footpr.linyux.cn'} />
|
||||
<nav className="flex-1 overflow-y-auto px-2 py-3">
|
||||
{NAV.map(group => (
|
||||
<div key={group.section} className="mb-4">
|
||||
@@ -82,18 +97,65 @@ export function Sidebar({ open, onClose }: { open: boolean; onClose: () => void
|
||||
</nav>
|
||||
|
||||
<div className="border-t border-line px-4 py-3">
|
||||
<div className="truncate text-[11px] text-ink-3" title={info.data?.db_path ?? ''}>
|
||||
{info.data ? '数据库:' + info.data.db_path.split(/[\\/]/).slice(-1)[0] : '加载中…'}
|
||||
</div>
|
||||
<div className="mt-0.5 flex items-center gap-1 text-[11px] text-ink-3">
|
||||
<ChevronsLeft size={11} />
|
||||
{sync.data
|
||||
? (sync.data.pending_ops > 0
|
||||
? '本地模式 · 待同步 ' + sync.data.pending_ops
|
||||
: '本地模式 · 数据已就绪')
|
||||
: '…'}
|
||||
</div>
|
||||
{/* 左下角:账号区 */}
|
||||
{getToken() ? (
|
||||
<div className="mb-2">
|
||||
{me && (
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<div className="min-w-0 truncate text-[12px] font-medium text-ink-1" title={me.username}>
|
||||
{me.is_admin && <span className="mr-1 text-brand-text">管理员</span>}
|
||||
{me.username}
|
||||
</div>
|
||||
<button
|
||||
className="shrink-0 text-[11px] text-ink-3 underline transition-colors hover:text-danger"
|
||||
onClick={() => {
|
||||
clearToken();
|
||||
localStorage.removeItem('fp-server-url');
|
||||
window.location.reload();
|
||||
}}
|
||||
>
|
||||
退出
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{!isWeb && (
|
||||
<button
|
||||
className="flex w-full items-center gap-1.5 rounded-md px-1 py-1 text-[12px] text-ink-2 transition-colors hover:bg-surface-3 hover:text-ink-1"
|
||||
onClick={() => setLoginOpen(true)}
|
||||
>
|
||||
<LogIn size={13} />
|
||||
{me ? "账号与同步设置" : "已注册 · 登录同步"}
|
||||
</button>
|
||||
)}
|
||||
{!isWeb && (
|
||||
<div className="num text-[10px] text-ink-3">
|
||||
{sync.data ? ("同步 · 待推送 " + sync.data.pending_ops) : "…"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<button
|
||||
className="flex w-full items-center gap-1.5 rounded-md bg-brand-subtle px-2 py-1.5 text-[12.5px] font-medium text-brand-text transition-colors hover:brightness-[0.97]"
|
||||
onClick={() => setLoginOpen(true)}
|
||||
>
|
||||
<LogIn size={13} />
|
||||
登录 / 注册账号
|
||||
</button>
|
||||
<div className="text-[10px] text-ink-3">未登录 = 本地模式,数据仅存本机</div>
|
||||
</div>
|
||||
)}
|
||||
{!isWeb && (
|
||||
<div className="mt-1.5 flex items-center gap-1 text-[11px] text-ink-3">
|
||||
<ChevronsLeftSvg />
|
||||
数据库 · 本地存储
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
|
||||
function ChevronsLeftSvg() {
|
||||
return <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M11 17l-5-5 5-5" /><path d="M18 17l-5-5 5-5" /></svg>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user