Prepare SearchBar for the integration with unified resources#34402
Prepare SearchBar for the integration with unified resources#34402
SearchBar for the integration with unified resources#34402Conversation
This matches the latest design.
This will be needed for unified resources integration, where it is possible to select more than one filter.
Otherwise, we will have to convert the resource filter type to the type expected by the unified resources component and vice-versa. It is just easier to use the same type.
| const [isOpen, setIsOpen] = useState(false); | ||
| const [inputValue, setInputValue] = useState(''); | ||
| const [activePicker, setActivePicker] = useState(actionPicker); | ||
| // TODO(ravicious): Consider using another data structure for search filters as we know that we |
There was a problem hiding this comment.
I removed the comment because now we can select multiple resource filters, and I think the current structure works quite well.
There was a problem hiding this comment.
We could have an object with fields clusterFilter and resourceFilters, this way you wouldn't need to do the filtering based on the type field. From what I see, 90% of the type we end up either plucking the cluster filter from the array or filtering it down just resource filters.
There rest are places which have logic like this:
teleport/web/packages/teleterm/src/ui/Search/useSearch.ts
Lines 369 to 371 in 97c3757
It could be encapsulated in functions which work on the SearchFilters type (which would be that object with the fields I mentioned), here we could have something like hasActiveSearchFilters(filters: SearchFilters): boolean.
…aaand while writing this comment I realized what was the original requirement behind using an array – we need to keep the order of them so that if you first enter a resource filter and then a cluster filter, they're rendered in the UI in the same way.
Let's keep them this way I guess.
There was a problem hiding this comment.
Yeah, the requirement to keep the filters in order didn't allow me to change the structure.
|
The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with |
| const [isOpen, setIsOpen] = useState(false); | ||
| const [inputValue, setInputValue] = useState(''); | ||
| const [activePicker, setActivePicker] = useState(actionPicker); | ||
| // TODO(ravicious): Consider using another data structure for search filters as we know that we |
There was a problem hiding this comment.
We could have an object with fields clusterFilter and resourceFilters, this way you wouldn't need to do the filtering based on the type field. From what I see, 90% of the type we end up either plucking the cluster filter from the array or filtering it down just resource filters.
There rest are places which have logic like this:
teleport/web/packages/teleterm/src/ui/Search/useSearch.ts
Lines 369 to 371 in 97c3757
It could be encapsulated in functions which work on the SearchFilters type (which would be that object with the fields I mentioned), here we could have something like hasActiveSearchFilters(filters: SearchFilters): boolean.
…aaand while writing this comment I realized what was the original requirement behind using an array – we need to keep the order of them so that if you first enter a resource filter and then a cluster filter, they're rendered in the UI in the same way.
Let's keep them this way I guess.
| // TODO(ravicious): Accept just `server | database | kube` as searchFilter here, wrap it in a | ||
| // variant of a discriminated union in searchResult.ts. | ||
| filter: ResourceTypeSearchFilter | undefined; | ||
| filters: SupportedResourceType[] | undefined; |
There was a problem hiding this comment.
The filter field used to be of type ResourceTypeSearchFilter | undefined because we wanted to communicate that there might be no filter. In cases where an array is accepted, I think it's better to require the callsite to always provide an array and represent the "no filter" case by passing an empty array.
| }; | ||
|
|
||
| export type SupportedResourceType = 'kubes' | 'servers' | 'databases'; | ||
| export type SupportedResourceType = 'kube_cluster' | 'node' | 'db'; |
There was a problem hiding this comment.
Could we use the Pick utility type to document that it's a subset of the type from unified resources?
There was a problem hiding this comment.
That's a good idea, btw I had to use Extract utility type to make a subset of union (Pick doesn't work).
| 'node' as const, | ||
| 'db' as const, | ||
| 'kube_cluster' as const, |
There was a problem hiding this comment.
const is not needed AFAIR if you declared the type of the variable itself.
There was a problem hiding this comment.
They were needed here because the type was specified for an array that was then filtered and TS couldn't narrow down the type:
Type string is not assignable to type SupportedResourceType
I overcome this by extracting a const var:
const SUPPORTED_RESOURCE_TYPES: SupportedResourceType[] = [
'node',
'db',
'kube_cluster',
];| @@ -126,7 +121,7 @@ export function ActionPicker(props: { input: ReactElement }) { | |||
| return ( | |||
| <FilterButton | |||
| key="resource-type" | |||
There was a problem hiding this comment.
Keys need to be made unique, otherwise there's an error about them being the same and removing them is buggy.
There was a problem hiding this comment.
Right, I forgot about this place.
| <span | ||
| title={props.text} | ||
| css={` | ||
| max-width: calc(${props => props.theme.space[9]}px * 2); |
There was a problem hiding this comment.
Done, I also removed text-overflow and overflow, they don't have any effect without max-width.
|
The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with |

Part of #30422
I tried to explain a reason for each change in the commit message, let me know if something is unclear.
I'm going to backport this PR to v14 and v13 to make future backports easier. The change that allows selecting multiple resource filters probably won't be very useful without unified resources, but I think it is okay to backport it.
The most "controversial" may be 596e1e82ce6e0824dff34dec7ec08875c33221c6 that changes the way of rendering the search bar. It was based on a comment from Kenny, he suggested that we may wan't to switch to more "modal" version of the search bar, that is not restricted by the input width.
The biggest difference it makes when the window is narrow and the cluster selector is visible.
Before:

After:

IMO the controversial part is that the selector now opens horizontally centered, while the search input isn't centered. But I think that is fine, the command bar in VS code behaves very similarly.
Changelog: Allow selecting multiple resource filters in the search bar in Connect