Files
bookhoard/web/src/library-switcher.ts
T
john-okeefe 7221906d53 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.
2026-05-18 17:53:27 -04:00

84 lines
2.2 KiB
TypeScript

import { Alpine } from "./alpine";
import { ALL_LIBRARIES, getSelectedLibrary, setSelectedLibrary } from "./storage";
import { showToast } from "./toast";
export function getCurrentLibraryId(): string {
return getSelectedLibrary();
}
export async function switchWithTransition(
containerId: string,
fetchFn: () => Promise<void>,
): Promise<void> {
const container = document.getElementById(containerId);
const loading = document.getElementById("loading-spinner");
if (!container || !loading) return;
try {
container.classList.add(
"opacity-0",
"transition-opacity",
"duration-150",
);
await new Promise((resolve) => setTimeout(resolve, 150));
loading.classList.remove("hidden");
await fetchFn();
container.classList.remove("duration-150");
container.classList.add("duration-300");
void container.offsetHeight;
container.classList.remove("opacity-0");
setTimeout(() => {
container.classList.remove("transition-opacity", "duration-300");
}, 300);
} catch (error) {
container.classList.remove(
"opacity-0",
"transition-opacity",
"duration-150",
"duration-300",
);
showToast("Failed to load library", "error");
console.error("Switch library error:", error);
} finally {
loading.classList.add("hidden");
}
}
export interface LibrarySwitcherOptions {
onSwitch: (libraryId: string) => Promise<void>;
}
export function initLibrarySwitcher(options: LibrarySwitcherOptions): void {
const librarySelect = document.getElementById(
"library-select",
) as HTMLSelectElement;
if (!librarySelect) return;
const stored = localStorage.getItem("selectedLibrary");
if (stored === ALL_LIBRARIES) {
librarySelect.value = "";
} else if (stored) {
const option = librarySelect.querySelector(
`option[value="${stored}"]`,
);
if (option) {
librarySelect.value = stored;
}
}
librarySelect.addEventListener("change", async (e) => {
const target = e.target as HTMLSelectElement;
const libraryId = target.value;
setSelectedLibrary(libraryId);
await options.onSwitch(libraryId);
});
}
Alpine.data("librarySwitcher", () => ({}));