1import {
2 createAssetEnvironmentAtoms,
3 createProjectFaviconUrlAtomFamily,
4} from "@t3tools/client-runtime/state/assets";
5import { Atom } from "effect/reactivity";
6
7import { connectionAtomRuntime } from "../connection/runtime";
8import { projectFaviconCache } from "../assets/projectFaviconCache";
9import { isElectron } from "../env";
10import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
11import { environmentProjectClonesAtom } from "./projectClones";
12import { environmentSession } from "./session";
13
14const localMediaEnvironment = Atom.make((get) => {
15 if (!isElectron) return null;
16 const environmentId = get(primaryEnvironmentIdAtom);
17 if (environmentId === null) return null;
18 const connection = get(environmentSession.preparedConnectionValueAtom(environmentId));
19 // The session's bootstrap config clears on disconnect and refreshes on reconnect.
20 const config = get(environmentSession.initialConfigValueAtom(environmentId));
21 return connection._tag === "None" || config === null
22 ? null
23 : { environmentId, httpBaseUrl: connection.value.httpBaseUrl };
24});
25
26export const assetEnvironment = createAssetEnvironmentAtoms(
27 connectionAtomRuntime,
28 localMediaEnvironment,
29);
30
31export const projectFaviconUrlAtom = createProjectFaviconUrlAtomFamily({
32 imageCache: projectFaviconCache,
33 createUrl: assetEnvironment.createUrl,
34 preparedConnection: environmentSession.preparedConnectionValueAtom,
35 projectClones: environmentProjectClonesAtom,
36});