-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add new ClusterDropdown component #36310
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
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
179 changes: 179 additions & 0 deletions
179
web/packages/shared/components/ClusterDropdown/ClusterDropdown.tsx
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,179 @@ | ||
| /** | ||
| * Teleport | ||
| * Copyright (C) 2023 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import React, { useState, useEffect } from 'react'; | ||
| import { useHistory } from 'react-router'; | ||
| import { ButtonSecondary, Flex, Menu, MenuItem, Text } from 'design'; | ||
| import { ChevronDown } from 'design/Icon'; | ||
| import cfg from 'teleport/config'; | ||
| import { Cluster } from 'teleport/services/clusters'; | ||
|
|
||
| import { HoverTooltip } from 'shared/components/ToolTip'; | ||
|
|
||
| export interface ClusterDropdownProps { | ||
| clusterLoader: ClusterLoader; | ||
| clusterId: string; | ||
| /* | ||
| * onChange is an optional prop. If onChange is not passed, it will use the built in "changeCluster" function | ||
| */ | ||
| onChange?: (newValue: string) => void; | ||
| /* | ||
| * onError is required because this dropdown can be placed on any page, it does not display its own error | ||
| * messages. Even if using the internal "loadClusters", we will pass the error back to be consumed by the parent. | ||
| */ | ||
| onError: (errorMessage: string) => void; | ||
| } | ||
|
|
||
| interface ClusterLoader { | ||
| fetchClusters: ( | ||
| signal?: AbortSignal, | ||
| fromCache?: boolean | ||
| ) => Promise<Cluster[]>; | ||
| clusters: Cluster[]; | ||
| } | ||
|
|
||
| function createOptions(clusters: Cluster[]) { | ||
| return clusters.map(cluster => ({ | ||
| value: cluster.clusterId, | ||
| label: cluster.clusterId, | ||
| })); | ||
| } | ||
|
|
||
| export function ClusterDropdown({ | ||
| clusterLoader, | ||
| clusterId, | ||
| onChange, | ||
| onError, | ||
| }: ClusterDropdownProps) { | ||
| const initialClusters = clusterLoader.clusters; | ||
| const [options, setOptions] = React.useState<Option[]>( | ||
| createOptions(initialClusters) | ||
| ); | ||
| const history = useHistory(); | ||
| const [anchorEl, setAnchorEl] = useState(null); | ||
|
|
||
| const selectedOption = { | ||
| value: clusterId, | ||
| label: clusterId, | ||
| }; | ||
|
|
||
| function loadClusters(signal: AbortSignal) { | ||
| onError(''); | ||
| try { | ||
| return clusterLoader.fetchClusters(signal); | ||
| } catch (err) { | ||
| onError(err.message); | ||
| } | ||
| } | ||
|
|
||
| function changeCluster(clusterId: string) { | ||
| const newPathName = cfg.getClusterRoute(clusterId); | ||
|
|
||
| const oldPathName = cfg.getClusterRoute(selectedOption.value); | ||
|
|
||
| const newPath = history.location.pathname.replace(oldPathName, newPathName); | ||
|
|
||
| // keep current view just change the clusterId | ||
| history.push(newPath); | ||
| } | ||
|
|
||
| function onChangeOption(clusterId: string) { | ||
| if (onChange) { | ||
| onChange(clusterId); | ||
| } else { | ||
| changeCluster(clusterId); | ||
| } | ||
| handleClose(); | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| const signal = new AbortController(); | ||
| async function getOptions() { | ||
| try { | ||
| const res = await loadClusters(signal.signal); | ||
| setOptions(createOptions(res)); | ||
| } catch (err) { | ||
| onError(err.message); | ||
| } | ||
| } | ||
|
|
||
| getOptions(); | ||
| return () => { | ||
| signal.abort(); | ||
| }; | ||
| }, []); | ||
|
|
||
| const handleOpen = event => { | ||
| setAnchorEl(event.currentTarget); | ||
| }; | ||
|
|
||
| const handleClose = () => { | ||
| setAnchorEl(null); | ||
| }; | ||
|
|
||
| if (options.length < 1) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Flex textAlign="center" alignItems="center"> | ||
| <HoverTooltip tipContent={'Select cluster'}> | ||
| <ButtonSecondary | ||
| px={2} | ||
| css={` | ||
| border-color: ${props => props.theme.colors.spotBackground[0]}; | ||
| `} | ||
| textTransform="none" | ||
| size="small" | ||
| onClick={handleOpen} | ||
| > | ||
| {selectedOption.label} | ||
| <ChevronDown ml={2} size="small" color="text.slightlyMuted" /> | ||
| </ButtonSecondary> | ||
| </HoverTooltip> | ||
| <Menu | ||
| popoverCss={() => `margin-top: 36px;`} | ||
| transformOrigin={{ | ||
| vertical: 'top', | ||
| horizontal: 'left', | ||
| }} | ||
| anchorOrigin={{ | ||
| vertical: 'bottom', | ||
| horizontal: 'left', | ||
| }} | ||
| anchorEl={anchorEl} | ||
| open={Boolean(anchorEl)} | ||
| onClose={handleClose} | ||
| > | ||
| {options.map(cluster => ( | ||
| <MenuItem | ||
| px={2} | ||
| key={cluster.value} | ||
| onClick={() => onChangeOption(cluster.value)} | ||
| > | ||
| <Text ml={2} fontWeight={300} fontSize={2}> | ||
| {cluster.label} | ||
| </Text> | ||
| </MenuItem> | ||
| ))} | ||
| </Menu> | ||
| </Flex> | ||
| ); | ||
| } | ||
|
|
||
| type Option = { value: string; label: string }; |
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
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 |
|---|---|---|
|
|
@@ -98,6 +98,7 @@ export default function useAuditEvents( | |
| range, | ||
| setRange, | ||
| rangeOptions, | ||
| ctx, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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 we should add an abort controller here on option change. in the unified resource changing filter mid way cancels previous requests, we should do the same for cluster filter.
when one cluster is loading, changing to another one will briefly flash the previous results before the new one (in my case my other cluster was down, so it briefly flashed me a red error banner before loading the new response)
Uh oh!
There was an error while loading. Please reload this page.
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.
This branch is a bit out of date, but we don't have a user groups dropdown anymore. It's no longer an option to select from the user groups table on master/v14
sure!
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'm so dumb. so the abort controller didn't fix what i was seeing (b/c duh it's cached..) the flash i'm getting it's from the non-fetch-cluster api call that fails (eg: get sessions with a cluster that's down). that would mean we'd have to add aborters everywhere but i don't think it's that a big deal to change atm