forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatureFlags.js
More file actions
85 lines (75 loc) · 2.65 KB
/
Copy pathfeatureFlags.js
File metadata and controls
85 lines (75 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Feature flag helpers.
*
* Flags are driven by NEXT_PUBLIC_* environment variables so they are
* embedded in the browser bundle at build time — no runtime fetch needed.
*
* Usage:
* import { isEnabled, FLAGS } from '../lib/featureFlags';
*
* if (isEnabled(FLAGS.STAKING)) { ... }
*
* Or use the withFeatureFlag HOC / useFeatureFlag hook for components.
*/
// ---------------------------------------------------------------------------
// Flag registry
// ---------------------------------------------------------------------------
/** Canonical flag names — use these constants instead of raw strings. */
export const FLAGS = /** @type {const} */ ({
STAKING: 'NEXT_PUBLIC_STAKING_ENABLED',
REFERRAL: 'NEXT_PUBLIC_REFERRAL_ENABLED',
});
// ---------------------------------------------------------------------------
// Core helper
// ---------------------------------------------------------------------------
/**
* Returns true when the given feature flag is enabled.
*
* @param {string} flagName - One of the FLAGS constants.
* @returns {boolean}
*/
export function isEnabled(flagName) {
return process.env[flagName] === 'true';
}
// ---------------------------------------------------------------------------
// React hook
// ---------------------------------------------------------------------------
/**
* React hook that returns whether a feature flag is enabled.
*
* Because flags are baked in at build time this never triggers a re-render;
* it is a hook purely for ergonomic consistency with the rest of the codebase.
*
* @param {string} flagName - One of the FLAGS constants.
* @returns {boolean}
*
* @example
* const stakingEnabled = useFeatureFlag(FLAGS.STAKING);
* if (!stakingEnabled) return null;
*/
export function useFeatureFlag(flagName) {
return isEnabled(flagName);
}
// ---------------------------------------------------------------------------
// Higher-order component
// ---------------------------------------------------------------------------
/**
* Wraps a component so it renders null when the given feature flag is off.
*
* @template {object} P
* @param {React.ComponentType<P>} Component
* @param {string} flagName - One of the FLAGS constants.
* @returns {React.ComponentType<P>}
*
* @example
* export default withFeatureFlag(StakingPanel, FLAGS.STAKING);
*/
export function withFeatureFlag(Component, flagName) {
const displayName = Component.displayName || Component.name || 'Component';
function FeatureFlagged(props) {
if (!isEnabled(flagName)) return null;
return <Component {...props} />;
}
FeatureFlagged.displayName = `withFeatureFlag(${displayName})`;
return FeatureFlagged;
}