Skip to content
Source
Sign inSign up

assets.ts

51 lines2.0 KB

1import type { ProjectFaviconTarget } from "@t3tools/client-runtime/project-favicon-cache";
2import {
3 createAssetEnvironmentAtoms,
4 createProjectFaviconUrlAtomFamily,
5 projectFaviconResource,
6} from "@t3tools/client-runtime/state/assets";
7import { Atom } from "effect/reactivity";
8​
9import { connectionAtomRuntime } from "../connection/runtime";
10import { appAtomRegistry } from "../rpc/atomRegistry";
11import { projectFaviconCache } from "../assets/projectFaviconCache";
12import { isElectron } from "../env";
13import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
14import { environmentProjectClonesAtom } from "./projectClones";
15import { environmentSession } from "./session";
16​
17const localMediaEnvironment = Atom.make((get) => {
18 if (!isElectron) return null;
19 const environmentId = get(primaryEnvironmentIdAtom);
20 if (environmentId === null) return null;
21 const connection = get(environmentSession.preparedConnectionValueAtom(environmentId));
22 const config = get(environmentSession.initialConfigValueAtom(environmentId));
23 return connection._tag === "None" || config === null
24 ? null
25 : { environmentId, httpBaseUrl: connection.value.httpBaseUrl };
26});
27​
28export const assetEnvironment = createAssetEnvironmentAtoms(
29 connectionAtomRuntime,
30 localMediaEnvironment,
31);
32​
33export const projectFaviconUrlAtom = createProjectFaviconUrlAtomFamily({
34 imageCache: projectFaviconCache,
35 createUrl: assetEnvironment.createUrl,
36 preparedConnection: environmentSession.preparedConnectionValueAtom,
37 projectClones: environmentProjectClonesAtom,
38});
39​
40/**
41 * Asks the server for a project's icon again. Choosing the icon path a project had before would
42 * otherwise reuse that path's cached URL, which says "missing" if the file did not exist then.
43 */
44export function refreshProjectFaviconUrl(target: ProjectFaviconTarget) {
45 appAtomRegistry.refresh(
46 assetEnvironment.createUrl({
47 environmentId: target.environmentId,
48 input: { resource: projectFaviconResource(target.cwd, target.faviconPath) },
49 }),
50 );