Spaces:
Runtime error
Runtime error
File size: 2,600 Bytes
ca10871 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import React, { useState } from "react";
import {
Card,
CardContent,
Typography,
Box,
IconButton,
Divider,
} from "@mui/material";
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import DocumentItem from "./DocumentItem";
function SourceCard({ source, documents, threadId }) {
const [pageIndex, setPageIndex] = useState(0);
const itemsPerPage = 8;
const handlePageChange = (direction) => {
setPageIndex((prev) =>
Math.max(
0,
Math.min(
documents.length - itemsPerPage,
prev + direction * itemsPerPage,
),
),
);
};
return (
<Card
sx={{
borderRadius: 0,
boxShadow: 0,
overflow: "hidden",
display: "flex",
flexDirection: "column",
background: "primary.main",
}}
>
<Divider />
<CardContent sx={{ p: 0 }}>
<Box
sx={{
display: "flex",
flexDirection: "column",
bgcolor: "background.paper",
}}
>
{documents
.slice(pageIndex, pageIndex + itemsPerPage)
.map((document, index) => (
<DocumentItem
key={`${document.metadata.id}-${pageIndex + index}`}
document={document}
threadId={threadId}
/>
))}
</Box>
</CardContent>
<Divider />
<Box
sx={{
p: 0,
bgcolor: "background.paper",
borderRadius: 0,
boxShadow: 0,
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
}}
>
<IconButton
onClick={() => handlePageChange(-1)}
disabled={pageIndex === 0}
size="small"
sx={{ color: "primary.main" }}
>
<ChevronLeftIcon />
</IconButton>
<Typography variant="caption" sx={{ color: "text.secondary" }}>
{`${pageIndex + 1} - ${Math.min(pageIndex + itemsPerPage, documents.length)} of ${documents.length}`}
</Typography>
<IconButton
onClick={() => handlePageChange(1)}
disabled={pageIndex + itemsPerPage >= documents.length}
size="small"
sx={{ color: "primary.main" }}
>
<ChevronRightIcon />
</IconButton>
</Box>
</Box>
</Card>
);
}
export default SourceCard;
|