Skip to content

Commit 98c844b

Browse files
authored
πŸ› fix: fix model list menu not display correctly (#2133)
* ♻️ refactor: remove preference hydration * πŸ› fix: fix model-list menu not display correctly * πŸ› fix: fix azure not show the deployment name * ♻️ refactor: rename the StoreHydration
1 parent e1de8ef commit 98c844b

File tree

12 files changed

+156
-153
lines changed

12 files changed

+156
-153
lines changed

β€Žsrc/app/settings/llm/components/ProviderModelList/Option.tsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,32 @@ import { GlobalLLMProviderKey } from '@/types/settings';
1111

1212
import CustomModelOption from './CustomModelOption';
1313

14-
const OptionRender = memo<{ displayName: string; id: string; provider: GlobalLLMProviderKey }>(
15-
({ displayName, id, provider }) => {
16-
const model = useGlobalStore((s) => modelProviderSelectors.getModelCardById(id)(s), isEqual);
14+
interface OptionRenderProps {
15+
displayName: string;
16+
id: string;
17+
isAzure?: boolean;
18+
provider: GlobalLLMProviderKey;
19+
}
20+
const OptionRender = memo<OptionRenderProps>(({ displayName, id, provider, isAzure }) => {
21+
const model = useGlobalStore((s) => modelProviderSelectors.getModelCardById(id)(s), isEqual);
1722

18-
// if there is isCustom, it means it is a user defined custom model
19-
if (model?.isCustom) return <CustomModelOption id={id} provider={provider} />;
23+
// if there is isCustom, it means it is a user defined custom model
24+
if (model?.isCustom || isAzure) return <CustomModelOption id={id} provider={provider} />;
2025

21-
return (
22-
<Flexbox align={'center'} gap={8} horizontal>
23-
<ModelIcon model={id} size={32} />
24-
<Flexbox>
25-
<Flexbox align={'center'} gap={8} horizontal>
26-
{displayName}
27-
<ModelInfoTags directionReverse placement={'top'} {...model!} />
28-
</Flexbox>
29-
<Typography.Text style={{ fontSize: 12 }} type={'secondary'}>
30-
{id}
31-
</Typography.Text>
26+
return (
27+
<Flexbox align={'center'} gap={8} horizontal>
28+
<ModelIcon model={id} size={32} />
29+
<Flexbox>
30+
<Flexbox align={'center'} gap={8} horizontal>
31+
{displayName}
32+
<ModelInfoTags directionReverse placement={'top'} {...model!} />
3233
</Flexbox>
34+
<Typography.Text style={{ fontSize: 12 }} type={'secondary'}>
35+
{id}
36+
</Typography.Text>
3337
</Flexbox>
34-
);
35-
},
36-
);
38+
</Flexbox>
39+
);
40+
});
3741

3842
export default OptionRender;

β€Žsrc/app/settings/llm/components/ProviderModelList/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const ProviderModelListSelect = memo<CustomModelSelectProps>(
111111
<OptionRender
112112
displayName={label as string}
113113
id={value as string}
114+
isAzure={showAzureDeployName}
114115
provider={provider}
115116
/>
116117
);

β€Žsrc/layout/GlobalProvider/StoreHydration.tsx

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use client';
2+
3+
import { useRouter } from 'next/navigation';
4+
import { memo, useEffect } from 'react';
5+
import { createStoreUpdater } from 'zustand-utils';
6+
7+
import { useIsMobile } from '@/hooks/useIsMobile';
8+
import { useEnabledDataSync } from '@/hooks/useSyncData';
9+
import { useGlobalStore } from '@/store/global';
10+
11+
const StoreInitialization = memo(() => {
12+
const [useFetchServerConfig, useFetchUserConfig, useInitPreference] = useGlobalStore((s) => [
13+
s.useFetchServerConfig,
14+
s.useFetchUserConfig,
15+
s.useInitPreference,
16+
]);
17+
// init the system preference
18+
useInitPreference();
19+
20+
const { isLoading } = useFetchServerConfig();
21+
useFetchUserConfig(!isLoading);
22+
23+
useEnabledDataSync();
24+
25+
const useStoreUpdater = createStoreUpdater(useGlobalStore);
26+
27+
const mobile = useIsMobile();
28+
const router = useRouter();
29+
30+
useStoreUpdater('isMobile', mobile);
31+
useStoreUpdater('router', router);
32+
33+
useEffect(() => {
34+
router.prefetch('/chat');
35+
router.prefetch('/chat/settings');
36+
router.prefetch('/market');
37+
router.prefetch('/settings/common');
38+
router.prefetch('/settings/agent');
39+
router.prefetch('/settings/sync');
40+
}, [router]);
41+
42+
return null;
43+
});
44+
45+
export default StoreInitialization;

β€Žsrc/layout/GlobalProvider/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getAntdLocale } from '@/utils/locale';
1313

