From 7706cfed9a112e22a39264f17878fa71ab2826ec Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Wed, 18 Sep 2024 13:00:20 +0500 Subject: [PATCH] mobile: new toast --- apps/mobile/app/components/toast/index.js | 235 --------------------- apps/mobile/app/components/toast/index.tsx | 221 +++++++++++++++++++ 2 files changed, 221 insertions(+), 235 deletions(-) delete mode 100644 apps/mobile/app/components/toast/index.js create mode 100644 apps/mobile/app/components/toast/index.tsx diff --git a/apps/mobile/app/components/toast/index.js b/apps/mobile/app/components/toast/index.js deleted file mode 100644 index 399fdd004..000000000 --- a/apps/mobile/app/components/toast/index.js +++ /dev/null @@ -1,235 +0,0 @@ -/* -This file is part of the Notesnook project (https://notesnook.com/) - -Copyright (C) 2023 Streetwriters (Private) Limited - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -import React, { useCallback, useEffect, useRef, useState } from "react"; -import { TouchableOpacity, View } from "react-native"; -import Icon from "react-native-vector-icons/MaterialCommunityIcons"; -import { notesnook } from "../../../e2e/test.ids"; -import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets"; -import { DDS } from "../../services/device-detection"; -import { - eSubscribeEvent, - eUnSubscribeEvent -} from "../../services/event-manager"; -import { useThemeColors } from "@notesnook/theme"; -import { getElevationStyle } from "../../utils/elevation"; -import { eHideToast, eShowToast } from "../../utils/events"; -import { SIZE } from "../../utils/size"; -import { Button } from "../ui/button"; -import Heading from "../ui/typography/heading"; -import Paragraph from "../ui/typography/paragraph"; -export const Toast = ({ context = "global" }) => { - const { colors } = useThemeColors(); - const [data, setData] = useState({}); - const insets = useGlobalSafeAreaInsets(); - const hideTimeout = useRef(); - const [visible, setVisible] = useState(false); - const toastMessages = useRef([]); - - const show = useCallback( - async (data) => { - if (!data) return; - if (data.context !== context) return; - if ( - toastMessages.current.findIndex((m) => m.message === data.message) >= 0 - ) { - return; - } - toastMessages.current.push(data); - if (toastMessages.current?.length > 1) return; - setData(data); - - setVisible(true); - if (hideTimeout.current) { - clearTimeout(hideTimeout.current); - } - hideTimeout.current = setTimeout(() => { - hide(); - }, data.duration); - }, - [context, hide] - ); - - const next = useCallback( - (data) => { - if (!data) { - hide(); - return; - } - setData(data); - if (hideTimeout.current) { - clearTimeout(hideTimeout.current); - } - hideTimeout.current = setTimeout(() => { - hide(); - }, data?.duration); - }, - [hide] - ); - - const hide = useCallback(() => { - if (hideTimeout.current) { - clearTimeout(hideTimeout.current); - } - let msg = - toastMessages.current.length > 1 ? toastMessages.current.shift() : null; - - if (msg) { - setVisible(false); - next(msg); - setTimeout(() => { - setVisible(true); - }, 300); - } else { - setVisible(false); - toastMessages.current.shift(); - setTimeout(() => { - setData({}); - if (hideTimeout.current) { - clearTimeout(hideTimeout.current); - } - }, 100); - } - }, [next]); - - useEffect(() => { - eSubscribeEvent(eShowToast, show); - eSubscribeEvent(eHideToast, hide); - return () => { - toastMessages.current = []; - eUnSubscribeEvent(eShowToast, show); - eUnSubscribeEvent(eHideToast, hide); - }; - }, [hide, show]); - - return ( - visible && ( - - - - - - - - - {data?.heading ? ( - { - hide(); - }} - > - {data.heading} - - ) : null} - - {data?.message ? ( - { - hide(); - }} - > - {data.message} - - ) : null} - - - - {data.func ? ( -