secret-key-modal.tsx 6.6 KB

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