Skip to content

Commit

Permalink
💄 style: Add feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Dec 25, 2024
1 parent 5a81e0d commit c68cef4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ vi.mock('@/const/auth', () => ({
},
}));

afterEach(() => {
enableAuth = true;
enableClerk = false;
});

// 目前对 enableAuth 的判定是在 useUserStore 中,所以需要 mock useUserStore
// 类型定义: enableAuth: () => boolean
describe('useCategory', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/app/(main)/(mobile)/me/(home)/features/useCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CellProps } from '@/components/Cell';
import { DOCUMENTS, FEEDBACK } from '@/const/url';
import { isServerMode } from '@/const/version';
import { usePWAInstall } from '@/hooks/usePWAInstall';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useUserStore } from '@/store/user';
import { authSelectors } from '@/store/user/slices/auth/selectors';

Expand All @@ -24,6 +25,7 @@ export const useCategory = () => {
const router = useRouter();
const { canInstall, install } = usePWAInstall();
const { t } = useTranslation(['common', 'setting', 'auth']);
const { hideDocs } = useServerConfigStore(featureFlagsSelectors);
const [isLogin, isLoginWithAuth, isLoginWithClerk, enableAuth, signOut, isLoginWithNextAuth] =
useUserStore((s) => [
authSelectors.isLogin(s),
Expand Down Expand Up @@ -131,7 +133,7 @@ export const useCategory = () => {
/* ↑ cloud slot ↑ */
...(canInstall ? pwa : []),
...(isLogin && !isServerMode ? data : []),
...helps,
...(!hideDocs ? helps : []),
...(enableAuth && isLoginWithNextAuth ? nextAuthSignOut : []),
].filter(Boolean) as CellProps[];

Expand Down
4 changes: 2 additions & 2 deletions src/app/(main)/changelog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const generateMetadata = async () => {
};

const Page = async () => {
const hideChangelog = serverFeatureFlags().hideChangelog;
const hideDocs = serverFeatureFlags().hideDocs;

if (hideChangelog) return notFound();
if (hideDocs) return notFound();

const mobile = await isMobileDevice();
const { t, locale } = await translation('metadata');
Expand Down
4 changes: 2 additions & 2 deletions src/app/(main)/chat/(workspace)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const generateMetadata = async () => {
};

const Page = async () => {
const hideChangelog = serverFeatureFlags().hideChangelog;
const hideDocs = serverFeatureFlags().hideDocs;
const mobile = await isMobileDevice();
const { t } = await translation('metadata');
const ld = ldModule.generate({
Expand All @@ -35,7 +35,7 @@ const Page = async () => {
<StructuredData ld={ld} />
<PageTitle />
<TelemetryNotification mobile={mobile} />
{!hideChangelog && (
{!hideDocs && (
<ChangelogModal currentId={await new ChangelogService().getLatestChangelogId()} />
)}
</>
Expand Down
4 changes: 2 additions & 2 deletions src/app/@modal/(.)changelog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import UpdateChangelogStatus from './features/UpdateChangelogStatus';
import Loading from './loading';

const Page = async () => {
const hideChangelog = serverFeatureFlags().hideChangelog;
const hideDocs = serverFeatureFlags().hideDocs;

if (hideChangelog) return notFound();
if (hideDocs) return notFound();

const locale = await getLocale();
const mobile = await isMobileDevice();
Expand Down
3 changes: 0 additions & 3 deletions src/config/featureFlags/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const FeatureFlagsSchema = z.object({
// please contact us for more information: [email protected]
commercial_hide_github: z.boolean().optional(),
commercial_hide_docs: z.boolean().optional(),
commercial_hide_changelog: z.boolean().optional(),
});

export type IFeatureFlags = z.infer<typeof FeatureFlagsSchema>;
Expand Down Expand Up @@ -79,7 +78,6 @@ export const DEFAULT_FEATURE_FLAGS: IFeatureFlags = {
// please contact us for more information: [email protected]
commercial_hide_github: false,
commercial_hide_docs: false,
commercial_hide_changelog: false,
};

export const mapFeatureFlagsEnvToState = (config: IFeatureFlags) => {
Expand Down Expand Up @@ -112,6 +110,5 @@ export const mapFeatureFlagsEnvToState = (config: IFeatureFlags) => {

hideGitHub: config.commercial_hide_github,
hideDocs: config.commercial_hide_docs,
hideChangelog: config.commercial_hide_changelog,
};
};
109 changes: 53 additions & 56 deletions src/features/User/UserPanel/useMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,71 +175,68 @@ export const useMenu = () => {
},
].filter(Boolean) as ItemType[]);

const helps: MenuProps['items'] = hideDocs
? []
: ([
showCloudPromotion && {
icon: <Icon icon={Cloudy} />,
key: 'cloud',
const helps: MenuProps['items'] = [
showCloudPromotion && {
icon: <Icon icon={Cloudy} />,
key: 'cloud',
label: (
<Link href={`${OFFICIAL_URL}?utm_source=${UTM_SOURCE}`} target={'_blank'}>
{t('userPanel.cloud', { name: LOBE_CHAT_CLOUD })}
</Link>
),
},
{
icon: <Icon icon={FileClockIcon} />,
key: 'changelog',
label: <Link href={'/changelog'}>{t('changelog')}</Link>,
},
{
children: [
{
icon: <Icon icon={Book} />,
key: 'docs',
label: (
<Link href={`${OFFICIAL_URL}?utm_source=${UTM_SOURCE}`} target={'_blank'}>
{t('userPanel.cloud', { name: LOBE_CHAT_CLOUD })}
<Link href={DOCUMENTS_REFER_URL} target={'_blank'}>
{t('userPanel.docs')}
</Link>
),
},
{
icon: <Icon icon={FileClockIcon} />,
key: 'changelog',
label: <Link href={'/changelog'}>{t('changelog')}</Link>,
icon: <Icon icon={Feather} />,
key: 'feedback',
label: (
<Link href={GITHUB_ISSUES} target={'_blank'}>
{t('userPanel.feedback')}
</Link>
),
},

{
children: [
{
icon: <Icon icon={Book} />,
key: 'docs',
label: (
<Link href={DOCUMENTS_REFER_URL} target={'_blank'}>
{t('userPanel.docs')}
</Link>
),
},
{
icon: <Icon icon={Feather} />,
key: 'feedback',
label: (
<Link href={GITHUB_ISSUES} target={'_blank'}>
{t('userPanel.feedback')}
</Link>
),
},
{
icon: <Icon icon={DiscordIcon} />,
key: 'discord',
label: (
<Link href={DISCORD} target={'_blank'}>
{t('userPanel.discord')}
</Link>
),
},
{
icon: <Icon icon={Mail} />,
key: 'email',
label: (
<Link href={mailTo(EMAIL_SUPPORT)} target={'_blank'}>
{t('userPanel.email')}
</Link>
),
},
],
icon: <Icon icon={LifeBuoy} />,
key: 'help',
label: t('userPanel.help'),
icon: <Icon icon={DiscordIcon} />,
key: 'discord',
label: (
<Link href={DISCORD} target={'_blank'}>
{t('userPanel.discord')}
</Link>
),
},
{
type: 'divider',
icon: <Icon icon={Mail} />,
key: 'email',
label: (
<Link href={mailTo(EMAIL_SUPPORT)} target={'_blank'}>
{t('userPanel.email')}
</Link>
),
},
].filter(Boolean) as ItemType[]);
],
icon: <Icon icon={LifeBuoy} />,
key: 'help',
label: t('userPanel.help'),
},
{
type: 'divider',
},
].filter(Boolean) as ItemType[];

const mainItems = [
{
Expand All @@ -252,7 +249,7 @@ export const useMenu = () => {
/* ↑ cloud slot ↑ */
...(canInstall ? pwa : []),
...data,
...helps,
...(!hideDocs ? helps : []),
].filter(Boolean) as MenuProps['items'];

const logoutItems: MenuProps['items'] = isLoginWithAuth
Expand Down
4 changes: 2 additions & 2 deletions src/server/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ export class Sitemap {
}

async getPage(): Promise<MetadataRoute.Sitemap> {
const hideChangelog = serverFeatureFlags().hideChangelog;
const hideDocs = serverFeatureFlags().hideDocs;
const assistantsCategory = Object.values(AssistantCategory);
const pluginCategory = Object.values(PluginCategory);
const modelCategory = await this.discoverService.getProviderList(DEFAULT_LANG);
return [
...this._genSitemap('/', { noLocales: true }),
...this._genSitemap('/chat', { noLocales: true }),
...(!hideChangelog ? this._genSitemap('/changelog', { noLocales: true }) : []),
...(!hideDocs ? this._genSitemap('/changelog', { noLocales: true }) : []),
/* ↓ cloud slot ↓ */

/* ↑ cloud slot ↑ */
Expand Down

0 comments on commit c68cef4

Please sign in to comment.