Merge pull request #2030 from Stijnus/BOLTDIY_Supabase_fix

fix: refactor localStorage access for Supabase state
This commit is contained in:
Stijnus
2025-10-23 14:49:33 +02:00
committed by GitHub

View File

@@ -51,8 +51,15 @@ export interface SupabaseConnectionState {
credentials?: SupabaseCredentials; credentials?: SupabaseCredentials;
} }
const savedConnection = typeof localStorage !== 'undefined' ? localStorage.getItem('supabase_connection') : null; const storage =
const savedCredentials = typeof localStorage !== 'undefined' ? localStorage.getItem('supabaseCredentials') : null; typeof globalThis !== 'undefined' &&
typeof globalThis.localStorage !== 'undefined' &&
typeof globalThis.localStorage.getItem === 'function'
? globalThis.localStorage
: null;
const savedConnection = storage ? storage.getItem('supabase_connection') : null;
const savedCredentials = storage ? storage.getItem('supabaseCredentials') : null;
const initialState: SupabaseConnectionState = savedConnection const initialState: SupabaseConnectionState = savedConnection
? JSON.parse(savedConnection) ? JSON.parse(savedConnection)
@@ -75,14 +82,14 @@ if (savedCredentials && !initialState.credentials) {
export const supabaseConnection = atom<SupabaseConnectionState>(initialState); export const supabaseConnection = atom<SupabaseConnectionState>(initialState);
if (initialState.token && !initialState.stats) {
fetchSupabaseStats(initialState.token).catch(console.error);
}
export const isConnecting = atom(false); export const isConnecting = atom(false);
export const isFetchingStats = atom(false); export const isFetchingStats = atom(false);
export const isFetchingApiKeys = atom(false); export const isFetchingApiKeys = atom(false);
if (initialState.token && !initialState.stats) {
fetchSupabaseStats(initialState.token).catch(console.error);
}
export function updateSupabaseConnection(connection: Partial<SupabaseConnectionState>) { export function updateSupabaseConnection(connection: Partial<SupabaseConnectionState>) {
const currentState = supabaseConnection.get(); const currentState = supabaseConnection.get();
@@ -123,16 +130,16 @@ export function updateSupabaseConnection(connection: Partial<SupabaseConnectionS
* Always save the connection state to localStorage to persist across chats * Always save the connection state to localStorage to persist across chats
*/ */
if (connection.user || connection.token || connection.selectedProjectId !== undefined || connection.credentials) { if (connection.user || connection.token || connection.selectedProjectId !== undefined || connection.credentials) {
localStorage.setItem('supabase_connection', JSON.stringify(newState)); storage?.setItem('supabase_connection', JSON.stringify(newState));
if (newState.credentials) { if (newState.credentials) {
localStorage.setItem('supabaseCredentials', JSON.stringify(newState.credentials)); storage?.setItem('supabaseCredentials', JSON.stringify(newState.credentials));
} else { } else {
localStorage.removeItem('supabaseCredentials'); storage?.removeItem('supabaseCredentials');
} }
} else { } else {
localStorage.removeItem('supabase_connection'); storage?.removeItem('supabase_connection');
localStorage.removeItem('supabaseCredentials'); storage?.removeItem('supabaseCredentials');
} }
} }