UNPKG

alinea

Version:
59 lines (57 loc) 1.81 kB
import { require_dataloader } from "../../chunks/chunk-3TNMMA3W.js"; import { atomFamily } from "../../chunks/chunk-WDCPVJJC.js"; import { atom } from "../../chunks/chunk-WJ67RR7S.js"; import { __toESM } from "../../chunks/chunk-NZLE2WMY.js"; // src/dashboard/atoms/EntrySummaryAtoms.ts var import_dataloader = __toESM(require_dataloader(), 1); import { summarySelection } from "alinea/core/media/Summary"; import { configAtom } from "./DashboardAtoms.js"; import { dbAtom, entryRevisionAtoms } from "./DbAtoms.js"; var entrySummaryLoaderAtom = atom((get) => { const graph = get(dbAtom); const { schema } = get(configAtom); const selection = summarySelection(schema); return new import_dataloader.default( async (ids) => { const res = /* @__PURE__ */ new Map(); const entries = await graph.find({ select: selection, id: { in: ids }, status: "preferDraft" }); for (const entry of entries) { const locale = entry.locale ?? ""; if (res.has(entry.id)) res.get(entry.id)[locale] = entry; else res.set(entry.id, { [locale]: entry }); } return ids.map((id) => res.get(id)); } ); }); var entrySummaryAtoms = atomFamily( (keys) => { return atom(async (get) => { const loader = get(entrySummaryLoaderAtom); const summaries = await loader.clear(keys.id).load(keys.id); if (!summaries) return; const summary = summaries[keys.locale ?? ""] ?? summaries[Object.keys(summaries)[0]]; get(entryRevisionAtoms(summary.id)); if (summary?.parents) for (const parent of summary.parents) get(entryRevisionAtoms(parent.id)); return summary; }); }, (a, b) => a.id === b.id && a.locale === b.locale ); export { entrySummaryAtoms, entrySummaryLoaderAtom };