From d2c6edacb6464b52513dbe467c8b5713abd6a9fc Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 28 Feb 2024 13:27:54 -0600 Subject: [PATCH] Protect against non functions being passed to close callback (#3019) --- src/components/Dialog/index.tsx | 15 ++++++++++++--- src/components/Dialog/index.web.tsx | 2 +- src/components/Dialog/types.ts | 12 ++++++------ src/components/Prompt.tsx | 2 +- .../StepModeration/AdultContentEnabledPref.tsx | 2 +- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/components/Dialog/index.tsx b/src/components/Dialog/index.tsx index 27f43afd..5c035027 100644 --- a/src/components/Dialog/index.tsx +++ b/src/components/Dialog/index.tsx @@ -11,6 +11,7 @@ import {useSafeAreaInsets} from 'react-native-safe-area-context' import {useTheme, atoms as a, flatten} from '#/alf' import {Portal} from '#/components/Portal' import {createInput} from '#/components/forms/TextField' +import {logger} from '#/logger' import { DialogOuterProps, @@ -56,7 +57,7 @@ export function Outer({ ) const close = React.useCallback(cb => { - if (cb) { + if (cb && typeof cb === 'function') { closeCallback.current = cb } sheet.current?.close() @@ -74,8 +75,16 @@ export function Outer({ const onChange = React.useCallback( (index: number) => { if (index === -1) { - closeCallback.current?.() - closeCallback.current = undefined + try { + closeCallback.current?.() + } catch (e: any) { + logger.error(`Dialog closeCallback failed`, { + message: e.message, + }) + } finally { + closeCallback.current = undefined + } + onClose?.() setOpenIndex(-1) } diff --git a/src/components/Dialog/index.web.tsx b/src/components/Dialog/index.web.tsx index fa29fbd6..ff05fed9 100644 --- a/src/components/Dialog/index.web.tsx +++ b/src/components/Dialog/index.web.tsx @@ -190,7 +190,7 @@ export function Close() { variant="ghost" color="secondary" shape="round" - onPress={close} + onPress={() => close()} label={_(msg`Close active dialog`)}> diff --git a/src/components/Dialog/types.ts b/src/components/Dialog/types.ts index 75ba825a..161c0373 100644 --- a/src/components/Dialog/types.ts +++ b/src/components/Dialog/types.ts @@ -6,8 +6,13 @@ import {ViewStyleProp} from '#/alf' type A11yProps = Required +export type DialogControlProps = { + open: (options?: DialogControlOpenOptions) => void + close: (callback?: () => void) => void +} + export type DialogContextProps = { - close: () => void + close: DialogControlProps['close'] } export type DialogControlOpenOptions = { @@ -20,11 +25,6 @@ export type DialogControlOpenOptions = { index?: number } -export type DialogControlProps = { - open: (options?: DialogControlOpenOptions) => void - close: (callback?: () => void) => void -} - export type DialogOuterProps = { control: { ref: React.RefObject diff --git a/src/components/Prompt.tsx b/src/components/Prompt.tsx index 41167910..8e55bd83 100644 --- a/src/components/Prompt.tsx +++ b/src/components/Prompt.tsx @@ -89,7 +89,7 @@ export function Cancel({ color="secondary" size="small" label={_(msg`Cancel`)} - onPress={close}> + onPress={() => close()}> {children} ) diff --git a/src/screens/Onboarding/StepModeration/AdultContentEnabledPref.tsx b/src/screens/Onboarding/StepModeration/AdultContentEnabledPref.tsx index b38b3df1..360025c0 100644 --- a/src/screens/Onboarding/StepModeration/AdultContentEnabledPref.tsx +++ b/src/screens/Onboarding/StepModeration/AdultContentEnabledPref.tsx @@ -114,7 +114,7 @@ export function AdultContentEnabledPref({ - OK + prompt.close()}>OK