Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 type ComposerPathSearchState,
4 type ComposerPathSearchTarget,
5 EMPTY_COMPOSER_PATH_SEARCH_ATOM,
6 EMPTY_COMPOSER_PATH_SEARCH_STATE,
7 composerPathSearchStateAtom,
8 createComposerPathSearchManager,
9 getComposerPathSearchTargetKey,
10 normalizeComposerPathSearchQuery,
11} from "@t3tools/client-runtime";
12import { useEffect, useMemo } from "react";
13
14import {
15 readEnvironmentConnection,
16 subscribeEnvironmentConnections,
17 subscribeProviderInvalidations,
18} from "../environments/runtime";
19import { appAtomRegistry } from "../rpc/atomRegistry";
20
21const COMPOSER_PATH_SEARCH_LIMIT = 80;
22const COMPOSER_PATH_SEARCH_DEBOUNCE_MS = 120;
23const COMPOSER_PATH_SEARCH_STALE_TIME_MS = 15_000;
24
25const composerPathSearchManager = createComposerPathSearchManager({
26 getRegistry: () => appAtomRegistry,
27 getClient: (environmentId) => readEnvironmentConnection(environmentId)?.client.projects ?? null,
28 subscribeClientChanges: subscribeEnvironmentConnections,
29 limit: COMPOSER_PATH_SEARCH_LIMIT,
30 debounceMs: COMPOSER_PATH_SEARCH_DEBOUNCE_MS,
31 staleTimeMs: COMPOSER_PATH_SEARCH_STALE_TIME_MS,
32});
33
34export function invalidateComposerPathSearches(): void {
35 composerPathSearchManager.invalidate();
36}
37
38subscribeProviderInvalidations(invalidateComposerPathSearches);
39
40export function useComposerPathSearch(target: ComposerPathSearchTarget): ComposerPathSearchState {
41 const stableTarget = useMemo(
42 () => ({
43 environmentId: target.environmentId,
44 cwd: target.cwd,
45 query: normalizeComposerPathSearchQuery(target.query),
46 }),
47 [target.cwd, target.environmentId, target.query],
48 );
49 const targetKey = getComposerPathSearchTargetKey(stableTarget);
50
51 useEffect(() => composerPathSearchManager.watch(stableTarget), [stableTarget]);
52
53 const state = useAtomValue(
54 targetKey !== null ? composerPathSearchStateAtom(targetKey) : EMPTY_COMPOSER_PATH_SEARCH_ATOM,
55 );
56 return targetKey === null ? EMPTY_COMPOSER_PATH_SEARCH_STATE : state;
57}