1414
import AppTheme from './AppTheme';
1515
import Locale from './Locale';
16-
import StoreHydration from './StoreHydration';
16+
import StoreInitialization from './StoreInitialization';
1717
import StyleRegistry from './StyleRegistry';
1818

1919
let DebugUI: FC = () => null;
@@ -50,7 +50,7 @@ const GlobalLayout = async ({ children }: GlobalLayoutProps) => {
5050
defaultNeutralColor={neutralColor?.value as any}
5151
defaultPrimaryColor={primaryColor?.value as any}
5252
>
53-
<StoreHydration />
53+
<StoreInitialization />
5454
{children}
5555
<DebugUI />
5656
</AppTheme>

β€Žsrc/store/global/hooks/useEffectAfterHydrated.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

β€Žsrc/store/global/slices/common/action.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const createCommonSlice: StateCreator<
147147
},
148148
{
149149
onSuccess: (syncEnabled) => {
150-
set({ syncEnabled });
150+
set({ syncEnabled }, false, n('useEnabledSync'));
151151
},
152152
revalidateOnFocus: false,
153153
},
@@ -165,7 +165,7 @@ export const createCommonSlice: StateCreator<
165165

166166
set({ defaultSettings, serverConfig: data }, false, n('initGlobalConfig'));
167167

168-
get().refreshDefaultModelProviderList();
168+
get().refreshDefaultModelProviderList({ trigger: 'fetchServerConfig' });
169169
}
170170
},
171171
revalidateOnFocus: false,
@@ -188,7 +188,7 @@ export const createCommonSlice: StateCreator<
188188
);
189189

190190
// when get the user config ,refresh the model provider list to the latest
191-
get().refreshModelProviderList();
191+
get().refreshDefaultModelProviderList({ trigger: 'fetchUserConfig' });
192192

