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:
@@ -1,10 +1,9 @@
|
||||
import { Alpine } from "./alpine";
|
||||
import { ALL_LIBRARIES, getSelectedLibrary, setSelectedLibrary } from "./storage";
|
||||
import { showToast } from "./toast";
|
||||
|
||||
const STORAGE_KEY = "selectedLibrary";
|
||||
|
||||
export function getCurrentLibraryId(): string {
|
||||
return localStorage.getItem(STORAGE_KEY) || "";
|
||||
return getSelectedLibrary();
|
||||
}
|
||||
|
||||
export async function switchWithTransition(
|
||||
@@ -60,8 +59,10 @@ export function initLibrarySwitcher(options: LibrarySwitcherOptions): void {
|
||||
) as HTMLSelectElement;
|
||||
if (!librarySelect) return;
|
||||
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
const stored = localStorage.getItem("selectedLibrary");
|
||||
if (stored === ALL_LIBRARIES) {
|
||||
librarySelect.value = "";
|
||||
} else if (stored) {
|
||||
const option = librarySelect.querySelector(
|
||||
`option[value="${stored}"]`,
|
||||
);
|
||||
@@ -72,10 +73,9 @@ export function initLibrarySwitcher(options: LibrarySwitcherOptions): void {
|
||||
|
||||
librarySelect.addEventListener("change", async (e) => {
|
||||
const target = e.target as HTMLSelectElement;
|
||||
if (!target.value && target.value !== "") return;
|
||||
|
||||
const libraryId = target.value;
|
||||
localStorage.setItem(STORAGE_KEY, libraryId);
|
||||
|
||||
setSelectedLibrary(libraryId);
|
||||
await options.onSwitch(libraryId);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user