feat: 优化web

This commit is contained in:
2026-04-23 18:58:13 +08:00
commit 544a2f3428
160 changed files with 27327 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
'use client';
import { TooltipProvider } from '@radix-ui/react-tooltip';
import { usePathname, useRouter } from 'next/navigation';
import { useEffect } from 'react';
import { LoginModal } from '@/components/auth/LoginModal';
import { ToastHost } from '@/components/feedback/ToastHost';
import { ErrorBoundary } from '@/components/providers/ErrorBoundary';
import { registerAuthEvents } from '@/lib/notify/auth-events';
import { subscribeRemoteLogout } from '@/lib/sync/logout-broadcast';
import { useAuthStore } from '@/stores/auth-store';
import { useAuthUiStore } from '@/stores/auth-ui-store';
import { useToastStore } from '@/stores/toast-store';
export function AppProviders({ children }: { children: React.ReactNode }) {
const router = useRouter();
const pathname = usePathname() ?? '';
useEffect(() => {
registerAuthEvents({
on401: (msg) => {
useAuthUiStore.getState().openLoginModal(msg);
},
on403: (msg) => {
useToastStore.getState().show(msg || '禁止访问', 'error');
},
});
}, []);
useEffect(() => {
return subscribeRemoteLogout(() => {
useAuthStore.getState().setTokens(null, null);
useToastStore.getState().show('已在其他标签退出登录', 'info');
if (pathname.startsWith('/dashboard')) {
router.replace(`/login?from=${encodeURIComponent(pathname)}`);
}
});
}, [pathname, router]);
return (
<TooltipProvider delayDuration={300}>
<ErrorBoundary>{children}</ErrorBoundary>
<ToastHost />
<LoginModal />
</TooltipProvider>
);
}
@@ -0,0 +1,49 @@
'use client';
import { Component, type ReactNode } from 'react';
type Props = {
children: ReactNode;
fallback?: ReactNode;
};
type State = {
hasError: boolean;
error: Error | null;
};
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
console.error('[ErrorBoundary]', error, info.componentStack);
}
render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<div className="flex min-h-[200px] flex-col items-center justify-center gap-3 p-6 text-center">
<p className="text-sm text-neutral-600"></p>
<button
type="button"
className="rounded bg-neutral-900 px-3 py-1.5 text-sm text-white hover:bg-neutral-800"
onClick={() => this.setState({ hasError: false, error: null })}
>
</button>
</div>
);
}
return this.props.children;
}
}