From 6ddb893b77526fa31c34b4e7012f042d5b661dc9 Mon Sep 17 00:00:00 2001 From: aliang <1098486429@qq.com> Date: Fri, 8 Nov 2024 01:39:13 +0700 Subject: [PATCH] fix(ui): initialize the persisted model name properly (#3385) --- ee/tabby-ui/app/(home)/page.tsx | 2 +- ee/tabby-ui/lib/hooks/use-models.tsx | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/ee/tabby-ui/app/(home)/page.tsx b/ee/tabby-ui/app/(home)/page.tsx index d735cc7cbcfd..7b7bc3a24353 100644 --- a/ee/tabby-ui/app/(home)/page.tsx +++ b/ee/tabby-ui/app/(home)/page.tsx @@ -5,13 +5,13 @@ import Image from 'next/image' import { useRouter } from 'next/navigation' import tabbyUrl from '@/assets/logo-dark.png' import { useQuery } from 'urql' +import { useStore } from 'zustand' import { SESSION_STORAGE_KEY } from '@/lib/constants' import { useHealth } from '@/lib/hooks/use-health' import { useMe } from '@/lib/hooks/use-me' import { useSelectedModel } from '@/lib/hooks/use-models' import { useIsChatEnabled } from '@/lib/hooks/use-server-info' -import { useStore } from '@/lib/hooks/use-store' import { updateSelectedModel } from '@/lib/stores/chat-actions' import { clearHomeScrollPosition, diff --git a/ee/tabby-ui/lib/hooks/use-models.tsx b/ee/tabby-ui/lib/hooks/use-models.tsx index 47c0ee7593a6..a2a02e5dc5e9 100644 --- a/ee/tabby-ui/lib/hooks/use-models.tsx +++ b/ee/tabby-ui/lib/hooks/use-models.tsx @@ -3,12 +3,12 @@ import { useEffect } from 'react' import { Maybe } from 'graphql/jsutils/Maybe' import useSWR, { SWRResponse } from 'swr' +import { useStore } from 'zustand' import fetcher from '@/lib/tabby/fetcher' import { updateSelectedModel } from '../stores/chat-actions' import { useChatStore } from '../stores/chat-store' -import { useStore } from './use-store' export interface ModelInfo { completion: Maybe> @@ -33,24 +33,20 @@ export function useModels(): SWRResponse { export function useSelectedModel() { const { data: modelData, isLoading: isFetchingModel } = useModels() - const isModelHydrated = useStore(useChatStore, state => state._hasHydrated) const selectedModel = useStore(useChatStore, state => state.selectedModel) - // once model hydrated, try to init model useEffect(() => { - if (isModelHydrated && !isFetchingModel) { - // check if current model is valid + if (!isFetchingModel) { + // init model const validModel = getModelFromModelInfo(selectedModel, modelData?.chat) - if (selectedModel !== validModel) { - updateSelectedModel(validModel) - } + updateSelectedModel(validModel) } - }, [isModelHydrated, isFetchingModel]) + }, [isFetchingModel]) return { // fetching model data or trying to get selected model from localstorage - isModelLoading: isFetchingModel || !isModelHydrated, + isModelLoading: isFetchingModel, selectedModel, models: modelData?.chat }