secret-key-modal.tsx 6.8 KB

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