UNPKG

alinea

Version:
87 lines (83 loc) 2.38 kB
import "../../chunks/chunk-WDCPVJJC.js"; import { useAtomValue, useSetAtom, useStore } from "../../chunks/chunk-TOJF2G3X.js"; import { atom } from "../../chunks/chunk-WJ67RR7S.js"; import { QueryClient } from "../../chunks/chunk-YWCPLD22.js"; import "../../chunks/chunk-NZLE2WMY.js"; // src/dashboard/atoms/DashboardAtoms.ts import { localUser } from "alinea/core/User"; // node_modules/jotai/esm/react/utils.mjs import { useCallback, useMemo } from "react"; var hydratedMap = /* @__PURE__ */ new WeakMap(); function useHydrateAtoms(values, options) { const store = useStore(options); const hydratedSet = getHydratedSet(store); for (const [atom2, value] of values) { if (!hydratedSet.has(atom2) || (options == null ? void 0 : options.dangerouslyForceHydrate)) { hydratedSet.add(atom2); store.set(atom2, value); } } } var getHydratedSet = (store) => { let hydratedSet = hydratedMap.get(store); if (!hydratedSet) { hydratedSet = /* @__PURE__ */ new WeakSet(); hydratedMap.set(store, hydratedSet); } return hydratedSet; }; // src/dashboard/atoms/DashboardAtoms.ts import { useEffect } from "react"; var sessionAtom = atom(void 0); var dashboardOptionsAtom = atom(void 0); function useSetDashboardOptions(options) { useHydrateAtoms([[dashboardOptionsAtom, options]]); const { client, config, local } = options; const isAnonymous = local && !process.env.ALINEA_FORCE_AUTH; if (isAnonymous) { const userData = typeof process !== "undefined" && process.env.ALINEA_USER; useHydrateAtoms([ [ sessionAtom, { user: userData ? JSON.parse(userData) : localUser, cnx: client } ] ]); } const setDashboardOptions = useSetAtom(dashboardOptionsAtom); useEffect( () => setDashboardOptions(options), [options.client, options.config] ); } var queryClientAtom = atom( new QueryClient({ defaultOptions: { queries: { retry: false } } }) ); var clientAtom = atom((get) => { return get(dashboardOptionsAtom).client; }); var configAtom = atom((get) => { return get(dashboardOptionsAtom).config; }); var useSession = () => useAtomValue(sessionAtom); var useConfig = () => useAtomValue(configAtom); export { clientAtom, configAtom, dashboardOptionsAtom, queryClientAtom, sessionAtom, useConfig, useSession, useSetDashboardOptions };