secret-key-modal.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use client'
  2. import {
  3. useEffect,
  4. useState,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { PlusIcon, XMarkIcon } from '@heroicons/react/20/solid'
  8. import useSWR, { useSWRConfig } from 'swr'
  9. import copy from 'copy-to-clipboard'
  10. import SecretKeyGenerateModal from './secret-key-generate'
  11. import s from './style.module.css'
  12. import Modal from '@/app/components/base/modal'
  13. import Button from '@/app/components/base/button'
  14. import {
  15. createApikey as createAppApikey,
  16. delApikey as delAppApikey,
  17. fetchApiKeysList as fetchAppApiKeysList,
  18. } from '@/service/apps'
  19. import {
  20. createApikey as createDatasetApikey,
  21. delApikey as delDatasetApikey,
  22. fetchApiKeysList as fetchDatasetApiKeysList,
  23. } from '@/service/datasets'
  24. import type { CreateApiKeyResponse } from '@/models/app'
  25. import Tooltip from '@/app/components/base/tooltip'
  26. import Loading from '@/app/components/base/loading'
  27. import Confirm from '@/app/components/base/confirm'
  28. import useTimestamp from '@/hooks/use-timestamp'
  29. import { useAppContext } from '@/context/app-context'
  30. type ISecretKeyModalProps = {
  31. isShow: boolean
  32. appId?: string
  33. onClose: () => void
  34. }
  35. const SecretKeyModal = ({
  36. isShow = false,
  37. appId,
  38. onClose,
  39. }: ISecretKeyModalProps) => {
  40. const { t } = useTranslation()
  41. const { formatTime } = useTimestamp()
  42. const { currentWorkspace, isCurrentWorkspaceManager, isCurrentWorkspaceEditor } = useAppContext()
  43. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  44. const [isVisible, setVisible] = useState(false)
  45. const [newKey, setNewKey] = useState<CreateApiKeyResponse | undefined>(undefined)
  46. const { mutate } = useSWRConfig()
  47. const commonParams = appId
  48. ? { url: `/apps/${appId}/api-keys`, params: {} }
  49. : { url: '/datasets/api-keys', params: {} }
  50. const fetchApiKeysList = appId ? fetchAppApiKeysList : fetchDatasetApiKeysList
  51. const { data: apiKeysList } = useSWR(commonParams, fetchApiKeysList)
  52. const [delKeyID, setDelKeyId] = useState('')
  53. const [copyValue, setCopyValue] = useState('')
  54. useEffect(() => {
  55. if (copyValue) {
  56. const timeout = setTimeout(() => {
  57. setCopyValue('')
  58. }, 1000)
  59. return () => {
  60. clearTimeout(timeout)
  61. }
  62. }
  63. }, [copyValue])
  64. const onDel = async () => {
  65. setShowConfirmDelete(false)
  66. if (!delKeyID)
  67. return
  68. const delApikey = appId ? delAppApikey : delDatasetApikey
  69. const params = appId
  70. ? { url: `/apps/${appId}/api-keys/${delKeyID}`, params: {} }
  71. : { url: `/datasets/api-keys/${delKeyID}`, params: {} }
  72. await delApikey(params)
  73. mutate(commonParams)
  74. }
  75. const onCreate = async () => {
  76. const params = appId
  77. ? { url: `/apps/${appId}/api-keys`, body: {} }
  78. : { url: '/datasets/api-keys', body: {} }
  79. const createApikey = appId ? createAppApikey : createDatasetApikey
  80. const res = await createApikey(params)
  81. setVisible(true)
  82. setNewKey(res)
  83. mutate(commonParams)
  84. }
  85. const generateToken = (token: string) => {
  86. return `${token.slice(0, 3)}...${token.slice(-20)}`
  87. }
  88. return (
  89. <Modal isShow={isShow} onClose={onClose} title={`${t('appApi.apiKeyModal.apiSecretKey')}`} className={`${s.customModal} px-8 flex flex-col`}>
  90. <XMarkIcon className={`w-6 h-6 absolute cursor-pointer text-gray-500 ${s.close}`} onClick={onClose} />
  91. <p className='mt-1 text-[13px] text-gray-500 font-normal leading-5 flex-shrink-0'>{t('appApi.apiKeyModal.apiSecretKeyTips')}</p>
  92. {!apiKeysList && <div className='mt-4'><Loading /></div>}
  93. {
  94. !!apiKeysList?.data?.length && (
  95. <div className='flex flex-col flex-grow mt-4 overflow-hidden'>
  96. <div className='flex items-center flex-shrink-0 text-xs font-semibold text-gray-500 border-b border-solid h-9'>
  97. <div className='flex-shrink-0 w-64 px-3'>{t('appApi.apiKeyModal.secretKey')}</div>
  98. <div className='flex-shrink-0 px-3 w-[200px]'>{t('appApi.apiKeyModal.created')}</div>
  99. <div className='flex-shrink-0 px-3 w-[200px]'>{t('appApi.apiKeyModal.lastUsed')}</div>
  100. <div className='flex-grow px-3'></div>
  101. </div>
  102. <div className='flex-grow overflow-auto'>
  103. {apiKeysList.data.map(api => (
  104. <div className='flex items-center text-sm font-normal text-gray-700 border-b border-solid h-9' key={api.id}>
  105. <div className='flex-shrink-0 w-64 px-3 font-mono truncate'>{generateToken(api.token)}</div>
  106. <div className='flex-shrink-0 px-3 truncate w-[200px]'>{formatTime(Number(api.created_at), t('appLog.dateTimeFormat') as string)}</div>
  107. <div className='flex-shrink-0 px-3 truncate w-[200px]'>{api.last_used_at ? formatTime(Number(api.last_used_at), t('appLog.dateTimeFormat') as string) : t('appApi.never')}</div>
  108. <div className='flex flex-grow px-3'>
  109. <Tooltip
  110. popupContent={copyValue === api.token ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  111. popupClassName='mr-1'
  112. >
  113. <div className={`flex items-center justify-center flex-shrink-0 w-6 h-6 mr-1 rounded-lg cursor-pointer hover:bg-gray-100 ${s.copyIcon} ${copyValue === api.token ? s.copied : ''}`} onClick={() => {
  114. // setIsCopied(true)
  115. copy(api.token)
  116. setCopyValue(api.token)
  117. }}></div>
  118. </Tooltip>
  119. {isCurrentWorkspaceManager
  120. && <div className={`flex items-center justify-center flex-shrink-0 w-6 h-6 rounded-lg cursor-pointer ${s.trashIcon}`} onClick={() => {
  121. setDelKeyId(api.id)
  122. setShowConfirmDelete(true)
  123. }}>
  124. </div>
  125. }
  126. </div>
  127. </div>
  128. ))}
  129. </div>
  130. </div>
  131. )
  132. }
  133. <div className='flex'>
  134. <Button className={`flex flex-shrink-0 mt-4 ${s.autoWidth}`} onClick={onCreate} disabled={!currentWorkspace || !isCurrentWorkspaceEditor}>
  135. <PlusIcon className='flex flex-shrink-0 w-4 h-4' />
  136. <div className='text-xs font-medium text-gray-800'>{t('appApi.apiKeyModal.createNewSecretKey')}</div>
  137. </Button>
  138. </div>
  139. <SecretKeyGenerateModal className='flex-shrink-0' isShow={isVisible} onClose={() => setVisible(false)} newKey={newKey} />
  140. {showConfirmDelete && (
  141. <Confirm
  142. title={`${t('appApi.actionMsg.deleteConfirmTitle')}`}
  143. content={`${t('appApi.actionMsg.deleteConfirmTips')}`}
  144. isShow={showConfirmDelete}
  145. onConfirm={onDel}
  146. onCancel={() => {
  147. setDelKeyId('')
  148. setShowConfirmDelete(false)
  149. }}
  150. />
  151. )}
  152. </Modal >
  153. )
  154. }
  155. export default SecretKeyModal