-
Notifications
You must be signed in to change notification settings - Fork 936
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1222a24
fix(site): fix resource selection when workspace resources change
BrunoQuaresma 8c6e334
Apply improvements from PRs
BrunoQuaresma 1d2e8f3
Use effect event
BrunoQuaresma 032c3a1
Merge branch 'main' of https://github.com/coder/coder into bq/fix-emp…
BrunoQuaresma ec2282e
Add more comments about the solution
BrunoQuaresma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { resourceOptionId, useResourcesNav } from "./useResourcesNav"; | ||
import { WorkspaceResource } from "api/typesGenerated"; | ||
import { MockWorkspaceResource } from "testHelpers/entities"; | ||
import { RouterProvider, createMemoryRouter } from "react-router-dom"; | ||
|
||
describe("useResourcesNav", () => { | ||
it("selects the first resource if it has agents and no resource is selected", () => { | ||
const resources: WorkspaceResource[] = [ | ||
MockWorkspaceResource, | ||
{ | ||
...MockWorkspaceResource, | ||
agents: [], | ||
}, | ||
]; | ||
const { result } = renderHook(() => useResourcesNav(resources), { | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }])} | ||
/> | ||
), | ||
}); | ||
expect(result.current.selected?.id).toBe(MockWorkspaceResource.id); | ||
}); | ||
|
||
it("selects the first resource if it has agents and selected resource is not find", async () => { | ||
const resources: WorkspaceResource[] = [ | ||
MockWorkspaceResource, | ||
{ | ||
...MockWorkspaceResource, | ||
agents: [], | ||
}, | ||
]; | ||
const { result } = renderHook(() => useResourcesNav(resources), { | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }], { | ||
initialEntries: ["/?resources=not_found_resource_id"], | ||
})} | ||
/> | ||
), | ||
}); | ||
expect(result.current.selected?.id).toBe(MockWorkspaceResource.id); | ||
}); | ||
|
||
it("selects the resource passed in the URL", () => { | ||
const resources: WorkspaceResource[] = [ | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_python", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_java", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_image", | ||
name: "coder_image_python", | ||
agents: [], | ||
}, | ||
]; | ||
const { result } = renderHook(() => useResourcesNav(resources), { | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }], { | ||
initialEntries: [`/?resources=${resourceOptionId(resources[1])}`], | ||
})} | ||
/> | ||
), | ||
}); | ||
expect(result.current.selected?.id).toBe(resources[1].id); | ||
}); | ||
|
||
it("selects a resource when resources are updated", () => { | ||
const startedResources: WorkspaceResource[] = [ | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_python", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_java", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_image", | ||
name: "coder_image_python", | ||
agents: [], | ||
}, | ||
]; | ||
const { result, rerender } = renderHook( | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
({ resources }) => useResourcesNav(resources), | ||
{ | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }])} | ||
/> | ||
), | ||
initialProps: { resources: startedResources }, | ||
}, | ||
); | ||
expect(result.current.selected?.id).toBe(startedResources[0].id); | ||
|
||
// 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, | ||
type: "docker_image", | ||
name: "coder_image_python", | ||
agents: [], | ||
}, | ||
]; | ||
rerender({ resources: stoppedResources }); | ||
expect(result.current.selectedValue).toBe( | ||
resourceOptionId(startedResources[0]), | ||
); | ||
|
||
// When a workspace is started again a resource is selected | ||
rerender({ resources: startedResources }); | ||
expect(result.current.selected?.id).toBe(startedResources[0].id); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { WorkspaceResource } from "api/typesGenerated"; | ||
import { useTab } from "hooks"; | ||
import { useEffectEvent } from "hooks/hookPolyfills"; | ||
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 resourcesNav = useTab("resources", ""); | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const isSelected = useCallback( | ||
(resource: WorkspaceResource) => { | ||
return resourceOptionId(resource) === resourcesNav.value; | ||
}, | ||
[resourcesNav.value], | ||
); | ||
|
||
const selectedResource = resources.find(isSelected); | ||
const onSelectedResourceChange = useEffectEvent( | ||
(previousResource?: WorkspaceResource) => { | ||
const hasResourcesWithAgents = | ||
resources.length > 0 && | ||
resources[0].agents && | ||
resources[0].agents.length > 0; | ||
if (!previousResource && hasResourcesWithAgents) { | ||
resourcesNav.set(resourceOptionId(resources[0])); | ||
} | ||
}, | ||
); | ||
useEffect(() => { | ||
onSelectedResourceChange(selectedResource); | ||
}, [onSelectedResourceChange, selectedResource]); | ||
|
||
const select = useCallback( | ||
(resource: WorkspaceResource) => { | ||
resourcesNav.set(resourceOptionId(resource)); | ||
}, | ||
[resourcesNav], | ||
); | ||
|
||
return { | ||
isSelected, | ||
select, | ||
selected: selectedResource, | ||
selectedValue: resourcesNav.value, | ||
}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
isSelected
change is an improvement, but I'm wondering if the implementation is getting a little too tied to howuseResourcesNav
is set up.What do you think of turning
isSelected
into just a boolean, and making the parent component call the function to get the right prop to pass in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to know what is the selected resource and since this is not just an attribute but a "calculation", I think this interface makes things easier to extend.