Fix React startup crash by adding a resilient mock Supabase fallback when keys are missing
Browse files
frontend/src/supabaseClient.js
CHANGED
|
@@ -3,4 +3,40 @@ import { createClient } from '@supabase/supabase-js'
|
|
| 3 |
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
|
| 4 |
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
|
| 4 |
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY
|
| 5 |
|
| 6 |
+
const hasCredentials = supabaseUrl && supabaseAnonKey &&
|
| 7 |
+
supabaseUrl !== 'undefined' && supabaseAnonKey !== 'undefined';
|
| 8 |
+
|
| 9 |
+
// Safe Chainable Thenable Promise to prevent React crashing on arbitrary queries
|
| 10 |
+
const createChainablePromise = (resolvedValue) => {
|
| 11 |
+
const promise = Promise.resolve(resolvedValue);
|
| 12 |
+
const chain = {
|
| 13 |
+
select: () => chain,
|
| 14 |
+
order: () => chain,
|
| 15 |
+
eq: () => chain,
|
| 16 |
+
then: (onFulfilled, onRejected) => promise.then(onFulfilled, onRejected),
|
| 17 |
+
catch: (onRejected) => promise.catch(onRejected)
|
| 18 |
+
};
|
| 19 |
+
return chain;
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
// Safe Mock to allow sandbox-only local operations if DB keys aren't set
|
| 23 |
+
const mockSupabase = {
|
| 24 |
+
from: () => ({
|
| 25 |
+
select: () => createChainablePromise({ data: [], error: null }),
|
| 26 |
+
insert: () => createChainablePromise({ data: null, error: new Error("Supabase credentials not configured in environment.") }),
|
| 27 |
+
update: () => ({ eq: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }) }),
|
| 28 |
+
delete: () => ({ eq: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }) })
|
| 29 |
+
}),
|
| 30 |
+
storage: {
|
| 31 |
+
from: () => ({
|
| 32 |
+
upload: () => createChainablePromise({ data: null, error: new Error("Supabase credentials not configured.") }),
|
| 33 |
+
remove: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }),
|
| 34 |
+
getPublicUrl: () => ({ data: { publicUrl: "" } })
|
| 35 |
+
})
|
| 36 |
+
}
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
export const supabase = hasCredentials
|
| 40 |
+
? createClient(supabaseUrl, supabaseAnonKey)
|
| 41 |
+
: mockSupabase;
|
| 42 |
+
|