UNPKG

alinea

Version:
108 lines (106 loc) 3.59 kB
import { atomWithStorage } from "../../chunks/chunk-WDCPVJJC.js"; import { useAtom } from "../../chunks/chunk-TOJF2G3X.js"; import { atom } from "../../chunks/chunk-WJ67RR7S.js"; import "../../chunks/chunk-NZLE2WMY.js"; // src/dashboard/atoms/NavigationAtoms.tsx import { Root } from "alinea/core/Root"; import { Workspace } from "alinea/core/Workspace"; import { keys } from "alinea/core/util/Objects"; import { dashboardNav, navMatchers } from "../DashboardNav.js"; import { configAtom } from "./DashboardAtoms.js"; import { matchAtoms } from "./LocationAtoms.js"; import { workspacePreferenceAtom } from "./PreferencesAtoms.js"; var workspaceNameAtom = atom((get) => { const config = get(configAtom); const match = get( matchAtoms({ route: navMatchers.matchWorkspace, loose: true }) ); const workspacePreference = get(workspacePreferenceAtom); const params = match ?? {}; const requested = [ params.workspace, workspacePreference, keys(config.workspaces)[0] ]; for (const name of requested) if (name && config.workspaces[name]) return name; throw new Error("No workspace found"); }); var workspaceAtom = atom((get) => { const config = get(configAtom); const name = get(workspaceNameAtom); return { name, ...Workspace.data(config.workspaces[name]) }; }); function parseRootPath(path) { return path.split(":"); } var rootNameAtom = atom((get) => { const workspace = get(workspaceAtom); const match = get(matchAtoms({ route: navMatchers.matchRoot, loose: true })); const params = match ?? {}; const requestedRoot = params.root ? parseRootPath(params.root)[0] : void 0; const requested = [requestedRoot, keys(workspace.roots)[0]]; for (const name of requested) if (name && workspace.roots[name]) return name; throw new Error("No root found"); }); var rootAtom = atom((get) => { const config = get(configAtom); const workspace = get(workspaceAtom); const name = get(rootNameAtom); const root = workspace.roots[name]; if (!root) throw new Error("No root found"); return { name, ...Root.data(config.workspaces[workspace.name][name]) }; }); var preferredLanguageAtom = atomWithStorage( "@alinea/locale", null ); var localeAtom = atom((get) => { const workspace = get(workspaceAtom); const root = get(rootAtom); const config = get(configAtom); const { i18n } = root; if (!i18n) return null; const match = get(matchAtoms({ route: navMatchers.matchRoot, loose: true })); const params = match ?? {}; const { root: rootKey } = params; if (rootKey) { const fromUrl = parseRootPath(rootKey)[1]; if (fromUrl && i18n.locales.includes(fromUrl)) return fromUrl; } const preferredLanguage = get(preferredLanguageAtom); if (preferredLanguage && i18n.locales.includes(preferredLanguage)) return preferredLanguage; return Root.defaultLocale(config.workspaces[workspace.name][root.name]) ?? null; }); var entryLocationAtom = atom((get) => { const match = get(matchAtoms({ route: navMatchers.matchEntryId })); const [root, locale] = parseRootPath(match?.root ?? ""); const params = { ...match, locale }; if (root) params.root = root; return params || void 0; }); var navAtom = atom((get) => { const { name: workspace } = get(workspaceAtom); const { name: root } = get(rootAtom); const locale = get(localeAtom); return dashboardNav({ workspace, root, locale }); }); var usePreferredLanguage = () => useAtom(preferredLanguageAtom); export { entryLocationAtom, localeAtom, navAtom, preferredLanguageAtom, rootAtom, usePreferredLanguage, workspaceAtom };