Skip to content
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

search frontend: allow multiple toggle state rules #16343

Merged
merged 1 commit into from Dec 2, 2020

Conversation

rvantonder
Copy link
Contributor

Helps with #13958 (comment). This refactor allows specifying multiple rules to disable toggles. See inline comments for more.

@@ -41,6 +41,7 @@ exports[`PlainQueryInput empty 1`] = `
</div>
<div
aria-checked={true}
aria-disabled={false}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The previous value here had type boolean | undefined. Let's just use boolean and set it explicitly.

if (rule.condition) {
disabled = true
tooltipValue = rule.reason
onToggle = (): void => undefined // Make the button untoggleable.
Copy link
Contributor Author

@rvantonder rvantonder Dec 2, 2020

Choose a reason for hiding this comment

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

Question for @attfarhan: I want to make the button untoggleable when one of the conditions is true. I think this is always going to be the case. So: "if we should disable -> also make untoggleable", and that's what this change does. It works, but I'm not sure if there's a better way.

What I do know is that the current way isn't great: previous to this PR, we would duplicate the "is structural" check, once inside the onToggle callback, and again as a disabledCondition. When I added more conditions, I couldn't figure out why I could still toggle the button even though it was greyed out/disabled. Then I found we have another callback that duplicates the check.

This duplication gets complicated and the code structure doesn't work. We can avoid that now with this solution, and I'm happy that the loop/logic above will take care of that: any time a disabledOn rule is added, we guarantee it will not be toggleable. But, let me know if there's something I'm missing that could be bad about overwriting onToggle in this part of the code. So the question is: things are working, but is overwriting the onToggle here safe/reasonable or should I be watching out for something?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this is fine, it should work. One thing you could do instead of reassigning onToggle is to make the onClick prop below take this: onClick={disabled ? undefined : onToggle} (this works since onClick is optional and can be undefined to denote no handler). IMO this makes it a bit clearer inline that the callback in onClick may change value based on disabled. Your call though, either way is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, I like that! Cheers

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, here's a proposal to convert <QueryInputToggle/> to a function component and make this logic more declarative thanks to hooks: #16351

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lguychard I think I wanted something like that, and want that change. But I'm going to merge this stack to save me an approve+merge+rebase cycle :-) I'll see if I can rebase your branch after.

Comment on lines -81 to -83
if (patternType === SearchPatternType.structural) {
return
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This disables the button from being toggleable, and previously the disabledCondition grayed it out. I found the logical separation of these two things confusing in the code, and don't think we should do this. When I add more things, I would have to inline more conditions/special cases into the other callbacks and it gets icky quickly. With the refactor, we don't need to check conditions inside the toggleable callback--instead, we will simply override the toggle callback to be the default (untoggleable).

@rvantonder rvantonder marked this pull request as ready for review December 2, 2020 05:54
@rvantonder rvantonder requested review from attfarhan and a team December 2, 2020 05:54
@codecov
Copy link

codecov bot commented Dec 2, 2020

Codecov Report

Merging #16343 (8b8cd84) into main (57e0d67) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main   #16343   +/-   ##
=======================================
  Coverage   52.92%   52.92%           
=======================================
  Files        1656     1656           
  Lines       82693    82698    +5     
  Branches     7455     7386   -69     
=======================================
+ Hits        43764    43770    +6     
+ Misses      35090    35088    -2     
- Partials     3839     3840    +1     
Flag Coverage Δ
go 52.46% <ø> (-0.01%) ⬇️
integration 29.63% <100.00%> (+0.05%) ⬆️
storybook 28.01% <60.00%> (+0.01%) ⬆️
typescript 54.04% <100.00%> (+0.02%) ⬆️
unit 35.80% <60.00%> (+<0.01%) ⬆️
Impacted Files Coverage Δ
client/web/src/search/input/toggles/Toggles.tsx 89.47% <ø> (+1.97%) ⬆️
.../web/src/search/input/toggles/QueryInputToggle.tsx 83.33% <100.00%> (+6.86%) ⬆️
.../internal/codeintel/resolvers/graphql/locations.go 83.50% <0.00%> (-2.07%) ⬇️
client/web/src/settings/SettingsPage.tsx 76.47% <0.00%> (+11.76%) ⬆️

Copy link
Contributor

@attfarhan attfarhan left a comment

Choose a reason for hiding this comment

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

if (rule.condition) {
disabled = true
tooltipValue = rule.reason
onToggle = (): void => undefined // Make the button untoggleable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this is fine, it should work. One thing you could do instead of reassigning onToggle is to make the onClick prop below take this: onClick={disabled ? undefined : onToggle} (this works since onClick is optional and can be undefined to denote no handler). IMO this makes it a bit clearer inline that the callback in onClick may change value based on disabled. Your call though, either way is fine.

@rvantonder rvantonder merged commit 01c7a9a into main Dec 2, 2020
@rvantonder rvantonder deleted the rvt/case-toggle-a branch December 2, 2020 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants