secret-key-modal.tsx 6.7 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 SecretKeyGenerateModal from './secret-key-generate'
  10. import s from './style.module.css'
  11. import Modal from '@/app/components/base/modal'
  12. import Button from '@/app/components/base/button'
  13. import { createApikey, delApikey, fetchApiKeysList } from '@/service/apps'
  14. import type { CreateApiKeyResponse } from '@/models/app'
  15. import Tooltip from '@/app/components/base/tooltip'
  16. import Loading from '@/app/components/base/loading'
  17. import Confirm from '@/app/components/base/confirm'
  18. import useCopyToClipboard from '@/hooks/use-copy-to-clipboard'
  19. import { useContext } from 'use-context-selector'
  20. import I18n from '@/context/i18n'
  21. type ISecretKeyModalProps = {
  22. isShow: boolean
  23. appId: string
  24. onClose: () => void
  25. }
  26. const SecretKeyModal = ({
  27. isShow = false,
  28. appId,
  29. onClose,
  30. }: ISecretKeyModalProps) => {
  31. const { t } = useTranslation()
  32. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  33. const [isVisible, setVisible] = useState(false)
  34. const [newKey, setNewKey] = useState<CreateApiKeyResponse | undefined>(undefined)
  35. const { mutate } = useSWRConfig()
  36. const commonParams = { url: `/apps/${appId}/api-keys`, params: {} }
  37. const { data: apiKeysList } = useSWR(commonParams, fetchApiKeysList)
  38. const [delKeyID, setDelKeyId] = useState('')
  39. const [_, copy] = useCopyToClipboard()
  40. const { locale } = useContext(I18n)
  41. // const [isCopied, setIsCopied] = useState(false)
  42. const [copyValue, setCopyValue] = useState('')
  43. useEffect(() => {
  44. if (copyValue) {
  45. const timeout = setTimeout(() => {
  46. setCopyValue('')
  47. }, 1000)
  48. return () => {
  49. clearTimeout(timeout)
  50. }
  51. }
  52. }, [copyValue])
  53. const onDel = async () => {
  54. setShowConfirmDelete(false)
  55. if (!delKeyID) {
  56. return
  57. }
  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: any) => {
  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. }
  77. return (
  78. <Modal isShow={isShow} onClose={onClose} title={`${t('appApi.apiKeyModal.apiSecretKey')}`} className={`${s.customModal} px-8 flex flex-col`}>
  79. <XMarkIcon className={`w-6 h-6 absolute cursor-pointer text-gray-500 ${s.close}`} onClick={onClose} />
  80. <p className='mt-1 text-[13px] text-gray-500 font-normal leading-5 flex-shrink-0'>{t('appApi.apiKeyModal.apiSecretKeyTips')}</p>
  81. {!apiKeysList && <div className='mt-4'><Loading /></div>}
  82. {
  83. !!apiKeysList?.data?.length && (
  84. <div className='flex flex-col flex-grow mt-4 overflow-hidden'>
  85. <div className='flex items-center flex-shrink-0 text-xs font-semibold text-gray-500 border-b border-solid h-9'>
  86. <div className='flex-shrink-0 w-64 px-3'>{t('appApi.apiKeyModal.secretKey')}</div>
  87. <div className='flex-shrink-0 px-3 w-28'>{t('appApi.apiKeyModal.created')}</div>
  88. <div className='flex-shrink-0 px-3 w-28'>{t('appApi.apiKeyModal.lastUsed')}</div>
  89. <div className='flex-grow px-3'></div>
  90. </div>
  91. <div className='flex-grow overflow-auto'>
  92. {apiKeysList.data.map(api => (
  93. <div className='flex items-center text-sm font-normal text-gray-700 border-b border-solid h-9' key={api.id}>
  94. <div className='flex-shrink-0 w-64 px-3 font-mono truncate'>{generateToken(api.token)}</div>
  95. <div className='flex-shrink-0 px-3 truncate w-28'>{formatDate(api.created_at)}</div>
  96. {/* <div className='flex-shrink-0 px-3 truncate w-28'>{dayjs((+api.created_at) * 1000).format('MMM D, YYYY')}</div> */}
  97. {/* <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> */}
  98. <div className='flex-shrink-0 px-3 truncate w-28'>{api.last_used_at ? formatDate(api.last_used_at) : t('appApi.never')}</div>
  99. <div className='flex flex-grow px-3'>
  100. <Tooltip
  101. selector="top-uniq"
  102. content={copyValue === api.token ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  103. className='z-10'
  104. >
  105. <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={() => {
  106. // setIsCopied(true)
  107. copy(api.token)
  108. setCopyValue(api.token)
  109. }}></div>
  110. </Tooltip>
  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. </div>
  117. </div>
  118. ))}
  119. </div>
  120. </div>
  121. )
  122. }
  123. <div className='flex'>
  124. <Button type='default' className={`flex flex-shrink-0 mt-4 ${s.autoWidth}`} onClick={() =>
  125. onCreate()
  126. }>
  127. <PlusIcon className='flex flex-shrink-0 w-4 h-4' />
  128. <div className='text-xs font-medium text-gray-800'>{t('appApi.apiKeyModal.createNewSecretKey')}</div>
  129. </Button>
  130. </div>
  131. <SecretKeyGenerateModal className='flex-shrink-0' isShow={isVisible} onClose={() => setVisible(false)} newKey={newKey} />
  132. {showConfirmDelete && (
  133. <Confirm
  134. title={`${t('appApi.actionMsg.deleteConfirmTitle')}`}
  135. content={`${t('appApi.actionMsg.deleteConfirmTips')}`}
  136. isShow={showConfirmDelete}
  137. onClose={() => {
  138. setDelKeyId('')
  139. setShowConfirmDelete(false)
  140. }}
  141. onConfirm={onDel}
  142. onCancel={() => {
  143. setDelKeyId('')
  144. setShowConfirmDelete(false)
  145. }}
  146. />
  147. )}
  148. </Modal >
  149. )
  150. }
  151. export default SecretKeyModal