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:
+18
-64
@@ -1,6 +1,6 @@
|
||||
import { Alpine } from "./alpine";
|
||||
import { showToast } from "./toast";
|
||||
import { setSelectedLibrary } from "./storage";
|
||||
import { initLibrarySwitcher, switchWithTransition } from "./library-switcher";
|
||||
|
||||
interface SeriesItem {
|
||||
name: string;
|
||||
@@ -10,8 +10,8 @@ interface SeriesItem {
|
||||
last_entry_at: string;
|
||||
}
|
||||
|
||||
function renderSeriesCard(series: SeriesItem, libraryId: string): string {
|
||||
const href = `/series/detail?name=${encodeURIComponent(series.name)}&library_id=${libraryId}`;
|
||||
function renderSeriesCard(series: SeriesItem): string {
|
||||
const href = `/series/detail?name=${encodeURIComponent(series.name)}`;
|
||||
const coverCount = series.cover_paths.length;
|
||||
const coverClass = `cover-count-${coverCount}`;
|
||||
|
||||
@@ -58,17 +58,18 @@ function renderSeriesContent(
|
||||
</main>`;
|
||||
}
|
||||
|
||||
const cardsHtml = seriesList.map((s) => renderSeriesCard(s, libraryId)).join("");
|
||||
const cardsHtml = seriesList.map((s) => renderSeriesCard(s)).join("");
|
||||
|
||||
let paginationHtml = "";
|
||||
if (totalPages > 1) {
|
||||
const libParam = libraryId ? `library_id=${libraryId}&` : "";
|
||||
const prevLink =
|
||||
currentPage > 1
|
||||
? `<a href="/series?library_id=${libraryId}&page=${currentPage - 1}" class="px-4 py-2 rounded-lg border" style="border-color: var(--border); color: var(--text-primary);">← Previous</a>`
|
||||
? `<a href="/series?${libParam}page=${currentPage - 1}" class="px-4 py-2 rounded-lg border" style="border-color: var(--border); color: var(--text-primary);">← Previous</a>`
|
||||
: "";
|
||||
const nextLink =
|
||||
currentPage < totalPages
|
||||
? `<a href="/series?library_id=${libraryId}&page=${currentPage + 1}" class="px-4 py-2 rounded-lg border" style="border-color: var(--border); color: var(--text-primary);">Next →</a>`
|
||||
? `<a href="/series?${libParam}page=${currentPage + 1}" class="px-4 py-2 rounded-lg border" style="border-color: var(--border); color: var(--text-primary);">Next →</a>`
|
||||
: "";
|
||||
paginationHtml = `
|
||||
<div class="flex justify-center items-center gap-4 mt-8">
|
||||
@@ -89,19 +90,10 @@ function renderSeriesContent(
|
||||
}
|
||||
|
||||
async function switchLibrary(libraryId: string): Promise<void> {
|
||||
const container = document.getElementById("series-container") as HTMLElement;
|
||||
const loading = document.getElementById("loading-spinner") as HTMLElement;
|
||||
|
||||
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 switchWithTransition("series-container", async () => {
|
||||
const param = libraryId ? `library_id=${libraryId}&` : "";
|
||||
const response = await fetch(
|
||||
`/api/series?library_id=${libraryId}&limit=24&offset=0`,
|
||||
`/api/series?${param}limit=24&offset=0`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem("token")}`,
|
||||
@@ -119,55 +111,17 @@ async function switchLibrary(libraryId: string): Promise<void> {
|
||||
const total: number = data.total || 0;
|
||||
const totalPages = Math.max(1, Math.ceil(total / 24));
|
||||
|
||||
container.innerHTML = renderSeriesContent(seriesList, libraryId, totalPages, 1);
|
||||
setSelectedLibrary(libraryId);
|
||||
} catch (error) {
|
||||
showToast("Failed to load series", "error");
|
||||
console.error("Switch library error:", error);
|
||||
} finally {
|
||||
loading.classList.add("hidden");
|
||||
|
||||
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);
|
||||
}
|
||||
const container = document.getElementById("series-container");
|
||||
if (container) {
|
||||
container.innerHTML = renderSeriesContent(seriesList, libraryId, totalPages, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Alpine.data("seriesPage", () => ({
|
||||
initSeriesPage() {
|
||||
const librarySelect = document.getElementById(
|
||||
"library-select",
|
||||
) as HTMLSelectElement;
|
||||
if (librarySelect) {
|
||||
librarySelect.addEventListener("change", () => {
|
||||
if (librarySelect.value) {
|
||||
switchLibrary(librarySelect.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
Alpine.data("seriesDetailPage", () => ({
|
||||
initSeriesDetailPage() {
|
||||
const librarySelect = document.getElementById(
|
||||
"library-select",
|
||||
) as HTMLSelectElement;
|
||||
if (librarySelect) {
|
||||
librarySelect.addEventListener("change", () => {
|
||||
if (librarySelect.value) {
|
||||
const url = new URL(window.location.href);
|
||||
const currentLib = url.searchParams.get("library_id");
|
||||
if (currentLib === librarySelect.value) return;
|
||||
url.searchParams.set("library_id", librarySelect.value);
|
||||
window.location.href = url.toString();
|
||||
}
|
||||
});
|
||||
}
|
||||
initLibrarySwitcher({
|
||||
onSwitch: switchLibrary,
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user