Skip to content

Commit d978405

Browse files
committed
✨(front) add a useFeatureFlag hook
Add a hook to know if a feature is enabled or not (AI Features, Interop Features, etc.)
1 parent b7c66c6 commit d978405

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/frontend/src/features/layouts/components/thread-view/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useMemo, useRef, useState } from "react"
2-
import { useAIFeaturesConfig } from "@/features/utils/ai-config";
2+
import { FEATURE_KEYS, useFeatureFlag } from "@/hooks/use-feature";
33
import { ActionBar } from "./components/thread-action-bar"
44
import { ThreadMessage } from "./components/thread-message"
55
import { useMailboxContext } from "@/features/providers/mailbox"
@@ -49,8 +49,7 @@ export const ThreadView = () => {
4949
});
5050
return rootMessages
5151
}, [messages]);
52-
const aiConfig = useAIFeaturesConfig();
53-
const isAISummaryEnabled = aiConfig.isAISummaryEnabled;
52+
const isAISummaryEnabled = useFeatureFlag(FEATURE_KEYS.AI_SUMMARY);
5453

5554
/**
5655
* If we are in the trash view, we only want to show trashed messages

src/frontend/src/features/utils/ai-config.ts

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

src/frontend/src/hooks/use-feature.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useConfig } from "@/features/providers/config";
2+
3+
export enum FEATURE_KEYS {
4+
DRIVE = 'drive',
5+
AI = 'ai',
6+
AI_SUMMARY = 'ai_summary',
7+
}
8+
9+
/**
10+
* A hook to check if a feature is enabled.
11+
*
12+
* Several features like ai features or interoperability can be
13+
* enabled/disabled according to the config. This utility hook
14+
* to know the state of a feature with ease.
15+
*/
16+
export const useFeatureFlag = (featureKey: FEATURE_KEYS) => {
17+
const config = useConfig();
18+
19+
switch (featureKey) {
20+
case FEATURE_KEYS.DRIVE:
21+
return config.DRIVE !== undefined;
22+
case FEATURE_KEYS.AI:
23+
return config.AI_ENABLED === true;
24+
case FEATURE_KEYS.AI_SUMMARY:
25+
return config.AI_ENABLED === true && config.AI_FEATURE_SUMMARY_ENABLED === true;
26+
default:
27+
throw new Error(`Unknown feature key: ${featureKey}`);
28+
}
29+
}

0 commit comments

Comments
 (0)