2024-12-29 23:31:41 +01:00
|
|
|
import React, { useRef } from 'react';
|
2024-07-29 07:45:59 -07:00
|
|
|
import { useDrag, useDrop } from 'react-dnd';
|
|
|
|
|
import type { TConversationTag } from 'librechat-data-provider';
|
|
|
|
|
import { DeleteBookmarkButton, EditBookmarkButton } from '~/components/Bookmarks';
|
2024-08-08 21:25:10 -04:00
|
|
|
import { useConversationTagMutation } from '~/data-provider';
|
2024-07-29 19:25:36 -04:00
|
|
|
import { TableRow, TableCell } from '~/components/ui';
|
2024-08-08 21:25:10 -04:00
|
|
|
import { NotificationSeverity } from '~/common';
|
|
|
|
|
import { useToastContext } from '~/Providers';
|
|
|
|
|
import { useLocalize } from '~/hooks';
|
2024-07-29 07:45:59 -07:00
|
|
|
|
2024-07-29 19:25:36 -04:00
|
|
|
interface BookmarkTableRowProps {
|
2024-07-29 07:45:59 -07:00
|
|
|
row: TConversationTag;
|
2024-07-29 19:25:36 -04:00
|
|
|
moveRow: (dragIndex: number, hoverIndex: number) => void;
|
|
|
|
|
position: number;
|
2024-07-29 07:45:59 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 21:25:10 -04:00
|
|
|
interface DragItem {
|
|
|
|
|
index: number;
|
|
|
|
|
id: string;
|
|
|
|
|
type: string;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 19:25:36 -04:00
|
|
|
const BookmarkTableRow: React.FC<BookmarkTableRowProps> = ({ row, moveRow, position }) => {
|
2024-08-08 21:25:10 -04:00
|
|
|
const ref = useRef<HTMLTableRowElement>(null);
|
2024-08-22 17:09:05 -04:00
|
|
|
const mutation = useConversationTagMutation({ context: 'BookmarkTableRow', tag: row.tag });
|
2024-08-08 21:25:10 -04:00
|
|
|
const localize = useLocalize();
|
|
|
|
|
const { showToast } = useToastContext();
|
|
|
|
|
|
|
|
|
|
const handleDrop = (item: DragItem) => {
|
2024-12-29 23:31:41 +01:00
|
|
|
mutation.mutate(
|
|
|
|
|
{ ...row, position: item.index },
|
|
|
|
|
{
|
2025-06-08 20:00:57 +02:00
|
|
|
onSuccess: () => {
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_ui_bookmarks_update_success'),
|
|
|
|
|
severity: NotificationSeverity.SUCCESS,
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-12-29 23:31:41 +01:00
|
|
|
onError: () => {
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_ui_bookmarks_update_error'),
|
|
|
|
|
severity: NotificationSeverity.ERROR,
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-08-08 21:25:10 -04:00
|
|
|
},
|
2024-12-29 23:31:41 +01:00
|
|
|
);
|
2024-08-08 21:25:10 -04:00
|
|
|
};
|
2024-07-29 07:45:59 -07:00
|
|
|
|
2024-07-29 19:25:36 -04:00
|
|
|
const [, drop] = useDrop({
|
|
|
|
|
accept: 'bookmark',
|
2024-12-29 23:31:41 +01:00
|
|
|
drop: handleDrop,
|
2024-08-08 21:25:10 -04:00
|
|
|
hover(item: DragItem) {
|
2025-06-08 20:00:57 +02:00
|
|
|
if (!ref.current || item.index === position) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-29 23:31:41 +01:00
|
|
|
moveRow(item.index, position);
|
|
|
|
|
item.index = position;
|
2024-07-29 07:45:59 -07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [{ isDragging }, drag] = useDrag({
|
2024-07-29 19:25:36 -04:00
|
|
|
type: 'bookmark',
|
|
|
|
|
item: { index: position },
|
2024-07-29 07:45:59 -07:00
|
|
|
collect: (monitor) => ({
|
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-29 19:25:36 -04:00
|
|
|
drag(drop(ref));
|
2024-07-29 07:45:59 -07:00
|
|
|
|
|
|
|
|
return (
|
2024-07-29 19:25:36 -04:00
|
|
|
<TableRow
|
2024-07-29 07:45:59 -07:00
|
|
|
ref={ref}
|
2024-12-29 23:31:41 +01:00
|
|
|
className="cursor-move hover:bg-surface-secondary"
|
2024-07-29 19:25:36 -04:00
|
|
|
style={{ opacity: isDragging ? 0.5 : 1 }}
|
2024-07-29 07:45:59 -07:00
|
|
|
>
|
2024-12-29 23:31:41 +01:00
|
|
|
<TableCell className="w-[70%] px-4 py-4">
|
|
|
|
|
<div className="overflow-hidden text-ellipsis whitespace-nowrap">{row.tag}</div>
|
2024-07-29 19:25:36 -04:00
|
|
|
</TableCell>
|
2024-12-29 23:31:41 +01:00
|
|
|
<TableCell className="w-[10%] px-12 py-4">{row.count}</TableCell>
|
|
|
|
|
<TableCell className="w-[20%] px-4 py-4">
|
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
<EditBookmarkButton bookmark={row} tabIndex={0} />
|
|
|
|
|
<DeleteBookmarkButton bookmark={row.tag} tabIndex={0} />
|
2024-07-29 19:25:36 -04:00
|
|
|
</div>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
2024-07-29 07:45:59 -07:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BookmarkTableRow;
|