item.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiDeleteBinLine,
  6. } from '@remixicon/react'
  7. import { Edit02 } from '@/app/components/base/icons/src/vender/line/general'
  8. import type { ApiBasedExtension } from '@/models/common'
  9. import { useModalContext } from '@/context/modal-context'
  10. import { deleteApiBasedExtension } from '@/service/common'
  11. import Confirm from '@/app/components/base/confirm'
  12. type ItemProps = {
  13. data: ApiBasedExtension
  14. onUpdate: () => void
  15. }
  16. const Item: FC<ItemProps> = ({
  17. data,
  18. onUpdate,
  19. }) => {
  20. const { t } = useTranslation()
  21. const { setShowApiBasedExtensionModal } = useModalContext()
  22. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
  23. const handleOpenApiBasedExtensionModal = () => {
  24. setShowApiBasedExtensionModal({
  25. payload: data,
  26. onSaveCallback: () => onUpdate(),
  27. })
  28. }
  29. const handleDeleteApiBasedExtension = async () => {
  30. await deleteApiBasedExtension(`/api-based-extension/${data.id}`)
  31. setShowDeleteConfirm(false)
  32. onUpdate()
  33. }
  34. return (
  35. <div className='group flex items-center mb-2 px-4 py-2 border-[0.5px] border-transparent rounded-xl bg-gray-50 hover:border-gray-200 hover:shadow-xs'>
  36. <div className='grow'>
  37. <div className='mb-0.5 text-[13px] font-medium text-gray-700'>{data.name}</div>
  38. <div className='text-xs text-gray-500'>{data.api_endpoint}</div>
  39. </div>
  40. <div className='hidden group-hover:flex items-center'>
  41. <div
  42. className='flex items-center mr-1 px-3 h-7 bg-white text-xs font-medium text-gray-700 rounded-md border-[0.5px] border-gray-200 shadow-xs cursor-pointer'
  43. onClick={handleOpenApiBasedExtensionModal}
  44. >
  45. <Edit02 className='mr-[5px] w-3.5 h-3.5' />
  46. {t('common.operation.edit')}
  47. </div>
  48. <div
  49. className='flex items-center justify-center w-7 h-7 bg-white text-gray-700 rounded-md border-[0.5px] border-gray-200 shadow-xs cursor-pointer'
  50. onClick={() => setShowDeleteConfirm(true)}
  51. >
  52. <RiDeleteBinLine className='w-4 h-4' />
  53. </div>
  54. </div>
  55. {
  56. showDeleteConfirm
  57. && <Confirm
  58. isShow={showDeleteConfirm}
  59. onCancel={() => setShowDeleteConfirm(false)}
  60. title={`${t('common.operation.delete')} “${data.name}”?`}
  61. onConfirm={handleDeleteApiBasedExtension}
  62. confirmText={t('common.operation.delete') || ''}
  63. />
  64. }
  65. </div>
  66. )
  67. }
  68. export default Item