Files
Zero/apps/mail/components/context/loading-context.tsx
needle 275c3fd514 refactor(nav): enhance account switcher (#1367)
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Introduced a global loading indicator with animated overlay and customizable messages for improved user feedback during operations.
- **Improvements**
  - Enhanced account switching with explicit loading states, improved cache management, and clearer success or error notifications.
  - Added new localized messages for account switching status.
- **Chores**
  - Updated package manager version for improved tooling support.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-06-21 13:03:24 -07:00

46 lines
1.5 KiB
TypeScript

import { createContext, useContext, useState, type ReactNode } from 'react';
import { Spinner } from '@/components/ui/spinner';
interface LoadingContextType {
isLoading: boolean;
loadingMessage?: string;
setLoading: (loading: boolean, message?: string) => void;
}
const LoadingContext = createContext<LoadingContextType | undefined>(undefined);
export function LoadingProvider({ children }: { children: ReactNode }) {
const [isLoading, setIsLoading] = useState(false);
const [loadingMessage, setLoadingMessage] = useState<string | undefined>();
const setLoading = (loading: boolean, message?: string) => {
setIsLoading(loading);
setLoadingMessage(message);
};
return (
<LoadingContext.Provider value={{ isLoading, loadingMessage, setLoading }}>
{children}
{isLoading && (
<div className="fixed inset-0 z-[9999] flex items-center justify-center bg-black/50 backdrop-blur-sm">
<div className="bg-panelLight dark:bg-panelDark flex flex-col items-center gap-4 rounded-xl border p-6 shadow-xl">
<Spinner size={32} />
<div className="text-center">
<p className="text-sm font-medium text-black dark:text-white">
{loadingMessage || 'Loading...'}
</p>
</div>
</div>
</div>
)}
</LoadingContext.Provider>
);
}
export function useLoading() {
const context = useContext(LoadingContext);
if (!context) {
throw new Error('useLoading must be used within a LoadingProvider');
}
return context;
}