UNPKG

alinea

Version:
79 lines (77 loc) 2.31 kB
import { atomFamily } from "../chunks/chunk-WDCPVJJC.js"; import { atom } from "../chunks/chunk-WJ67RR7S.js"; import "../chunks/chunk-NZLE2WMY.js"; // src/dashboard/Routes.tsx import { Entry } from "alinea/core/Entry"; import { dbAtom } from "alinea/dashboard/atoms/DbAtoms"; import { locationAtom, useNavigate } from "alinea/dashboard/atoms/LocationAtoms"; import { useNav } from "alinea/dashboard/hook/UseNav"; import { Loader } from "alinea/ui"; import { useLayoutEffect } from "react"; import { entryEditorAtoms } from "./atoms/EntryEditorAtoms.js"; import { entryLocationAtom, localeAtom } from "./atoms/NavigationAtoms.js"; import { Route, Router } from "./atoms/RouterAtoms.js"; import { ContentView } from "./pages/ContentView.js"; import { jsx } from "react/jsx-runtime"; var editorLoader = atomFamily(() => { return atom(async (get) => { const entryLocation = get(entryLocationAtom); const locale = get(localeAtom); return { editor: await get(entryEditorAtoms({ locale, id: entryLocation?.id })) }; }); }); var entryRoute = new Route({ path: "*", loader: editorLoader, component: ContentView }); var editLoader = atomFamily(() => { return atom(async (get) => { const location = get(locationAtom); const searchParams = new URLSearchParams(location.search); const url = searchParams.get("url"); const workspace = searchParams.get("workspace") ?? void 0; const root = searchParams.get("root") ?? void 0; const graph = await get(dbAtom); const entry = await graph.first({ select: { id: Entry.id, locale: Entry.locale, root: Entry.root, workspace: Entry.workspace }, url, workspace, root, status: "preferDraft" }); if (!entry) return null; return { ...entry, locale: entry.locale || void 0 }; }); }); var editRoute = new Route({ path: "/edit", loader: editLoader, component: EditRoute }); function EditRoute(location) { const nav = useNav(); const navigate = useNavigate(); useLayoutEffect(() => { if (location) navigate(nav.entry(location)); }, [location]); return /* @__PURE__ */ jsx(Loader, { absolute: true }); } var routes = [editRoute, entryRoute]; var router = new Router({ routes }); export { editRoute, entryRoute, router };