2024-07-29 07:45:59 -07:00
|
|
|
import React, { useEffect } from 'react';
|
2024-08-22 17:09:05 -04:00
|
|
|
import { QueryKeys } from 'librechat-data-provider';
|
2024-07-29 07:45:59 -07:00
|
|
|
import { Controller, useForm } from 'react-hook-form';
|
2024-08-22 17:09:05 -04:00
|
|
|
import { useQueryClient } from '@tanstack/react-query';
|
2024-07-29 07:45:59 -07:00
|
|
|
import type {
|
|
|
|
|
TConversation,
|
2024-08-22 17:09:05 -04:00
|
|
|
TConversationTag,
|
2024-07-29 07:45:59 -07:00
|
|
|
TConversationTagRequest,
|
|
|
|
|
} from 'librechat-data-provider';
|
2024-08-22 17:09:05 -04:00
|
|
|
import { cn, removeFocusOutlines, defaultTextProps, logger } from '~/utils';
|
2024-10-19 14:30:52 +02:00
|
|
|
import { Checkbox, Label, TextareaAutosize, Input } from '~/components';
|
2024-07-29 07:45:59 -07:00
|
|
|
import { useBookmarkContext } from '~/Providers/BookmarkContext';
|
|
|
|
|
import { useConversationTagMutation } from '~/data-provider';
|
|
|
|
|
import { useToastContext } from '~/Providers';
|
2024-08-22 17:09:05 -04:00
|
|
|
import { useLocalize } from '~/hooks';
|
2024-07-29 07:45:59 -07:00
|
|
|
|
|
|
|
|
type TBookmarkFormProps = {
|
2024-08-22 17:09:05 -04:00
|
|
|
tags?: string[];
|
2024-07-29 07:45:59 -07:00
|
|
|
bookmark?: TConversationTag;
|
|
|
|
|
conversation?: TConversation;
|
|
|
|
|
formRef: React.RefObject<HTMLFormElement>;
|
2024-08-22 17:09:05 -04:00
|
|
|
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
|
mutation: ReturnType<typeof useConversationTagMutation>;
|
2024-07-29 07:45:59 -07:00
|
|
|
};
|
|
|
|
|
const BookmarkForm = ({
|
2024-08-22 17:09:05 -04:00
|
|
|
tags,
|
2024-07-29 07:45:59 -07:00
|
|
|
bookmark,
|
2024-08-22 17:09:05 -04:00
|
|
|
mutation,
|
2024-07-29 07:45:59 -07:00
|
|
|
conversation,
|
2024-08-22 17:09:05 -04:00
|
|
|
setOpen,
|
2024-07-29 07:45:59 -07:00
|
|
|
formRef,
|
|
|
|
|
}: TBookmarkFormProps) => {
|
|
|
|
|
const localize = useLocalize();
|
2024-08-22 17:09:05 -04:00
|
|
|
const queryClient = useQueryClient();
|
2024-08-08 21:25:10 -04:00
|
|
|
const { showToast } = useToastContext();
|
2024-07-29 07:45:59 -07:00
|
|
|
const { bookmarks } = useBookmarkContext();
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
setValue,
|
|
|
|
|
getValues,
|
|
|
|
|
control,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
} = useForm<TConversationTagRequest>({
|
|
|
|
|
defaultValues: {
|
2024-08-22 17:09:05 -04:00
|
|
|
tag: bookmark?.tag ?? '',
|
|
|
|
|
description: bookmark?.description ?? '',
|
|
|
|
|
conversationId: conversation?.conversationId ?? '',
|
2024-07-29 07:45:59 -07:00
|
|
|
addToConversation: conversation ? true : false,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-08-22 17:09:05 -04:00
|
|
|
if (bookmark && bookmark.tag) {
|
|
|
|
|
setValue('tag', bookmark.tag);
|
|
|
|
|
setValue('description', bookmark.description ?? '');
|
2024-07-29 07:45:59 -07:00
|
|
|
}
|
|
|
|
|
}, [bookmark, setValue]);
|
|
|
|
|
|
|
|
|
|
const onSubmit = (data: TConversationTagRequest) => {
|
2024-08-22 17:09:05 -04:00
|
|
|
logger.log('tag_mutation', 'BookmarkForm - onSubmit: data', data);
|
2024-07-29 07:45:59 -07:00
|
|
|
if (mutation.isLoading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (data.tag === bookmark?.tag && data.description === bookmark?.description) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-22 17:09:05 -04:00
|
|
|
if (data.tag != null && (tags ?? []).includes(data.tag)) {
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_ui_bookmarks_create_exists'),
|
|
|
|
|
status: 'warning',
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const allTags =
|
|
|
|
|
queryClient.getQueryData<TConversationTag[]>([QueryKeys.conversationTags]) ?? [];
|
|
|
|
|
if (allTags.some((tag) => tag.tag === data.tag)) {
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_ui_bookmarks_create_exists'),
|
|
|
|
|
status: 'warning',
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-29 07:45:59 -07:00
|
|
|
|
2024-08-22 17:09:05 -04:00
|
|
|
mutation.mutate(data);
|
|
|
|
|
setOpen(false);
|
2024-07-29 07:45:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
ref={formRef}
|
|
|
|
|
className="mt-6"
|
|
|
|
|
aria-label="Bookmark form"
|
|
|
|
|
method="POST"
|
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex w-full flex-col items-center gap-2">
|
|
|
|
|
<div className="grid w-full items-center gap-2">
|
|
|
|
|
<Label htmlFor="bookmark-tag" className="text-left text-sm font-medium">
|
|
|
|
|
{localize('com_ui_bookmarks_title')}
|
|
|
|
|
</Label>
|
2024-10-19 14:30:52 +02:00
|
|
|
<Input
|
2024-07-29 07:45:59 -07:00
|
|
|
type="text"
|
|
|
|
|
id="bookmark-tag"
|
|
|
|
|
aria-label="Bookmark"
|
|
|
|
|
{...register('tag', {
|
|
|
|
|
required: 'tag is required',
|
|
|
|
|
maxLength: {
|
|
|
|
|
value: 128,
|
|
|
|
|
message: localize('com_auth_password_max_length'),
|
|
|
|
|
},
|
|
|
|
|
validate: (value) => {
|
|
|
|
|
return (
|
|
|
|
|
value === bookmark?.tag ||
|
|
|
|
|
bookmarks.every((bookmark) => bookmark.tag !== value) ||
|
|
|
|
|
'tag must be unique'
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
})}
|
|
|
|
|
aria-invalid={!!errors.tag}
|
2024-10-19 14:30:52 +02:00
|
|
|
placeholder="Bookmark"
|
2024-07-29 07:45:59 -07:00
|
|
|
/>
|
|
|
|
|
{errors.tag && <span className="text-sm text-red-500">{errors.tag.message}</span>}
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-10-19 14:30:52 +02:00
|
|
|
<div className="mt-4 grid w-full items-center gap-2">
|
2024-07-29 07:45:59 -07:00
|
|
|
<Label htmlFor="bookmark-description" className="text-left text-sm font-medium">
|
|
|
|
|
{localize('com_ui_bookmarks_description')}
|
|
|
|
|
</Label>
|
|
|
|
|
<TextareaAutosize
|
|
|
|
|
{...register('description', {
|
|
|
|
|
maxLength: {
|
|
|
|
|
value: 1048,
|
|
|
|
|
message: 'Maximum 1048 characters',
|
|
|
|
|
},
|
|
|
|
|
})}
|
|
|
|
|
id="bookmark-description"
|
|
|
|
|
disabled={false}
|
|
|
|
|
className={cn(
|
2024-10-19 14:30:52 +02:00
|
|
|
'flex h-10 max-h-[250px] min-h-[100px] w-full resize-none rounded-lg border border-input bg-transparent px-3 py-2 text-sm ring-offset-background focus-visible:outline-none',
|
2024-07-29 07:45:59 -07:00
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{conversation && (
|
2024-07-29 19:25:36 -04:00
|
|
|
<div className="mt-2 flex w-full items-center">
|
2024-07-29 07:45:59 -07:00
|
|
|
<Controller
|
|
|
|
|
name="addToConversation"
|
|
|
|
|
control={control}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<Checkbox
|
|
|
|
|
{...field}
|
|
|
|
|
checked={field.value}
|
|
|
|
|
onCheckedChange={field.onChange}
|
|
|
|
|
className="relative float-left mr-2 inline-flex h-4 w-4 cursor-pointer"
|
2024-08-16 10:30:14 +02:00
|
|
|
value={field.value?.toString()}
|
2024-07-29 07:45:59 -07:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2024-08-08 21:25:10 -04:00
|
|
|
<button
|
2024-08-22 17:09:05 -04:00
|
|
|
type="button"
|
2024-08-08 21:25:10 -04:00
|
|
|
aria-label={localize('com_ui_bookmarks_add_to_conversation')}
|
2024-08-22 17:09:05 -04:00
|
|
|
className="form-check-label w-full cursor-pointer text-text-primary"
|
2024-07-29 07:45:59 -07:00
|
|
|
onClick={() =>
|
2024-08-22 17:09:05 -04:00
|
|
|
setValue('addToConversation', !(getValues('addToConversation') ?? false), {
|
2024-07-29 07:45:59 -07:00
|
|
|
shouldDirty: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex select-none items-center">
|
|
|
|
|
{localize('com_ui_bookmarks_add_to_conversation')}
|
|
|
|
|
</div>
|
2024-08-08 21:25:10 -04:00
|
|
|
</button>
|
2024-07-29 07:45:59 -07:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BookmarkForm;
|