Skip to content

Commit

Permalink
♻️ refactor: rename globalStore to userStore (lobehub#2264)
Browse files Browse the repository at this point in the history
* ♻️ refactor: rename globalStore to userStore

* ♻️ refactor: refactor the globalStore usage

* ♻️ refactor: clean the user preference

* 🐛 fix: fix preference init

* ♻️ refactor: refactor the global Store

* 🎨 chore: improve code
  • Loading branch information
arvinxx authored and pull[bot] committed Jun 14, 2024
1 parent 90d5e01 commit 1911738
Show file tree
Hide file tree
Showing 129 changed files with 820 additions and 686 deletions.
6 changes: 3 additions & 3 deletions src/app/chat/(desktop)/features/ChatHeader/Tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import ModelTag from '@/components/ModelTag';
import ModelSwitchPanel from '@/features/ModelSwitchPanel';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useGlobalStore } from '@/store/global';
import { modelProviderSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { modelProviderSelectors } from '@/store/user/selectors';

import PluginTag from '../../../features/PluginTag';

Expand All @@ -16,7 +16,7 @@ const TitleTags = memo(() => {
agentSelectors.currentAgentPlugins(s),
]);

const showPlugin = useGlobalStore(modelProviderSelectors.isModelEnabledFunctionCall(model));
const showPlugin = useUserStore(modelProviderSelectors.isModelEnabledFunctionCall(model));

return (
<Flexbox gap={8} horizontal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Center, Flexbox } from 'react-layout-kit';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useFileStore } from '@/store/file';
import { useGlobalStore } from '@/store/global';
import { modelProviderSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { modelProviderSelectors } from '@/store/user/selectors';

const useStyles = createStyles(({ css, token, stylish }) => {
return {
Expand Down Expand Up @@ -77,7 +77,7 @@ const DragUpload = memo(() => {

const model = useAgentStore(agentSelectors.currentAgentModel);

const enabledFiles = useGlobalStore(modelProviderSelectors.isModelEnabledFiles(model));
const enabledFiles = useUserStore(modelProviderSelectors.isModelEnabledFiles(model));

const uploadImages = async (fileList: FileList | undefined) => {
if (!fileList || fileList.length === 0) return;
Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/(desktop)/features/ChatInput/Footer/SendMore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import HotKeys from '@/components/HotKeys';
import { ALT_KEY } from '@/const/hotkeys';
import { useSendMessage } from '@/features/ChatInput/useSend';
import { useChatStore } from '@/store/chat';
import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { preferenceSelectors } from '@/store/user/selectors';
import { isMacOS } from '@/utils/platform';

const useStyles = createStyles(({ css, prefixCls }) => {
Expand All @@ -32,7 +32,7 @@ const SendMore = memo(() => {

const { styles } = useStyles();

const [useCmdEnterToSend, updatePreference] = useGlobalStore((s) => [
const [useCmdEnterToSend, updatePreference] = useUserStore((s) => [
preferenceSelectors.useCmdEnterToSend(s),
s.updatePreference,
]);
Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/(desktop)/features/ChatInput/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { useSendMessage } from '@/features/ChatInput/useSend';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/slices/chat';
import { useChatStore } from '@/store/chat';
import { useGlobalStore } from '@/store/global';
import { modelProviderSelectors, preferenceSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { modelProviderSelectors, preferenceSelectors } from '@/store/user/selectors';
import { isMacOS } from '@/utils/platform';

import DragUpload from './DragUpload';
Expand Down Expand Up @@ -66,7 +66,7 @@ const Footer = memo<FooterProps>(({ setExpand }) => {

const model = useAgentStore(agentSelectors.currentAgentModel);

const [useCmdEnterToSend, canUpload] = useGlobalStore((s) => [
const [useCmdEnterToSend, canUpload] = useUserStore((s) => [
preferenceSelectors.useCmdEnterToSend(s),
modelProviderSelectors.isModelEnabledUpload(model)(s),
]);
Expand Down
10 changes: 5 additions & 5 deletions src/app/chat/(desktop)/features/ChatInput/TextArea.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { useChatStore } from '@/store/chat';
import { useGlobalStore } from '@/store/global';
import { useUserStore } from '@/store/user';

import InputArea from './TextArea';

Expand Down Expand Up @@ -232,7 +232,7 @@ describe('<InputArea />', () => {
inputMessage: '123',
sendMessage: sendMessageMock,
});
useGlobalStore.getState().updatePreference({ useCmdEnterToSend: true });
useUserStore.getState().updatePreference({ useCmdEnterToSend: true });
});

render(<InputArea setExpand={setExpandMock} />);
Expand All @@ -252,7 +252,7 @@ describe('<InputArea />', () => {
sendMessage: sendMessageMock,
updateInputMessage: updateInputMessageMock,
});
useGlobalStore.getState().updatePreference({ useCmdEnterToSend: false });
useUserStore.getState().updatePreference({ useCmdEnterToSend: false });
});

render(<InputArea setExpand={setExpandMock} />);
Expand All @@ -275,7 +275,7 @@ describe('<InputArea />', () => {
inputMessage: '123',
sendMessage: sendMessageMock,
});
useGlobalStore.getState().updatePreference({ useCmdEnterToSend: true });
useUserStore.getState().updatePreference({ useCmdEnterToSend: true });
});

render(<InputArea setExpand={setExpandMock} />);
Expand All @@ -300,7 +300,7 @@ describe('<InputArea />', () => {
sendMessage: sendMessageMock,
updateInputMessage: updateInputMessageMock,
});
useGlobalStore.getState().updatePreference({ useCmdEnterToSend: false });
useUserStore.getState().updatePreference({ useCmdEnterToSend: false });
});

render(<InputArea setExpand={setExpandMock} />);
Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/(desktop)/features/ChatInput/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useTranslation } from 'react-i18next';

import { useSendMessage } from '@/features/ChatInput/useSend';
import { useChatStore } from '@/store/chat';
import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { preferenceSelectors } from '@/store/user/selectors';
import { isCommandPressed } from '@/utils/keyboard';

import { useAutoFocus } from './useAutoFocus';
Expand Down Expand Up @@ -47,7 +47,7 @@ const InputArea = memo<InputAreaProps>(({ setExpand }) => {
s.updateInputMessage,
]);

const useCmdEnterToSend = useGlobalStore(preferenceSelectors.useCmdEnterToSend);
const useCmdEnterToSend = useUserStore(preferenceSelectors.useCmdEnterToSend);

const sendMessage = useSendMessage();

Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/(mobile)/features/SessionHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { Flexbox } from 'react-layout-kit';

import { MOBILE_HEADER_ICON_SIZE } from '@/const/layoutTokens';
import SyncStatusInspector from '@/features/SyncStatusInspector';
import { useGlobalStore } from '@/store/global';
import { commonSelectors } from '@/store/global/selectors';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useSessionStore } from '@/store/session';
import { useUserStore } from '@/store/user';
import { commonSelectors } from '@/store/user/selectors';
import { mobileHeaderSticky } from '@/styles/mobileHeader';

export const useStyles = createStyles(({ css, token }) => ({
Expand All @@ -26,7 +26,7 @@ export const useStyles = createStyles(({ css, token }) => ({
const Header = memo(() => {
const [createSession] = useSessionStore((s) => [s.createSession]);
const router = useRouter();
const avatar = useGlobalStore(commonSelectors.userAvatar);
const avatar = useUserStore(commonSelectors.userAvatar);
const { showCreateSession } = useServerConfigStore(featureFlagsSelectors);

return (
Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/features/ShareButton/ShareModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Flexbox } from 'react-layout-kit';

import { FORM_STYLE } from '@/const/layoutTokens';
import { useChatStore } from '@/store/chat';
import { useGlobalStore } from '@/store/global';
import { commonSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { commonSelectors } from '@/store/user/selectors';

import Preview from './Preview';
import { FieldType, ImageType } from './type';
Expand Down Expand Up @@ -49,7 +49,7 @@ const ShareModal = memo<ModalProps>(({ onCancel, open }) => {
const [fieldValue, setFieldValue] = useState<FieldType>(DEFAULT_FIELD_VALUE);
const [tab, setTab] = useState<Tab>(Tab.Screenshot);
const { t } = useTranslation('chat');
const avatar = useGlobalStore(commonSelectors.userAvatar);
const avatar = useUserStore(commonSelectors.userAvatar);
const [shareLoading, shareToShareGPT] = useChatStore((s) => [s.shareLoading, s.shareToShareGPT]);
const { loading, onDownload, title } = useScreenshot(fieldValue.imageType);

Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/features/TelemetryNotification/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { PRIVACY_URL } from '@/const/url';
import { useGlobalStore } from '@/store/global';
import { useServerConfigStore } from '@/store/serverConfig';
import { serverConfigSelectors } from '@/store/serverConfig/selectors';
import { useUserStore } from '@/store/user';

const useStyles = createStyles(({ css, token, isDarkMode }) => ({
container: css`
Expand Down Expand Up @@ -58,7 +58,7 @@ const TelemetryNotification = memo<{ mobile?: boolean }>(({ mobile }) => {
const { t } = useTranslation('common');
const shouldCheck = useServerConfigStore(serverConfigSelectors.enabledTelemetryChat);

const [useCheckTrace, updatePreference] = useGlobalStore((s) => [
const [useCheckTrace, updatePreference] = useUserStore((s) => [
s.useCheckTrace,
s.updatePreference,
]);
Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/features/TopicListContent/Topic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Virtuoso, VirtuosoHandle } from 'react-virtuoso';
import { imageUrl } from '@/const/url';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
import { useGlobalStore } from '@/store/global';
import { useUserStore } from '@/store/user';
import { ChatTopic } from '@/types/topic';

import { Placeholder, SkeletonList } from './SkeletonList';
Expand All @@ -30,7 +30,7 @@ export const Topic = memo(() => {
s.activeTopicId,
topicSelectors.currentTopicLength(s),
]);
const [visible, updateGuideState] = useGlobalStore((s) => [
const [visible, updateGuideState] = useUserStore((s) => [
s.preference.guide?.topic,
s.updateGuideState,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import { AGENTS_INDEX_GITHUB_ISSUE } from '@/const/url';
import AgentInfo from '@/features/AgentInfo';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useSessionStore } from '@/store/session';
import { sessionMetaSelectors } from '@/store/session/selectors';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';

const SubmitAgentModal = memo<ModalProps>(({ open, onCancel }) => {
const { t } = useTranslation('setting');
const [identifier, setIdentifier] = useState('');
const systemRole = useAgentStore(agentSelectors.currentAgentSystemRole);
const theme = useTheme();
const meta = useSessionStore(sessionMetaSelectors.currentAgentMeta, isEqual);
const language = useGlobalStore((s) => settingsSelectors.currentSettings(s).language);
const language = useUserStore((s) => settingsSelectors.currentSettings(s).language);

const isMetaPass = Boolean(
meta && meta.title && meta.description && (meta.tags as string[])?.length > 0 && meta.avatar,
Expand Down
6 changes: 3 additions & 3 deletions src/app/settings/(mobile)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { Center, Flexbox } from 'react-layout-kit';

import { CURRENT_VERSION } from '@/const/version';
import AvatarWithUpload from '@/features/AvatarWithUpload';
import { useGlobalStore } from '@/store/global';
import { commonSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { commonSelectors } from '@/store/user/selectors';

import SettingList from '../features/SettingList';
import AvatarBanner from './features/AvatarBanner';
Expand All @@ -26,7 +26,7 @@ const useStyles = createStyles(({ css, token }) => ({
}));

const Setting = memo(() => {
const avatar = useGlobalStore(commonSelectors.userAvatar);
const avatar = useUserStore(commonSelectors.userAvatar);
const { styles } = useStyles();

return (
Expand Down
8 changes: 4 additions & 4 deletions src/app/settings/about/Analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { useStyles } from '@/app/settings/about/style';
import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { preferenceSelectors } from '@/store/user/selectors';

const Analytics = memo(() => {
const { t } = useTranslation('setting');
const { styles } = useStyles();
const checked = useGlobalStore(preferenceSelectors.userAllowTrace);
const [updatePreference] = useGlobalStore((s) => [s.updatePreference]);
const checked = useUserStore(preferenceSelectors.userAllowTrace);
const [updatePreference] = useUserStore((s) => [s.updatePreference]);

return (
<div className={styles.wrapper}>
Expand Down
8 changes: 4 additions & 4 deletions src/app/settings/agent/Agent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { INBOX_SESSION_ID } from '@/const/session';
import AgentSetting from '@/features/AgentSetting';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';

const Agent = memo(() => {
const config = useAgentStore(agentSelectors.defaultAgentConfig, isEqual);
const meta = useGlobalStore(settingsSelectors.defaultAgentMeta, isEqual);
const [updateAgent] = useGlobalStore((s) => [s.updateDefaultAgent]);
const meta = useUserStore(settingsSelectors.defaultAgentMeta, isEqual);
const [updateAgent] = useUserStore((s) => [s.updateDefaultAgent]);

return (
<AgentSetting
Expand Down
8 changes: 4 additions & 4 deletions src/app/settings/common/Common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { DEFAULT_SETTINGS } from '@/const/settings';
import { useOAuthSession } from '@/hooks/useOAuthSession';
import { useChatStore } from '@/store/chat';
import { useFileStore } from '@/store/file';
import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useSessionStore } from '@/store/session';
import { useToolStore } from '@/store/tool';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';

type SettingItemGroup = ItemGroup;

Expand All @@ -40,8 +40,8 @@ const Common = memo<SettingsCommonProps>(({ showAccessCodeConfig, showOAuthLogin
]);
const [removeAllFiles] = useFileStore((s) => [s.removeAllFiles]);
const removeAllPlugins = useToolStore((s) => s.removeAllPlugins);
const settings = useGlobalStore(settingsSelectors.currentSettings, isEqual);
const [setSettings, resetSettings] = useGlobalStore((s) => [s.setSettings, s.resetSettings]);
const settings = useUserStore(settingsSelectors.currentSettings, isEqual);
const [setSettings, resetSettings] = useUserStore((s) => [s.setSettings, s.resetSettings]);

const { message, modal } = App.useApp();

Expand Down
8 changes: 4 additions & 4 deletions src/app/settings/common/Theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { FORM_STYLE } from '@/const/layoutTokens';
import { imageUrl } from '@/const/url';
import AvatarWithUpload from '@/features/AvatarWithUpload';
import { localeOptions } from '@/locales/resources';
import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';
import { switchLang } from '@/utils/client/switchLang';

import { ThemeSwatchesNeutral, ThemeSwatchesPrimary } from '../features/ThemeSwatches';
Expand All @@ -22,8 +22,8 @@ const Theme = memo(() => {
const { t } = useTranslation('setting');
const [form] = AntForm.useForm();

const settings = useGlobalStore(settingsSelectors.currentSettings, isEqual);
const [setThemeMode, setSettings] = useGlobalStore((s) => [s.switchThemeMode, s.setSettings]);
const settings = useUserStore(settingsSelectors.currentSettings, isEqual);
const [setThemeMode, setSettings] = useUserStore((s) => [s.switchThemeMode, s.setSettings]);

useSyncSettings(form);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
} from '@lobehub/ui';
import { memo } from 'react';

import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';

const ThemeSwatchesNeutral = memo(() => {
const [neutralColor, setSettings] = useGlobalStore((s) => [
const [neutralColor, setSettings] = useUserStore((s) => [
settingsSelectors.currentSettings(s).neutralColor,
s.setSettings,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
} from '@lobehub/ui';
import { memo } from 'react';

import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';

const ThemeSwatchesPrimary = memo(() => {
const [primaryColor, setSettings] = useGlobalStore((s) => [
const [primaryColor, setSettings] = useUserStore((s) => [
settingsSelectors.currentSettings(s).primaryColor,
s.setSettings,
]);
Expand Down
6 changes: 3 additions & 3 deletions src/app/settings/hooks/useSyncSettings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FormInstance } from 'antd/es/form/hooks/useForm';
import { useEffect } from 'react';

import { useGlobalStore } from '@/store/global';
import { useUserStore } from '@/store/user';

export const useSyncSettings = (form: FormInstance) => {
useEffect(() => {
// set the first time
form.setFieldsValue(useGlobalStore.getState().settings);
form.setFieldsValue(useUserStore.getState().settings);

// sync with later updated settings
const unsubscribe = useGlobalStore.subscribe(
const unsubscribe = useUserStore.subscribe(
(s) => s.settings,
(settings) => {
form.setFieldsValue(settings);
Expand Down
Loading

0 comments on commit 1911738

Please sign in to comment.