|
| 1 | +import React, { useState, useMemo } from 'react'; |
| 2 | +import { Box, Flex, Text, Grid } from '@chakra-ui/react'; |
| 3 | +import Icon from '@fastgpt/web/components/common/Icon'; |
| 4 | +import MySelect from '@fastgpt/web/components/common/MySelect'; |
| 5 | +import { useSafeTranslation } from '@fastgpt/web/hooks/useSafeTranslation'; |
| 6 | + |
| 7 | +const TableBlock: React.FC<{ code: string }> = ({ code }) => { |
| 8 | + const { t } = useSafeTranslation(); |
| 9 | + const tableData = JSON.parse(code); |
| 10 | + const [currentPage, setCurrentPage] = useState(1); |
| 11 | + const [perPage, setPerPage] = useState(10); |
| 12 | + |
| 13 | + const headers = Object.keys(tableData[0]); |
| 14 | + |
| 15 | + // calculate paginated data |
| 16 | + const { paginatedData, totalPages } = useMemo(() => { |
| 17 | + const total = Math.ceil(tableData.length / perPage); |
| 18 | + const startIndex = (currentPage - 1) * perPage; |
| 19 | + const endIndex = startIndex + perPage; |
| 20 | + const paginated = tableData.slice(startIndex, endIndex); |
| 21 | + |
| 22 | + return { |
| 23 | + paginatedData: paginated, |
| 24 | + totalPages: total |
| 25 | + }; |
| 26 | + }, [tableData, currentPage, perPage]); |
| 27 | + |
| 28 | + const handlePrevPage = () => { |
| 29 | + setCurrentPage((prev) => Math.max(prev - 1, 1)); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleNextPage = () => { |
| 33 | + setCurrentPage((prev) => Math.min(prev + 1, totalPages)); |
| 34 | + }; |
| 35 | + |
| 36 | + const handlePerPageChange = (value: string) => { |
| 37 | + setPerPage(Number(value)); |
| 38 | + setCurrentPage(1); // reset to first page |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Box my={4}> |
| 43 | + <Flex overflowX="auto"> |
| 44 | + <table style={{ width: '100%', borderCollapse: 'collapse', border: '1px solid #e2e8f0' }}> |
| 45 | + <thead> |
| 46 | + <tr style={{ backgroundColor: '#f7fafc' }}> |
| 47 | + {headers.map((header, index) => ( |
| 48 | + <th |
| 49 | + key={index} |
| 50 | + style={{ |
| 51 | + padding: '12px', |
| 52 | + border: '1px solid #e2e8f0', |
| 53 | + textAlign: 'left', |
| 54 | + fontWeight: 'bold', |
| 55 | + whiteSpace: 'nowrap' |
| 56 | + }} |
| 57 | + > |
| 58 | + {header} |
| 59 | + </th> |
| 60 | + ))} |
| 61 | + </tr> |
| 62 | + </thead> |
| 63 | + <tbody> |
| 64 | + {paginatedData.map((row: any, rowIndex: number) => ( |
| 65 | + <tr |
| 66 | + key={rowIndex} |
| 67 | + style={{ backgroundColor: rowIndex % 2 === 0 ? '#ffffff' : '#f9f9f9' }} |
| 68 | + > |
| 69 | + {headers.map((header, colIndex) => ( |
| 70 | + <td |
| 71 | + key={colIndex} |
| 72 | + style={{ |
| 73 | + padding: '12px', |
| 74 | + border: '1px solid #e2e8f0', |
| 75 | + whiteSpace: 'nowrap' |
| 76 | + }} |
| 77 | + > |
| 78 | + {row[header] || ''} |
| 79 | + </td> |
| 80 | + ))} |
| 81 | + </tr> |
| 82 | + ))} |
| 83 | + </tbody> |
| 84 | + </table> |
| 85 | + </Flex> |
| 86 | + |
| 87 | + <Grid width="full" gridTemplateColumns="1fr auto 1fr" alignItems="center" gap={4}> |
| 88 | + <Flex gap={1} align="center" gridColumn="2"> |
| 89 | + <Icon |
| 90 | + name="core/chat/chevronLeft" |
| 91 | + w="16px" |
| 92 | + height="16px" |
| 93 | + cursor={currentPage === 1 ? 'not-allowed' : 'pointer'} |
| 94 | + opacity={currentPage === 1 ? 0.5 : 1} |
| 95 | + onClick={currentPage === 1 ? undefined : handlePrevPage} |
| 96 | + /> |
| 97 | + <Text> |
| 98 | + {currentPage} / {totalPages} |
| 99 | + </Text> |
| 100 | + <Icon |
| 101 | + name="core/chat/chevronRight" |
| 102 | + w="16px" |
| 103 | + height="16px" |
| 104 | + cursor={currentPage === totalPages ? 'not-allowed' : 'pointer'} |
| 105 | + opacity={currentPage === totalPages ? 0.5 : 1} |
| 106 | + onClick={currentPage === totalPages ? undefined : handleNextPage} |
| 107 | + /> |
| 108 | + </Flex> |
| 109 | + |
| 110 | + {totalPages > 1 && ( |
| 111 | + <Flex gridColumn="3"> |
| 112 | + <MySelect |
| 113 | + value={perPage.toString()} |
| 114 | + onChange={handlePerPageChange} |
| 115 | + list={[ |
| 116 | + { label: t('common:core.chat.table.per_page', { num: 5 }), value: '5' }, |
| 117 | + { |
| 118 | + label: t('common:core.chat.table.per_page', { num: 10 }), |
| 119 | + value: '10' |
| 120 | + }, |
| 121 | + { |
| 122 | + label: t('common:core.chat.table.per_page', { num: 20 }), |
| 123 | + value: '20' |
| 124 | + }, |
| 125 | + { |
| 126 | + label: t('common:core.chat.table.per_page', { num: 50 }), |
| 127 | + value: '50' |
| 128 | + }, |
| 129 | + { |
| 130 | + label: t('common:core.chat.table.per_page', { num: 100 }), |
| 131 | + value: '100' |
| 132 | + } |
| 133 | + ]} |
| 134 | + /> |
| 135 | + </Flex> |
| 136 | + )} |
| 137 | + </Grid> |
| 138 | + </Box> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +export default TableBlock; |
0 commit comments