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

Conversation

BrunoQuaresma
Copy link
Collaborator

No description provided.

@BrunoQuaresma BrunoQuaresma requested a review from a team January 11, 2024 17:49
@BrunoQuaresma BrunoQuaresma self-assigned this Jan 11, 2024
@BrunoQuaresma BrunoQuaresma requested review from aslilac and Parkreiner and removed request for a team January 11, 2024 17:49
@BrunoQuaresma
Copy link
Collaborator Author

Close #11561

Parkreiner
Parkreiner previously approved these changes Jan 11, 2024
Copy link
Member

@Parkreiner Parkreiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I'd approve this – I think the current approach works, just with a few small changes, but I'm going to be deferring to @aslilac for final judgment. She's been converting me more to Team "Hooks Bad"

I think there's a case for having a custom hook that "just works", but if we can factor things out to remove the hook without decentralizing the edge cases, I think that would be preferable

if (!selectedResource && hasResourcesWithAgents) {
resourcesNav.set(resourceOptionId(resources[0]));
}
}, [resources, selectedResource, resourcesNav]);
Copy link
Member

@Parkreiner Parkreiner Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the only cue for synchronization seems to be when selectedResource changes, could this be redefined with useEffectEvent to make sure useEffect doesn't run too often?

const syncSelectionChange = useEffectEvent((previousResource) => {
  const hasResourcesWithAgents =
    resources.length > 0 &&
    resources[0].agents &&
    resources[0].agents.length > 0;
  if (!previousResource && hasResourcesWithAgents) {
    resourcesNav.set(resourceOptionId(resources[0]));
  }
});

useEffect(() => {
  syncSelectionChange(selectedResource);
}, [syncSelectionChange, selectedResource]);

Even if resources changes by array reference, if the selected resource is still in there, I wouldn't think that should be a cause for making the effect re-run. And because the set function doesn't have memoization, it would cause the effect to run on every render

Though I think there's one more edge case: what would happen if we had a resource selected, but then the resources array becomes completely empty? Would we need to set things back to an empty string?

Copy link
Member

@Parkreiner Parkreiner Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, though, I think the effect logic is what's pushing me to think that a custom hook might be justified, mainly because it's relying on URL syncs via React Router. We can't really get away with derived values or inline state syncs because we'd have to talk to an outside system during the renders

I do think there's value in abstracting over those syncs and giving you a guarantee that things will "just work". I'm just wondering if it's worth testing the hook itself, or if it's better to treat it like an implementation detail, and test the whole component

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this would be the ideal scenario but rendering the workspace component and testing this behavior requires some server events mocking which I would like to avoid. I rather have a simple solution like this for now and only go for more complex ones if it is needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I think there's one more edge case: what would happen if we had a resource selected, but then the resources array becomes completely empty? Would we need to set things back to an empty string?

Hm... I think this would not happen because when resources are returned, we can expect at least one resource. I think Coder does not allow workspace creations without a resource but if it does, I think it is not a huge concern right now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And because the set function doesn't have memoization, it would cause the effect to run on every render

I would not care too much about effects running on every render if it is not going to mess up with the state or cause a loop. I would rather optimize it to be ok running on every render one million times.

Copy link
Collaborator Author

@BrunoQuaresma BrunoQuaresma Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said all of that but after some thought I think the useEffectEvent is a good use case for this as you suggested, and I appreciate you providing the code for refactoring for that ❤️

onChange: (resourceId: string) => void;
selected: string;
onChange: (resource: WorkspaceResource) => void;
isSelected: (resource: WorkspaceResource) => boolean;
Copy link
Member

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 how useResourcesNav 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?

Copy link
Collaborator Author

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.

},
];
rerender({ resources: stoppedResources });
expect(result.current.selected).toBe(undefined);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it makes sense to add testing logic to check that the URL is syncing correctly on renders/re-renders, too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is too much since we don't care too much about the URL but what the hook is returning. What use case could justify this test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more for making sure that the value we're getting back isn't getting out of sync with the URL. I don't think the current implementation has any real risks of that, but the way I see the hook, it does two things:

  • Exposes the selection values
  • Fires side effects to sync the selections with the URL

And we're only testing one of those things

@Parkreiner Parkreiner self-requested a review January 11, 2024 18:56
@Parkreiner Parkreiner dismissed their stale review January 11, 2024 19:00

Accidentally approved

Copy link
Member

@Parkreiner Parkreiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going through with the approval again – @aslilac and I think the hooks approach is fine for now

@BrunoQuaresma BrunoQuaresma merged commit aeb1ab8 into main Jan 12, 2024
@BrunoQuaresma BrunoQuaresma deleted the bq/fix-empty-workspace-screen branch January 12, 2024 13:14
@github-actions github-actions bot locked and limited conversation to collaborators Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants