2024-08-22 17:09:05 -04:00
|
|
|
import React, { useRef, Dispatch, SetStateAction } from 'react';
|
2024-07-29 07:45:59 -07:00
|
|
|
import { TConversationTag, TConversation } from 'librechat-data-provider';
|
2024-07-29 19:25:36 -04:00
|
|
|
import OGDialogTemplate from '~/components/ui/OGDialogTemplate';
|
2024-08-22 17:09:05 -04:00
|
|
|
import { useConversationTagMutation } from '~/data-provider';
|
|
|
|
|
import { NotificationSeverity } from '~/common';
|
|
|
|
|
import { useToastContext } from '~/Providers';
|
|
|
|
|
import { OGDialog } from '~/components/ui';
|
|
|
|
|
import { Spinner } from '~/components/svg';
|
2024-07-29 07:45:59 -07:00
|
|
|
import BookmarkForm from './BookmarkForm';
|
|
|
|
|
import { useLocalize } from '~/hooks';
|
2024-08-22 17:09:05 -04:00
|
|
|
import { logger } from '~/utils';
|
2024-07-29 07:45:59 -07:00
|
|
|
|
|
|
|
|
type BookmarkEditDialogProps = {
|
2024-08-22 17:09:05 -04:00
|
|
|
context: string;
|
2024-07-29 07:45:59 -07:00
|
|
|
bookmark?: TConversationTag;
|
|
|
|
|
conversation?: TConversation;
|
|
|
|
|
tags?: string[];
|
|
|
|
|
setTags?: (tags: string[]) => void;
|
2024-08-16 10:30:14 +02:00
|
|
|
open: boolean;
|
|
|
|
|
setOpen: Dispatch<SetStateAction<boolean>>;
|
2024-07-29 07:45:59 -07:00
|
|
|
};
|
2024-08-16 10:30:14 +02:00
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
const BookmarkEditDialog = ({
|
2024-08-22 17:09:05 -04:00
|
|
|
context,
|
2024-07-29 07:45:59 -07:00
|
|
|
bookmark,
|
|
|
|
|
conversation,
|
|
|
|
|
tags,
|
|
|
|
|
setTags,
|
2024-08-16 10:30:14 +02:00
|
|
|
open,
|
|
|
|
|
setOpen,
|
2024-07-29 07:45:59 -07:00
|
|
|
}: BookmarkEditDialogProps) => {
|
|
|
|
|
const localize = useLocalize();
|
|
|
|
|
const formRef = useRef<HTMLFormElement>(null);
|
|
|
|
|
|
2024-08-22 17:09:05 -04:00
|
|
|
const { showToast } = useToastContext();
|
|
|
|
|
const mutation = useConversationTagMutation({
|
|
|
|
|
context,
|
|
|
|
|
tag: bookmark?.tag,
|
|
|
|
|
options: {
|
|
|
|
|
onSuccess: (_data, vars) => {
|
|
|
|
|
showToast({
|
|
|
|
|
message: bookmark
|
|
|
|
|
? localize('com_ui_bookmarks_update_success')
|
|
|
|
|
: localize('com_ui_bookmarks_create_success'),
|
|
|
|
|
});
|
|
|
|
|
setOpen(false);
|
|
|
|
|
logger.log('tag_mutation', 'tags before setting', tags);
|
|
|
|
|
if (setTags && vars.addToConversation === true) {
|
|
|
|
|
const newTags = [...(tags || []), vars.tag].filter(
|
|
|
|
|
(tag) => tag !== undefined,
|
|
|
|
|
) as string[];
|
|
|
|
|
setTags(newTags);
|
|
|
|
|
logger.log('tag_mutation', 'tags after', newTags);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onError: () => {
|
|
|
|
|
showToast({
|
|
|
|
|
message: bookmark
|
|
|
|
|
? localize('com_ui_bookmarks_update_error')
|
|
|
|
|
: localize('com_ui_bookmarks_create_error'),
|
|
|
|
|
severity: NotificationSeverity.ERROR,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-29 07:45:59 -07:00
|
|
|
const handleSubmitForm = () => {
|
|
|
|
|
if (formRef.current) {
|
|
|
|
|
formRef.current.dispatchEvent(new Event('submit', { cancelable: true, bubbles: true }));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2024-07-29 19:25:36 -04:00
|
|
|
<OGDialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
<OGDialogTemplate
|
2024-07-29 07:45:59 -07:00
|
|
|
title="Bookmark"
|
|
|
|
|
showCloseButton={false}
|
|
|
|
|
main={
|
|
|
|
|
<BookmarkForm
|
2024-08-22 17:09:05 -04:00
|
|
|
tags={tags}
|
|
|
|
|
setOpen={setOpen}
|
|
|
|
|
mutation={mutation}
|
2024-07-29 07:45:59 -07:00
|
|
|
conversation={conversation}
|
|
|
|
|
bookmark={bookmark}
|
|
|
|
|
formRef={formRef}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
buttons={
|
2024-08-22 17:09:05 -04:00
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={mutation.isLoading}
|
|
|
|
|
onClick={handleSubmitForm}
|
|
|
|
|
className="btn rounded bg-green-500 font-bold text-white transition-all hover:bg-green-600"
|
|
|
|
|
>
|
|
|
|
|
{mutation.isLoading ? <Spinner /> : localize('com_ui_save')}
|
|
|
|
|
</button>
|
2024-07-29 07:45:59 -07:00
|
|
|
}
|
|
|
|
|
/>
|
2024-07-29 19:25:36 -04:00
|
|
|
</OGDialog>
|
2024-07-29 07:45:59 -07:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BookmarkEditDialog;
|