@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.
@@ -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 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk";
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: import("./store").FeatureFlag[];
6
+ flags: FeatureFlag[];
6
7
  loading: boolean;
7
8
  };
@@ -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 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtocHBnc2VodnZsdWt6ZmRxYnVvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTI2ODU1MzQsImV4cCI6MjA2ODI2MTUzNH0.8Z4VY4HFMm95UgO21c-DnDkbLPN_0mbDBZJPExaghDk'; // Keep this secured
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 = typeof window !== 'undefined' ? window.location.hostname : 'localhost') {
10
- const [state, setState] = useAtom(featureFlagsAtom);
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
- if (passedKey && passedKey !== getApiKey()) {
13
- setApiKey(passedKey);
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 response = await fetch(`${DEFAULT_SUPABASE_URL}/functions/v1/get-feature-flags`, {
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 { flags } = await response.json();
37
- if (!response.ok || !Array.isArray(flags)) {
38
- console.warn('Invalid response from edge function');
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('Unexpected error fetching flags:', err.message);
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
- }, [environment, apiKey]);
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geejay/use-feature-flags",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "React hook for feature flag management using Supabase",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",