feat(ts): centralized library storage with cookie-based SSR support

- storage.ts: Centralize ALL_LIBRARIES = "__all__" sentinel constant.
  setSelectedLibrary() now writes both localStorage and a cookie
  (selectedLibrary, Path=/, SameSite=Lax, max-age=365d). The
  sentinel "__all__" is used in both storage mediums — empty strings
  are never stored. getSelectedLibrary() maps __all__ back to "".
  Cookie enables server-side rendering to read the stored library
  selection without access to localStorage.

- library-switcher.ts: Import ALL_LIBRARIES and setSelectedLibrary/
  getSelectedLibrary from storage.ts instead of managing localStorage
  directly. Remove local constants.

- dashboard.ts: Remove duplicate localStorage.setItem call that was
  overwriting the __all__ sentinel with raw empty string. Fix
  reloadPage() and scan-complete handler to work with empty libraryId.
  openDashboardSettings/saveDashboardSettings show clear messages for
  All Libraries mode.

- collections.ts: Remove library switcher initialization from the
  collections list page — the list page no longer has a switcher.

- series.ts: Rewrite to use initLibrarySwitcher from library-switcher
  module and switchWithTransition for navigation. Series card links
  no longer include library_id in their URLs.

- bookshelf.ts: Autocomplete fetch calls handle empty libraryId
  correctly for All Libraries mode.

- search.ts, collection-rules.ts: Use setSelectedLibrary() and
  getSelectedLibrary() from storage.ts instead of direct localStorage
  access.
This commit is contained in:
2026-05-18 17:53:27 -04:00
parent e047a627a8
commit 19fc222fb3
8 changed files with 52 additions and 112 deletions
+8 -13
View File
@@ -21,7 +21,7 @@ async function openDashboardSettings(): Promise<void> {
) as HTMLSelectElement;
const libraryId = librarySelect?.value;
if (!libraryId) {
showToast("No library selected", "error");
showToast("Select a specific library to customize preferences", "error");
return;
}
@@ -95,8 +95,9 @@ function closeDashboardSettings(): void {
}
async function fetchAndRenderSections(libraryId: string): Promise<void> {
const param = libraryId ? `library_id=${libraryId}` : "";
const response = await fetch(
`/api/dashboard/sections?library_id=${libraryId}`,
`/api/dashboard/sections?${param}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
@@ -107,7 +108,6 @@ async function fetchAndRenderSections(libraryId: string): Promise<void> {
if (!response.ok) throw new Error("Failed to load sections");
const data = await response.json();
renderDashboardCollections(data.sections);
localStorage.setItem("selectedLibrary", libraryId);
}
async function saveDashboardSettings(): Promise<void> {
@@ -150,7 +150,7 @@ async function saveDashboardSettings(): Promise<void> {
showToast("Dashboard settings saved", "success");
closeDashboardSettings();
if (!libraryId) {
showToast("Unable to refresh dashboard - no library selected", "error");
showToast("Select a specific library to customize preferences", "error");
return;
}
await fetchAndRenderSections(libraryId);
@@ -302,12 +302,7 @@ async function reloadPage(): Promise<void> {
const librarySelect = document.getElementById(
"library-select",
) as HTMLSelectElement;
const libraryId = librarySelect?.value;
if (!libraryId) {
showToast("No library selected", "error");
return;
}
const libraryId = librarySelect?.value || "";
await switchWithTransition("collections-container", () =>
fetchAndRenderSections(libraryId),
@@ -444,13 +439,13 @@ function initDashboard() {
const librarySelect = document.getElementById(
"library-select",
) as HTMLSelectElement;
const libraryId = librarySelect?.value;
const libraryId = librarySelect?.value || "";
console.log("[dashboard] libraryId:", libraryId);
if (!libraryId) return;
try {
const param = libraryId ? `library_id=${libraryId}` : "";
const response = await fetch(
`/api/dashboard/sections?library_id=${libraryId}`,
`/api/dashboard/sections?${param}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,