Files
bookhoard/web/src/storage.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

76 lines
1.6 KiB
TypeScript

const ALL_LIBRARIES = "__all__";
function getToken(): string | null {
return localStorage.getItem("token");
}
function setToken(token: string): void {
localStorage.setItem("token", token);
}
function removeToken(): void {
localStorage.removeItem("token");
}
function getRefreshToken(): string | null {
return localStorage.getItem("refresh_token");
}
function setRefreshToken(token: string): void {
localStorage.setItem("refresh_token", token);
}
function removeRefreshToken(): void {
localStorage.removeItem("refresh_token");
}
function getTheme(): string {
return localStorage.getItem("theme") || "tokyo-night";
}
function setTheme(theme: string): void {
localStorage.setItem("theme", theme);
}
function getSelectedLibrary(): string {
const stored = localStorage.getItem("selectedLibrary");
if (stored === ALL_LIBRARIES) return "";
if (stored) return stored;
return "";
}
function setSelectedLibrary(libraryId: string): void {
const value = libraryId === "" ? ALL_LIBRARIES : libraryId;
localStorage.setItem("selectedLibrary", value);
document.cookie = `selectedLibrary=${encodeURIComponent(value)};path=/;max-age=${365 * 24 * 60 * 60};samesite=lax`;
}
function getSelectedBook(): string | null {
return localStorage.getItem("selectedBook");
}
function setSelectedBook(bookId: string): void {
localStorage.setItem("selectedBook", bookId);
}
function clearAll(): void {
localStorage.clear();
}
export {
ALL_LIBRARIES,
clearAll,
getRefreshToken,
getSelectedBook,
getSelectedLibrary,
getTheme,
getToken,
removeRefreshToken,
removeToken,
setRefreshToken,
setSelectedBook,
setSelectedLibrary,
setTheme,
setToken
};