2022-11-15 09:35:48 +01:00
|
|
|
import { CopyButton } from '@/components/CopyButton'
|
2023-10-27 09:23:50 +02:00
|
|
|
import { useTranslate } from '@tolgee/react'
|
2022-06-03 13:20:19 +02:00
|
|
|
import {
|
|
|
|
|
Modal,
|
|
|
|
|
ModalOverlay,
|
|
|
|
|
ModalContent,
|
|
|
|
|
ModalHeader,
|
|
|
|
|
ModalCloseButton,
|
|
|
|
|
ModalBody,
|
|
|
|
|
Stack,
|
|
|
|
|
Input,
|
|
|
|
|
ModalFooter,
|
|
|
|
|
Button,
|
|
|
|
|
Text,
|
|
|
|
|
InputGroup,
|
|
|
|
|
InputRightElement,
|
|
|
|
|
} from '@chakra-ui/react'
|
2023-08-29 10:01:28 +02:00
|
|
|
import React, { FormEvent, useRef, useState } from 'react'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { createApiTokenQuery } from '../queries/createApiTokenQuery'
|
|
|
|
|
import { ApiTokenFromServer } from '../types'
|
2022-06-03 13:20:19 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
userId: string
|
|
|
|
|
isOpen: boolean
|
|
|
|
|
onNewToken: (token: ApiTokenFromServer) => void
|
|
|
|
|
onClose: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CreateTokenModal = ({
|
|
|
|
|
userId,
|
|
|
|
|
isOpen,
|
|
|
|
|
onClose,
|
|
|
|
|
onNewToken,
|
|
|
|
|
}: Props) => {
|
2023-08-29 10:01:28 +02:00
|
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
2023-10-27 09:23:50 +02:00
|
|
|
const { t } = useTranslate()
|
2022-06-03 13:20:19 +02:00
|
|
|
const [name, setName] = useState('')
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
const [newTokenValue, setNewTokenValue] = useState<string>()
|
|
|
|
|
|
|
|
|
|
const createToken = async (e: FormEvent) => {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
setIsSubmitting(true)
|
2022-11-15 09:35:48 +01:00
|
|
|
const { data } = await createApiTokenQuery(userId, { name })
|
2022-06-03 13:20:19 +02:00
|
|
|
if (data?.apiToken) {
|
|
|
|
|
setNewTokenValue(data.apiToken.token)
|
|
|
|
|
onNewToken(data.apiToken)
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(false)
|
|
|
|
|
}
|
2023-08-29 10:01:28 +02:00
|
|
|
|
2022-06-03 13:20:19 +02:00
|
|
|
return (
|
2023-08-29 10:01:28 +02:00
|
|
|
<Modal isOpen={isOpen} onClose={onClose} initialFocusRef={inputRef}>
|
2022-06-03 13:20:19 +02:00
|
|
|
<ModalOverlay />
|
|
|
|
|
<ModalContent>
|
|
|
|
|
<ModalHeader>
|
2023-10-27 09:23:50 +02:00
|
|
|
{newTokenValue
|
|
|
|
|
? t('account.apiTokens.createModal.createdHeading')
|
|
|
|
|
: t('account.apiTokens.createModal.createHeading')}
|
2022-06-03 13:20:19 +02:00
|
|
|
</ModalHeader>
|
|
|
|
|
<ModalCloseButton />
|
|
|
|
|
{newTokenValue ? (
|
|
|
|
|
<ModalBody as={Stack} spacing="4">
|
|
|
|
|
<Text>
|
2023-10-27 09:23:50 +02:00
|
|
|
{t('account.apiTokens.createModal.copyInstruction')}{' '}
|
|
|
|
|
<strong>
|
|
|
|
|
{t('account.apiTokens.createModal.securityWarning')}
|
|
|
|
|
</strong>
|
2022-06-03 13:20:19 +02:00
|
|
|
</Text>
|
|
|
|
|
<InputGroup size="md">
|
|
|
|
|
<Input readOnly pr="4.5rem" value={newTokenValue} />
|
|
|
|
|
<InputRightElement width="4.5rem">
|
|
|
|
|
<CopyButton h="1.75rem" size="sm" textToCopy={newTokenValue} />
|
|
|
|
|
</InputRightElement>
|
|
|
|
|
</InputGroup>
|
|
|
|
|
</ModalBody>
|
|
|
|
|
) : (
|
|
|
|
|
<ModalBody as="form" onSubmit={createToken}>
|
2023-10-27 09:23:50 +02:00
|
|
|
<Text mb="4">
|
|
|
|
|
{t('account.apiTokens.createModal.nameInput.label')}
|
|
|
|
|
</Text>
|
2022-06-03 13:20:19 +02:00
|
|
|
<Input
|
2023-08-29 10:01:28 +02:00
|
|
|
ref={inputRef}
|
2023-10-27 09:23:50 +02:00
|
|
|
placeholder={t(
|
|
|
|
|
'account.apiTokens.createModal.nameInput.placeholder'
|
|
|
|
|
)}
|
2022-06-03 13:20:19 +02:00
|
|
|
onChange={(e) => setName(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</ModalBody>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<ModalFooter>
|
|
|
|
|
{newTokenValue ? (
|
|
|
|
|
<Button onClick={onClose} colorScheme="blue">
|
2023-10-27 09:23:50 +02:00
|
|
|
{t('account.apiTokens.createModal.doneButton.label')}
|
2022-06-03 13:20:19 +02:00
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
colorScheme="blue"
|
|
|
|
|
isDisabled={name.length === 0}
|
|
|
|
|
isLoading={isSubmitting}
|
|
|
|
|
onClick={createToken}
|
|
|
|
|
>
|
2023-10-27 09:23:50 +02:00
|
|
|
{t('account.apiTokens.createModal.createButton.label')}
|
2022-06-03 13:20:19 +02:00
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</ModalFooter>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
</Modal>
|
|
|
|
|
)
|
|
|
|
|
}
|