From 6db7f0e0e7f7229fe107c2a6480b7f3a8ec057da Mon Sep 17 00:00:00 2001 From: Avinash Lingaloo Date: Wed, 27 Apr 2022 16:43:47 +0400 Subject: [PATCH] using EphemeralStore and listeners to discard overlays --- app/screens/snack_bar/index.tsx | 2 ++ app/store/ephemeral_store.ts | 22 ++++++++++++++++++++++ app/utils/snack_bar/index.ts | 1 + index.ts | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/app/screens/snack_bar/index.tsx b/app/screens/snack_bar/index.tsx index 4e8cdc7357..ffd8024e39 100644 --- a/app/screens/snack_bar/index.tsx +++ b/app/screens/snack_bar/index.tsx @@ -14,6 +14,7 @@ import {BOTTOM_TAB_HEIGHT, TABLET_SIDEBAR_WIDTH} from '@constants/view'; import {useTheme} from '@context/theme'; import {useIsTablet} from '@hooks/device'; import {dismissOverlay} from '@screens/navigation'; +import EphemeralStore from '@store/ephemeral_store'; import {makeStyleSheetFromTheme} from '@utils/theme'; import {typography} from '@utils/typography'; @@ -140,6 +141,7 @@ const SnackBar = ({barType, componentId, onUndoPress, sourceScreen}: SnackBarPro useEffect(() => { setShowToast(true); + EphemeralStore.addNavigationOverlay(componentId); const t = setTimeout(() => { setShowToast(false); }, 3000); diff --git a/app/store/ephemeral_store.ts b/app/store/ephemeral_store.ts index 1201589095..1b6e0831f1 100644 --- a/app/store/ephemeral_store.ts +++ b/app/store/ephemeral_store.ts @@ -1,6 +1,8 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import {dismissOverlay} from '@screens/navigation'; + class EphemeralStore { allNavigationComponentIds: string[] = []; navigationComponentIdStack: string[] = []; @@ -9,6 +11,7 @@ class EphemeralStore { visibleTab = 'Home'; creatingChannel = false; creatingDMorGMTeammates: string[] = []; + overlays: string[] = []; addNavigationComponentId = (componentId: string) => { this.addToNavigationComponentIdStack(componentId); @@ -34,6 +37,10 @@ class EphemeralStore { this.navigationModalStack.unshift(componentId); }; + addNavigationOverlay = (componentId: string) => { + this.overlays.unshift(componentId); + }; + clearNavigationComponents = () => { this.navigationComponentIdStack = []; this.navigationModalStack = []; @@ -44,6 +51,10 @@ class EphemeralStore { this.navigationModalStack = []; }; + clearNavigationOverlays = () => { + this.overlays = []; + }; + getAllNavigationComponents = () => this.allNavigationComponentIds; getNavigationTopComponentId = () => { @@ -77,6 +88,17 @@ class EphemeralStore { } }; + removeNavigationOverlays = () => { + const hasOverlays = this.overlays.length; + + if (hasOverlays) { + this.overlays.forEach((o) => { + dismissOverlay(o); + }); + this.clearNavigationOverlays(); + } + }; + setVisibleTap = (tab: string) => { this.visibleTab = tab; }; diff --git a/app/utils/snack_bar/index.ts b/app/utils/snack_bar/index.ts index 7ae325b176..a99fdaed3b 100644 --- a/app/utils/snack_bar/index.ts +++ b/app/utils/snack_bar/index.ts @@ -9,6 +9,7 @@ type ShowSnackBarArgs = { onPress?: () => void; sourceScreen?: typeof Screens[keyof typeof Screens]; }; + export const showSnackBar = (passProps: ShowSnackBarArgs) => { const screen = Screens.SNACK_BAR; showOverlay(screen, passProps); diff --git a/index.ts b/index.ts index 2a2c98b22a..34cbba2672 100644 --- a/index.ts +++ b/index.ts @@ -98,6 +98,8 @@ function componentDidAppearListener({componentId, passProps}: ComponentDidAppear if (!(passProps as any)?.overlay) { EphemeralStore.addNavigationComponentId(componentId); } + + EphemeralStore.removeNavigationOverlays(); } function componentDidDisappearListener({componentId}: ComponentDidDisappearEvent) { @@ -112,4 +114,6 @@ function componentDidDisappearListener({componentId}: ComponentDidDisappearEvent DeviceEventEmitter.emit(Events.TAB_BAR_VISIBLE, true); } } + + EphemeralStore.removeNavigationOverlays(); }