| 1 | import { |
| 2 | createAssetEnvironmentAtoms, |
| 3 | createProjectFaviconUrlAtomFamily, |
| 4 | } from "@t3tools/client-runtime/state/assets"; |
| 5 | import { Atom } from "effect/reactivity"; |
| 6 | |
| 7 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 8 | import { projectFaviconCache } from "../assets/projectFaviconCache"; |
| 9 | import { isElectron } from "../env"; |
| 10 | import { primaryEnvironmentIdAtom } from "./primaryEnvironment"; |
| 11 | import { environmentProjectClonesAtom } from "./projectClones"; |
| 12 | import { environmentSession } from "./session"; |
| 13 | |
| 14 | const 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 | |
| 26 | export const assetEnvironment = createAssetEnvironmentAtoms( |
| 27 | connectionAtomRuntime, |
| 28 | localMediaEnvironment, |
| 29 | ); |
| 30 | |
| 31 | export const projectFaviconUrlAtom = createProjectFaviconUrlAtomFamily({ |
| 32 | imageCache: projectFaviconCache, |
| 33 | createUrl: assetEnvironment.createUrl, |
| 34 | preparedConnection: environmentSession.preparedConnectionValueAtom, |
| 35 | projectClones: environmentProjectClonesAtom, |
| 36 | }); |