From 1fad306f672accdd67bd92d844ed1bb34b750fb3 Mon Sep 17 00:00:00 2001 From: Whitewater Date: Sun, 27 Apr 2025 15:17:50 +0800 Subject: [PATCH] fix: toast message handling on android (#3604) * fix(toast): ensure default duration is applied when showing toast messages * fix: handle potential undefined toastRefs and improve conflict warning * chore: correct spelling of 'dismiss' in ToastRef and related usages --- apps/mobile/src/components/ui/toast/CenteredToast.tsx | 2 +- apps/mobile/src/components/ui/toast/manager.tsx | 5 ++++- apps/mobile/src/components/ui/toast/types.ts | 2 +- apps/mobile/src/lib/toast.tsx | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/mobile/src/components/ui/toast/CenteredToast.tsx b/apps/mobile/src/components/ui/toast/CenteredToast.tsx index 9eb9ae6e6..32f95458f 100644 --- a/apps/mobile/src/components/ui/toast/CenteredToast.tsx +++ b/apps/mobile/src/components/ui/toast/CenteredToast.tsx @@ -14,7 +14,7 @@ export const CenteredToast = (props: ToastProps) => { const { register } = useContext(ToastActionContext) useEffect(() => { const disposer = register(props.currentIndex, { - dimiss: async () => {}, + dismiss: async () => {}, }) return () => { disposer() diff --git a/apps/mobile/src/components/ui/toast/manager.tsx b/apps/mobile/src/components/ui/toast/manager.tsx index 1f49c4293..d912af629 100644 --- a/apps/mobile/src/components/ui/toast/manager.tsx +++ b/apps/mobile/src/components/ui/toast/manager.tsx @@ -28,6 +28,9 @@ export class ToastManager { private toastRefs = {} as Record private register(currentIndex: number, ref: ToastRef) { + if (currentIndex in this.toastRefs) { + console.warn("Conflict toast ref", currentIndex, this.toastRefs[currentIndex], ref) + } this.toastRefs[currentIndex] = ref return () => { delete this.toastRefs[currentIndex] @@ -71,7 +74,7 @@ export class ToastManager { } setTimeout(async () => { - await this.toastRefs[index]!.dimiss() + await this.toastRefs[index]?.dismiss() this.remove(index) }, props.duration) } diff --git a/apps/mobile/src/components/ui/toast/types.ts b/apps/mobile/src/components/ui/toast/types.ts index 4402b0e1f..818addc9a 100644 --- a/apps/mobile/src/components/ui/toast/types.ts +++ b/apps/mobile/src/components/ui/toast/types.ts @@ -29,5 +29,5 @@ export type BottomToastProps = Partial & { } export interface ToastRef { - dimiss: () => Promise + dismiss: () => Promise } diff --git a/apps/mobile/src/lib/toast.tsx b/apps/mobile/src/lib/toast.tsx index 13a80199c..3c10c87b0 100644 --- a/apps/mobile/src/lib/toast.tsx +++ b/apps/mobile/src/lib/toast.tsx @@ -48,6 +48,7 @@ export const toast = { type, message, variant: "center-replace", + duration: options.duration ?? 1500, ...options, }) }