Skip to content

Commit

Permalink
馃拕 style: add two feature flags: check_updates 銆亀elcome_suggest (#2555)
Browse files Browse the repository at this point in the history
* feat: add two feature flags: check_updates 銆亀elcome_suggest

* refactor code

* refactor code

* refactor code

* remove unused code

* fix test code

* optimize code
  • Loading branch information
coulsontl committed May 19, 2024
1 parent 54cf16a commit 84c69c9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/config/featureFlags/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('mapFeatureFlagsEnvToState', () => {
create_session: true,
edit_agent: false,
dalle: true,
check_updates: true,
welcome_suggest: true,
};

const expectedState = {
Expand All @@ -46,6 +48,8 @@ describe('mapFeatureFlagsEnvToState', () => {
showOpenAIApiKey: true,
showOpenAIProxyUrl: false,
showDalle: true,
enableCheckUpdates: true,
showWelcomeSuggest: true,
};

const mappedState = mapFeatureFlagsEnvToState(config);
Expand Down
9 changes: 9 additions & 0 deletions src/config/featureFlags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const FeatureFlagsSchema = z.object({
edit_agent: z.boolean().optional(),

dalle: z.boolean().optional(),

check_updates: z.boolean().optional(),
welcome_suggest: z.boolean().optional(),
});

// TypeScript 绫诲瀷锛屼粠 Zod schema 鐢熸垚
Expand All @@ -30,6 +33,9 @@ export const DEFAULT_FEATURE_FLAGS: IFeatureFlags = {
edit_agent: true,

dalle: true,

check_updates: true,
welcome_suggest: true,
};

export const mapFeatureFlagsEnvToState = (config: IFeatureFlags) => {
Expand All @@ -44,5 +50,8 @@ export const mapFeatureFlagsEnvToState = (config: IFeatureFlags) => {
showOpenAIProxyUrl: config.openai_proxy_url,

showDalle: config.dalle,

enableCheckUpdates: config.check_updates,
showWelcomeSuggest: config.welcome_suggest,
};
};
11 changes: 8 additions & 3 deletions src/features/Conversation/components/InboxWelcome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useTranslation } from 'react-i18next';
import { Center, Flexbox } from 'react-layout-kit';

import { useGreeting } from '@/hooks/useGreeting';
import { useServerConfigStore } from '@/store/serverConfig';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';

import AgentsSuggest from './AgentsSuggest';
import QuestionSuggest from './QuestionSuggest';
Expand Down Expand Up @@ -44,6 +44,7 @@ const InboxWelcome = memo(() => {
const { styles } = useStyles();
const mobile = useServerConfigStore((s) => s.isMobile);
const greeting = useGreeting();
const { showWelcomeSuggest } = useServerConfigStore(featureFlagsSelectors);

return (
<Center padding={16} width={'100%'}>
Expand All @@ -55,8 +56,12 @@ const InboxWelcome = memo(() => {
<Markdown className={styles.desc} variant={'chat'}>
{t('guide.defaultMessage')}
</Markdown>
<AgentsSuggest mobile={mobile} />
<QuestionSuggest mobile={mobile} />
{
showWelcomeSuggest && <>
<AgentsSuggest mobile={mobile} />
<QuestionSuggest mobile={mobile} />
</>
}
</Flexbox>
</Center>
);
Expand Down
4 changes: 3 additions & 1 deletion src/features/User/UserPanel/useNewVersion.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useGlobalStore } from '@/store/global';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';

export const useNewVersion = () => {
const [hasNewVersion, useCheckLatestVersion] = useGlobalStore((s) => [
s.hasNewVersion,
s.useCheckLatestVersion,
]);

useCheckLatestVersion();
const { enableCheckUpdates } = useServerConfigStore(featureFlagsSelectors);
useCheckLatestVersion(enableCheckUpdates);

return hasNewVersion;
};
12 changes: 8 additions & 4 deletions src/features/User/__tests__/useMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { act, renderHook } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { useUserStore } from '@/store/user';
import { ServerConfigStoreProvider } from '@/store/serverConfig';

import { useMenu } from '../UserPanel/useMenu';

const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> = ({ children }) =>
<ServerConfigStoreProvider>{children}</ServerConfigStoreProvider>

// Mock dependencies
vi.mock('next/link', () => ({
default: vi.fn(({ children }) => <div>{children}</div>),
Expand Down Expand Up @@ -69,7 +73,7 @@ describe('useMenu', () => {
enableAuth = true;
enableClerk = false;

const { result } = renderHook(() => useMenu());
const { result } = renderHook(() => useMenu(), { wrapper });

act(() => {
const { mainItems, logoutItems } = result.current;
Expand All @@ -89,7 +93,7 @@ describe('useMenu', () => {
enableAuth = true;
enableClerk = true;

const { result } = renderHook(() => useMenu());
const { result } = renderHook(() => useMenu(), { wrapper });

act(() => {
const { mainItems, logoutItems } = result.current;
Expand All @@ -108,7 +112,7 @@ describe('useMenu', () => {
});
enableAuth = false;

const { result } = renderHook(() => useMenu());
const { result } = renderHook(() => useMenu(), { wrapper });

act(() => {
const { mainItems, logoutItems } = result.current;
Expand All @@ -127,7 +131,7 @@ describe('useMenu', () => {
});
enableAuth = true;

const { result } = renderHook(() => useMenu());
const { result } = renderHook(() => useMenu(), { wrapper });

act(() => {
const { mainItems, logoutItems } = result.current;
Expand Down
6 changes: 3 additions & 3 deletions src/store/global/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface GlobalStoreAction {
toggleMobileTopic: (visible?: boolean) => void;
toggleSystemRole: (visible?: boolean) => void;
updatePreference: (preference: Partial<GlobalPreference>, action?: any) => void;
useCheckLatestVersion: () => SWRResponse<string>;
useCheckLatestVersion: (enabledCheck?: boolean) => SWRResponse<string>;
useInitGlobalPreference: () => SWRResponse;
}

Expand Down Expand Up @@ -79,8 +79,8 @@ export const globalActionSlice: StateCreator<
get().preferenceStorage.saveToLocalStorage(nextPreference);
},

useCheckLatestVersion: () =>
useSWR('checkLatestVersion', globalService.getLatestVersion, {
useCheckLatestVersion: (enabledCheck = true) =>
useSWR(enabledCheck ? 'checkLatestVersion' : null, globalService.getLatestVersion, {
// check latest version every 30 minutes
focusThrottleInterval: 1000 * 60 * 30,
onSuccess: (data: string) => {
Expand Down
2 changes: 2 additions & 0 deletions src/store/serverConfig/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('featureFlagsSelectors', () => {
showLLM: false,
showOpenAIApiKey: true,
showOpenAIProxyUrl: true,
enableCheckUpdates: true,
showWelcomeSuggest: true,
});
});
});
Expand Down

0 comments on commit 84c69c9

Please sign in to comment.