mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 09:50:15 +01:00
🚧 refactor: Attempt Default Preset Fix & Other Changes (#2342)
* fix(useTextarea): trigger SendButton re-render on undo and clearing text * refactor(PresetItems): show pin icon for default preset * fix(ChatRoute): do not use conversation.model for useEffect, do not set default Preset if real model list is not yet fetched
This commit is contained in:
parent
476767355b
commit
334b603247
4 changed files with 23 additions and 15 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import { Trash2 } from 'lucide-react';
|
|
||||||
import { useRecoilValue } from 'recoil';
|
import { useRecoilValue } from 'recoil';
|
||||||
import { Close } from '@radix-ui/react-popover';
|
import { Close } from '@radix-ui/react-popover';
|
||||||
import { Flipper, Flipped } from 'react-flip-toolkit';
|
import { Flipper, Flipped } from 'react-flip-toolkit';
|
||||||
|
|
@ -13,6 +12,7 @@ import { Dialog, DialogTrigger, Label } from '~/components/ui/';
|
||||||
import { MenuSeparator, MenuItem } from '../UI';
|
import { MenuSeparator, MenuItem } from '../UI';
|
||||||
import { icons } from '../Endpoints/Icons';
|
import { icons } from '../Endpoints/Icons';
|
||||||
import { useLocalize } from '~/hooks';
|
import { useLocalize } from '~/hooks';
|
||||||
|
import { cn } from '~/utils';
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
const PresetItems: FC<{
|
const PresetItems: FC<{
|
||||||
|
|
@ -143,7 +143,12 @@ const PresetItems: FC<{
|
||||||
>
|
>
|
||||||
<div className="flex h-full items-center justify-end gap-1">
|
<div className="flex h-full items-center justify-end gap-1">
|
||||||
<button
|
<button
|
||||||
className="m-0 h-full rounded-md p-2 text-gray-400 hover:text-gray-700 dark:bg-gray-600 dark:text-gray-400 dark:hover:text-gray-200 sm:invisible sm:group-hover:visible"
|
className={cn(
|
||||||
|
'm-0 h-full rounded-md bg-transparent p-2 text-gray-400 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200',
|
||||||
|
defaultPreset?.presetId === preset.presetId
|
||||||
|
? ''
|
||||||
|
: 'sm:invisible sm:group-hover:visible',
|
||||||
|
)}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ import filenamify from 'filenamify';
|
||||||
import exportFromJSON from 'export-from-json';
|
import exportFromJSON from 'export-from-json';
|
||||||
import { useCallback, useEffect, useRef } from 'react';
|
import { useCallback, useEffect, useRef } from 'react';
|
||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
import { QueryKeys, modularEndpoints, EModelEndpoint } from 'librechat-data-provider';
|
|
||||||
import { useRecoilState, useSetRecoilState, useRecoilValue } from 'recoil';
|
import { useRecoilState, useSetRecoilState, useRecoilValue } from 'recoil';
|
||||||
import { useCreatePresetMutation } from 'librechat-data-provider/react-query';
|
import { QueryKeys, modularEndpoints, EModelEndpoint } from 'librechat-data-provider';
|
||||||
|
import { useCreatePresetMutation, useGetModelsQuery } from 'librechat-data-provider/react-query';
|
||||||
import type { TPreset, TEndpointsConfig } from 'librechat-data-provider';
|
import type { TPreset, TEndpointsConfig } from 'librechat-data-provider';
|
||||||
import {
|
import {
|
||||||
useUpdatePresetMutation,
|
useUpdatePresetMutation,
|
||||||
|
|
@ -17,6 +17,7 @@ import useDefaultConvo from '~/hooks/useDefaultConvo';
|
||||||
import { useAuthContext } from '~/hooks/AuthContext';
|
import { useAuthContext } from '~/hooks/AuthContext';
|
||||||
import { NotificationSeverity } from '~/common';
|
import { NotificationSeverity } from '~/common';
|
||||||
import useLocalize from '~/hooks/useLocalize';
|
import useLocalize from '~/hooks/useLocalize';
|
||||||
|
import useNewConvo from '~/hooks/useNewConvo';
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
export default function usePresets() {
|
export default function usePresets() {
|
||||||
|
|
@ -27,12 +28,18 @@ export default function usePresets() {
|
||||||
const { user, isAuthenticated } = useAuthContext();
|
const { user, isAuthenticated } = useAuthContext();
|
||||||
|
|
||||||
const modularChat = useRecoilValue(store.modularChat);
|
const modularChat = useRecoilValue(store.modularChat);
|
||||||
const [_defaultPreset, setDefaultPreset] = useRecoilState(store.defaultPreset);
|
|
||||||
const setPresetModalVisible = useSetRecoilState(store.presetModalVisible);
|
const setPresetModalVisible = useSetRecoilState(store.presetModalVisible);
|
||||||
const { preset, conversation, newConversation, setPreset } = useChatContext();
|
const [_defaultPreset, setDefaultPreset] = useRecoilState(store.defaultPreset);
|
||||||
const presetsQuery = useGetPresetsQuery({ enabled: !!user && isAuthenticated });
|
const presetsQuery = useGetPresetsQuery({ enabled: !!user && isAuthenticated });
|
||||||
|
const { preset, conversation, index, setPreset } = useChatContext();
|
||||||
|
const { data: modelsData } = useGetModelsQuery();
|
||||||
|
const { newConversation } = useNewConvo(index);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (modelsData?.initial) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const { data: presets } = presetsQuery;
|
const { data: presets } = presetsQuery;
|
||||||
if (_defaultPreset || !presets || hasLoaded.current) {
|
if (_defaultPreset || !presets || hasLoaded.current) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -50,12 +57,12 @@ export default function usePresets() {
|
||||||
}
|
}
|
||||||
setDefaultPreset(defaultPreset);
|
setDefaultPreset(defaultPreset);
|
||||||
if (!conversation?.conversationId || conversation.conversationId === 'new') {
|
if (!conversation?.conversationId || conversation.conversationId === 'new') {
|
||||||
newConversation({ preset: defaultPreset });
|
newConversation({ preset: defaultPreset, modelsData });
|
||||||
}
|
}
|
||||||
hasLoaded.current = true;
|
hasLoaded.current = true;
|
||||||
// dependencies are stable and only needed once
|
// dependencies are stable and only needed once
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [presetsQuery.data, user]);
|
}, [presetsQuery.data, user, modelsData]);
|
||||||
|
|
||||||
const setPresets = useCallback(
|
const setPresets = useCallback(
|
||||||
(presets: TPreset[]) => {
|
(presets: TPreset[]) => {
|
||||||
|
|
|
||||||
|
|
@ -160,9 +160,11 @@ export default function useTextarea({
|
||||||
const isUndo = e.key === 'z' && (e.ctrlKey || e.metaKey);
|
const isUndo = e.key === 'z' && (e.ctrlKey || e.metaKey);
|
||||||
if (isUndo && target.value.trim() === '') {
|
if (isUndo && target.value.trim() === '') {
|
||||||
textAreaRef.current?.setRangeText('', 0, textAreaRef.current?.value?.length, 'end');
|
textAreaRef.current?.setRangeText('', 0, textAreaRef.current?.value?.length, 'end');
|
||||||
|
setValue('text', '', { shouldValidate: true });
|
||||||
forceResize(textAreaRef);
|
forceResize(textAreaRef);
|
||||||
} else if (isUndo) {
|
} else if (isUndo) {
|
||||||
trimUndoneRange(textAreaRef);
|
trimUndoneRange(textAreaRef);
|
||||||
|
setValue('text', '', { shouldValidate: true });
|
||||||
forceResize(textAreaRef);
|
forceResize(textAreaRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,13 +90,7 @@ export default function ChatRoute() {
|
||||||
}
|
}
|
||||||
/* Creates infinite render if all dependencies included */
|
/* Creates infinite render if all dependencies included */
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [
|
}, [initialConvoQuery.data, endpointsQuery.data, modelsQuery.data, assistants]);
|
||||||
initialConvoQuery.data,
|
|
||||||
modelsQuery.data,
|
|
||||||
endpointsQuery.data,
|
|
||||||
assistants,
|
|
||||||
conversation?.model,
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (endpointsQuery.isLoading || modelsQuery.isLoading) {
|
if (endpointsQuery.isLoading || modelsQuery.isLoading) {
|
||||||
return <Spinner className="m-auto text-black dark:text-white" />;
|
return <Spinner className="m-auto text-black dark:text-white" />;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue