Rework scan screen, make lookups non-blocking, fix review findings
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
468cafc6db
commit
b5defb4e4f
46 files changed
+1957
-406
No files matched your search
@@ -7,7 +7,35 @@ import type { BookAction, BookState } from './store.ts';
|
||||
let state: BookState = emptyState;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
/** When each book was first seen waiting for its lookup, for the elapsed-seconds display. */
|
||||
const pendingSince = new Map<number, number>();
|
||||
function trackPending(action: BookAction): void {
|
||||
const note = (b: Book, fromServerTime: boolean) => {
|
||||
if (b.status !== 'pending') { pendingSince.delete(b.id); return; }
|
||||
if (pendingSince.has(b.id)) return;
|
||||
const created = Date.parse(b.createdAt);
|
||||
pendingSince.set(b.id, fromServerTime && Number.isFinite(created) ? Math.min(created, Date.now()) : Date.now());
|
||||
};
|
||||
if (action.type === 'upsert') note(action.book, false);
|
||||
else if (action.type === 'replace') { pendingSince.clear(); action.books.forEach(b => note(b, true)); }
|
||||
else pendingSince.delete(action.id);
|
||||
}
|
||||
export function lookupStartedAt(book: Book): number { return pendingSince.get(book.id) ?? Date.now(); }
|
||||
|
||||
/** Re-renders once a second while `active`, and returns the current time. */
|
||||
export function useNow(active: boolean): number {
|
||||
const [now, setNow] = useState(Date.now());
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
setNow(Date.now());
|
||||
const t = setInterval(() => setNow(Date.now()), 1000);
|
||||
return () => clearInterval(t);
|
||||
}, [active]);
|
||||
return now;
|
||||
}
|
||||
|
||||
export function dispatch(action: BookAction): void {
|
||||
trackPending(action);
|
||||
state = reduce(state, action);
|
||||
listeners.forEach(l => l());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user