Skip to content

Commit

Permalink
feat: Improve API key UX (#4102)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg authored Jun 5, 2024
1 parent 9d1622f commit 1600ed7
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions frontend/web/components/AdminAPIKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
getRolesMasterAPIKeyWithMasterAPIKeyRoles,
updateMasterAPIKeyWithMasterAPIKeyRoles,
} from 'common/services/useMasterAPIKeyWithMasterAPIKeyRole'
import { setInterceptClose, setModalTitle } from './modals/base/ModalDefault'
import SuccessMessage from './SuccessMessage'

export class CreateAPIKey extends PureComponent {
state = {
Expand All @@ -31,6 +33,28 @@ export class CreateAPIKey extends PureComponent {

componentDidMount() {
this.props.isEdit && this.getApiKeyByPrefix(this.props.prefix)
if (!this.props.isEdit) {
setInterceptClose(() => {
if (!this.state.key) {
return Promise.resolve(true)
} else {
return new Promise((resolve) => {
openConfirm({
body: 'Please confirm you have a copy of the API key prior to closing.',
noText: 'Cancel',
onNo: () => resolve(false),
onYes: () => resolve(true),
title: 'Confirm saved API key',
yesText: 'Confirm',
})
})
}
})
}
}

componentWillUnmount() {
setInterceptClose(null)
}

submit = () => {
Expand All @@ -50,6 +74,7 @@ export class CreateAPIKey extends PureComponent {
isSaving: false,
key: res.key,
})
setModalTitle('Save your new API key')
Promise.all(
this.state.roles.map((role) =>
createRoleMasterApiKey(getStore(), {
Expand Down Expand Up @@ -163,6 +188,11 @@ export class CreateAPIKey extends PureComponent {
<label>Name</label>
</div>
<Input
ref={(v) => {
setTimeout(() => {
v.focus()
}, 500)
}}
value={this.state.name}
onChange={(e) =>
this.setState({ name: Utils.safeParseEventValue(e) })
Expand Down Expand Up @@ -257,11 +287,11 @@ export class CreateAPIKey extends PureComponent {
)}

{this.state.key && (
<div className='mb-4'>
<InfoMessage>
Please keep a note of your API key once it's created, we do not
store it.
</InfoMessage>
<div className='my-4'>
<SuccessMessage>
Your key has been created. Please keep a note of your API key
once it's created, we do not store it.
</SuccessMessage>

<Token show token={this.state.key} />
</div>
Expand Down

0 comments on commit 1600ed7

Please sign in to comment.