A full-featured CRM built with React, shadcn/ui, and Supabase.
Fix systematic getIsInitialized query at startup prevents offline mode
François Zaninotto1ba845840f35
diff --git a/src/components/atomic-crm/providers/supabase/authProvider.ts b/src/components/atomic-crm/providers/supabase/authProvider.tsindex f37d117f..98a06546 100644--- a/src/components/atomic-crm/providers/supabase/authProvider.ts+++ b/src/components/atomic-crm/providers/supabase/authProvider.ts@@ -1,5 +1,3 @@-/* eslint-disable no-var */-/* eslint-disable @typescript-eslint/no-namespace */import type { AuthProvider } from "ra-core";import { supabaseAuthProvider } from "ra-supabase-core";@@ -8,7 +6,7 @@ import { supabase } from "./supabase";const baseAuthProvider = supabaseAuthProvider(supabase, {getIdentity: async () => {- const sale = await getSaleFromCache();+ const sale = await getSale();if (sale == null) {throw new Error();@@ -22,27 +20,74 @@ const baseAuthProvider = supabaseAuthProvider(supabase, {},});+// To speed up checks, we cache them:+// - in memory for the current sale+// - in local storage for the initialization state++const IS_INITIALIZED_CACHE_KEY = "crm_is_initialized";++function getLocalStorage(): Storage | null {+ if (typeof window !== "undefined" && window.localStorage) {+ return window.localStorage;+ }+ return null;+}+export async function getIsInitialized() {- if (getIsInitialized._is_initialized_cache == null) {- const { data } = await supabase.from("init_state").select("is_initialized");+ const storage = getLocalStorage();+ const cachedValue = storage?.getItem(IS_INITIALIZED_CACHE_KEY);- getIsInitialized._is_initialized_cache = data?.at(0)?.is_initialized > 0;+ if (cachedValue != null) {+ return cachedValue === "true";}- return getIsInitialized._is_initialized_cache;+ const { data } = await supabase.from("init_state").select("is_initialized");+ const isInitialized = data?.at(0)?.is_initialized > 0;++ storage?.setItem(IS_INITIALIZED_CACHE_KEY, String(isInitialized));++ return isInitialized;}-export namespace getIsInitialized {- export var _is_initialized_cache: boolean | null = null;+let cachedSale: any;++const getSale = async () => {+ if (cachedSale != null) return cachedSale;++ const { data: dataSession, error: errorSession } =+ await supabase.auth.getSession();++ // Shouldn't happen after login but just in case+ if (dataSession?.session?.user == null || errorSession) {+ return undefined;+ }++ const { data: dataSale, error: errorSale } = await supabase+ .from("sales")+ .select("id, first_name, last_name, avatar, administrator")+ .match({ user_id: dataSession?.session?.user.id })+ .single();++ // Shouldn't happen either as all users are sales but just in case+ if (dataSale == null || errorSale) {+ return undefined;+ }++ cachedSale = dataSale;+ return dataSale;+};++function clearCache() {+ cachedSale = undefined;+ const storage = getLocalStorage();+ storage?.removeItem(IS_INITIALIZED_CACHE_KEY);}export const authProvider: AuthProvider = {...baseAuthProvider,- login: async (params) => {- const result = await baseAuthProvider.login(params);- // clear cached sale- cachedSale = undefined;- return result;+ logout: async (params) => {+ clearCache();+ return baseAuthProvider.logout(params);},checkAuth: async (params) => {// Users are on the set-password page, nothing to do@@ -84,7 +129,7 @@ export const authProvider: AuthProvider = {if (!isInitialized) return false;// Get the current user- const sale = await getSaleFromCache();+ const sale = await getSale();if (sale == null) return false;// Compute access rights from the sale role@@ -102,29 +147,3 @@ export const authProvider: AuthProvider = {},};-let cachedSale: any;-const getSaleFromCache = async () => {- if (cachedSale != null) return cachedSale;-- const { data: dataSession, error: errorSession } =- await supabase.auth.getSession();-- // Shouldn't happen after login but just in case- if (dataSession?.session?.user == null || errorSession) {- return undefined;- }-- const { data: dataSale, error: errorSale } = await supabase- .from("sales")- .select("id, first_name, last_name, avatar, administrator")- .match({ user_id: dataSession?.session?.user.id })- .single();-- // Shouldn't happen either as all users are sales but just in case- if (dataSale == null || errorSale) {- return undefined;- }-- cachedSale = dataSale;- return dataSale;-};