Skip to content

Commit

Permalink
Tweak loading states (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Feb 13, 2025
1 parent 47201b5 commit e5552e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
13 changes: 10 additions & 3 deletions browser-extensions/chrome/src/app/pages/CredentialsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CredentialsList: React.FC = () => {
/**
* Loading state with minimum duration for more fluid UX.
*/
const [isLoading, setIsLoading] = useMinDurationLoading(true, 150);
const [isLoading, setIsLoading] = useMinDurationLoading(true, 100);

/**
* Retrieve latest vault and refresh the page.
Expand Down Expand Up @@ -56,7 +56,6 @@ const CredentialsList: React.FC = () => {
const results = dbContext.sqliteClient.getAllCredentials();
setCredentials(results);
setIsLoading(false);
setIsInitialLoading(false);
} catch (err) {
console.error('Error loading credentials:', err);
}
Expand Down Expand Up @@ -87,7 +86,6 @@ const CredentialsList: React.FC = () => {
const results = dbContext.sqliteClient.getAllCredentials();
setCredentials(results);
setIsLoading(false);
setIsInitialLoading(false);
} catch (err) {
console.error('Error loading credentials:', err);
}
Expand All @@ -96,6 +94,15 @@ const CredentialsList: React.FC = () => {
checkStatus();
}, [authContext, dbContext?.sqliteClient, dbContext?.vaultRevision, onRefresh, webApi, setIsInitialLoading, setIsLoading]);

/**
* Make sure the initial loading state is set to false when this component is loaded itself.
*/
useEffect(() => {
if (!isLoading) {
setIsInitialLoading(false);
}
}, [setIsInitialLoading, isLoading]);

/**
* Manually refresh the credentials list.
*/
Expand Down
2 changes: 1 addition & 1 deletion browser-extensions/chrome/src/app/pages/EmailsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EmailsList: React.FC = () => {
/**
* Loading state with minimum duration for more fluid UX.
*/
const [isLoading, setIsLoading] = useMinDurationLoading(true, 150);
const [isLoading, setIsLoading] = useMinDurationLoading(true, 100);

/**
* Loads emails from the web API.
Expand Down
35 changes: 18 additions & 17 deletions browser-extensions/chrome/src/app/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,40 @@ const Home: React.FC = () => {
const authContext = useAuth();
const dbContext = useDb();
const navigate = useNavigate();
const webApi = useWebApi();
const { setIsInitialLoading } = useLoading();
const [isInlineUnlockMode, setIsInlineUnlockMode] = useState(false);
const initialized = authContext.isInitialized && dbContext.dbInitialized;
const needsUnlock = initialized && (!authContext.isLoggedIn || !dbContext.dbAvailable);

// Initialization state.
const isFullyInitialized = authContext.isInitialized && dbContext.dbInitialized;
const isAuthenticated = authContext.isLoggedIn;
const isDatabaseAvailable = dbContext.dbAvailable;
const requireLoginOrUnlock = isFullyInitialized && (!isAuthenticated || !isDatabaseAvailable);

useEffect(() => {
// Detect if the user is coming from the unlock page with mode=inline_unlock
// Detect if the user is coming from the unlock page with mode=inline_unlock.
const urlParams = new URLSearchParams(window.location.search);
const isInlineUnlockMode = urlParams.get('mode') === 'inline_unlock';
setIsInlineUnlockMode(isInlineUnlockMode);

// Do a status check to see if the auth tokens are still valid, if not, redirect to the login page.
const checkStatus = async () => {
const status = await webApi.get('Status');
if (status.status !== 0) {
authContext.logout();
}
};

if (initialized && !needsUnlock) {
// Redirect to credentials if fully initialized and doesn't need unlock.
if (isFullyInitialized && !requireLoginOrUnlock) {
navigate('/credentials', { replace: true });
}
}, [initialized,needsUnlock, isInlineUnlockMode, navigate]);
}, [isFullyInitialized, requireLoginOrUnlock, isInlineUnlockMode, navigate]);

// Show loading state if not fully initialized or when about to redirect to credentials.
if (!isFullyInitialized || (isFullyInitialized && !requireLoginOrUnlock)) {
// Global loading spinner will be shown by the parent component.
return null;
}

// Set initial loading state to false once the page is loaded until here.
setIsInitialLoading(false);

if (!authContext.isLoggedIn) {
if (!isAuthenticated) {
return <Login />;
}

if (needsUnlock) {
if (requireLoginOrUnlock) {
return <Unlock />;
}

Expand Down

0 comments on commit e5552e8

Please sign in to comment.