csv-downloader.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import {
  5. useCSVDownloader,
  6. } from 'react-papaparse'
  7. import { useTranslation } from 'react-i18next'
  8. import { useContext } from 'use-context-selector'
  9. import { Download02 as DownloadIcon } from '@/app/components/base/icons/src/vender/solid/general'
  10. import { DocForm } from '@/models/datasets'
  11. import I18n from '@/context/i18n'
  12. import { LanguagesSupported } from '@/i18n/language'
  13. const CSV_TEMPLATE_QA_EN = [
  14. ['question', 'answer'],
  15. ['question1', 'answer1'],
  16. ['question2', 'answer2'],
  17. ]
  18. const CSV_TEMPLATE_QA_CN = [
  19. ['问题', '答案'],
  20. ['问题 1', '答案 1'],
  21. ['问题 2', '答案 2'],
  22. ]
  23. const CSV_TEMPLATE_EN = [
  24. ['segment content'],
  25. ['content1'],
  26. ['content2'],
  27. ]
  28. const CSV_TEMPLATE_CN = [
  29. ['分段内容'],
  30. ['内容 1'],
  31. ['内容 2'],
  32. ]
  33. const CSVDownload: FC<{ docForm: DocForm }> = ({ docForm }) => {
  34. const { t } = useTranslation()
  35. const { locale } = useContext(I18n)
  36. const { CSVDownloader, Type } = useCSVDownloader()
  37. const getTemplate = () => {
  38. if (locale === LanguagesSupported[1]) {
  39. if (docForm === DocForm.QA)
  40. return CSV_TEMPLATE_QA_CN
  41. return CSV_TEMPLATE_CN
  42. }
  43. if (docForm === DocForm.QA)
  44. return CSV_TEMPLATE_QA_EN
  45. return CSV_TEMPLATE_EN
  46. }
  47. return (
  48. <div className='mt-6'>
  49. <div className='text-sm text-gray-900 font-medium'>{t('share.generation.csvStructureTitle')}</div>
  50. <div className='mt-2 max-h-[500px] overflow-auto'>
  51. {docForm === DocForm.QA && (
  52. <table className='table-fixed w-full border-separate border-spacing-0 border border-gray-200 rounded-lg text-xs'>
  53. <thead className='text-gray-500'>
  54. <tr>
  55. <td className='h-9 pl-3 pr-2 border-b border-gray-200'>{t('datasetDocuments.list.batchModal.question')}</td>
  56. <td className='h-9 pl-3 pr-2 border-b border-gray-200'>{t('datasetDocuments.list.batchModal.answer')}</td>
  57. </tr>
  58. </thead>
  59. <tbody className='text-gray-700'>
  60. <tr>
  61. <td className='h-9 pl-3 pr-2 border-b border-gray-100 text-[13px]'>{t('datasetDocuments.list.batchModal.question')} 1</td>
  62. <td className='h-9 pl-3 pr-2 border-b border-gray-100 text-[13px]'>{t('datasetDocuments.list.batchModal.answer')} 1</td>
  63. </tr>
  64. <tr>
  65. <td className='h-9 pl-3 pr-2 text-[13px]'>{t('datasetDocuments.list.batchModal.question')} 2</td>
  66. <td className='h-9 pl-3 pr-2 text-[13px]'>{t('datasetDocuments.list.batchModal.answer')} 2</td>
  67. </tr>
  68. </tbody>
  69. </table>
  70. )}
  71. {docForm === DocForm.TEXT && (
  72. <table className='table-fixed w-full border-separate border-spacing-0 border border-gray-200 rounded-lg text-xs'>
  73. <thead className='text-gray-500'>
  74. <tr>
  75. <td className='h-9 pl-3 pr-2 border-b border-gray-200'>{t('datasetDocuments.list.batchModal.contentTitle')}</td>
  76. </tr>
  77. </thead>
  78. <tbody className='text-gray-700'>
  79. <tr>
  80. <td className='h-9 pl-3 pr-2 border-b border-gray-100 text-[13px]'>{t('datasetDocuments.list.batchModal.content')} 1</td>
  81. </tr>
  82. <tr>
  83. <td className='h-9 pl-3 pr-2 text-[13px]'>{t('datasetDocuments.list.batchModal.content')} 2</td>
  84. </tr>
  85. </tbody>
  86. </table>
  87. )}
  88. </div>
  89. <CSVDownloader
  90. className="block mt-2 cursor-pointer"
  91. type={Type.Link}
  92. filename={'template'}
  93. bom={true}
  94. data={getTemplate()}
  95. >
  96. <div className='flex items-center h-[18px] space-x-1 text-[#155EEF] text-xs font-medium'>
  97. <DownloadIcon className='w-3 h-3 mr-1' />
  98. {t('datasetDocuments.list.batchModal.template')}
  99. </div>
  100. </CSVDownloader>
  101. </div>
  102. )
  103. }
  104. export default React.memo(CSVDownload)