fix(web): preserve active library query from sidebar

This commit is contained in:
zZebrahz
2026-05-30 02:07:37 +00:00
parent 9c56f40170
commit 449fcf394c
2 changed files with 35 additions and 8 deletions
+14
View File
@@ -1,3 +1,5 @@
// @vitest-environment node
import type { ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { MemoryRouter, Route, Routes } from "react-router";
@@ -47,6 +49,12 @@ vi.mock("@/hooks/queries/pluginSettings", () => ({
}),
}));
vi.mock("@/hooks/queries/useRequests", () => ({
useRequestFeatureStatus: () => ({
data: { requests_enabled: false },
}),
}));
vi.mock("@/hooks/useViewTransition", () => ({
useViewTransitionNavigate: () => vi.fn(),
}));
@@ -133,6 +141,12 @@ describe("AppSidebar", () => {
expect(markup).not.toContain("text-sidebar-primary-foreground bg-sidebar-accent");
});
it("preserves the current library query when linking to the active library", () => {
const markup = renderSidebar("/library/7?tab=library&sort=year&order=desc");
expect(markup).toContain('href="/library/7?tab=library&sort=year&order=desc"');
});
it("keeps collapsed navigation rows left-anchored instead of centering icons", () => {
const markup = renderSidebar("/item/42", { collapsed: true });
+21 -8
View File
@@ -68,6 +68,13 @@ function getLibraryIcon(type: string) {
}
}
function getLibraryIdFromPathname(pathname: string): number | null {
const match = pathname.match(/^\/library\/(\d+)(?:\/|$)/);
if (!match) return null;
const id = Number(match[1]);
return Number.isInteger(id) && id > 0 ? id : null;
}
function SidebarLabel({ children, show }: { children: ReactNode; show: boolean }) {
return (
<span
@@ -187,13 +194,15 @@ export default function AppSidebar({ onNavigate, collapsed = false }: AppSidebar
: null,
[location.pathname, location.search],
);
const activeLibraryId = params.libraryId
? Number(params.libraryId)
: catalogState?.source === "section" && catalogState.scope === "library"
? (catalogState.library_id ?? null)
: catalogState?.source === "library_collection"
? (catalogState.library_id ?? null)
: null;
const activeLibraryId =
params.libraryId && Number.isInteger(Number(params.libraryId))
? Number(params.libraryId)
: (getLibraryIdFromPathname(location.pathname) ??
(catalogState?.source === "section" && catalogState.scope === "library"
? (catalogState.library_id ?? null)
: catalogState?.source === "library_collection"
? (catalogState.library_id ?? null)
: null));
// Hover-to-expand when collapsed (150ms enter delay prevents accidental expansion)
const [hovered, setHovered] = useState(false);
const [profileMenuOpen, setProfileMenuOpen] = useState(false);
@@ -338,8 +347,12 @@ export default function AppSidebar({ onNavigate, collapsed = false }: AppSidebar
{(showLabels ? librariesExpanded : true) && (
<ul className="list-none space-y-0.5">
{libraries.map((lib) => {
const href = `/library/${lib.id}`;
const baseHref = `/library/${lib.id}`;
const active = activeLibraryId === lib.id;
const href =
active && location.pathname === baseHref
? `${baseHref}${location.search}`
: baseHref;
const libraryPins = pins[String(lib.id)] ?? [];
const hasPins = libraryPins.length > 0;
const isExpanded = hasPins && !expandedLibraries.has(lib.id);