2024-07-29 07:45:59 -07:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
|
|
import type { ConversationTagsResponse, TConversationTag } from 'librechat-data-provider';
|
2024-07-29 19:25:36 -04:00
|
|
|
import { Table, TableHeader, TableBody, TableRow, TableCell, Input, Button } from '~/components/ui';
|
2024-07-29 07:45:59 -07:00
|
|
|
import { BookmarkContext, useBookmarkContext } from '~/Providers/BookmarkContext';
|
|
|
|
|
import BookmarkTableRow from './BookmarkTableRow';
|
|
|
|
|
import { useLocalize } from '~/hooks';
|
|
|
|
|
|
|
|
|
|
const BookmarkTable = () => {
|
|
|
|
|
const localize = useLocalize();
|
|
|
|
|
const [rows, setRows] = useState<ConversationTagsResponse>([]);
|
2024-07-29 19:25:36 -04:00
|
|
|
const [pageIndex, setPageIndex] = useState(0);
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
|
const pageSize = 10;
|
2024-07-29 07:45:59 -07:00
|
|
|
|
|
|
|
|
const { bookmarks } = useBookmarkContext();
|
|
|
|
|
useEffect(() => {
|
2024-08-08 21:25:10 -04:00
|
|
|
setRows(
|
|
|
|
|
bookmarks
|
2024-08-16 10:30:14 +02:00
|
|
|
.map((item) => ({ id: item.tag, ...item }))
|
2024-08-08 21:25:10 -04:00
|
|
|
.sort((a, b) => a.position - b.position) || [],
|
|
|
|
|
);
|
2024-07-29 07:45:59 -07:00
|
|
|
}, [bookmarks]);
|
|
|
|
|
|
|
|
|
|
const moveRow = useCallback((dragIndex: number, hoverIndex: number) => {
|
|
|
|
|
setRows((prevTags: TConversationTag[]) => {
|
|
|
|
|
const updatedRows = [...prevTags];
|
|
|
|
|
const [movedRow] = updatedRows.splice(dragIndex, 1);
|
|
|
|
|
updatedRows.splice(hoverIndex, 0, movedRow);
|
2024-08-08 21:25:10 -04:00
|
|
|
return updatedRows.map((row, index) => ({ ...row, position: index }));
|
2024-07-29 07:45:59 -07:00
|
|
|
});
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-08-08 21:25:10 -04:00
|
|
|
const renderRow = useCallback(
|
|
|
|
|
(row: TConversationTag) => {
|
|
|
|
|
return <BookmarkTableRow key={row.tag} moveRow={moveRow} row={row} position={row.position} />;
|
|
|
|
|
},
|
|
|
|
|
[moveRow],
|
|
|
|
|
);
|
2024-07-29 07:45:59 -07:00
|
|
|
|
2024-07-29 19:25:36 -04:00
|
|
|
const filteredRows = rows.filter((row) =>
|
|
|
|
|
row.tag.toLowerCase().includes(searchQuery.toLowerCase()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const currentRows = filteredRows.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize);
|
|
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
return (
|
|
|
|
|
<BookmarkContext.Provider value={{ bookmarks }}>
|
2024-07-29 19:25:36 -04:00
|
|
|
<div className="flex items-center gap-4 py-4">
|
|
|
|
|
<Input
|
|
|
|
|
placeholder={localize('com_ui_bookmarks_filter')}
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
2024-08-16 13:50:47 -04:00
|
|
|
className="w-full border-border-light placeholder:text-text-secondary"
|
2024-07-29 19:25:36 -04:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="overflow-y-auto rounded-md border border-black/10 dark:border-white/10">
|
|
|
|
|
<Table className="table-fixed border-separate border-spacing-0">
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell className="w-full px-3 py-3.5 pl-6 dark:bg-gray-700">
|
2024-07-29 07:45:59 -07:00
|
|
|
<div>{localize('com_ui_bookmarks_title')}</div>
|
2024-07-29 19:25:36 -04:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className="w-full px-3 py-3.5 dark:bg-gray-700 sm:pl-6">
|
2024-07-29 07:45:59 -07:00
|
|
|
<div>{localize('com_ui_bookmarks_count')}</div>
|
2024-07-29 19:25:36 -04:00
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
2024-08-08 21:25:10 -04:00
|
|
|
<TableBody>{currentRows.map((row) => renderRow(row))}</TableBody>
|
2024-07-29 19:25:36 -04:00
|
|
|
</Table>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center justify-between py-4">
|
|
|
|
|
<div className="pl-1 text-gray-400">
|
|
|
|
|
{localize('com_ui_showing')} {pageIndex * pageSize + 1} -{' '}
|
|
|
|
|
{Math.min((pageIndex + 1) * pageSize, filteredRows.length)} {localize('com_ui_of')}{' '}
|
|
|
|
|
{filteredRows.length}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setPageIndex((prev) => Math.max(prev - 1, 0))}
|
|
|
|
|
disabled={pageIndex === 0}
|
|
|
|
|
>
|
|
|
|
|
{localize('com_ui_prev')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
setPageIndex((prev) =>
|
|
|
|
|
(prev + 1) * pageSize < filteredRows.length ? prev + 1 : prev,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
disabled={(pageIndex + 1) * pageSize >= filteredRows.length}
|
|
|
|
|
>
|
|
|
|
|
{localize('com_ui_next')}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2024-07-29 07:45:59 -07:00
|
|
|
</div>
|
|
|
|
|
</BookmarkContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BookmarkTable;
|