-
Notifications
You must be signed in to change notification settings - Fork 899
feat: add 'Show all tokens' toggle for owners #6325
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 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a103b3
add tokens switch
Kira-Pilot 6402745
reorged TokensPage
Kira-Pilot 340a9d1
using Trans component for description
Kira-Pilot ff2ac68
using Trans component on DeleteDialog
Kira-Pilot ade9548
add owner col
Kira-Pilot dd587e8
simplify hook return
Kira-Pilot 2ce518c
lint
Kira-Pilot 740c4a2
type for response
Kira-Pilot bb6ce20
Merge remote-tracking branch 'origin/main' into add-token-switch/kira…
Kira-Pilot b54c70b
PR feedback
Kira-Pilot 7b87fb1
fix lint
Kira-Pilot 41fed93
Merge remote-tracking branch 'origin/main' into add-token-switch/kira…
Kira-Pilot 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,11 @@ func (api *API) apiKey(rw http.ResponseWriter, r *http.Request) { | |
httpapi.Write(ctx, rw, http.StatusOK, convertAPIKey(key)) | ||
} | ||
|
||
type ConvertedAPIKey struct { | ||
codersdk.APIKey | ||
Username string `json:"username"` | ||
} | ||
|
||
// @Summary Get user tokens | ||
// @ID get-user-tokens | ||
// @Security CoderSessionToken | ||
|
@@ -216,9 +221,20 @@ func (api *API) tokens(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
var apiKeys []codersdk.APIKey | ||
var apiKeys []ConvertedAPIKey | ||
for _, key := range keys { | ||
apiKeys = append(apiKeys, convertAPIKey(key)) | ||
user, err := api.Database.GetUserByID(ctx, key.UserID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use Generally, we should never query in |
||
if err != nil { | ||
apiKeys = append(apiKeys, ConvertedAPIKey{ | ||
APIKey: convertAPIKey(key), | ||
Username: "", | ||
}) | ||
} else { | ||
apiKeys = append(apiKeys, ConvertedAPIKey{ | ||
APIKey: convertAPIKey(key), | ||
Username: user.Username, | ||
}) | ||
} | ||
} | ||
|
||
httpapi.Write(ctx, rw, http.StatusOK, apiKeys) | ||
|
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
58 changes: 58 additions & 0 deletions
58
site/src/pages/UserSettingsPage/TokensPage/components/ConfirmDeleteDialog.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,58 @@ | ||
import { FC } from "react" | ||
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog" | ||
import { useTranslation, Trans } from "react-i18next" | ||
import { useDeleteToken } from "../hooks" | ||
import { displaySuccess, displayError } from "components/GlobalSnackbar/utils" | ||
import { getErrorMessage } from "api/errors" | ||
|
||
export const ConfirmDeleteDialog: FC<{ | ||
queryKey: (string | boolean)[] | ||
tokenId: string | undefined | ||
setTokenId: (arg: string | undefined) => void | ||
}> = ({ queryKey, tokenId, setTokenId }) => { | ||
const { t } = useTranslation("tokensPage") | ||
|
||
const description = ( | ||
<Trans t={t} i18nKey="deleteToken.deleteCaption" values={{ tokenId }}> | ||
Are you sure you want to delete this token? | ||
<br /> | ||
<br /> | ||
{{ tokenId }} | ||
</Trans> | ||
) | ||
|
||
const { mutate: deleteToken, isLoading: isDeleting } = | ||
useDeleteToken(queryKey) | ||
|
||
const onDeleteSuccess = () => { | ||
displaySuccess(t("deleteToken.deleteSuccess")) | ||
setTokenId(undefined) | ||
} | ||
|
||
const onDeleteError = (error: unknown) => { | ||
const message = getErrorMessage(error, t("deleteToken.deleteFailure")) | ||
displayError(message) | ||
setTokenId(undefined) | ||
} | ||
|
||
return ( | ||
<ConfirmDialog | ||
title={t("deleteToken.delete")} | ||
description={description} | ||
open={Boolean(tokenId) || isDeleting} | ||
confirmLoading={isDeleting} | ||
onConfirm={() => { | ||
if (!tokenId) { | ||
return | ||
} | ||
deleteToken(tokenId, { | ||
onError: onDeleteError, | ||
onSuccess: onDeleteSuccess, | ||
}) | ||
}} | ||
onClose={() => { | ||
setTokenId(undefined) | ||
}} | ||
/> | ||
) | ||
} |
48 changes: 48 additions & 0 deletions
48
site/src/pages/UserSettingsPage/TokensPage/components/TokensSwitch.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,48 @@ | ||
import { FC } from "react" | ||
import Switch from "@material-ui/core/Switch" | ||
import FormGroup from "@material-ui/core/FormGroup" | ||
import FormControlLabel from "@material-ui/core/FormControlLabel" | ||
import makeStyles from "@material-ui/core/styles/makeStyles" | ||
import { useTranslation } from "react-i18next" | ||
|
||
export const TokensSwitch: FC<{ | ||
hasReadAll: boolean | ||
viewAllTokens: boolean | ||
setViewAllTokens: (arg: boolean) => void | ||
}> = ({ hasReadAll, viewAllTokens, setViewAllTokens }) => { | ||
const styles = useStyles() | ||
const { t } = useTranslation("tokensPage") | ||
|
||
return ( | ||
<FormGroup row className={styles.formRow}> | ||
{hasReadAll && ( | ||
<FormControlLabel | ||
control={ | ||
<Switch | ||
className={styles.selectAllSwitch} | ||
checked={viewAllTokens} | ||
onChange={() => setViewAllTokens(!viewAllTokens)} | ||
name="viewAllTokens" | ||
color="primary" | ||
/> | ||
} | ||
label={t("toggleLabel")} | ||
/> | ||
)} | ||
</FormGroup> | ||
) | ||
} | ||
|
||
const useStyles = makeStyles(() => ({ | ||
formRow: { | ||
justifyContent: "end", | ||
marginBottom: "10px", | ||
}, | ||
selectAllSwitch: { | ||
// decrease the hover state on the switch | ||
// so that it isn't hidden behind the container | ||
"& .MuiIconButton-root": { | ||
padding: "8px", | ||
}, | ||
}, | ||
})) |
2 changes: 2 additions & 0 deletions
2
site/src/pages/UserSettingsPage/TokensPage/components/index.ts
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,2 @@ | ||
export { ConfirmDeleteDialog } from "./ConfirmDeleteDialog" | ||
export { TokensSwitch } from "./TokensSwitch" |
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.
Uh oh!
There was an error while loading. Please reload this page.