Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply improvements from PRs
  • Loading branch information
BrunoQuaresma committed Jan 12, 2024
commit 8c6e3341936504dc39f8e47af04ec7b65130550d
36 changes: 20 additions & 16 deletions site/src/pages/WorkspacePage/useResourcesNav.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import { WorkspaceResource } from "api/typesGenerated";
import { useTab } from "hooks";
import { useCallback, useEffect } from "react";
import { useCallback, useEffect, useMemo } from "react";

export const resourceOptionId = (resource: WorkspaceResource) => {
return `${resource.type}_${resource.name}`;
};

export const useResourcesNav = (resources: WorkspaceResource[]) => {
const resourcesNav = useTab("resources", "");
const selectedResource = resources.find(
(r) => resourceOptionId(r) === resourcesNav.value,
const firstResource = useMemo(
() => (resources.length > 0 ? resources[0] : undefined),
[resources],
);
const resourcesNav = useTab(
"resources",
firstResource ? resourceOptionId(firstResource) : "",
);

const isSelected = useCallback(
(resource: WorkspaceResource) => {
return resourceOptionId(resource) === resourcesNav.value;
},
[resourcesNav.value],
);

const selectedResource = resources.find(isSelected);

useEffect(() => {
const hasResourcesWithAgents =
resources.length > 0 &&
resources[0].agents &&
resources[0].agents.length > 0;
firstResource && firstResource.agents && firstResource.agents.length > 0;
if (!selectedResource && hasResourcesWithAgents) {
resourcesNav.set(resourceOptionId(resources[0]));
resourcesNav.set(resourceOptionId(firstResource));
}
}, [resources, selectedResource, resourcesNav]);
}, [firstResource, resourcesNav, selectedResource]);

const select = useCallback(
(resource: WorkspaceResource) => {
Expand All @@ -29,13 +40,6 @@ export const useResourcesNav = (resources: WorkspaceResource[]) => {
[resourcesNav],
);

const isSelected = useCallback(
(resource: WorkspaceResource) => {
return resourceOptionId(resource) === resourcesNav.value;
},
[resourcesNav.value],
);

return {
isSelected,
select,
Expand Down