Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add generic loading spinner context (#541)

Leendert de Borstacd26ee67b29
diff --git a/browser-extensions/chrome/src/App.tsx b/browser-extensions/chrome/src/App.tsx
index bbd5efb67..3872f15f6 100644
--- a/browser-extensions/chrome/src/App.tsx
+++ b/browser-extensions/chrome/src/App.tsx
@@ -39,8 +39,13 @@ const App: React.FC = () => {
/**
* Handle logout
*/
- const handleLogout = () : void => {
- logout();
+ const handleLogout = async (): Promise<void> => {
+ setIsLoading(true);
+ try {
+ await logout();
+ } finally {
+ setIsLoading(false);
+ }
};
/**
diff --git a/browser-extensions/chrome/src/components/LoadingSpinner.tsx b/browser-extensions/chrome/src/components/LoadingSpinner.tsx
index e83180539..8ea0fec0d 100644
--- a/browser-extensions/chrome/src/components/LoadingSpinner.tsx
+++ b/browser-extensions/chrome/src/components/LoadingSpinner.tsx
@@ -1,37 +1,38 @@
import React from 'react';
-type LoadingSpinnerProps = {
- size?: number;
-}
-
/**
- * Loading spinner component
+ * Loading spinner component used throughout the app for showing a loading spinner
+ * inline in the page.
*/
-const LoadingSpinner: React.FC<LoadingSpinnerProps> = ({
- size = 40,
-}) => {
+const LoadingSpinner: React.FC = () => {
const spinnerStyle: React.CSSProperties = {
- width: `${size}px`,
- height: `${size}px`,
+ width: '40px',
+ height: '40px',
borderRadius: '50%',
animation: 'spin 1s linear infinite',
};
- return (
+ const spinner = (
<>
- <style>
- {`
- @keyframes spin {
- 0% { transform: rotate(0deg); }
- 100% { transform: rotate(360deg); }
- }
- `}
- </style>
- <div
- className="border-[4px] border-solid border-current/10 dark:border-white/10 border-t-current dark:border-t-white"
- style={spinnerStyle}
- />
- </>
+ <style>
+ {`
+ @keyframes spin {
+ 0% { transform: rotate(0deg); }
+ 100% { transform: rotate(360deg); }
+ }
+ `}
+ </style>
+ <div
+ className="border-[4px] border-solid border-current/10 dark:border-white/10 border-t-current dark:border-t-white"
+ style={spinnerStyle}
+ />
+ </>
+ );
+
+ return (
+ <div className="inline-flex items-center">
+ {spinner}
+ </div>
);
};
diff --git a/browser-extensions/chrome/src/components/LoadingSpinnerFullScreen.tsx b/browser-extensions/chrome/src/components/LoadingSpinnerFullScreen.tsx
new file mode 100644
index 000000000..c8b987125
--- /dev/null
+++ b/browser-extensions/chrome/src/components/LoadingSpinnerFullScreen.tsx
@@ -0,0 +1,46 @@
+import React from 'react';
+import { useLoading } from '../context/LoadingContext';
+
+/**
+ * Loading spinner full screen component used throughout the app for showing a loading spinner
+ * that covers the entire screen.
+ */
+const LoadingSpinnerFullScreen: React.FC = () => {
+ const { isLoading } = useLoading();
+
+ if (!isLoading) return null;
+
+ const spinnerStyle: React.CSSProperties = {
+ width: '40px',
+ height: '40px',
+ borderRadius: '50%',
+ animation: 'spin 1s linear infinite',
+ };
+
+ const spinner = (
+ <>
+ <style>
+ {`
+ @keyframes spin {
+ 0% { transform: rotate(0deg); }
+ 100% { transform: rotate(360deg); }
+ }
+ `}
+ </style>
+ <div
+ className="border-[4px] border-solid border-current/10 dark:border-white/10 border-t-current dark:border-t-white"
+ style={spinnerStyle}
+ />
+ </>
+ );
+
+ return (
+ <div className="fixed inset-0 w-full h-full z-50 bg-gray-200 dark:bg-gray-500 bg-opacity-90 flex items-center justify-center">
+ <div className="relative">
+ {spinner}
+ </div>
+ </div>
+ );
+};
+
+export default LoadingSpinnerFullScreen;
diff --git a/browser-extensions/chrome/src/context/LoadingContext.tsx b/browser-extensions/chrome/src/context/LoadingContext.tsx
new file mode 100644
index 000000000..8db97d76f
--- /dev/null
+++ b/browser-extensions/chrome/src/context/LoadingContext.tsx
@@ -0,0 +1,35 @@
+import React, { createContext, useContext, useState } from 'react';
+import LoadingSpinnerFullScreen from '../components/LoadingSpinnerFullScreen';
+
+type LoadingContextType = {
+ isLoading: boolean;
+ showLoading: () => void;
+ hideLoading: () => void;
+}
+
+const LoadingContext = createContext<LoadingContextType | undefined>(undefined);
+
+/**
+ * Loading provider
+ */
+export const LoadingProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
+ const [isLoading, setIsLoading] = useState(false);
+
+ const showLoading = () => setIsLoading(true);
+ const hideLoading = () => setIsLoading(false);
+
+ return (
+ <LoadingContext.Provider value={{ isLoading, showLoading, hideLoading }}>
+ <LoadingSpinnerFullScreen />
+ {children}
+ </LoadingContext.Provider>
+ );
+};
+
+export const useLoading = () => {
+ const context = useContext(LoadingContext);
+ if (context === undefined) {
+ throw new Error('useLoading must be used within a LoadingProvider');
+ }
+ return context;
+};
\ No newline at end of file
diff --git a/browser-extensions/chrome/src/hooks/useMinDurationLoading.ts b/browser-extensions/chrome/src/hooks/useMinDurationLoading.ts
index 1b475c646..f58919a95 100644
--- a/browser-extensions/chrome/src/hooks/useMinDurationLoading.ts
+++ b/browser-extensions/chrome/src/hooks/useMinDurationLoading.ts
@@ -1,4 +1,4 @@
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useCallback, useRef } from 'react';
/**
* Hook that ensures a loading state persists for a minimum duration before being set to false.
@@ -11,28 +11,49 @@ import { useState, useEffect } from 'react';
export const useMinDurationLoading = (
initialState: boolean = false,
minDuration: number = 300
-): [boolean, (loading: boolean) => void] => {
+): [boolean, (value: boolean) => void] => {
const [isLoading, setIsLoading] = useState(initialState);
- const [loadingStartTime, setLoadingStartTime] = useState<number | null>(null);
- const [internalLoading, setInternalLoading] = useState(initialState);
+ const timeoutRef = useRef<NodeJS.Timeout>();
+ const startTimeRef = useRef<number>();
- useEffect(() => {
- if (internalLoading) {
+ const setLoadingState = useCallback((value: boolean) => {
+ if (value) {
+ // Starting to load
setIsLoading(true);
- setLoadingStartTime(Date.now());
- } else if (loadingStartTime) {
- const elapsedTime = Date.now() - loadingStartTime;
+ startTimeRef.current = Date.now();
+ } else {
+ // Finishing loading - ensure minimum duration
+ const elapsedTime = startTimeRef.current ? Date.now() - startTimeRef.current : 0;
const remainingTime = Math.max(0, minDuration - elapsedTime);
if (remainingTime === 0) {
setIsLoading(false);
} else {
- setTimeout(() => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+ timeoutRef.current = setTimeout(() => {
setIsLoading(false);
}, remainingTime);
}
}
- }, [internalLoading, loadingStartTime, minDuration]);
+ }, [minDuration]);
+
+ // Handle initial loading state only once
+ useEffect(() => {
+ if (initialState) {
+ setIsLoading(true);
+ startTimeRef.current = Date.now();
+ }
+ }, []); // Empty dependency array ensures this only runs once
+
+ useEffect(() => {
+ return () => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+ };
+ }, []);
- return [isLoading, setInternalLoading];
+ return [isLoading, setLoadingState];
};
\ No newline at end of file
diff --git a/browser-extensions/chrome/src/index.tsx b/browser-extensions/chrome/src/index.tsx
index adf294d10..de98515b1 100644
--- a/browser-extensions/chrome/src/index.tsx
+++ b/browser-extensions/chrome/src/index.tsx
@@ -3,13 +3,16 @@ import App from './App';
import { AuthProvider } from './context/AuthContext';
import { WebApiProvider } from './context/WebApiContext';
import { DbProvider } from './context/DbContext';
+import { LoadingProvider } from './context/LoadingContext';
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<AuthProvider>
<DbProvider>
<WebApiProvider>
- <App />
+ <LoadingProvider>
+ <App />
+ </LoadingProvider>
</WebApiProvider>
</DbProvider>
</AuthProvider>
diff --git a/browser-extensions/chrome/src/pages/Login.tsx b/browser-extensions/chrome/src/pages/Login.tsx
index d65709224..11a89e9a3 100644
--- a/browser-extensions/chrome/src/pages/Login.tsx
+++ b/browser-extensions/chrome/src/pages/Login.tsx
@@ -7,6 +7,7 @@ import Button from '../components/Button';
import EncryptionUtility from '../utils/EncryptionUtility';
import SrpUtility from '../utils/SrpUtility';
import { VaultResponse } from '../types/webapi/VaultResponse';
+import { useLoading } from '../context/LoadingContext';
/**
* Login page
@@ -17,6 +18,7 @@ const Login: React.FC = () => {
username: '',
password: '',
});
+ const { showLoading, hideLoading } = useLoading();
const [rememberMe, setRememberMe] = useState(false);
const [error, setError] = useState<string | null>(null);
const dbContext = useDb();
@@ -32,6 +34,8 @@ const Login: React.FC = () => {
setError(null);
try {
+ showLoading();
+
// Use the srpUtil instance instead of the imported singleton
const loginResponse = await srpUtil.initiateLogin(credentials.username);
@@ -85,9 +89,11 @@ const Login: React.FC = () => {
* window.location.href = '/';
*/
+ hideLoading();
} catch (err) {
setError('Login failed. Please check your credentials and try again.');
console.error('Login error:', err);
+ hideLoading();
}
};