'use client' import { useEffect, useState, } from 'react' import { useTranslation } from 'react-i18next' import { PlusIcon, XMarkIcon } from '@heroicons/react/20/solid' import useSWR, { useSWRConfig } from 'swr' import copy from 'copy-to-clipboard' import SecretKeyGenerateModal from './secret-key-generate' import s from './style.module.css' import Modal from '@/app/components/base/modal' import Button from '@/app/components/base/button' import { createApikey as createAppApikey, delApikey as delAppApikey, fetchApiKeysList as fetchAppApiKeysList, } from '@/service/apps' import { createApikey as createDatasetApikey, delApikey as delDatasetApikey, fetchApiKeysList as fetchDatasetApiKeysList, } from '@/service/datasets' import type { CreateApiKeyResponse } from '@/models/app' import Tooltip from '@/app/components/base/tooltip' import Loading from '@/app/components/base/loading' import Confirm from '@/app/components/base/confirm' import useTimestamp from '@/hooks/use-timestamp' import { useAppContext } from '@/context/app-context' type ISecretKeyModalProps = { isShow: boolean appId?: string onClose: () => void } const SecretKeyModal = ({ isShow = false, appId, onClose, }: ISecretKeyModalProps) => { const { t } = useTranslation() const { formatTime } = useTimestamp() const { currentWorkspace, isCurrentWorkspaceManager, isCurrentWorkspaceEditor } = useAppContext() const [showConfirmDelete, setShowConfirmDelete] = useState(false) const [isVisible, setVisible] = useState(false) const [newKey, setNewKey] = useState(undefined) const { mutate } = useSWRConfig() const commonParams = appId ? { url: `/apps/${appId}/api-keys`, params: {} } : { url: '/datasets/api-keys', params: {} } const fetchApiKeysList = appId ? fetchAppApiKeysList : fetchDatasetApiKeysList const { data: apiKeysList } = useSWR(commonParams, fetchApiKeysList) const [delKeyID, setDelKeyId] = useState('') const [copyValue, setCopyValue] = useState('') useEffect(() => { if (copyValue) { const timeout = setTimeout(() => { setCopyValue('') }, 1000) return () => { clearTimeout(timeout) } } }, [copyValue]) const onDel = async () => { setShowConfirmDelete(false) if (!delKeyID) return const delApikey = appId ? delAppApikey : delDatasetApikey const params = appId ? { url: `/apps/${appId}/api-keys/${delKeyID}`, params: {} } : { url: `/datasets/api-keys/${delKeyID}`, params: {} } await delApikey(params) mutate(commonParams) } const onCreate = async () => { const params = appId ? { url: `/apps/${appId}/api-keys`, body: {} } : { url: '/datasets/api-keys', body: {} } const createApikey = appId ? createAppApikey : createDatasetApikey const res = await createApikey(params) setVisible(true) setNewKey(res) mutate(commonParams) } const generateToken = (token: string) => { return `${token.slice(0, 3)}...${token.slice(-20)}` } return (

{t('appApi.apiKeyModal.apiSecretKeyTips')}

{!apiKeysList &&
} { !!apiKeysList?.data?.length && (
{t('appApi.apiKeyModal.secretKey')}
{t('appApi.apiKeyModal.created')}
{t('appApi.apiKeyModal.lastUsed')}
{apiKeysList.data.map(api => (
{generateToken(api.token)}
{formatTime(Number(api.created_at), t('appLog.dateTimeFormat') as string)}
{api.last_used_at ? formatTime(Number(api.last_used_at), t('appLog.dateTimeFormat') as string) : t('appApi.never')}
{ // setIsCopied(true) copy(api.token) setCopyValue(api.token) }}>
{isCurrentWorkspaceManager &&
{ setDelKeyId(api.id) setShowConfirmDelete(true) }}>
}
))}
) }
setVisible(false)} newKey={newKey} /> {showConfirmDelete && ( { setDelKeyId('') setShowConfirmDelete(false) }} /> )}
) } export default SecretKeyModal