🔖 feat: Enhance Bookmarks UX, add RBAC, toggle via librechat.yaml (#3747)

* chore: update package version to 0.7.416

* chore: Update Role.js imports order

* refactor: move updateTagsInConvo to tags route, add RBAC for tags

* refactor: add updateTagsInConvoOptions

* fix: loading state for bookmark form

* refactor: update primaryText class in TitleButton component

* refactor: remove duplicate bookmarks and theming

* refactor: update EditIcon component to use React.forwardRef

* refactor: add _id field to tConversationTagSchema

* refactor: remove promises

* refactor: move mutation logic from BookmarkForm -> BookmarkEditDialog

* refactor: update button class in BookmarkForm component

* fix: conversation mutations and add better logging to useConversationTagMutation

* refactor: update logger message in BookmarkEditDialog component

* refactor: improve UI consistency in BookmarkNav and NewChat components

* refactor: update logger message in BookmarkEditDialog component

* refactor: Add tags prop to BookmarkForm component

* refactor: Update BookmarkForm to avoid tag mutation if the tag already exists; also close dialog on submission programmatically

* refactor: general role helper function to support updating access permissions for different permission types

* refactor: Update getLatestText function to handle undefined values in message.content

* refactor: Update useHasAccess hook to handle null role values for authenticated users

* feat: toggle bookmarks access

* refactor: Update PromptsCommand to handle access permissions for prompts

* feat: updateConversationSelector

* refactor: rename `vars` to `tagToDelete` for clarity

* fix: prevent recreation of deleted tags in BookmarkMenu on Item Click

* ci: mock updateBookmarksAccess function

* ci: mock updateBookmarksAccess function
This commit is contained in:
Danny Avila 2024-08-22 17:09:05 -04:00 committed by GitHub
parent 366e4c5adb
commit f86e9dd04c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 530 additions and 298 deletions

View file

@ -1,41 +1,39 @@
import React, { useEffect } from 'react';
import { QueryKeys } from 'librechat-data-provider';
import { Controller, useForm } from 'react-hook-form';
import { useQueryClient } from '@tanstack/react-query';
import type {
TConversationTag,
TConversation,
TConversationTag,
TConversationTagRequest,
} from 'librechat-data-provider';
import { cn, removeFocusOutlines, defaultTextProps } from '~/utils/';
import { cn, removeFocusOutlines, defaultTextProps, logger } from '~/utils';
import { Checkbox, Label, TextareaAutosize } from '~/components/ui';
import { useBookmarkContext } from '~/Providers/BookmarkContext';
import { useConversationTagMutation } from '~/data-provider';
import { Checkbox, Label, TextareaAutosize } from '~/components/ui/';
import { useLocalize, useBookmarkSuccess } from '~/hooks';
import { NotificationSeverity } from '~/common';
import { useToastContext } from '~/Providers';
import { useLocalize } from '~/hooks';
type TBookmarkFormProps = {
tags?: string[];
bookmark?: TConversationTag;
conversation?: TConversation;
onOpenChange: React.Dispatch<React.SetStateAction<boolean>>;
formRef: React.RefObject<HTMLFormElement>;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
tags?: string[];
setTags?: (tags: string[]) => void;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
mutation: ReturnType<typeof useConversationTagMutation>;
};
const BookmarkForm = ({
bookmark,
conversation,
onOpenChange,
formRef,
setIsLoading,
tags,
setTags,
bookmark,
mutation,
conversation,
setOpen,
formRef,
}: TBookmarkFormProps) => {
const localize = useLocalize();
const queryClient = useQueryClient();
const { showToast } = useToastContext();
const { bookmarks } = useBookmarkContext();
const mutation = useConversationTagMutation(bookmark?.tag);
const onSuccess = useBookmarkSuccess(conversation?.conversationId || '');
const {
register,
@ -46,56 +44,47 @@ const BookmarkForm = ({
formState: { errors },
} = useForm<TConversationTagRequest>({
defaultValues: {
tag: bookmark?.tag || '',
description: bookmark?.description || '',
conversationId: conversation?.conversationId || '',
tag: bookmark?.tag ?? '',
description: bookmark?.description ?? '',
conversationId: conversation?.conversationId ?? '',
addToConversation: conversation ? true : false,
},
});
useEffect(() => {
if (bookmark) {
setValue('tag', bookmark.tag || '');
setValue('description', bookmark.description || '');
if (bookmark && bookmark.tag) {
setValue('tag', bookmark.tag);
setValue('description', bookmark.description ?? '');
}
}, [bookmark, setValue]);
const onSubmit = (data: TConversationTagRequest) => {
logger.log('tag_mutation', 'BookmarkForm - onSubmit: data', data);
if (mutation.isLoading) {
return;
}
if (data.tag === bookmark?.tag && data.description === bookmark?.description) {
return;
}
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;
}
setIsLoading(true);
mutation.mutate(data, {
onSuccess: () => {
showToast({
message: bookmark
? localize('com_ui_bookmarks_update_success')
: localize('com_ui_bookmarks_create_success'),
});
setIsLoading(false);
onOpenChange(false);
if (setTags && data.addToConversation) {
const newTags = [...(tags || []), data.tag].filter(
(tag) => tag !== undefined,
) as string[];
setTags(newTags);
onSuccess(newTags);
}
},
onError: () => {
showToast({
message: bookmark
? localize('com_ui_bookmarks_update_error')
: localize('com_ui_bookmarks_create_error'),
severity: NotificationSeverity.ERROR,
});
setIsLoading(false);
},
});
mutation.mutate(data);
setOpen(false);
};
return (
@ -175,10 +164,11 @@ const BookmarkForm = ({
)}
/>
<button
type="button"
aria-label={localize('com_ui_bookmarks_add_to_conversation')}
className="form-check-label text-token-text-primary w-full cursor-pointer"
className="form-check-label w-full cursor-pointer text-text-primary"
onClick={() =>
setValue('addToConversation', !getValues('addToConversation'), {
setValue('addToConversation', !(getValues('addToConversation') ?? false), {
shouldDirty: true,
})
}