memos/web/src/layouts/MainLayout.tsx
Steven eb541d04cc fix(web): resolve tag sidebar filtering and reactivity issues
This fixes multiple issues with the tag sidebar and activity calendar:

1. Tag disappearing bug: When filtering by a tag, the sidebar now shows all tags instead of only the selected tag
2. Activity calendar filtering: Calendar now shows full activity history instead of filtered results
3. Auto-update on memo changes: Sidebar tags and calendar now update automatically when creating/editing memos without requiring manual page refresh

Technical changes:
- Modified useFilteredMemoStats to fetch unfiltered UserStats from backend API for Home/Profile pages
- Fixed key mismatch bug in userStore where stats were stored with inconsistent keys
- Added statsStateId update in fetchUserStats to trigger reactivity
- Updated MainLayout to pass appropriate userName based on page context
- Archived/Explore pages continue to compute from cached memos (correct behavior)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-25 22:17:01 +08:00

91 lines
3.7 KiB
TypeScript

import { observer } from "mobx-react-lite";
import { useEffect, useMemo, useState } from "react";
import { matchPath, Outlet, useLocation } from "react-router-dom";
import type { MemoExplorerContext } from "@/components/MemoExplorer";
import { MemoExplorer, MemoExplorerDrawer } from "@/components/MemoExplorer";
import MobileHeader from "@/components/MobileHeader";
import useCurrentUser from "@/hooks/useCurrentUser";
import { useFilteredMemoStats } from "@/hooks/useFilteredMemoStats";
import useResponsiveWidth from "@/hooks/useResponsiveWidth";
import { cn } from "@/lib/utils";
import { Routes } from "@/router";
import { userStore } from "@/store";
const MainLayout = observer(() => {
const { md, lg } = useResponsiveWidth();
const location = useLocation();
const currentUser = useCurrentUser();
const [profileUserName, setProfileUserName] = useState<string | undefined>();
// Determine context based on current route
const context: MemoExplorerContext = useMemo(() => {
if (location.pathname === Routes.ROOT) return "home";
if (location.pathname === Routes.EXPLORE) return "explore";
if (matchPath("/archived", location.pathname)) return "archived";
if (matchPath("/u/:username", location.pathname)) return "profile";
return "home"; // fallback
}, [location.pathname]);
// Extract username from URL for profile context
useEffect(() => {
const match = matchPath("/u/:username", location.pathname);
if (match && context === "profile") {
const username = match.params.username;
if (username) {
// Fetch or get user to obtain user name (e.g., "users/123")
// Note: User stats will be fetched by useFilteredMemoStats
userStore
.getOrFetchUserByUsername(username)
.then((user) => {
setProfileUserName(user.name);
})
.catch((error) => {
console.error("Failed to fetch profile user:", error);
setProfileUserName(undefined);
});
}
} else {
setProfileUserName(undefined);
}
}, [location.pathname, context]);
// Determine which user name to use for stats
// - home: current user (uses backend user stats for normal memos)
// - profile: viewed user (uses backend user stats for normal memos)
// - archived: undefined (compute from cached archived memos, since user stats only includes normal memos)
// - explore: undefined (compute from cached memos)
const statsUserName = useMemo(() => {
if (context === "home") {
return currentUser?.name;
} else if (context === "profile") {
return profileUserName;
}
return undefined; // archived and explore contexts compute from cache
}, [context, currentUser, profileUserName]);
// Fetch stats from memo store cache (populated by PagedMemoList)
// For user-scoped contexts, use backend user stats for tags (unaffected by filters)
const { statistics, tags } = useFilteredMemoStats({ userName: statsUserName });
return (
<section className="@container w-full min-h-full flex flex-col justify-start items-center">
{!md && (
<MobileHeader>
<MemoExplorerDrawer context={context} statisticsData={statistics} tagCount={tags} />
</MobileHeader>
)}
{md && (
<div className={cn("fixed top-0 left-16 shrink-0 h-svh transition-all", "border-r border-border", lg ? "w-72" : "w-56")}>
<MemoExplorer className={cn("px-3 py-6")} context={context} statisticsData={statistics} tagCount={tags} />
</div>
)}
<div className={cn("w-full min-h-full", lg ? "pl-72" : md ? "pl-56" : "")}>
<div className={cn("w-full mx-auto px-4 sm:px-6 md:pt-6 pb-8")}>
<Outlet />
</div>
</div>
</section>
);
});
export default MainLayout;