Skip to content

Commit

Permalink
force redirect to 0 on tab switching if inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Mar 10, 2024
1 parent ea286b7 commit 6bfa54b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/hooks/useRedirectOnInactive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: GPL-3.0-only

import { useApi } from 'contexts/Api';
import { useTabs } from 'contexts/Tabs';
import { useSection } from 'library/Page/provider';
import { useEffect } from 'react';

// NOTE: This hook can only be used within <Page> components, depending on SectionProvider.
export const useRedirectOnInactive = (tabId: number) => {
const { activeTabId } = useTabs();
const { getApiStatus } = useApi();
const { setActiveSection } = useSection();

Expand All @@ -15,10 +17,11 @@ export const useRedirectOnInactive = (tabId: number) => {
const apiInactive = INACTIVE_API_STATUSES.includes(apiStatus);

// Redirect to section 0 if api is no longer connected. Do not persist - user might want to land
// on this section again on subsequent visits.
// on this section again on subsequent visits. Forces redirect on tab change if that tab was on a
// different section.
useEffect(() => {
if (apiInactive) {
setActiveSection(0, false);
}
}, [apiStatus]);
}, [apiStatus, activeTabId]);
};

0 comments on commit 6bfa54b

Please sign in to comment.