Skip to content

Commit

Permalink
fix: stop infinite loop on api error
Browse files Browse the repository at this point in the history
  • Loading branch information
Saelmala committed Nov 21, 2024
1 parent 1c36f01 commit 9e4c8be
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const App = () => {
const { denied, permission } = useDevicePermissions({ continueToApp });
const initializedGlobalState = useInitializeGlobalStateReducer();
const [, dispatch] = initializedGlobalState;
const [apiError, setApiError] = useState(false);

useFetchDevices({
dispatch,
Expand Down Expand Up @@ -101,12 +102,22 @@ const App = () => {
/>
</DisplayBoxPositioningContainer>
)}
{permission && !denied && (
{apiError && (
<DisplayBoxPositioningContainer>
<DisplayWarning
text="The server is not available. Reload page to try again."
title="Server not available"
/>
</DisplayBoxPositioningContainer>
)}
{permission && !denied && !apiError && (
<Routes>
<>
<Route
path="/"
element={<LandingPage />}
element={
<LandingPage setApiError={() => setApiError(true)} />
}
errorElement={<ErrorPage />}
/>
<Route
Expand Down
11 changes: 9 additions & 2 deletions src/components/landing-page/landing-page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from "react";
import { JoinProduction } from "./join-production.tsx";
import { CreateProduction } from "./create-production.tsx";
import { ProductionsListContainer } from "./productions-list-container.tsx";
Expand All @@ -6,8 +7,14 @@ import { DisplayContainer, FlexContainer } from "../generic-components.ts";
import { useGlobalState } from "../../global-state/context-provider.tsx";
import { isMobile } from "../../bowser.ts";

export const LandingPage = () => {
const [{ joinProductionOptions }] = useGlobalState();
export const LandingPage = ({ setApiError }: { setApiError: () => void }) => {
const [{ joinProductionOptions, apiError }] = useGlobalState();

useEffect(() => {
if (apiError) {
setApiError();
}
}, [apiError, setApiError]);

useNavigateToProduction(joinProductionOptions);

Expand Down
3 changes: 3 additions & 0 deletions src/components/landing-page/use-fetch-production-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export const useFetchProductionList = (filter?: GetProductionListFilter) => {
setError(null);
})
.catch((e) => {
dispatch({
type: "API_NOT_AVAILABLE",
});
setError(
e instanceof Error
? e
Expand Down
5 changes: 5 additions & 0 deletions src/global-state/global-state-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TJoinProductionOptions } from "../components/production-line/types.ts";
export type TGlobalStateAction =
| TPublishError
| TProductionCreated
| TApiNotAvailable
| TProductionListFetched
| TUpdateDevicesAction
| TUpdateJoinProductionOptions
Expand All @@ -25,6 +26,10 @@ export type TProductionCreated = {
type: "PRODUCTION_UPDATED";
};

export type TApiNotAvailable = {
type: "API_NOT_AVAILABLE";
};

export type TProductionListFetched = {
type: "PRODUCTION_LIST_FETCHED";
};
Expand Down
6 changes: 6 additions & 0 deletions src/global-state/global-state-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const initialGlobalState: TGlobalState = {
dominantSpeaker: null,
audioLevelAboveThreshold: false,
selectedProductionId: null,
apiError: false,
};

const globalReducer: Reducer<TGlobalState, TGlobalStateAction> = (
Expand All @@ -31,6 +32,11 @@ const globalReducer: Reducer<TGlobalState, TGlobalStateAction> = (
...state,
reloadProductionList: true,
};
case "API_NOT_AVAILABLE":
return {
...state,
apiError: new Error("API not available"),
};
case "PRODUCTION_LIST_FETCHED":
return {
...state,
Expand Down
1 change: 1 addition & 0 deletions src/global-state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export type TGlobalState = {
dominantSpeaker: string | null;
audioLevelAboveThreshold: boolean;
selectedProductionId: string | null;
apiError: Error | false;
};

0 comments on commit 9e4c8be

Please sign in to comment.