feat: Display a loading spinner during initial app load if no user session is stored, otherwise skip the loading state.

This commit is contained in:
2026-02-27 15:29:52 +00:00
parent 24cb02bf0c
commit c59e0ba1b0

View File

@@ -61,7 +61,16 @@ const initialState: State = {
const AppContext = createContext<AppContextValue | undefined>(undefined);
export const AppProvider = ({ children }: { children: React.ReactNode }) => {
const [loading, setLoading] = useState(true);
// Se já há sessão guardada, não mostra loading (app aparece de imediato)
const hasStoredUser = Boolean((() => {
try {
const raw = localStorage.getItem('smart-agenda');
if (!raw) return false;
const parsed = JSON.parse(raw);
return parsed?.user != null;
} catch { return false; }
})());
const [loading, setLoading] = useState(!hasStoredUser);
const [state, setState] = useState<State>(() => {
const stored = storage.get<Partial<State> | null>('smart-agenda', null);
const safeStored = stored && typeof stored === 'object' ? stored : {};
@@ -511,8 +520,13 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
refreshShops,
};
// Loading Shield evita quebra generalizada se o app renderizar sem BD disponível
if (loading) return null;
// Loading Shield — mostra spinner enquanto autentica pela primeira vez
if (loading) return (
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '100vh' }}>
<div style={{ width: 40, height: 40, border: '4px solid #e2e8f0', borderTopColor: '#6366f1', borderRadius: '50%', animation: 'spin 0.8s linear infinite' }} />
<style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style>
</div>
);
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
};