Skip to content

Commit 1a55bd5

Browse files
authored
Merge pull request #2030 from Stijnus/BOLTDIY_Supabase_fix
fix: refactor localStorage access for Supabase state
2 parents cb3c536 + f138b9c commit 1a55bd5

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

app/lib/stores/supabase.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ export interface SupabaseConnectionState {
5151
credentials?: SupabaseCredentials;
5252
}
5353

54-
const savedConnection = typeof localStorage !== 'undefined' ? localStorage.getItem('supabase_connection') : null;
55-
const savedCredentials = typeof localStorage !== 'undefined' ? localStorage.getItem('supabaseCredentials') : null;
54+
const storage =
55+
typeof globalThis !== 'undefined' &&
56+
typeof globalThis.localStorage !== 'undefined' &&
57+
typeof globalThis.localStorage.getItem === 'function'
58+
? globalThis.localStorage
59+
: null;
60+
61+
const savedConnection = storage ? storage.getItem('supabase_connection') : null;
62+
const savedCredentials = storage ? storage.getItem('supabaseCredentials') : null;
5663

5764
const initialState: SupabaseConnectionState = savedConnection
5865
? JSON.parse(savedConnection)
@@ -75,14 +82,14 @@ if (savedCredentials && !initialState.credentials) {
7582

7683
export const supabaseConnection = atom<SupabaseConnectionState>(initialState);
7784

78-
if (initialState.token && !initialState.stats) {
79-
fetchSupabaseStats(initialState.token).catch(console.error);
80-
}
81-
8285
export const isConnecting = atom(false);
8386
export const isFetchingStats = atom(false);
8487
export const isFetchingApiKeys = atom(false);
8588

89+
if (initialState.token && !initialState.stats) {
90+
fetchSupabaseStats(initialState.token).catch(console.error);
91+
}
92+
8693
export function updateSupabaseConnection(connection: Partial<SupabaseConnectionState>) {
8794
const currentState = supabaseConnection.get();
8895

@@ -123,16 +130,16 @@ export function updateSupabaseConnection(connection: Partial<SupabaseConnectionS
123130
* Always save the connection state to localStorage to persist across chats
124131
*/
125132
if (connection.user || connection.token || connection.selectedProjectId !== undefined || connection.credentials) {
126-
localStorage.setItem('supabase_connection', JSON.stringify(newState));
133+
storage?.setItem('supabase_connection', JSON.stringify(newState));
127134

128135
if (newState.credentials) {
129-
localStorage.setItem('supabaseCredentials', JSON.stringify(newState.credentials));
136+
storage?.setItem('supabaseCredentials', JSON.stringify(newState.credentials));
130137
} else {
131-
localStorage.removeItem('supabaseCredentials');
138+
storage?.removeItem('supabaseCredentials');
132139
}
133140
} else {
134-
localStorage.removeItem('supabase_connection');
135-
localStorage.removeItem('supabaseCredentials');
141+
storage?.removeItem('supabase_connection');
142+
storage?.removeItem('supabaseCredentials');
136143
}
137144
}
138145

0 commit comments

Comments
 (0)