@geejay/use-feature-flags 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/useFeatureFlags.js +16 -7
- package/package.json +1 -1
package/dist/useFeatureFlags.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from 'react';
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import { createClient } from '@supabase/supabase-js';
|
|
3
3
|
import { featureFlagsAtom } from './store';
|
|
4
4
|
import { useAtom } from 'jotai';
|
|
@@ -6,12 +6,16 @@ export const DEFAULT_SUPABASE_URL = 'https://khppgsehvvlukzfdqbuo.supabase.co';
|
|
|
6
6
|
export const DEFAULT_SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk';
|
|
7
7
|
const EDGE_FN_URL = `${DEFAULT_SUPABASE_URL}/functions/v1/get-feature-flags`;
|
|
8
8
|
let initialized = false;
|
|
9
|
-
export function useFeatureFlags(passedKey, environment = window.location.
|
|
9
|
+
export function useFeatureFlags(passedKey, environment = window.location.hostname || 'localhost') {
|
|
10
|
+
const sanitizedEnvironment = useMemo(() => environment.replace(/:\d+$/, ''), [environment]);
|
|
10
11
|
const [state, setState] = useAtom(featureFlagsAtom);
|
|
11
12
|
const [envId, setEnvId] = useState(null);
|
|
12
13
|
const debounceTimeout = useRef(null);
|
|
13
14
|
const cleanupRef = useRef();
|
|
14
15
|
const apiKey = passedKey;
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
console.log('[use-feature-flags] initializing', `environment: ${sanitizedEnvironment}`, `apiKey provided: ${Boolean(apiKey)}`);
|
|
18
|
+
}, [sanitizedEnvironment, apiKey]);
|
|
15
19
|
const supabase = createClient(DEFAULT_SUPABASE_URL, DEFAULT_SUPABASE_KEY, {
|
|
16
20
|
global: {
|
|
17
21
|
headers: {
|
|
@@ -21,6 +25,7 @@ export function useFeatureFlags(passedKey, environment = window.location.host ||
|
|
|
21
25
|
});
|
|
22
26
|
const fetchFlags = async () => {
|
|
23
27
|
setState((prev) => ({ ...prev, loading: true }));
|
|
28
|
+
console.log('[use-feature-flags] fetching flags for', sanitizedEnvironment);
|
|
24
29
|
try {
|
|
25
30
|
const res = await fetch(EDGE_FN_URL, {
|
|
26
31
|
method: 'POST',
|
|
@@ -28,7 +33,7 @@ export function useFeatureFlags(passedKey, environment = window.location.host ||
|
|
|
28
33
|
'Content-Type': 'application/json',
|
|
29
34
|
'api-key': apiKey,
|
|
30
35
|
},
|
|
31
|
-
body: JSON.stringify({ environment }),
|
|
36
|
+
body: JSON.stringify({ environment: sanitizedEnvironment }),
|
|
32
37
|
});
|
|
33
38
|
const json = await res.json();
|
|
34
39
|
if (!res.ok) {
|
|
@@ -38,7 +43,7 @@ export function useFeatureFlags(passedKey, environment = window.location.host ||
|
|
|
38
43
|
}
|
|
39
44
|
const flags = json.flags || [];
|
|
40
45
|
setState({ flags, loading: false });
|
|
41
|
-
console.log(
|
|
46
|
+
console.log('[use-feature-flags] fetched flags', flags);
|
|
42
47
|
// Store environment_id from first flag (assumes all have same env)
|
|
43
48
|
if (flags.length > 0 && flags[0].environment_id) {
|
|
44
49
|
setEnvId(flags[0].environment_id);
|
|
@@ -60,13 +65,13 @@ export function useFeatureFlags(passedKey, environment = window.location.host ||
|
|
|
60
65
|
if (cleanupRef.current)
|
|
61
66
|
cleanupRef.current();
|
|
62
67
|
};
|
|
63
|
-
}, [
|
|
68
|
+
}, [sanitizedEnvironment, apiKey]);
|
|
64
69
|
// Subscribe to flag changes for the correct environment
|
|
65
70
|
useEffect(() => {
|
|
66
71
|
if (!envId)
|
|
67
72
|
return;
|
|
68
73
|
const channel = supabase
|
|
69
|
-
.channel(`flags-${
|
|
74
|
+
.channel(`flags-${sanitizedEnvironment}`)
|
|
70
75
|
.on('postgres_changes', {
|
|
71
76
|
event: '*',
|
|
72
77
|
schema: 'public',
|
|
@@ -88,7 +93,11 @@ export function useFeatureFlags(passedKey, environment = window.location.host ||
|
|
|
88
93
|
};
|
|
89
94
|
}, [envId]);
|
|
90
95
|
return {
|
|
91
|
-
isActive: (key) =>
|
|
96
|
+
isActive: (key) => {
|
|
97
|
+
const active = state.flags.some((f) => f.key === key && f.enabled === true);
|
|
98
|
+
console.log('[use-feature-flags] isActive', key, active);
|
|
99
|
+
return active;
|
|
100
|
+
},
|
|
92
101
|
flags: state.flags,
|
|
93
102
|
loading: state.loading,
|
|
94
103
|
};
|