From 4a83e32081f279662f59cc91c5d3b0966e43f3e6 Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Thu, 29 Aug 2024 00:01:52 +0800 Subject: [PATCH] feat(ui): use new alert modal for addon installs --- src/core/ui/components/AddonPage.tsx | 89 ++++++++++++++++---- src/core/ui/settings/pages/Plugins/index.tsx | 12 ++- src/lib/utils/isValidHttpUrl.ts | 12 +++ 3 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 src/lib/utils/isValidHttpUrl.ts diff --git a/src/core/ui/components/AddonPage.tsx b/src/core/ui/components/AddonPage.tsx index b5dec66..170d675 100644 --- a/src/core/ui/components/AddonPage.tsx +++ b/src/core/ui/components/AddonPage.tsx @@ -1,20 +1,20 @@ -import { Strings } from "@core/i18n"; import { CardWrapper } from "@core/ui/components/AddonCard"; import { findAssetId } from "@lib/api/assets"; import { settings } from "@lib/api/settings"; import { useProxy } from "@lib/api/storage"; -import { HTTP_REGEX_MULTI } from "@lib/utils/constants"; +import isValidHttpUrl from "@lib/utils/isValidHttpUrl"; import { lazyDestructure } from "@lib/utils/lazy"; import { findByProps } from "@metro"; import { clipboard } from "@metro/common"; -import { Button, FlashList, FloatingActionButton, HelpMessage, IconButton, Text } from "@metro/common/components"; -import { showInputAlert } from "@ui/alerts"; +import { Button, FlashList, FloatingActionButton, HelpMessage, IconButton, Stack, Text, TextInput } from "@metro/common/components"; import { ErrorBoundary, Search } from "@ui/components"; import fuzzysort from "fuzzysort"; import { ComponentType, ReactNode, useCallback, useMemo } from "react"; -import { Image, View } from "react-native"; +import { Image, ScrollView, View } from "react-native"; const { showSimpleActionSheet, hideActionSheet } = lazyDestructure(() => findByProps("showSimpleActionSheet")); +const { openAlert, dismissAlert } = lazyDestructure(() => findByProps("openAlert", "dismissAlert")); +const { AlertModal, AlertActionButton } = lazyDestructure(() => findByProps("AlertModal", "AlertActions")); type SearchKeywords = Array string)>; @@ -39,6 +39,72 @@ interface AddonPageProps { ListFooterComponent?: ComponentType; } +function InputAlert(props: { label: string, fetchFn: (url: string) => Promise }) { + const [value, setValue] = React.useState(""); + const [error, setError] = React.useState(""); + const [isFetching, setIsFetching] = React.useState(false); + + function onConfirmWrapper() { + setIsFetching(true); + + props.fetchFn(value) + .then(() => dismissAlert("AddonInputAlert")) + .catch((e: unknown) => e instanceof Error ? setError(e.message) : String(e)) + .finally(() => setIsFetching(false)); + } + + return + { + setValue(v); + if (error) setError(""); + }} + returnKeyType="done" + onSubmitEditing={onConfirmWrapper} + state={error ? "error" : undefined} + errorMessage={error || undefined} + /> + +