From 0c1215dbf0a3e50ecb295e60e305c857c4538255 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 22 Oct 2024 19:48:12 +0800 Subject: [PATCH] fix: boost modal use async modal Signed-off-by: Innei --- .../ui/modal/helper/async-loading.tsx | 55 +++++++++++++++---- .../ui/modal/helper/use-async-modal.tsx | 4 ++ apps/renderer/src/modules/boost/hooks.tsx | 13 ++++- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/apps/renderer/src/components/ui/modal/helper/async-loading.tsx b/apps/renderer/src/components/ui/modal/helper/async-loading.tsx index 5a1ba2e70..972029302 100644 --- a/apps/renderer/src/components/ui/modal/helper/async-loading.tsx +++ b/apps/renderer/src/components/ui/modal/helper/async-loading.tsx @@ -1,9 +1,11 @@ +import { m } from "framer-motion" import * as React from "react" import { useTranslation } from "react-i18next" import { Button } from "~/components/ui/button" import { LoadingCircle } from "~/components/ui/loading" import { useCurrentModal } from "~/components/ui/modal" +import { stopPropagation } from "~/lib/dom" import { createErrorToaster } from "~/lib/error-parser" import { Tooltip, TooltipContent, TooltipTrigger } from "../../tooltip" @@ -20,6 +22,20 @@ interface AsyncModalContentProps { loadingComponent?: React.ReactNode } +function useCountdown(durationInSeconds: number): boolean { + const [isFinished, setIsFinished] = React.useState(false) + + React.useEffect(() => { + const timer = setTimeout(() => { + setIsFinished(true) + }, durationInSeconds * 1000) + + return () => clearTimeout(timer) + }, [durationInSeconds]) + + return isFinished +} + export function AsyncModalContent({ queryResult, renderContent, @@ -28,6 +44,7 @@ export function AsyncModalContent({ const { data, isLoading, error, refetch } = queryResult const { dismiss } = useCurrentModal() + const shouldShowCloseButton = useCountdown(2) React.useEffect(() => { if (error) { createErrorToaster()(error) @@ -38,16 +55,23 @@ export function AsyncModalContent({ if (isLoading || !data) { return ( loadingComponent || ( -
+
-
+ {!!error && ( - + @@ -55,15 +79,22 @@ export function AsyncModalContent({ {t("retry")} )} - - - - - {t("close")} - -
+ {(shouldShowCloseButton || !!error) && ( + + + + + {t("close")} + + )} +
) ) diff --git a/apps/renderer/src/components/ui/modal/helper/use-async-modal.tsx b/apps/renderer/src/components/ui/modal/helper/use-async-modal.tsx index 960f7f922..381e9af16 100644 --- a/apps/renderer/src/components/ui/modal/helper/use-async-modal.tsx +++ b/apps/renderer/src/components/ui/modal/helper/use-async-modal.tsx @@ -17,6 +17,9 @@ export type AsyncModalOptions = { icon?: (data: T) => React.ReactNode useDataFetcher: () => UseAsyncFetcher content: FC + + // Modal options + overlay?: boolean } const AsyncModalContext = createContext>(null!) export const useAsyncModal = () => { @@ -32,6 +35,7 @@ export const useAsyncModal = () => { ), title: "Loading...", CustomModalComponent: NoopChildren, + overlay: options.overlay, }) }) } diff --git a/apps/renderer/src/modules/boost/hooks.tsx b/apps/renderer/src/modules/boost/hooks.tsx index fa4a4ce1b..a9888ecdc 100644 --- a/apps/renderer/src/modules/boost/hooks.tsx +++ b/apps/renderer/src/modules/boost/hooks.tsx @@ -1,19 +1,26 @@ import { useCallback } from "react" -import { useModalStack } from "~/components/ui/modal" +import { useAsyncModal } from "~/components/ui/modal/helper/use-async-modal" +import { useAuthQuery } from "~/hooks/common" +import { boosts } from "~/queries/boosts" import { BoostModalContent } from "./modal" export const useBoostModal = () => { - const { present } = useModalStack() + const present = useAsyncModal() return useCallback( (feedId: string) => { - present({ + const useDataFetcher = () => useAuthQuery(boosts.getStatus({ feedId })) + + type ResponseType = Awaited["fn"]>> + + present({ id: "boost", title: "Boost", content: () => , overlay: true, + useDataFetcher, }) }, [present],