THE CLASS: the /api JWT middleware's TokenLookup is
"cookie:token,header:Authorization:Bearer" — the token cookie always
rides same-origin fetches, so a signed-in user never needs the
localStorage bearer. But a dozen call sites gated their fetch on
`localStorage.getItem("token")` and early-returned when absent. The
global 401 interceptor CLEARS that localStorage entry on any session
rotation/blip — after which every gated write silently did nothing:
theme changes reverted on reload (the reported "can no longer change
themes", on top of the interpolation bug d5dbaa48 fixed), chain-order
selects, collection deletion, Add-to-collection submits, filter
save/load/delete, conflict actions, linking actions, stats charts,
tag suggestions, device management, admin scanner actions — all dead
for perfectly authenticated cookie users.
Fix: storage.ts exports authHeaders() (bearer when present, cookie
carries otherwise) and every gate/header site now uses it — theme.ts,
collections.ts (htmx auth, doRemoveBook/doBulkRemove, setChainOrder),
bookPicker submit, filter_builder suggestions + match count,
collection-rules apply hook, bookshelf save/load/delete filter,
conflicts x4, linking x3, admin scanner actions + polls (requireToken
removed — its "Session expired" toast fired for cookie users too),
dashboard preferences/sections, custom-section-builder, header
scanner poll, stats x3, tag suggestions, profile delete-account
(which also wrongly redirected cookie users to /login), and
device-management x4. Reads keep their graceful-empty behavior; the
bearers still ride along when present.
Pinned: the knob-matrix theme spec now REMOVES localStorage.token and
asserts the theme PUT still fires (cookie-only) and the class flips —
the exact strand that started this.
Found by noticing the panel browser's Nord click switched the class
but produced no PUT: the persistence half was gated on a token the
401 interceptor had cleared.
231 lines
6.9 KiB
TypeScript
231 lines
6.9 KiB
TypeScript
import { Alpine } from "./alpine";
|
|
import { authHeaders } from "./storage";
|
|
import { showToast } from "./toast";
|
|
|
|
async function refreshConflicts(): Promise<void> {
|
|
// Rendering ownership: the list and stats are server-rendered markup.
|
|
// Swap in the templ partial (stats arrive as an out-of-band swap)
|
|
// instead of re-rendering cards from JSON.
|
|
try {
|
|
await window.htmx.ajax("GET", "/conflicts/list", {
|
|
target: "#conflicts-list",
|
|
swap: "outerHTML",
|
|
});
|
|
const list = document.getElementById("conflicts-list");
|
|
if (list) Alpine.initTree(list);
|
|
filterConflicts();
|
|
updateBulkActions();
|
|
} catch (error) {
|
|
console.error("Failed to refresh conflicts:", error);
|
|
}
|
|
}
|
|
|
|
// --- Client-side filter and bulk selection -------------------------------
|
|
// Pure functions over the server-rendered conflict cards (rendering
|
|
// ownership: JS decides what shows; templ rendered what they look like).
|
|
|
|
function filterConflicts(): void {
|
|
const select = document.getElementById("status-filter") as HTMLSelectElement | null;
|
|
const filter = select?.value ?? "unresolved";
|
|
let total = 0;
|
|
let visible = 0;
|
|
document.querySelectorAll<HTMLElement>(".conflict-item").forEach((item) => {
|
|
total++;
|
|
const status = item.dataset.status ?? "unresolved";
|
|
const show = filter === "all" || status === filter;
|
|
item.style.display = show ? "" : "none";
|
|
if (show) visible++;
|
|
});
|
|
const empty = document.getElementById("filter-empty");
|
|
if (empty) empty.classList.toggle("hidden", visible > 0 || total === 0);
|
|
// Cards hidden by the filter can't stay selected.
|
|
if (visible !== total) updateBulkActions();
|
|
}
|
|
|
|
function toggleAllConflicts(): void {
|
|
const selectAll = document.getElementById("select-all-conflicts") as HTMLInputElement | null;
|
|
if (!selectAll) return;
|
|
document.querySelectorAll<HTMLElement>(".conflict-item").forEach((item) => {
|
|
if (item.style.display === "none") return;
|
|
const box = item.querySelector<HTMLInputElement>(".conflict-checkbox");
|
|
if (box) box.checked = selectAll.checked;
|
|
});
|
|
updateBulkActions();
|
|
}
|
|
|
|
function getSelectedConflictIds(): string[] {
|
|
return Array.from(document.querySelectorAll<HTMLInputElement>(".conflict-checkbox:checked"))
|
|
.map((box) => box.dataset.conflictId || "")
|
|
.filter((id) => id !== "");
|
|
}
|
|
|
|
function updateBulkActions(): void {
|
|
const selected = getSelectedConflictIds();
|
|
const bar = document.getElementById("bulk-actions");
|
|
if (bar) bar.classList.toggle("hidden", selected.length === 0);
|
|
const count = document.getElementById("selected-count");
|
|
if (count) count.textContent = String(selected.length);
|
|
}
|
|
|
|
function resolveSelected(): void {
|
|
const ids = getSelectedConflictIds();
|
|
if (ids.length === 0) return;
|
|
const strategyEl = document.getElementById("bulk-strategy") as HTMLSelectElement | null;
|
|
const strategy = (strategyEl?.value ?? "most_recent") as "most_recent" | "highest_progress";
|
|
void bulkResolve(strategy, ids);
|
|
}
|
|
|
|
function dismissSelected(): void {
|
|
const ids = getSelectedConflictIds();
|
|
if (ids.length === 0) return;
|
|
void bulkDismiss(ids);
|
|
}
|
|
|
|
async function resolveConflict(
|
|
conflictId: string,
|
|
winner: string,
|
|
manualData?: Record<string, unknown>,
|
|
): Promise<void> {
|
|
// cookie auth carries the call; a null bearer must not strand it
|
|
|
|
try {
|
|
const response = await fetch(`/api/conflicts/${conflictId}/resolve`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ winner, manual_data: manualData }),
|
|
});
|
|
|
|
if (response.ok) {
|
|
showToast("Conflict resolved", "success");
|
|
refreshConflicts();
|
|
} else {
|
|
const error = await response.json();
|
|
showToast(error.error || "Failed to resolve conflict", "error");
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to resolve conflict:", error);
|
|
showToast("Failed to resolve conflict", "error");
|
|
}
|
|
}
|
|
|
|
async function bulkResolve(
|
|
strategy: "most_recent" | "highest_progress",
|
|
conflictIds: string[],
|
|
): Promise<void> {
|
|
// cookie auth carries the call; a null bearer must not strand it
|
|
|
|
try {
|
|
const response = await fetch("/api/conflicts/bulk-resolve", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ conflict_ids: conflictIds, strategy }),
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data: BulkResolveResponse = await response.json();
|
|
showToast(`Resolved ${data.success} conflicts`, "success");
|
|
refreshConflicts();
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to bulk resolve:", error);
|
|
showToast("Failed to bulk resolve conflicts", "error");
|
|
}
|
|
}
|
|
|
|
async function bulkDismiss(conflictIds: string[]): Promise<void> {
|
|
// cookie auth carries the call; a null bearer must not strand it
|
|
|
|
try {
|
|
const response = await fetch("/api/conflicts/bulk-dismiss", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ conflict_ids: conflictIds }),
|
|
});
|
|
|
|
if (response.ok) {
|
|
showToast("Conflicts dismissed", "success");
|
|
refreshConflicts();
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to dismiss conflicts:", error);
|
|
showToast("Failed to dismiss conflicts", "error");
|
|
}
|
|
}
|
|
|
|
async function dismissAllResolved(): Promise<void> {
|
|
// cookie auth carries the call; a null bearer must not strand it
|
|
|
|
try {
|
|
const response = await fetch("/api/conflicts/dismiss-resolved", {
|
|
method: "POST",
|
|
headers: authHeaders(),
|
|
});
|
|
|
|
if (response.ok) {
|
|
showToast("Resolved conflicts dismissed", "success");
|
|
refreshConflicts();
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to dismiss resolved:", error);
|
|
showToast("Failed to dismiss resolved conflicts", "error");
|
|
}
|
|
}
|
|
|
|
function showResolveModal(conflictId: string): void {
|
|
const modal = document.getElementById("resolve-modal");
|
|
const conflictIdInput = document.getElementById("resolve-conflict-id") as HTMLInputElement;
|
|
|
|
if (modal && conflictIdInput) {
|
|
conflictIdInput.value = conflictId;
|
|
modal.classList.remove("hidden");
|
|
}
|
|
}
|
|
|
|
function hideResolveModal(): void {
|
|
const modal = document.getElementById("resolve-modal");
|
|
if (modal) {
|
|
modal.classList.add("hidden");
|
|
}
|
|
}
|
|
|
|
function handleResolveSubmit(event: Event): void {
|
|
event.preventDefault();
|
|
|
|
const form = event.target as HTMLFormElement;
|
|
const conflictId = (form.querySelector("#resolve-conflict-id") as HTMLInputElement)?.value;
|
|
const winner = (form.querySelector('input[name="winner"]:checked') as HTMLInputElement)?.value;
|
|
|
|
if (!conflictId || !winner) {
|
|
showToast("Please select a winner", "error");
|
|
return;
|
|
}
|
|
|
|
resolveConflict(conflictId, winner);
|
|
hideResolveModal();
|
|
}
|
|
|
|
Alpine.data("conflicts", () => ({
|
|
init() {
|
|
filterConflicts();
|
|
},
|
|
bulkDismiss,
|
|
bulkResolve,
|
|
dismissAllResolved,
|
|
handleResolveSubmit,
|
|
hideResolveModal,
|
|
refreshConflicts,
|
|
resolveConflict,
|
|
showResolveModal,
|
|
filterConflicts,
|
|
toggleAllConflicts,
|
|
updateBulkActions,
|
|
resolveSelected,
|
|
dismissSelected,
|
|
}));
|