File size: 1,718 Bytes
cd5198d
 
 
 
 
1bc59b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { createClient } from '@supabase/supabase-js'

const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY

const hasCredentials = supabaseUrl && supabaseAnonKey && 
  supabaseUrl !== 'undefined' && supabaseAnonKey !== 'undefined';

// Safe Chainable Thenable Promise to prevent React crashing on arbitrary queries
const createChainablePromise = (resolvedValue) => {
  const promise = Promise.resolve(resolvedValue);
  const chain = {
    select: () => chain,
    order: () => chain,
    eq: () => chain,
    then: (onFulfilled, onRejected) => promise.then(onFulfilled, onRejected),
    catch: (onRejected) => promise.catch(onRejected)
  };
  return chain;
};

// Safe Mock to allow sandbox-only local operations if DB keys aren't set
const mockSupabase = {
  from: () => ({
    select: () => createChainablePromise({ data: [], error: null }),
    insert: () => createChainablePromise({ data: null, error: new Error("Supabase credentials not configured in environment.") }),
    update: () => ({ eq: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }) }),
    delete: () => ({ eq: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }) })
  }),
  storage: {
    from: () => ({
      upload: () => createChainablePromise({ data: null, error: new Error("Supabase credentials not configured.") }),
      remove: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }),
      getPublicUrl: () => ({ data: { publicUrl: "" } })
    })
  }
};

export const supabase = hasCredentials 
  ? createClient(supabaseUrl, supabaseAnonKey) 
  : mockSupabase;