Add resolver, server, PDF rendering and web app
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a7fcdc4b8
commit
bdfdf581f3
59 files changed
+4088
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import type { Book } from '../shared/types.ts';
|
||||
|
||||
export interface BookState { byId: Record<number, Book> }
|
||||
export type BookAction =
|
||||
| { type: 'upsert'; book: Book }
|
||||
| { type: 'delete'; id: number }
|
||||
| { type: 'replace'; books: Book[] };
|
||||
|
||||
export const emptyState: BookState = { byId: {} };
|
||||
|
||||
export function reduce(state: BookState, action: BookAction): BookState {
|
||||
switch (action.type) {
|
||||
case 'upsert':
|
||||
return { byId: { ...state.byId, [action.book.id]: action.book } };
|
||||
case 'delete': {
|
||||
const byId = { ...state.byId };
|
||||
delete byId[action.id];
|
||||
return { byId };
|
||||
}
|
||||
case 'replace': {
|
||||
const byId: Record<number, Book> = {};
|
||||
for (const b of action.books) byId[b.id] = b;
|
||||
return { byId };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Shelf order: by sortKey, books without one last, then title. */
|
||||
export function compareShelf(a: Book, b: Book): number {
|
||||
if (a.sortKey !== b.sortKey) {
|
||||
if (a.sortKey === null) return 1;
|
||||
if (b.sortKey === null) return -1;
|
||||
return a.sortKey < b.sortKey ? -1 : 1;
|
||||
}
|
||||
return a.title.localeCompare(b.title);
|
||||
}
|
||||
|
||||
export function shelfOrder(state: BookState): Book[] {
|
||||
return Object.values(state.byId).sort(compareShelf);
|
||||
}
|
||||
Reference in new issue
Block a user