mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-18 23:45:26 +02:00
feat(api-key): Add auto create
This commit is contained in:
parent
899208986f
commit
b4b0514bff
@ -48,6 +48,9 @@ export interface CreateNewAPIKeyDialogProps {
|
|||||||
open: boolean
|
open: boolean
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
onCreated: (key: APIKey & {key: string}) => void
|
onCreated: (key: APIKey & {key: string}) => void
|
||||||
|
|
||||||
|
prefilledLabel: string
|
||||||
|
prefilledScopes: APIKeyScope[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const PRESET_DAYS: number[] = [1, 7, 30, 180, 360]
|
const PRESET_DAYS: number[] = [1, 7, 30, 180, 360]
|
||||||
@ -76,6 +79,8 @@ const normalizeTime = (date: Date) =>
|
|||||||
|
|
||||||
export default function CreateNewAPIKeyDialog({
|
export default function CreateNewAPIKeyDialog({
|
||||||
open,
|
open,
|
||||||
|
prefilledLabel,
|
||||||
|
prefilledScopes,
|
||||||
onClose,
|
onClose,
|
||||||
onCreated,
|
onCreated,
|
||||||
}: CreateNewAPIKeyDialogProps): ReactElement {
|
}: CreateNewAPIKeyDialogProps): ReactElement {
|
||||||
@ -108,9 +113,9 @@ export default function CreateNewAPIKeyDialog({
|
|||||||
const formik = useFormik<CreateAPIKeyData & {detail: string}>({
|
const formik = useFormik<CreateAPIKeyData & {detail: string}>({
|
||||||
validationSchema: scheme,
|
validationSchema: scheme,
|
||||||
initialValues: {
|
initialValues: {
|
||||||
label: "",
|
label: prefilledLabel,
|
||||||
expiresAt: addDays(new Date(), 30),
|
expiresAt: addDays(new Date(), 30),
|
||||||
scopes: [],
|
scopes: [...prefilledScopes],
|
||||||
detail: "",
|
detail: "",
|
||||||
},
|
},
|
||||||
onSubmit: async (values, {setErrors}) => {
|
onSubmit: async (values, {setErrors}) => {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {ReactElement, useState} from "react"
|
import {ReactElement, useLayoutEffect, useState} from "react"
|
||||||
import {useTranslation} from "react-i18next"
|
import {useTranslation} from "react-i18next"
|
||||||
import {useQuery} from "@tanstack/react-query"
|
import {useQuery} from "@tanstack/react-query"
|
||||||
import {APIKey, PaginationResult} from "~/server-types"
|
import {APIKey, PaginationResult} from "~/server-types"
|
||||||
@ -7,17 +7,38 @@ import {getAPIKeys} from "~/apis"
|
|||||||
import {QueryResult, SimplePage} from "~/components"
|
import {QueryResult, SimplePage} from "~/components"
|
||||||
import {Button, List} from "@mui/material"
|
import {Button, List} from "@mui/material"
|
||||||
import {MdAdd} from "react-icons/md"
|
import {MdAdd} from "react-icons/md"
|
||||||
|
import {useQueryParams} from "~/hooks"
|
||||||
|
import {isArray} from "lodash"
|
||||||
|
import {API_KEY_SCOPES} from "~/constants/values"
|
||||||
|
import {useNavigate} from "react-router-dom"
|
||||||
import APIKeyListItem from "~/route-widgets/SettingsAPIKeysRoute/APIKeyListItem"
|
import APIKeyListItem from "~/route-widgets/SettingsAPIKeysRoute/APIKeyListItem"
|
||||||
import CreateNewAPIKeyDialog from "../route-widgets/SettingsAPIKeysRoute/CreateNewAPIKeyDialog"
|
import CreateNewAPIKeyDialog from "../route-widgets/SettingsAPIKeysRoute/CreateNewAPIKeyDialog"
|
||||||
import EmptyStateScreen from "~/route-widgets/SettingsAPIKeysRoute/EmptyStateScreen"
|
import EmptyStateScreen from "~/route-widgets/SettingsAPIKeysRoute/EmptyStateScreen"
|
||||||
|
|
||||||
export default function SettingsAPIKeysRoute(): ReactElement {
|
export default function SettingsAPIKeysRoute(): ReactElement {
|
||||||
const {t} = useTranslation("settings-api-keys")
|
const {t} = useTranslation("settings-api-keys")
|
||||||
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
const rawParams = useQueryParams<{action?: any; scopes?: any; label?: any}>()
|
||||||
|
const params = {
|
||||||
|
action: rawParams.action === "create-new" ? "create-new" : undefined,
|
||||||
|
scopes: isArray(rawParams.scopes?.split(","))
|
||||||
|
? rawParams.scopes.split(",").filter((scope: string) => API_KEY_SCOPES.includes(scope))
|
||||||
|
: [],
|
||||||
|
label: rawParams.label,
|
||||||
|
}
|
||||||
|
|
||||||
const queryKey = ["get_api_keys"]
|
const queryKey = ["get_api_keys"]
|
||||||
const query = useQuery<PaginationResult<APIKey>, AxiosError>(queryKey, () => getAPIKeys())
|
const query = useQuery<PaginationResult<APIKey>, AxiosError>(queryKey, () => getAPIKeys())
|
||||||
|
|
||||||
const [createdAPIKey, setCreatedAPIKey] = useState<(APIKey & {key: string}) | null>(null)
|
const [createdAPIKey, setCreatedAPIKey] = useState<(APIKey & {key: string}) | null>(null)
|
||||||
const [createNew, setCreateNew] = useState<boolean>(false)
|
const [createNew, setCreateNew] = useState<boolean>(params.action === "create-new")
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
if (params.action === "create-new") {
|
||||||
|
navigate(location.pathname, {replace: true})
|
||||||
|
}
|
||||||
|
}, [params.action, navigate])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -60,6 +81,8 @@ export default function SettingsAPIKeysRoute(): ReactElement {
|
|||||||
<CreateNewAPIKeyDialog
|
<CreateNewAPIKeyDialog
|
||||||
key={createNew.toString()}
|
key={createNew.toString()}
|
||||||
open={createNew}
|
open={createNew}
|
||||||
|
prefilledScopes={params.scopes}
|
||||||
|
prefilledLabel={params.label}
|
||||||
onClose={() => setCreateNew(false)}
|
onClose={() => setCreateNew(false)}
|
||||||
onCreated={key => {
|
onCreated={key => {
|
||||||
query.refetch()
|
query.refetch()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user