secret-key-modal.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 { useContext } from 'use-context-selector'
  10. import copy from 'copy-to-clipboard'
  11. import SecretKeyGenerateModal from './secret-key-generate'
  12. import s from './style.module.css'
  13. import Modal from '@/app/components/base/modal'
  14. import Button from '@/app/components/base/button'
  15. import {
  16. createApikey as createAppApikey,
  17. delApikey as delAppApikey,
  18. fetchApiKeysList as fetchAppApiKeysList,
  19. } from '@/service/apps'
  20. import {
  21. createApikey as createDatasetApikey,
  22. delApikey as delDatasetApikey,
  23. fetchApiKeysList as fetchDatasetApiKeysList,
  24. } from '@/service/datasets'
  25. import type { CreateApiKeyResponse } from '@/models/app'
  26. import Tooltip from '@/app/components/base/tooltip'
  27. import Loading from '@/app/components/base/loading'
  28. import Confirm from '@/app/components/base/confirm'
  29. import I18n from '@/context/i18n'
  30. import { useAppContext } from '@/context/app-context'
  31. type ISecretKeyModalProps = {
  32. isShow: boolean
  33. appId?: string
  34. onClose: () => void
  35. }
  36. const SecretKeyModal = ({
  37. isShow = false,
  38. appId,
  39. onClose,
  40. }: ISecretKeyModalProps) => {
  41. const { t } = useTranslation()
  42. const { currentWorkspace, isCurrentWorkspaceManager } = 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 { locale } = useContext(I18n)
  54. // const [isCopied, setIsCopied] = useState(false)
  55. const [copyValue, setCopyValue] = useState('')
  56. useEffect(() => {
  57. if (copyValue) {
  58. const timeout = setTimeout(() => {
  59. setCopyValue('')
  60. }, 1000)
  61. return () => {
  62. clearTimeout(timeout)
  63. }
  64. }
  65. }, [copyValue])
  66. const onDel = async () => {
  67. setShowConfirmDelete(false)
  68. if (!delKeyID)
  69. return
  70. const delApikey = appId ? delAppApikey : delDatasetApikey
  71. const params = appId
  72. ? { url: `/apps/${appId}/api-keys/${delKeyID}`, params: {} }
  73. : { url: `/datasets/api-keys/${delKeyID}`, params: {} }
  74. await delApikey(params)
  75. mutate(commonParams)
  76. }
  77. const onCreate = async () => {
  78. const params = appId
  79. ? { url: `/apps/${appId}/api-keys`, body: {} }
  80. : { url: '/datasets/api-keys', body: {} }
  81. const createApikey = appId ? createAppApikey : createDatasetApikey
  82. const res = await createApikey(params)
  83. setVisible(true)
  84. setNewKey(res)
  85. mutate(commonParams)
  86. }
  87. const generateToken = (token: string) => {
  88. return `${token.slice(0, 3)}...${token.slice(-20)}`
  89. }
  90. const formatDate = (timestamp: string) => {
  91. if (locale === 'en')
  92. return new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric' }).format((+timestamp) * 1000)
  93. else
  94. return new Intl.DateTimeFormat('fr-CA', { year: 'numeric', month: '2-digit', day: '2-digit' }).format((+timestamp) * 1000)
  95. }
  96. return (
  97. <Modal isShow={isShow} onClose={onClose} title={`${t('appApi.apiKeyModal.apiSecretKey')}`} className={`${s.customModal} px-8 flex flex-col`}>
  98. <XMarkIcon className={`w-6 h-6 absolute cursor-pointer text-gray-500 ${s.close}`} onClick={onClose} />
  99. <p className='mt-1 text-[13px] text-gray-500 font-normal leading-5 flex-shrink-0'>{t('appApi.apiKeyModal.apiSecretKeyTips')}</p>
  100. {!apiKeysList && <div className='mt-4'><Loading /></div>}
  101. {
  102. !!apiKeysList?.data?.length && (
  103. <div className='flex flex-col flex-grow mt-4 overflow-hidden'>
  104. <div className='flex items-center flex-shrink-0 text-xs font-semibold text-gray-500 border-b border-solid h-9'>
  105. <div className='flex-shrink-0 w-64 px-3'>{t('appApi.apiKeyModal.secretKey')}</div>
  106. <div className='flex-shrink-0 px-3 w-28'>{t('appApi.apiKeyModal.created')}</div>
  107. <div className='flex-shrink-0 px-3 w-28'>{t('appApi.apiKeyModal.lastUsed')}</div>
  108. <div className='flex-grow px-3'></div>
  109. </div>
  110. <div className='flex-grow overflow-auto'>
  111. {apiKeysList.data.map(api => (
  112. <div className='flex items-center text-sm font-normal text-gray-700 border-b border-solid h-9' key={api.id}>
  113. <div className='flex-shrink-0 w-64 px-3 font-mono truncate'>{generateToken(api.token)}</div>
  114. <div className='flex-shrink-0 px-3 truncate w-28'>{formatDate(api.created_at)}</div>
  115. {/* <div className='flex-shrink-0 px-3 truncate w-28'>{dayjs((+api.created_at) * 1000).format('MMM D, YYYY')}</div> */}
  116. {/* <div className='flex-shrink-0 px-3 truncate w-28'>{api.last_used_at ? dayjs((+api.last_used_at) * 1000).format('MMM D, YYYY') : 'Never'}</div> */}
  117. <div className='flex-shrink-0 px-3 truncate w-28'>{api.last_used_at ? formatDate(api.last_used_at) : t('appApi.never')}</div>
  118. <div className='flex flex-grow px-3'>
  119. <Tooltip
  120. selector={`key-${api.token}`}
  121. content={copyValue === api.token ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  122. className='z-10'
  123. >
  124. <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={() => {
  125. // setIsCopied(true)
  126. copy(api.token)
  127. setCopyValue(api.token)
  128. }}></div>
  129. </Tooltip>
  130. { isCurrentWorkspaceManager
  131. && <div className={`flex items-center justify-center flex-shrink-0 w-6 h-6 rounded-lg cursor-pointer ${s.trashIcon}`} onClick={() => {
  132. setDelKeyId(api.id)
  133. setShowConfirmDelete(true)
  134. }}>
  135. </div>
  136. }
  137. </div>
  138. </div>
  139. ))}
  140. </div>
  141. </div>
  142. )
  143. }
  144. <div className='flex'>
  145. <Button type='default' className={`flex flex-shrink-0 mt-4 ${s.autoWidth}`} onClick={onCreate} disabled={ !currentWorkspace || currentWorkspace.role === 'normal'}>
  146. <PlusIcon className='flex flex-shrink-0 w-4 h-4' />
  147. <div className='text-xs font-medium text-gray-800'>{t('appApi.apiKeyModal.createNewSecretKey')}</div>
  148. </Button>
  149. </div>
  150. <SecretKeyGenerateModal className='flex-shrink-0' isShow={isVisible} onClose={() => setVisible(false)} newKey={newKey} />
  151. {showConfirmDelete && (
  152. <Confirm
  153. title={`${t('appApi.actionMsg.deleteConfirmTitle')}`}
  154. content={`${t('appApi.actionMsg.deleteConfirmTips')}`}
  155. isShow={showConfirmDelete}
  156. onClose={() => {
  157. setDelKeyId('')
  158. setShowConfirmDelete(false)
  159. }}
  160. onConfirm={onDel}
  161. onCancel={() => {
  162. setDelKeyId('')
  163. setShowConfirmDelete(false)
  164. }}
  165. />
  166. )}
  167. </Modal >
  168. )
  169. }
  170. export default SecretKeyModal