memos/web/src/router/index.tsx
Steven 1d7efb1580 refactor(web): unify memo stats/filters with context-aware MainLayout
Create unified architecture for memo statistics, filters, and sorting
across all pages (Home, Explore, Archived, Profile) with proper
visibility filtering and consistent data flow.

Key changes:
- Rename HomeLayout → MainLayout to reflect broader usage
- Create useFilteredMemoStats hook for unified stats computation
- Create useMemoFilters/useMemoSorting hooks to eliminate duplication
- Refactor all pages to use unified hooks (~147 lines removed)
- Move Explore route under MainLayout (was sibling before)
- Fix masonry column calculation threshold (1024px → 688px+)

Architecture improvements:
- MainLayout computes filter/stats per route context
- Stats/tags based on same filter as memo list (consistency)
- Proper visibility filtering (PUBLIC/PROTECTED) on Explore
- MemoExplorer/StatisticsView accept stats as required props
- Eliminated optional fallbacks and redundant data fetching

Benefits:
- Single source of truth for stats computation
- Stats remain static (don't change with user filters)
- Reduced code duplication across 4 pages
- Better maintainability and type safety
- Proper security (no private memo leakage on Explore)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-05 08:46:52 +08:00

184 lines
4.8 KiB
TypeScript

import { Suspense, lazy } from "react";
import { createBrowserRouter } from "react-router-dom";
import App from "@/App";
import MainLayout from "@/layouts/MainLayout";
import RootLayout from "@/layouts/RootLayout";
import Home from "@/pages/Home";
import Loading from "@/pages/Loading";
const AdminSignIn = lazy(() => import("@/pages/AdminSignIn"));
const Archived = lazy(() => import("@/pages/Archived"));
const AuthCallback = lazy(() => import("@/pages/AuthCallback"));
const Explore = lazy(() => import("@/pages/Explore"));
const Inboxes = lazy(() => import("@/pages/Inboxes"));
const MemoDetail = lazy(() => import("@/pages/MemoDetail"));
const NotFound = lazy(() => import("@/pages/NotFound"));
const PermissionDenied = lazy(() => import("@/pages/PermissionDenied"));
const Attachments = lazy(() => import("@/pages/Attachments"));
const Setting = lazy(() => import("@/pages/Setting"));
const SignIn = lazy(() => import("@/pages/SignIn"));
const SignUp = lazy(() => import("@/pages/SignUp"));
const UserProfile = lazy(() => import("@/pages/UserProfile"));
const MemoDetailRedirect = lazy(() => import("./MemoDetailRedirect"));
export enum Routes {
ROOT = "/",
ATTACHMENTS = "/attachments",
INBOX = "/inbox",
ARCHIVED = "/archived",
SETTING = "/setting",
EXPLORE = "/explore",
AUTH = "/auth",
}
const router = createBrowserRouter([
{
path: "/",
element: <App />,
children: [
{
path: Routes.AUTH,
children: [
{
path: "",
element: (
<Suspense fallback={<Loading />}>
<SignIn />
</Suspense>
),
},
{
path: "admin",
element: (
<Suspense fallback={<Loading />}>
<AdminSignIn />
</Suspense>
),
},
{
path: "signup",
element: (
<Suspense fallback={<Loading />}>
<SignUp />
</Suspense>
),
},
{
path: "callback",
element: (
<Suspense fallback={<Loading />}>
<AuthCallback />
</Suspense>
),
},
],
},
{
path: Routes.ROOT,
element: <RootLayout />,
children: [
{
element: <MainLayout />,
children: [
{
path: "",
element: <Home />,
},
{
path: Routes.EXPLORE,
element: (
<Suspense fallback={<Loading />}>
<Explore />
</Suspense>
),
},
{
path: Routes.ARCHIVED,
element: (
<Suspense fallback={<Loading />}>
<Archived />
</Suspense>
),
},
{
path: "u/:username",
element: (
<Suspense fallback={<Loading />}>
<UserProfile />
</Suspense>
),
},
],
},
{
path: Routes.ATTACHMENTS,
element: (
<Suspense fallback={<Loading />}>
<Attachments />
</Suspense>
),
},
{
path: Routes.INBOX,
element: (
<Suspense fallback={<Loading />}>
<Inboxes />
</Suspense>
),
},
{
path: Routes.SETTING,
element: (
<Suspense fallback={<Loading />}>
<Setting />
</Suspense>
),
},
{
path: "memos/:uid",
element: (
<Suspense fallback={<Loading />}>
<MemoDetail />
</Suspense>
),
},
// Redirect old path to new path.
{
path: "m/:uid",
element: (
<Suspense fallback={<Loading />}>
<MemoDetailRedirect />
</Suspense>
),
},
{
path: "403",
element: (
<Suspense fallback={<Loading />}>
<PermissionDenied />
</Suspense>
),
},
{
path: "404",
element: (
<Suspense fallback={<Loading />}>
<NotFound />
</Suspense>
),
},
{
path: "*",
element: (
<Suspense fallback={<Loading />}>
<NotFound />
</Suspense>
),
},
],
},
],
},
]);
export default router;