ref(DocumentManager): Implement document sorting with previous sortDocuments function

This commit is contained in:
Eric Shao
2025-04-03 09:39:32 +08:00
parent b79dac9d63
commit 4a49f557f0
3 changed files with 162 additions and 137 deletions

File diff suppressed because one or more lines are too long

View File

@@ -8,7 +8,7 @@
<link rel="icon" type="image/svg+xml" href="logo.png" /> <link rel="icon" type="image/svg+xml" href="logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lightrag</title> <title>Lightrag</title>
<script type="module" crossorigin src="/webui/assets/index-CfYQAXql.js"></script> <script type="module" crossorigin src="/webui/assets/index-D8zGvNlV.js"></script>
<link rel="stylesheet" crossorigin href="/webui/assets/index-CD5HxTy1.css"> <link rel="stylesheet" crossorigin href="/webui/assets/index-CD5HxTy1.css">
</head> </head>
<body> <body>

View File

@@ -165,6 +165,37 @@ export default function DocumentManager() {
setSortDirection('desc') setSortDirection('desc')
} }
} }
// Sort documents based on current sort field and direction
const sortDocuments = (documents: DocStatusResponse[]) => {
return [...documents].sort((a, b) => {
let valueA, valueB;
// Special handling for ID field based on showFileName setting
if (sortField === 'id' && showFileName) {
valueA = getDisplayFileName(a);
valueB = getDisplayFileName(b);
} else if (sortField === 'id') {
valueA = a.id;
valueB = b.id;
} else {
// Date fields
valueA = new Date(a[sortField]).getTime();
valueB = new Date(b[sortField]).getTime();
}
// Apply sort direction
const sortMultiplier = sortDirection === 'asc' ? 1 : -1;
// Compare values
if (typeof valueA === 'string' && typeof valueB === 'string') {
return sortMultiplier * valueA.localeCompare(valueB);
} else {
return sortMultiplier * (valueA > valueB ? 1 : valueA < valueB ? -1 : 0);
}
});
}
const filteredAndSortedDocs = useMemo(() => { const filteredAndSortedDocs = useMemo(() => {
if (!docs) return null; if (!docs) return null;
@@ -186,13 +217,7 @@ export default function DocumentManager() {
if (!sortField || !sortDirection) return filteredDocs; if (!sortField || !sortDirection) return filteredDocs;
const sortedStatuses = Object.entries(filteredDocs.statuses).reduce((acc, [status, documents]) => { const sortedStatuses = Object.entries(filteredDocs.statuses).reduce((acc, [status, documents]) => {
const sortedDocuments = [...documents].sort((a, b) => { const sortedDocuments = sortDocuments(documents);
if (sortDirection === 'asc') {
return (a[sortField] ?? 0) > (b[sortField] ?? 0) ? 1 : -1;
} else {
return (a[sortField] ?? 0) < (b[sortField] ?? 0) ? 1 : -1;
}
});
acc[status as DocStatus] = sortedDocuments; acc[status as DocStatus] = sortedDocuments;
return acc; return acc;
}, {} as DocsStatusesResponse['statuses']); }, {} as DocsStatusesResponse['statuses']);