From 85fb03651c2923b591a05c8fb86ed012013502ec Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Mon, 22 May 2023 10:49:49 +0800 Subject: [PATCH] feat(console): regenerate signing key (#3866) --- packages/console/src/assets/images/redo.svg | 3 + .../SigningKeyField/index.module.scss | 9 ++ .../components/SigningKeyField/index.tsx | 91 +++++++++++++++++++ .../WebhookDetails/WebhookSettings/index.tsx | 9 +- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 packages/console/src/assets/images/redo.svg create mode 100644 packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.module.scss create mode 100644 packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.tsx diff --git a/packages/console/src/assets/images/redo.svg b/packages/console/src/assets/images/redo.svg new file mode 100644 index 000000000..7a8330e21 --- /dev/null +++ b/packages/console/src/assets/images/redo.svg @@ -0,0 +1,3 @@ + + + diff --git a/packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.module.scss b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.module.scss new file mode 100644 index 000000000..24a30df14 --- /dev/null +++ b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.module.scss @@ -0,0 +1,9 @@ +@use '@/scss/underscore' as _; + +.regenerateButton { + margin-top: _.unit(3); +} + +.signingKeyField { + width: 100%; +} diff --git a/packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.tsx b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.tsx new file mode 100644 index 000000000..7ef5c16af --- /dev/null +++ b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/SigningKeyField/index.tsx @@ -0,0 +1,91 @@ +import { type Hook } from '@logto/schemas'; +import { useCallback, useEffect, useState } from 'react'; +import { toast } from 'react-hot-toast'; +import { useTranslation } from 'react-i18next'; + +import Redo from '@/assets/images/redo.svg'; +import Button from '@/components/Button'; +import ConfirmModal from '@/components/ConfirmModal'; +import CopyToClipboard from '@/components/CopyToClipboard'; +import DynamicT from '@/components/DynamicT'; +import FormField from '@/components/FormField'; +import useApi from '@/hooks/use-api'; + +import * as styles from './index.module.scss'; + +type Props = { + hookId: string; + signingKey: string; + onSigningKeyUpdated: (signingKey: string) => void; +}; + +function SigningKeyField({ hookId, signingKey, onSigningKeyUpdated }: Props) { + const { t } = useTranslation(undefined); + const api = useApi(); + const [isRegenerateFormOpen, setIsRegenerateFormOpen] = useState(false); + const [isRegenerating, setIsRegenerating] = useState(false); + + const regenerateSigningKey = useCallback( + async (silent = false) => { + if (isRegenerating) { + return; + } + setIsRegenerating(true); + try { + const { signingKey } = await api.patch(`api/hooks/${hookId}/signing-key`).json(); + if (!silent) { + toast.success(t('admin_console.webhook_details.settings.regenerated')); + } + setIsRegenerateFormOpen(false); + onSigningKeyUpdated(signingKey); + } finally { + setIsRegenerating(false); + } + }, + [api, hookId, isRegenerating, onSigningKeyUpdated, t] + ); + + useEffect(() => { + if (!signingKey) { + void regenerateSigningKey(true); + } + }, [regenerateSigningKey, signingKey]); + + return ( + } + > + +