alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
171 lines (169 loc) • 5.36 kB
JavaScript
import {
useAtomValue
} from "../../chunks/chunk-WF77DMLN.js";
import {
atom
} from "../../chunks/chunk-OBOPLPUQ.js";
import {
require_dataloader
} from "../../chunks/chunk-ICLGQ53P.js";
import {
__toESM
} from "../../chunks/chunk-U5RRZUYZ.js";
// src/dashboard/atoms/EntryAtoms.ts
var import_dataloader = __toESM(require_dataloader(), 1);
import { Type } from "alinea/core";
import { Entry } from "alinea/core/Entry";
import { entryFileName } from "alinea/core/EntryFilenames";
import { MutationType } from "alinea/core/Mutation";
import { generateKeyBetween } from "alinea/core/util/FractionalIndexing";
import { entries } from "alinea/core/util/Objects";
import { useMemo } from "react";
import { useDashboard } from "../hook/UseDashboard.js";
import { configAtom } from "./DashboardAtoms.js";
import { graphAtom, useMutate } from "./DbAtoms.js";
import { rootAtom, workspaceAtom } from "./NavigationAtoms.js";
function rootId(rootName) {
return `@alinea/root-${rootName}`;
}
var visibleTypesAtom = atom((get) => {
const { schema } = get(configAtom);
return entries(schema).filter(([_, type]) => !Type.meta(type).isHidden).map(([name]) => name);
});
async function entryTreeRoot(active, workspace, root, visibleTypes) {
const rootEntries = Entry().where(
Entry.workspace.is(workspace),
Entry.root.is(root),
Entry.parent.isNull(),
Entry.active,
Entry.type.isIn(visibleTypes)
).select(Entry.i18nId).groupBy(Entry.i18nId).orderBy(Entry.index.asc());
const children = await active.find(rootEntries);
return {
id: rootId(root),
index: "",
isFolder: true,
entries: [],
children
};
}
var entryTreeItemLoaderAtom = atom(async (get) => {
const graph = await get(graphAtom);
const visibleTypes = get(visibleTypesAtom);
const { schema } = get(configAtom);
const root = get(rootAtom);
const workspace = get(workspaceAtom);
return new import_dataloader.default(async (ids) => {
const indexed = /* @__PURE__ */ new Map();
const search = ids.filter((id) => id !== rootId(root.name));
const data = {
id: Entry.i18nId,
entryId: Entry.entryId,
type: Entry.type,
title: Entry.title,
phase: Entry.phase,
locale: Entry.locale,
workspace: Entry.workspace,
root: Entry.root,
path: Entry.path,
parentPaths({ parents }) {
return parents(Entry).select(Entry.path);
},
children({ children }) {
return children(Entry).where(Entry.type.isIn(visibleTypes)).select(Entry.i18nId).groupBy(Entry.i18nId).orderBy(Entry.index.asc());
}
};
const entries2 = Entry().select({
id: Entry.i18nId,
index: Entry.index,
data,
translations({ translations }) {
return translations().select(data);
}
}).groupBy(Entry.i18nId).where(Entry.i18nId.isIn(search));
const rows = await graph.preferDraft.find(entries2);
for (const row of rows) {
const entries3 = [row.data].concat(row.translations);
indexed.set(row.id, {
id: row.id,
index: row.index,
entries: entries3,
children: [...new Set(entries3.flatMap((entry) => entry.children))]
});
}
const res = [];
for (const id of ids) {
if (id === rootId(root.name)) {
res.push(
await entryTreeRoot(
graph.preferDraft,
workspace.name,
root.name,
visibleTypes
)
);
continue;
}
const entry = indexed.get(id);
if (!entry) {
res.push(void 0);
continue;
}
const typeName = entry.entries[0].type;
const type = schema[typeName];
const isFolder = Type.isContainer(type);
res.push({ ...entry, isFolder });
}
return res;
});
});
var loaderAtom = atom((get) => {
return { loader: get(entryTreeItemLoaderAtom) };
});
function useEntryTreeProvider() {
const { loader } = useAtomValue(loaderAtom);
const mutate = useMutate();
const { config } = useDashboard();
return useMemo(() => {
return {
onDrop(items, { item: parent, childIndex, insertionIndex }) {
if (items.length !== 1)
return;
const [dropping] = items;
if (childIndex === null)
return;
if (dropping.getParent() !== parent) {
console.log("Todo: move entries");
return;
}
const previous = parent.getChildren()[childIndex - 1];
const next = parent.getChildren()[childIndex];
const previousIndex = previous?.getItemData()?.index ?? null;
const nextIndex = next?.getItemData()?.index ?? null;
try {
const newIndex = generateKeyBetween(previousIndex, nextIndex);
for (const entry of dropping.getItemData().entries) {
mutate({
type: MutationType.Order,
entryId: entry.entryId,
file: entryFileName(config, entry, entry.parentPaths),
index: newIndex
});
}
} catch (err) {
console.error(err);
}
},
async getItem(id) {
return await (await loader).clear(id).load(id);
},
async getChildren(id) {
return this.getItem(id).then((item) => item?.children ?? []);
}
};
}, [loader]);
}
export {
rootId,
useEntryTreeProvider
};