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
Use effect event
  • Loading branch information
BrunoQuaresma committed Jan 12, 2024
commit 1d2e8f376215615cbef62ae746588ff6a83e05f1
21 changes: 14 additions & 7 deletions site/src/pages/WorkspacePage/useResourcesNav.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WorkspaceResource } from "api/typesGenerated";
import { useTab } from "hooks";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useCallback, useEffect, useMemo } from "react";

export const resourceOptionId = (resource: WorkspaceResource) => {
Expand All @@ -24,14 +25,20 @@ export const useResourcesNav = (resources: WorkspaceResource[]) => {
);

const selectedResource = resources.find(isSelected);

const onSelectedResourceChange = useEffectEvent(
(previousResource?: WorkspaceResource) => {
const hasResourcesWithAgents =
firstResource &&
firstResource.agents &&
firstResource.agents.length > 0;
if (!previousResource && hasResourcesWithAgents) {
resourcesNav.set(resourceOptionId(firstResource));
}
},
);
useEffect(() => {
const hasResourcesWithAgents =
firstResource && firstResource.agents && firstResource.agents.length > 0;
if (!selectedResource && hasResourcesWithAgents) {
resourcesNav.set(resourceOptionId(firstResource));
}
}, [firstResource, resourcesNav, selectedResource]);
onSelectedResourceChange(selectedResource);
}, [onSelectedResourceChange, selectedResource]);

const select = useCallback(
(resource: WorkspaceResource) => {
Expand Down