Open History to see the last commit
1import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
2import { type EnvironmentId, type ProviderInstanceId } from "@t3tools/contracts";
3import {
4 createProviderWorkspaceSnapshotRefreshAtomFamily,
5 isProviderWorkspaceSnapshotScanning,
6 type ProviderWorkspaceSnapshotRefreshState,
7} from "@t3tools/client-runtime/providerSkills";
8import { Atom } from "effect/reactivity";
9
10import { serverEnvironment } from "../../state/server";
11
12const providerWorkspaceSnapshotRefresh = createProviderWorkspaceSnapshotRefreshAtomFamily({
13 providers: serverEnvironment.providersValueAtom,
14 refreshProviders: serverEnvironment.refreshProviders,
15});
16
17const idleRefreshAtom: Atom.Atom<ProviderWorkspaceSnapshotRefreshState | undefined> = Atom.make<
18 ProviderWorkspaceSnapshotRefreshState | undefined
19>(undefined).pipe(Atom.withLabel("provider-workspace-snapshot-refresh:idle"));
20
21/**
22 * Keeps a provider's workspace snapshot fresh for the composer's cwd while
23 * mounted. Returns a check to run when the user types, and whether the cwd's
24 * skills are still being scanned.
25 */
26export function useProviderWorkspaceSnapshotRefresh(input: {
27 readonly environmentId: EnvironmentId | null;
28 readonly gitCwd: string | null;
29 readonly instanceId: ProviderInstanceId | undefined;
30}): { readonly refresh: () => void; readonly scanning: boolean } {
31 const { environmentId, gitCwd, instanceId } = input;
32 const atom =
33 environmentId && gitCwd && instanceId
34 ? providerWorkspaceSnapshotRefresh({ environmentId, instanceId, cwd: gitCwd })
35 : idleRefreshAtom;
36 return {
37 refresh: useAtomRefresh(atom),
38 scanning: isProviderWorkspaceSnapshotScanning(useAtomValue(atom)),
39 };
40}