193193
const { language } = settingsSelectors.currentSettings(get());
194194
if (language === 'auto') {

β€Žsrc/store/global/slices/preference/action.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { produce } from 'immer';
2+
import { SWRResponse } from 'swr';
23
import type { StateCreator } from 'zustand/vanilla';
34

5+
import { useClientDataSWR } from '@/libs/swr';
46
import type { GlobalStore } from '@/store/global';
57
import { merge } from '@/utils/merge';
68
import { setNamespace } from '@/utils/storeDebug';
79

8-
import type { GlobalPreference, GlobalPreferenceState, Guide } from './initialState';
10+
import type { GlobalPreference, Guide } from './initialState';
911

1012
const n = setNamespace('preference');
1113

@@ -18,7 +20,8 @@ export interface PreferenceAction {
1820
toggleMobileTopic: (visible?: boolean) => void;
1921
toggleSystemRole: (visible?: boolean) => void;
2022
updateGuideState: (guide: Partial<Guide>) => void;
21-
updatePreference: (preference: Partial<GlobalPreference>, action?: string) => void;
23+
updatePreference: (preference: Partial<GlobalPreference>, action?: any) => void;
24+
useInitPreference: () => SWRResponse;
2225
}
2326

2427
export const createPreferenceSlice: StateCreator<
@@ -31,7 +34,7 @@ export const createPreferenceSlice: StateCreator<
3134
const showChatSideBar =
3235
typeof newValue === 'boolean' ? newValue : !get().preference.showChatSideBar;
3336

34-
get().updatePreference({ showChatSideBar }, n('toggleAgentPanel', newValue) as string);
37+
get().updatePreference({ showChatSideBar }, n('toggleAgentPanel', newValue));
3538
},
3639
toggleExpandSessionGroup: (id, expand) => {
3740
const { preference } = get();
@@ -50,26 +53,37 @@ export const createPreferenceSlice: StateCreator<
5053
const mobileShowTopic =
5154
typeof newValue === 'boolean' ? newValue : !get().preference.mobileShowTopic;
5255

53-
get().updatePreference({ mobileShowTopic }, n('toggleMobileTopic', newValue) as string);
56+
get().updatePreference({ mobileShowTopic }, n('toggleMobileTopic', newValue));
5457
},
5558
toggleSystemRole: (newValue) => {
5659
const showSystemRole =
5760
typeof newValue === 'boolean' ? newValue : !get().preference.mobileShowTopic;
5861

59-
get().updatePreference({ showSystemRole }, n('toggleMobileTopic', newValue) as string);
62+
get().updatePreference({ showSystemRole }, n('toggleMobileTopic', newValue));
6063
},
6164
updateGuideState: (guide) => {
6265
const { updatePreference } = get();
6366
const nextGuide = merge(get().preference.guide, guide);
6467
updatePreference({ guide: nextGuide });
6568
},
6669
updatePreference: (preference, action) => {
67-
set(
68-
produce((draft: GlobalPreferenceState) => {
69-
draft.preference = merge(draft.preference, preference);
70-
}),
71-
false,
72-
action,
73-
);
70+
const nextPreference = merge(get().preference, preference);
71+
72+
set({ preference: nextPreference }, false, action || n('updatePreference'));
73+
74+
get().preferenceStorage.saveToLocalStorage(nextPreference);
7475
},
76+
77+
useInitPreference: () =>
78+
useClientDataSWR<GlobalPreference>(
79+
'preference',
80+
() => get().preferenceStorage.getFromLocalStorage(),
81+
{
82+
onSuccess: (preference) => {
83+
if (preference) {
84+
set({ preference }, false, n('initPreference'));
85+
}
86+
},
87+
},
88+
),
7589
});

β€Žsrc/store/global/slices/preference/initialState.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SessionDefaultGroup, SessionGroupId } from '@/types/session';
2+
import { AsyncLocalStorage } from '@/utils/localStorage';
23

34
export interface Guide {
45
// Topic εΌ•ε―Ό
@@ -18,6 +19,7 @@ export interface GlobalPreference {
1819
showSessionPanel?: boolean;
1920
showSystemRole?: boolean;
2021
telemetry: boolean | null;
22+
2123
/**
2224
* whether to use cmd + enter to send message
2325
*/
@@ -26,10 +28,10 @@ export interface GlobalPreference {
2628

2729
export interface GlobalPreferenceState {
2830
/**
29-
* η”¨ζˆ·εε₯½ηš„ UI ηŠΆζ€
30-
* @localStorage
31+
* the user preference, which only store in local storage
3132
*/
3233
preference: GlobalPreference;
34+
preferenceStorage: AsyncLocalStorage<GlobalPreference>;
3335
}
3436

3537
export const initialPreferenceState: GlobalPreferenceState = {
@@ -45,4 +47,5 @@ export const initialPreferenceState: GlobalPreferenceState = {
4547
telemetry: null,
4648
useCmdEnterToSend: false,
4749
},
50+
preferenceStorage: new AsyncLocalStorage('LOBE_PREFERENCE'),
4851
};

0 commit comments

Comments
Β (0)