Skip to content

Commit

Permalink
🚧 refactor: Attempt Default Preset Fix & Other Changes (#2342)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danny-avila authored Apr 6, 2024
1 parent 4767673 commit 334b603
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
9 changes: 7 additions & 2 deletions client/src/components/Chat/Menus/Presets/PresetItems.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Trash2 } from 'lucide-react';
import { useRecoilValue } from 'recoil';
import { Close } from '@radix-ui/react-popover';
import { Flipper, Flipped } from 'react-flip-toolkit';
Expand All @@ -13,6 +12,7 @@ import { Dialog, DialogTrigger, Label } from '~/components/ui/';
import { MenuSeparator, MenuItem } from '../UI';
import { icons } from '../Endpoints/Icons';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
import store from '~/store';

const PresetItems: FC<{
Expand Down Expand Up @@ -143,7 +143,12 @@ const PresetItems: FC<{
>
<div className="flex h-full items-center justify-end gap-1">
<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) => {
e.preventDefault();
e.stopPropagation();
Expand Down
19 changes: 13 additions & 6 deletions client/src/hooks/Conversations/usePresets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import filenamify from 'filenamify';
import exportFromJSON from 'export-from-json';
import { useCallback, useEffect, useRef } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { QueryKeys, modularEndpoints, EModelEndpoint } from 'librechat-data-provider';
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 {
useUpdatePresetMutation,
Expand All @@ -17,6 +17,7 @@ import useDefaultConvo from '~/hooks/useDefaultConvo';
import { useAuthContext } from '~/hooks/AuthContext';
import { NotificationSeverity } from '~/common';
import useLocalize from '~/hooks/useLocalize';
import useNewConvo from '~/hooks/useNewConvo';
import store from '~/store';

export default function usePresets() {
Expand All @@ -27,12 +28,18 @@ export default function usePresets() {
const { user, isAuthenticated } = useAuthContext();

const modularChat = useRecoilValue(store.modularChat);
const [_defaultPreset, setDefaultPreset] = useRecoilState(store.defaultPreset);
const setPresetModalVisible = useSetRecoilState(store.presetModalVisible);
const { preset, conversation, newConversation, setPreset } = useChatContext();
const [_defaultPreset, setDefaultPreset] = useRecoilState(store.defaultPreset);
const presetsQuery = useGetPresetsQuery({ enabled: !!user && isAuthenticated });
const { preset, conversation, index, setPreset } = useChatContext();
const { data: modelsData } = useGetModelsQuery();
const { newConversation } = useNewConvo(index);

useEffect(() => {
if (modelsData?.initial) {
return;
}

const { data: presets } = presetsQuery;
if (_defaultPreset || !presets || hasLoaded.current) {
return;
Expand All @@ -50,12 +57,12 @@ export default function usePresets() {
}
setDefaultPreset(defaultPreset);
if (!conversation?.conversationId || conversation.conversationId === 'new') {
newConversation({ preset: defaultPreset });
newConversation({ preset: defaultPreset, modelsData });
}
hasLoaded.current = true;
// dependencies are stable and only needed once
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [presetsQuery.data, user]);
}, [presetsQuery.data, user, modelsData]);

const setPresets = useCallback(
(presets: TPreset[]) => {
Expand Down
2 changes: 2 additions & 0 deletions client/src/hooks/Input/useTextarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ export default function useTextarea({
const isUndo = e.key === 'z' && (e.ctrlKey || e.metaKey);
if (isUndo && target.value.trim() === '') {
textAreaRef.current?.setRangeText('', 0, textAreaRef.current?.value?.length, 'end');
setValue('text', '', { shouldValidate: true });
forceResize(textAreaRef);
} else if (isUndo) {
trimUndoneRange(textAreaRef);
setValue('text', '', { shouldValidate: true });
forceResize(textAreaRef);
}

Expand Down
8 changes: 1 addition & 7 deletions client/src/routes/ChatRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ export default function ChatRoute() {
}
/* Creates infinite render if all dependencies included */
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
initialConvoQuery.data,
modelsQuery.data,
endpointsQuery.data,
assistants,
conversation?.model,
]);
}, [initialConvoQuery.data, endpointsQuery.data, modelsQuery.data, assistants]);

if (endpointsQuery.isLoading || modelsQuery.isLoading) {
return <Spinner className="m-auto text-black dark:text-white" />;
Expand Down

0 comments on commit 334b603

Please sign in to comment.