Skip to content

Commit

Permalink
♻️ refactor: refactor feature flags store to server config store (lob…
Browse files Browse the repository at this point in the history
…ehub#2263)

* 🚚 refactor: rename featureFlag store to serverConfig store

* 🚚 refactor: use serverConfigSelectors to replace commonSelectors

* 🎨 chore: clean code
  • Loading branch information
arvinxx committed Apr 28, 2024
1 parent 80d8eec commit 2e991d7
Show file tree
Hide file tree
Showing 30 changed files with 382 additions and 216 deletions.
4 changes: 2 additions & 2 deletions src/app/chat/(desktop)/features/ChatHeader/HeaderAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { DESKTOP_HEADER_ICON_SIZE } from '@/const/layoutTokens';
import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { useGlobalStore } from '@/store/global';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';

import SettingButton from '../../../features/SettingButton';
import ShareButton from '../../../features/ShareButton';
Expand All @@ -18,7 +18,7 @@ const HeaderAction = memo(() => {
s.toggleChatSideBar,
]);

const { isAgentEditable } = useFeatureFlagStore(featureFlagsSelectors);
const { isAgentEditable } = useServerConfigStore(featureFlagsSelectors);

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/(desktop)/features/SideBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { memo } from 'react';
import TopicListContent from '@/app/chat/features/TopicListContent';
import SafeSpacing from '@/components/SafeSpacing';
import { CHAT_SIDEBAR_WIDTH } from '@/const/layoutTokens';
import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { useGlobalStore } from '@/store/global';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useSessionStore } from '@/store/session';
import { sessionSelectors } from '@/store/session/selectors';

Expand Down Expand Up @@ -35,7 +35,7 @@ const Desktop = memo(() => {
s.toggleChatSideBar,
]);

const { isAgentEditable: showSystemRole } = useFeatureFlagStore(featureFlagsSelectors);
const { isAgentEditable: showSystemRole } = useServerConfigStore(featureFlagsSelectors);
const isInbox = useSessionStore(sessionSelectors.isInboxSession);

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

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

