File
Blob: src/client/stores/db/shared-inbox.ts
| 1 | import type { SharedInboxWorkspaceSummary, SharedWithMeItem } from "@/shared/types"; |
| 2 | import { db, type SharedInboxItemRow, type SharedInboxSummaryRow } from "./bland-db"; |
| 3 | |
| 4 | interface ReplaceAllInput { |
| 5 | items: SharedWithMeItem[]; |
| 6 | workspaceSummaries: SharedInboxWorkspaceSummary[]; |
| 7 | } |
| 8 | |
| 9 | async function replaceAll(input: ReplaceAllInput): Promise<void> { |
| 10 | const itemRows: SharedInboxItemRow[] = input.items.map((item, rank) => ({ |
| 11 | pageId: item.page_id, |
| 12 | workspaceId: item.workspace.id, |
| 13 | rank, |
| 14 | item, |
| 15 | })); |
| 16 | const summaryRows: SharedInboxSummaryRow[] = input.workspaceSummaries.map((summary, rank) => ({ |
| 17 | workspaceId: summary.workspace.id, |
| 18 | rank, |
| 19 | summary, |
| 20 | })); |
| 21 | |
| 22 | await db.transaction("rw", [db.sharedInboxItems, db.sharedInboxWorkspaceSummaries], async () => { |
| 23 | await db.sharedInboxItems.clear(); |
| 24 | if (itemRows.length > 0) { |
| 25 | await db.sharedInboxItems.bulkPut(itemRows); |
| 26 | } |
| 27 | await db.sharedInboxWorkspaceSummaries.clear(); |
| 28 | if (summaryRows.length > 0) { |
| 29 | await db.sharedInboxWorkspaceSummaries.bulkPut(summaryRows); |
| 30 | } |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | export const sharedInboxCommands = { |
| 35 | replaceAll, |
| 36 | }; |