@geejay/use-feature-flags 1.0.2 → 1.0.4
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.d.ts +3 -2
- package/dist/useFeatureFlags.js +36 -28
- package/package.json +1 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { FeatureFlag } from './store';
|
|
1
2
|
export declare const DEFAULT_SUPABASE_URL = "https://khppgsehvvlukzfdqbuo.supabase.co";
|
|
2
|
-
export declare const DEFAULT_SUPABASE_KEY = "
|
|
3
|
+
export declare const DEFAULT_SUPABASE_KEY = "yeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk";
|
|
3
4
|
export declare function useFeatureFlags(passedKey?: string, environment?: string): {
|
|
4
5
|
isActive: (key: string) => boolean;
|
|
5
|
-
flags:
|
|
6
|
+
flags: FeatureFlag[];
|
|
6
7
|
loading: boolean;
|
|
7
8
|
};
|
package/dist/useFeatureFlags.js
CHANGED
|
@@ -1,31 +1,20 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
2
|
-
import { useAtom } from 'jotai';
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
2
|
import { createClient } from '@supabase/supabase-js';
|
|
4
|
-
import { featureFlagsAtom, getApiKey, setApiKey } from './store';
|
|
5
|
-
// Default config
|
|
6
3
|
export const DEFAULT_SUPABASE_URL = 'https://khppgsehvvlukzfdqbuo.supabase.co';
|
|
7
|
-
export const DEFAULT_SUPABASE_KEY = '
|
|
4
|
+
export const DEFAULT_SUPABASE_KEY = 'yeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk';
|
|
5
|
+
const EDGE_FN_URL = `${DEFAULT_SUPABASE_URL}/functions/v1/get-feature-flags`;
|
|
6
|
+
const supabase = createClient(DEFAULT_SUPABASE_URL, DEFAULT_SUPABASE_KEY);
|
|
8
7
|
let initialized = false;
|
|
9
|
-
export function useFeatureFlags(passedKey, environment =
|
|
10
|
-
const [state, setState] =
|
|
8
|
+
export function useFeatureFlags(passedKey, environment = 'localhost') {
|
|
9
|
+
const [state, setState] = useState({ flags: [], loading: true });
|
|
10
|
+
const [envId, setEnvId] = useState(null);
|
|
11
11
|
const debounceTimeout = useRef(null);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const apiKey = getApiKey();
|
|
16
|
-
if (!apiKey) {
|
|
17
|
-
console.warn('API key is missing');
|
|
18
|
-
return {
|
|
19
|
-
isActive: () => false,
|
|
20
|
-
flags: [],
|
|
21
|
-
loading: false,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
const supabase = createClient(DEFAULT_SUPABASE_URL, DEFAULT_SUPABASE_KEY);
|
|
12
|
+
const cleanupRef = useRef();
|
|
13
|
+
const apiKey = passedKey || DEFAULT_SUPABASE_KEY;
|
|
25
14
|
const fetchFlags = async () => {
|
|
26
15
|
setState((prev) => ({ ...prev, loading: true }));
|
|
27
16
|
try {
|
|
28
|
-
const
|
|
17
|
+
const res = await fetch(EDGE_FN_URL, {
|
|
29
18
|
method: 'POST',
|
|
30
19
|
headers: {
|
|
31
20
|
'Content-Type': 'application/json',
|
|
@@ -33,16 +22,21 @@ export function useFeatureFlags(passedKey, environment = typeof window !== 'unde
|
|
|
33
22
|
},
|
|
34
23
|
body: JSON.stringify({ environment }),
|
|
35
24
|
});
|
|
36
|
-
const
|
|
37
|
-
if (!
|
|
38
|
-
console.warn('
|
|
25
|
+
const json = await res.json();
|
|
26
|
+
if (!res.ok) {
|
|
27
|
+
console.warn('Edge function error:', (json === null || json === void 0 ? void 0 : json.error) || res.statusText);
|
|
39
28
|
setState({ flags: [], loading: false });
|
|
40
29
|
return;
|
|
41
30
|
}
|
|
31
|
+
const flags = json.flags || [];
|
|
42
32
|
setState({ flags, loading: false });
|
|
33
|
+
// Store environment_id from first flag (assumes all have same env)
|
|
34
|
+
if (flags.length > 0 && flags[0].environment_id) {
|
|
35
|
+
setEnvId(flags[0].environment_id);
|
|
36
|
+
}
|
|
43
37
|
}
|
|
44
38
|
catch (err) {
|
|
45
|
-
console.error('
|
|
39
|
+
console.error('Error fetching flags:', err.message);
|
|
46
40
|
setState({ flags: [], loading: false });
|
|
47
41
|
}
|
|
48
42
|
};
|
|
@@ -51,27 +45,41 @@ export function useFeatureFlags(passedKey, environment = typeof window !== 'unde
|
|
|
51
45
|
return;
|
|
52
46
|
initialized = true;
|
|
53
47
|
fetchFlags();
|
|
48
|
+
return () => {
|
|
49
|
+
if (debounceTimeout.current)
|
|
50
|
+
clearTimeout(debounceTimeout.current);
|
|
51
|
+
if (cleanupRef.current)
|
|
52
|
+
cleanupRef.current();
|
|
53
|
+
};
|
|
54
|
+
}, [environment, apiKey]);
|
|
55
|
+
// Subscribe to flag changes for the correct environment
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!envId)
|
|
58
|
+
return;
|
|
54
59
|
const channel = supabase
|
|
55
60
|
.channel(`flags-${environment}`)
|
|
56
61
|
.on('postgres_changes', {
|
|
57
62
|
event: '*',
|
|
58
63
|
schema: 'public',
|
|
59
64
|
table: 'feature_flags',
|
|
65
|
+
filter: `environment_id=eq.${envId}`,
|
|
60
66
|
}, () => {
|
|
61
67
|
if (debounceTimeout.current)
|
|
62
68
|
clearTimeout(debounceTimeout.current);
|
|
63
69
|
debounceTimeout.current = setTimeout(fetchFlags, 300);
|
|
64
70
|
})
|
|
65
71
|
.subscribe();
|
|
72
|
+
if (cleanupRef.current)
|
|
73
|
+
cleanupRef.current();
|
|
74
|
+
cleanupRef.current = () => supabase.removeChannel(channel);
|
|
66
75
|
return () => {
|
|
67
76
|
if (debounceTimeout.current)
|
|
68
77
|
clearTimeout(debounceTimeout.current);
|
|
69
78
|
supabase.removeChannel(channel);
|
|
70
79
|
};
|
|
71
|
-
}, [
|
|
72
|
-
const isActive = (key) => state.flags.some((flag) => flag.key === key && flag.enabled);
|
|
80
|
+
}, [envId]);
|
|
73
81
|
return {
|
|
74
|
-
isActive,
|
|
82
|
+
isActive: (key) => state.flags.some((f) => f.key === key && f.enabled),
|
|
75
83
|
flags: state.flags,
|
|
76
84
|
loading: state.loading,
|
|
77
85
|
};
|