Expand All @@ -27,7 +27,7 @@ const Header = memo(() => {
const [createSession] = useSessionStore((s) => [s.createSession]);
const router = useRouter();
const avatar = useGlobalStore(commonSelectors.userAvatar);
const { showCreateSession } = useFeatureFlagStore(featureFlagsSelectors);
const { showCreateSession } = useServerConfigStore(featureFlagsSelectors);

return (
<MobileNavBar
Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/(mobile)/mobile/ChatHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MobileNavBar } from '@lobehub/ui';
import { useRouter } from 'next/navigation';
import { memo, useState } from 'react';

import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';

import SettingButton from '../../../features/SettingButton';
import ShareButton from '../../../features/ShareButton';
Expand All @@ -12,7 +12,7 @@ const MobileHeader = memo(() => {
const router = useRouter();
const [open, setOpen] = useState(false);

const { isAgentEditable } = useFeatureFlagStore(featureFlagsSelectors);
const { isAgentEditable } = useServerConfigStore(featureFlagsSelectors);

// const items: MenuProps['items'] = [
// {
Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/_layout/Desktop/SessionHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Flexbox } from 'react-layout-kit';
import { DESKTOP_HEADER_ICON_SIZE } from '@/const/layoutTokens';
import SyncStatusTag from '@/features/SyncStatusInspector';
import { useActionSWR } from '@/libs/swr';
import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useSessionStore } from '@/store/session';

import SessionSearchBar from '../../features/SessionSearchBar';
Expand All @@ -27,7 +27,7 @@ const Header = memo(() => {
const { styles } = useStyles();
const { t } = useTranslation('chat');
const [createSession] = useSessionStore((s) => [s.createSession]);
const { enableWebrtc, showCreateSession } = useFeatureFlagStore(featureFlagsSelectors);
const { enableWebrtc, showCreateSession } = useServerConfigStore(featureFlagsSelectors);

const { mutate, isValidating } = useActionSWR('session.createSession', () => createSession());

Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/features/PluginTag/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import isEqual from 'fast-deep-equal';
import { LucideToyBrick } from 'lucide-react';
import { memo } from 'react';

import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { pluginHelpers, useToolStore } from '@/store/tool';
import { toolSelectors } from '@/store/tool/selectors';

Expand All @@ -16,7 +16,7 @@ export interface PluginTagProps {
}

const PluginTag = memo<PluginTagProps>(({ plugins }) => {
const { showDalle } = useFeatureFlagStore(featureFlagsSelectors);
const { showDalle } = useServerConfigStore(featureFlagsSelectors);
const list = useToolStore(toolSelectors.metaList(showDalle), isEqual);
const displayPlugin = useToolStore(toolSelectors.getMetaById(plugins[0]), isEqual);

Expand Down
4 changes: 2 additions & 2 deletions src/app/chat/features/SessionListContent/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Center } from 'react-layout-kit';
import LazyLoad from 'react-lazy-load';

import { SESSION_CHAT_URL } from '@/const/url';
import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useSessionStore } from '@/store/session';
import { sessionSelectors } from '@/store/session/selectors';
import { LobeAgentSession } from '@/types/session';
Expand All @@ -30,7 +30,7 @@ interface SessionListProps {
const SessionList = memo<SessionListProps>(({ dataSource, groupId, showAddButton = true }) => {
const { t } = useTranslation('chat');
const isInit = useSessionStore((s) => sessionSelectors.isSessionListInit(s));
const { showCreateSession } = useFeatureFlagStore(featureFlagsSelectors);
const { showCreateSession } = useServerConfigStore(featureFlagsSelectors);
const { styles } = useStyles();

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

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

const useStyles = createStyles(({ css, token, isDarkMode }) => ({
container: css`
Expand Down Expand Up @@ -55,8 +56,9 @@ const TelemetryNotification = memo<{ mobile?: boolean }>(({ mobile }) => {
const { styles, theme, cx } = useStyles();

const { t } = useTranslation('common');
const [shouldCheck, useCheckTrace, updatePreference] = useGlobalStore((s) => [
commonSelectors.enabledTelemetryChat(s),
const shouldCheck = useServerConfigStore(serverConfigSelectors.enabledTelemetryChat);

const [useCheckTrace, updatePreference] = useGlobalStore((s) => [
s.useCheckTrace,
s.updatePreference,
]);
Expand Down
6 changes: 3 additions & 3 deletions src/app/settings/about/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import PageTitle from '@/components/PageTitle';
import { useGlobalStore } from '@/store/global';
import { commonSelectors } from '@/store/global/selectors';
import { useServerConfigStore } from '@/store/serverConfig';
import { serverConfigSelectors } from '@/store/serverConfig/selectors';

import AboutList from './AboutList';
import Analytics from './Analytics';
Expand All @@ -23,7 +23,7 @@ export default memo(() => {
const { t } = useTranslation('setting');

const { styles } = useStyles();
const enabledTelemetryChat = useGlobalStore(commonSelectors.enabledTelemetryChat);
const enabledTelemetryChat = useServerConfigStore(serverConfigSelectors.enabledTelemetryChat);

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/app/settings/features/SettingList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Link from 'next/link';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { SettingsTabs } from '@/store/global/initialState';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';

import Item from './Item';

Expand All @@ -21,7 +21,7 @@ export interface SettingListProps {

const SettingList = memo<SettingListProps>(({ activeTab, mobile }) => {
const { t } = useTranslation('setting');
const { enableWebrtc, showLLM } = useFeatureFlagStore(featureFlagsSelectors);
const { enableWebrtc, showLLM } = useServerConfigStore(featureFlagsSelectors);

const items = [
{ icon: Settings2, label: t('tab.common'), value: SettingsTabs.Common },
Expand Down
4 changes: 2 additions & 2 deletions src/app/settings/llm/OpenAI/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { OpenAI } from '@lobehub/icons';
import { memo } from 'react';

import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';

import ProviderConfig from '../components/ProviderConfig';

const OpenAIProvider = memo(() => {
const { showOpenAIProxyUrl, showOpenAIApiKey } = useFeatureFlagStore(featureFlagsSelectors);
const { showOpenAIProxyUrl, showOpenAIApiKey } = useServerConfigStore(featureFlagsSelectors);

return (
<ProviderConfig
Expand Down
4 changes: 2 additions & 2 deletions src/features/AgentSetting/AgentPlugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Center, Flexbox } from 'react-layout-kit';
import { FORM_STYLE } from '@/const/layoutTokens';
import PluginStore from '@/features/PluginStore';
import PluginTag from '@/features/PluginStore/PluginItem/PluginTag';
import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { pluginHelpers, useToolStore } from '@/store/tool';
import { toolSelectors } from '@/store/tool/selectors';

Expand All @@ -29,7 +29,7 @@ const AgentPlugin = memo(() => {
s.toggleAgentPlugin,
]);

const { showDalle } = useFeatureFlagStore(featureFlagsSelectors);
const { showDalle } = useServerConfigStore(featureFlagsSelectors);
const installedPlugins = useToolStore(toolSelectors.metaList(showDalle), isEqual);
const useFetchInstalledPlugins = useToolStore((s) => s.useFetchInstalledPlugins);

Expand Down
4 changes: 2 additions & 2 deletions src/features/ChatInput/ActionBar/Tools/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { Flexbox } from 'react-layout-kit';
import PluginStore from '@/features/PluginStore';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { featureFlagsSelectors, useFeatureFlagStore } from '@/store/featureFlags';
import { useGlobalStore } from '@/store/global';
import { modelProviderSelectors } from '@/store/global/selectors';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { pluginHelpers, useToolStore } from '@/store/tool';
import { builtinToolSelectors, pluginSelectors } from '@/store/tool/selectors';

Expand All @@ -35,7 +35,7 @@ const useStyles = createStyles(({ css, prefixCls }) => ({
const Tools = memo(() => {
const { t } = useTranslation('setting');
const list = useToolStore(pluginSelectors.installedPluginMetaList, isEqual);
const { showDalle } = useFeatureFlagStore(featureFlagsSelectors);
const { showDalle } = useServerConfigStore(featureFlagsSelectors);
const builtinList = useToolStore(builtinToolSelectors.metaList(showDalle), isEqual);

const enablePluginCount = useAgentStore(
Expand Down
6 changes: 3 additions & 3 deletions src/features/Conversation/Error/InvalidAccessCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { useGlobalStore } from '@/store/global';
import { commonSelectors } from '@/store/global/selectors';
import { useServerConfigStore } from '@/store/serverConfig';
import { serverConfigSelectors } from '@/store/serverConfig/selectors';

import APIKeyForm from './APIKeyForm';
import AccessCodeForm from './AccessCodeForm';
Expand All @@ -27,7 +27,7 @@ interface InvalidAccessCodeProps {

const InvalidAccessCode = memo<InvalidAccessCodeProps>(({ id, provider }) => {
const { t } = useTranslation('error');
const isEnabledOAuth = useGlobalStore(commonSelectors.enabledOAuthSSO);
const isEnabledOAuth = useServerConfigStore(serverConfigSelectors.enabledOAuthSSO);
const defaultTab = isEnabledOAuth ? Tab.Oauth : Tab.Password;
const [mode, setMode] = useState<Tab>(defaultTab);

Expand Down
8 changes: 5 additions & 3 deletions src/layout/GlobalProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
LOBE_THEME_NEUTRAL_COLOR,
LOBE_THEME_PRIMARY_COLOR,
} from '@/const/theme';
import { FeatureFlagStoreProvider } from '@/store/featureFlags';
import { getServerGlobalConfig } from '@/server/globalConfig';
import { ServerConfigStoreProvider } from '@/store/serverConfig';
import { getAntdLocale } from '@/utils/locale';

import AppTheme from './AppTheme';
Expand Down Expand Up @@ -46,6 +47,7 @@ const GlobalLayout = async ({ children }: GlobalLayoutProps) => {

// get default feature flags to use with ssr
const serverFeatureFlags = getServerFeatureFlagsValue();
const serverConfig = getServerGlobalConfig();
return (
<StyleRegistry>
<Locale antdLocale={antdLocale} defaultLang={defaultLang?.value}>
Expand All @@ -55,9 +57,9 @@ const GlobalLayout = async ({ children }: GlobalLayoutProps) => {
defaultPrimaryColor={primaryColor?.value as any}
>
<StoreInitialization />
<FeatureFlagStoreProvider featureFlags={serverFeatureFlags}>
<ServerConfigStoreProvider featureFlags={serverFeatureFlags} serverConfig={serverConfig}>
{children}
</FeatureFlagStoreProvider>
</ServerConfigStoreProvider>
<DebugUI />
</AppTheme>
</Locale>
Expand Down
Loading

0 comments on commit 2e991d7

Please sign in to comment.