-
Notifications
You must be signed in to change notification settings - Fork 928
feat: Manage tokens in dashboard #5444
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a6a7b79
wip
f0ssel 7783557
wip view
f0ssel 7297a50
lang
f0ssel b2169e5
lang
f0ssel c64b0b2
get deleting working
f0ssel f3bc600
confirm dialog
f0ssel e12e873
lang
f0ssel fce4deb
add storybook
f0ssel 8a09fde
mock tokens
f0ssel 24e3f99
add util lastUsedOrNever
f0ssel f885760
make fmt
f0ssel 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
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { FC, PropsWithChildren } from "react" | ||
import { Section } from "../../../components/Section/Section" | ||
import { TokensPageView } from "./TokensPageView" | ||
import { tokensMachine } from "xServices/tokens/tokensXService" | ||
import { useMachine } from "@xstate/react" | ||
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog" | ||
import { Typography } from "components/Typography/Typography" | ||
import makeStyles from "@material-ui/core/styles/makeStyles" | ||
|
||
export const Language = { | ||
title: "Tokens", | ||
descriptionPrefix: | ||
"Tokens are used to authenticate with the Coder API. You can create a token with the Coder CLI using the ", | ||
deleteTitle: "Delete Token", | ||
deleteDescription: "Are you sure you want to delete this token?", | ||
} | ||
|
||
export const TokensPage: FC<PropsWithChildren<unknown>> = () => { | ||
const [tokensState, tokensSend] = useMachine(tokensMachine) | ||
const isLoading = tokensState.matches("gettingTokens") | ||
const hasLoaded = tokensState.matches("loaded") | ||
const { getTokensError, tokens, deleteTokenId } = tokensState.context | ||
const styles = useStyles() | ||
const description = ( | ||
<p> | ||
{Language.descriptionPrefix}{" "} | ||
<code className={styles.code}>coder tokens create</code> command. | ||
</p> | ||
) | ||
|
||
const content = ( | ||
<Typography> | ||
{Language.deleteDescription} | ||
<br /> | ||
<br /> | ||
{deleteTokenId} | ||
</Typography> | ||
) | ||
|
||
return ( | ||
<> | ||
<Section title={Language.title} description={description} layout="fluid"> | ||
<TokensPageView | ||
tokens={tokens} | ||
isLoading={isLoading} | ||
hasLoaded={hasLoaded} | ||
getTokensError={getTokensError} | ||
onDelete={(id) => { | ||
tokensSend({ type: "DELETE_TOKEN", id }) | ||
}} | ||
/> | ||
</Section> | ||
|
||
<ConfirmDialog | ||
title={Language.deleteTitle} | ||
description={content} | ||
open={tokensState.matches("confirmTokenDelete")} | ||
confirmLoading={tokensState.matches("deletingToken")} | ||
onConfirm={() => { | ||
tokensSend("CONFIRM_DELETE_TOKEN") | ||
}} | ||
onClose={() => { | ||
tokensSend("CANCEL_DELETE_TOKEN") | ||
}} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
code: { | ||
background: theme.palette.divider, | ||
fontSize: 12, | ||
padding: "2px 4px", | ||
color: theme.palette.text.primary, | ||
borderRadius: 2, | ||
}, | ||
})) | ||
|
||
export default TokensPage |
56 changes: 56 additions & 0 deletions
56
site/src/pages/UserSettingsPage/TokensPage/TokensPageView.stories.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,56 @@ | ||
import { Story } from "@storybook/react" | ||
import { makeMockApiError, MockTokens } from "testHelpers/entities" | ||
import { TokensPageView, TokensPageViewProps } from "./TokensPageView" | ||
|
||
export default { | ||
title: "components/TokensPageView", | ||
component: TokensPageView, | ||
argTypes: { | ||
onRegenerateClick: { action: "Submit" }, | ||
}, | ||
} | ||
|
||
const Template: Story<TokensPageViewProps> = (args: TokensPageViewProps) => ( | ||
<TokensPageView {...args} /> | ||
) | ||
|
||
export const Example = Template.bind({}) | ||
Example.args = { | ||
isLoading: false, | ||
hasLoaded: true, | ||
tokens: MockTokens, | ||
onDelete: () => { | ||
return Promise.resolve() | ||
}, | ||
} | ||
|
||
export const Loading = Template.bind({}) | ||
Loading.args = { | ||
...Example.args, | ||
isLoading: true, | ||
hasLoaded: false, | ||
} | ||
|
||
export const Empty = Template.bind({}) | ||
Empty.args = { | ||
...Example.args, | ||
tokens: [], | ||
} | ||
|
||
export const WithGetTokensError = Template.bind({}) | ||
WithGetTokensError.args = { | ||
...Example.args, | ||
hasLoaded: false, | ||
getTokensError: makeMockApiError({ | ||
message: "Failed to get tokens.", | ||
}), | ||
} | ||
|
||
export const WithDeleteTokenError = Template.bind({}) | ||
WithDeleteTokenError.args = { | ||
...Example.args, | ||
hasLoaded: false, | ||
deleteTokenError: makeMockApiError({ | ||
message: "Failed to delete token.", | ||
}), | ||
} |
132 changes: 132 additions & 0 deletions
132
site/src/pages/UserSettingsPage/TokensPage/TokensPageView.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,132 @@ | ||
import { useTheme } from "@material-ui/core/styles" | ||
import Table from "@material-ui/core/Table" | ||
import TableBody from "@material-ui/core/TableBody" | ||
import TableCell from "@material-ui/core/TableCell" | ||
import TableContainer from "@material-ui/core/TableContainer" | ||
import TableHead from "@material-ui/core/TableHead" | ||
import TableRow from "@material-ui/core/TableRow" | ||
import { APIKey } from "api/typesGenerated" | ||
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne" | ||
import { Stack } from "components/Stack/Stack" | ||
import { TableEmpty } from "components/TableEmpty/TableEmpty" | ||
import { TableLoader } from "components/TableLoader/TableLoader" | ||
import DeleteOutlineIcon from "@material-ui/icons/DeleteOutline" | ||
import dayjs from "dayjs" | ||
import { FC } from "react" | ||
import { AlertBanner } from "components/AlertBanner/AlertBanner" | ||
import IconButton from "@material-ui/core/IconButton/IconButton" | ||
|
||
export const Language = { | ||
idLabel: "ID", | ||
createdAtLabel: "Created At", | ||
lastUsedLabel: "Last Used", | ||
expiresAtLabel: "Expires At", | ||
emptyMessage: "No tokens found", | ||
ariaDeleteLabel: "Delete Token", | ||
} | ||
|
||
const lastUsedOrNever = (lastUsed: string) => { | ||
const t = dayjs(lastUsed) | ||
const now = dayjs() | ||
return now.isBefore(t.add(100, "year")) ? t.fromNow() : "Never" | ||
} | ||
|
||
export interface TokensPageViewProps { | ||
tokens?: APIKey[] | ||
getTokensError?: Error | unknown | ||
isLoading: boolean | ||
hasLoaded: boolean | ||
onDelete: (id: string) => void | ||
deleteTokenError?: Error | unknown | ||
} | ||
|
||
export const TokensPageView: FC< | ||
React.PropsWithChildren<TokensPageViewProps> | ||
> = ({ | ||
tokens, | ||
getTokensError, | ||
isLoading, | ||
hasLoaded, | ||
onDelete, | ||
deleteTokenError, | ||
}) => { | ||
const theme = useTheme() | ||
|
||
return ( | ||
<Stack> | ||
{Boolean(getTokensError) && ( | ||
<AlertBanner severity="error" error={getTokensError} /> | ||
)} | ||
{Boolean(deleteTokenError) && ( | ||
<AlertBanner severity="error" error={deleteTokenError} /> | ||
)} | ||
<TableContainer> | ||
<Table> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell width="25%">{Language.idLabel}</TableCell> | ||
<TableCell width="25%">{Language.createdAtLabel}</TableCell> | ||
<TableCell width="25%">{Language.lastUsedLabel}</TableCell> | ||
<TableCell width="25%">{Language.expiresAtLabel}</TableCell> | ||
<TableCell width="0%"></TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
<ChooseOne> | ||
<Cond condition={isLoading}> | ||
<TableLoader /> | ||
</Cond> | ||
<Cond condition={hasLoaded && tokens?.length === 0}> | ||
<TableEmpty message={Language.emptyMessage} /> | ||
</Cond> | ||
<Cond> | ||
{tokens?.map((token) => { | ||
return ( | ||
<TableRow | ||
key={token.id} | ||
data-testid={`token-${token.id}`} | ||
tabIndex={0} | ||
> | ||
<TableCell> | ||
<span style={{ color: theme.palette.text.secondary }}> | ||
{token.id} | ||
</span> | ||
</TableCell> | ||
|
||
<TableCell> | ||
<span style={{ color: theme.palette.text.secondary }}> | ||
{dayjs(token.created_at).fromNow()} | ||
</span> | ||
</TableCell> | ||
|
||
<TableCell>{lastUsedOrNever(token.last_used)}</TableCell> | ||
|
||
<TableCell> | ||
<span style={{ color: theme.palette.text.secondary }}> | ||
{dayjs(token.expires_at).fromNow()} | ||
</span> | ||
</TableCell> | ||
<TableCell> | ||
<span style={{ color: theme.palette.text.secondary }}> | ||
<IconButton | ||
onClick={() => { | ||
onDelete(token.id) | ||
}} | ||
size="medium" | ||
aria-label={Language.ariaDeleteLabel} | ||
> | ||
<DeleteOutlineIcon /> | ||
</IconButton> | ||
</span> | ||
</TableCell> | ||
</TableRow> | ||
) | ||
})} | ||
</Cond> | ||
</ChooseOne> | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
</Stack> | ||
) | ||
} |
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.
We have an alternative to
Language
now, react-i18n, but if it's a hassle, don't worry about it, we're going to revisit whether it's worth the time