Skip to content

fix(site): fix resource selection when workspace resources change #11581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
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
Add more comments about the solution
  • Loading branch information
BrunoQuaresma committed Jan 12, 2024
commit ec2282e29b5ea14f75991ed2e53afb50d583e12e
11 changes: 9 additions & 2 deletions site/src/pages/WorkspacePage/useResourcesNav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ describe("useResourcesNav", () => {
);
expect(result.current.selected?.id).toBe(startedResources[0].id);

// When a workspace is stopped, there is no resource with agents
// When a workspace is stopped, there are no resources with agents, so we
// need to retain the currently selected resource. This ensures consistency
// when handling workspace updates that involve a sequence of stopping and
// starting. By preserving the resource selection, we maintain the desired
// configuration and prevent unintended changes during the stop-and-start
// process.
const stoppedResources: WorkspaceResource[] = [
{
...MockWorkspaceResource,
Expand All @@ -116,7 +121,9 @@ describe("useResourcesNav", () => {
},
];
rerender({ resources: stoppedResources });
expect(result.current.selected).toBe(undefined);
expect(result.current.selectedValue).toBe(
resourceOptionId(startedResources[0]),
);

// When a workspace is started again a resource is selected
rerender({ resources: startedResources });
Expand Down
26 changes: 13 additions & 13 deletions site/src/pages/WorkspacePage/useResourcesNav.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { WorkspaceResource } from "api/typesGenerated";
import { useTab } from "hooks";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useCallback, useEffect, useMemo } from "react";
import { useCallback, useEffect } from "react";

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

// TODO: This currently serves as a temporary workaround for synchronizing the
// resources tab during workspace transitions. The optimal resolution involves
// eliminating the sync and updating the URL within the workspace data update
// event in the WebSocket "onData" event. However, this requires substantial
// refactoring. Consider revisiting this solution in the future for a more
// robust implementation.
export const useResourcesNav = (resources: WorkspaceResource[]) => {
const firstResource = useMemo(
() => (resources.length > 0 ? resources[0] : undefined),
[resources],
);
const resourcesNav = useTab(
"resources",
firstResource ? resourceOptionId(firstResource) : "",
);
const resourcesNav = useTab("resources", "");

const isSelected = useCallback(
(resource: WorkspaceResource) => {
Expand All @@ -28,11 +27,11 @@ export const useResourcesNav = (resources: WorkspaceResource[]) => {
const onSelectedResourceChange = useEffectEvent(
(previousResource?: WorkspaceResource) => {
const hasResourcesWithAgents =
firstResource &&
firstResource.agents &&
firstResource.agents.length > 0;
resources.length > 0 &&
resources[0].agents &&
resources[0].agents.length > 0;
if (!previousResource && hasResourcesWithAgents) {
resourcesNav.set(resourceOptionId(firstResource));
resourcesNav.set(resourceOptionId(resources[0]));
}
},
);
Expand All @@ -51,5 +50,6 @@ export const useResourcesNav = (resources: WorkspaceResource[]) => {
isSelected,
select,
selected: selectedResource,
selectedValue: resourcesNav.value,
};
};