Files
bookhoard/web/src/library-switcher.ts
T
John O'Keefe 8631aa00bf refactor: remove dead code across server, templ pages, and client modules
Deadcode-verified removals (RTA reachability from main, cross-checked
with -test and textual xrefs):

Go server:
- whole files: middleware/transaction.go, middleware/error_handler.go,
  middleware/password_validator.go (live registration machinery kept),
  handlers/sync.go, sync/offline.go
- unrouted endpoints: auth UpdateUsername/UpdateEmail, timezone
  settings handler, OPDS RegisterOPDS, Kobo LibrarySync handler,
  ReaderHandler.ShowReader, JSON-data twins on collections/library
- duplicate book-matching handlers (book_matching.go Handler twins,
  matching.go MatchingHandler twins), superseded helpers
  (router.parseUUID, handlers.ValidateDeviceToken, normalizePath,
  userExists, generateLockID)

Templ/client:
- never-routed pages: api_explorer.templ, queue.templ and their
  client modules api-explorer.ts, api-explorer-docs.ts, queue.ts
- fully orphaned wrapper modules events.ts, dom.ts
- broken clearSyncQueue (called a nonexistent endpoint) and the
  hidden, never-populated sync-queue section on the devices page
- 166+ unused exports demoted to private (knip-verified); ~85 truly
  dead declarations removed via tsc noUnusedLocals fixpoint
- stale 'interactive API Explorer' claims dropped from docs

The docs drift-gate package (internal/docs), templ-bound modules
(dashboard, stats, header, index, login, profile*, bookshelf,
collections, toast-error), sqlc-generated code, and web/static are
intentionally untouched.
2026-10-25 11:06:08 -04:00

101 lines
3.3 KiB
TypeScript

import { Alpine } from "./alpine";
import { ALL_LIBRARIES, setSelectedLibrary } from "./storage";
import { showToast } from "./toast";
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;
// View Transitions (native, where supported): the browser snapshots the
// old shelves, fetchFn swaps the DOM, and the two snapshots crossfade —
// no blank phase, no manual opacity choreography. The dim + spinner is
// shown before the snapshot and hidden inside the update, so the dim
// blends away as part of the crossfade. Browsers without the API fall
// through to the dim + fade sequence below.
const doc = document as Document & {
startViewTransition?: (updateCallback: () => void | Promise<void>) => {
finished: Promise<void>;
};
};
if (doc.startViewTransition) {
try {
loading.classList.remove("hidden");
const transition = doc.startViewTransition(async () => {
await fetchFn();
loading.classList.add("hidden");
});
await transition.finished;
} catch (error) {
loading.classList.add("hidden");
showToast("Failed to load library", "error");
console.error("Switch library error:", error);
}
return;
}
try {
container.classList.add("opacity-0", "transition-opacity", "duration-150");
await new Promise((resolve) => setTimeout(resolve, 150));
loading.classList.remove("hidden");
await fetchFn();
// Lift the dim BEFORE the fade-in starts, so the new content's fade
// plays fully visible instead of being hard-cut by the overlay
// disappearing mid-transition.
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);
} 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 {
// Async switchers (SSR navigation awaits nothing, API-backed switchers
// await their fetch) — hence void | Promise<void>.
onSwitch: (libraryId: string) => void | 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", () => ({}));