MCPcopy Create free account
hub / github.com/coder/coder / useResourcesNav

Function useResourcesNav

site/src/pages/WorkspacePage/useResourcesNav.ts:14–51  ·  view source on GitHub ↗
(resources: WorkspaceResource[])

Source from the content-addressed store, hash-verified

12// refactoring. Consider revisiting this solution in the future for a more
13// robust implementation.
14export const useResourcesNav = (resources: WorkspaceResource[]) => {
15 const resourcesNav = useSearchParamsKey({ key: "resources" });
16 const isSelected = useCallback(
17 (resource: WorkspaceResource) => {
18 return resourceOptionValue(resource) === resourcesNav.value;
19 },
20 [resourcesNav.value],
21 );
22
23 const onResourceChanges = useEffectEvent(
24 (resources?: WorkspaceResource[]) => {
25 const hasSelectedResource = resourcesNav.value !== "";
26 const hasResources = resources && resources.length > 0;
27 const hasResourcesWithAgents =
28 hasResources && resources[0].agents && resources[0].agents.length > 0;
29
30 if (!hasSelectedResource && hasResourcesWithAgents) {
31 resourcesNav.setValue(resourceOptionValue(resources[0]));
32 }
33 },
34 );
35 useEffect(() => {
36 onResourceChanges(resources);
37 }, [resources]);
38
39 const select = useCallback(
40 (resource: WorkspaceResource) => {
41 resourcesNav.setValue(resourceOptionValue(resource));
42 },
43 [resourcesNav],
44 );
45
46 return {
47 isSelected,
48 select,
49 value: resourcesNav.value,
50 };
51};

Callers 2

WorkspaceFunction · 0.90

Calls 2

useSearchParamsKeyFunction · 0.90
resourceOptionValueFunction · 0.85

Tested by

no test coverage detected