From 03b5f0f2f9705d1c65027d742bc28e18f237e0ad Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 17 Mar 2025 11:26:16 +0100 Subject: [PATCH] Fixup: error boundary context not needed, local error resets already --- src/room/GroupCallErrorBoundaryContext.tsx | 18 ------- .../GroupCallErrorBoundaryContextProvider.tsx | 54 ------------------- src/room/GroupCallView.tsx | 11 +--- ...test.tsx => useCallErrorBoundary.test.tsx} | 9 ++-- src/room/useCallErrorBoundary.ts | 29 +--------- 5 files changed, 5 insertions(+), 116 deletions(-) delete mode 100644 src/room/GroupCallErrorBoundaryContext.tsx delete mode 100644 src/room/GroupCallErrorBoundaryContextProvider.tsx rename src/room/{GroupCallErrorBoundaryContextProvider.test.tsx => useCallErrorBoundary.test.tsx} (80%) diff --git a/src/room/GroupCallErrorBoundaryContext.tsx b/src/room/GroupCallErrorBoundaryContext.tsx deleted file mode 100644 index f1dcf461..00000000 --- a/src/room/GroupCallErrorBoundaryContext.tsx +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright 2025 New Vector Ltd. - -SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial -Please see LICENSE in the repository root for full details. -*/ - -import { createContext } from "react"; - -import { type ElementCallError } from "../utils/errors.ts"; - -export type GroupCallErrorBoundaryContextType = { - subscribe: (cb: (error: ElementCallError) => void) => () => void; - notifyHandled: (error: ElementCallError) => void; -}; - -export const GroupCallErrorBoundaryContext = - createContext(null); diff --git a/src/room/GroupCallErrorBoundaryContextProvider.tsx b/src/room/GroupCallErrorBoundaryContextProvider.tsx deleted file mode 100644 index b7292624..00000000 --- a/src/room/GroupCallErrorBoundaryContextProvider.tsx +++ /dev/null @@ -1,54 +0,0 @@ -/* -Copyright 2025 New Vector Ltd. - -SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial -Please see LICENSE in the repository root for full details. -*/ - -import { - type FC, - type PropsWithChildren, - useCallback, - useMemo, - useRef, -} from "react"; - -import type { ElementCallError } from "../utils/errors.ts"; -import { - GroupCallErrorBoundaryContext, - type GroupCallErrorBoundaryContextType, -} from "./GroupCallErrorBoundaryContext.tsx"; - -export const GroupCallErrorBoundaryContextProvider: FC = ({ - children, -}) => { - const subscribers = useRef void>>(new Set()); - - // Register a component for updates - const subscribe = useCallback( - (cb: (error: ElementCallError) => void): (() => void) => { - subscribers.current.add(cb); - return (): boolean => subscribers.current.delete(cb); // Unsubscribe function - }, - [], - ); - - // Notify all subscribers - const notify = useCallback((error: ElementCallError) => { - subscribers.current.forEach((callback) => callback(error)); - }, []); - - const context: GroupCallErrorBoundaryContextType = useMemo( - () => ({ - notifyHandled: notify, - subscribe, - }), - [subscribe, notify], - ); - - return ( - - {children} - - ); -}; diff --git a/src/room/GroupCallView.tsx b/src/room/GroupCallView.tsx index fad91065..49b3173d 100644 --- a/src/room/GroupCallView.tsx +++ b/src/room/GroupCallView.tsx @@ -68,7 +68,6 @@ import { useSetting, } from "../settings/settings"; import { useTypedEventEmitter } from "../useEvents"; -import { GroupCallErrorBoundaryContextProvider } from "./GroupCallErrorBoundaryContextProvider.tsx"; import { useGroupCallErrorBoundary } from "./useCallErrorBoundary.ts"; declare global { @@ -90,15 +89,7 @@ interface Props { widget: WidgetHelpers | null; } -export const GroupCallView: FC = (props) => { - return ( - - - - ); -}; - -export const GroupCallViewInner: FC = ({ +export const GroupCallView: FC = ({ client, isPasswordlessUser, confineToRoom, diff --git a/src/room/GroupCallErrorBoundaryContextProvider.test.tsx b/src/room/useCallErrorBoundary.test.tsx similarity index 80% rename from src/room/GroupCallErrorBoundaryContextProvider.test.tsx rename to src/room/useCallErrorBoundary.test.tsx index 116aa7db..eccb8039 100644 --- a/src/room/GroupCallErrorBoundaryContextProvider.test.tsx +++ b/src/room/useCallErrorBoundary.test.tsx @@ -11,7 +11,6 @@ import { type ReactElement, useCallback } from "react"; import userEvent from "@testing-library/user-event"; import { BrowserRouter } from "react-router-dom"; -import { GroupCallErrorBoundaryContextProvider } from "./GroupCallErrorBoundaryContextProvider.tsx"; import { GroupCallErrorBoundary } from "./GroupCallErrorBoundary.tsx"; import { useGroupCallErrorBoundary } from "./useCallErrorBoundary.ts"; import { ConnectionLostError } from "../utils/errors.ts"; @@ -36,11 +35,9 @@ it("should show async error", async () => { render( - - - - - + + + , ); diff --git a/src/room/useCallErrorBoundary.ts b/src/room/useCallErrorBoundary.ts index b8b0a034..f89abf77 100644 --- a/src/room/useCallErrorBoundary.ts +++ b/src/room/useCallErrorBoundary.ts @@ -5,44 +5,17 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ -import { useCallback, useContext, useEffect, useMemo, useState } from "react"; +import { useMemo, useState } from "react"; import type { ElementCallError } from "../utils/errors.ts"; -import { GroupCallErrorBoundaryContext } from "./GroupCallErrorBoundaryContext.tsx"; export type UseErrorBoundaryApi = { showGroupCallErrorBoundary: (error: ElementCallError) => void; }; export function useGroupCallErrorBoundary(): UseErrorBoundaryApi { - const context = useContext(GroupCallErrorBoundaryContext); - - if (!context) - throw new Error( - "useGroupCallErrorBoundary must be used within an GoupCallErrorBoundary", - ); - const [error, setError] = useState(null); - const resetErrorIfNeeded = useCallback( - (handled: ElementCallError): void => { - // There might be several useGroupCallErrorBoundary in the tree, - // so only clear our state if it's the one we're handling? - if (error && handled === error) { - // reset current state - setError(null); - } - }, - [error], - ); - - useEffect(() => { - // return a function to unsubscribe - return context.subscribe((error: ElementCallError): void => { - resetErrorIfNeeded(error); - }); - }, [resetErrorIfNeeded, context]); - const memoized: UseErrorBoundaryApi = useMemo( () => ({ showGroupCallErrorBoundary: (error: ElementCallError) => setError(error),