From 963ddc50c54ddf6a6a19ef1578ff682c91d5aec8 Mon Sep 17 00:00:00 2001 From: maisy Date: Sun, 17 Dec 2023 03:39:57 +0000 Subject: [PATCH] [UI > InputAlert] Fix for new UI --- src/def.d.ts | 1 + src/lib/plugins.ts | 1 - src/ui/alerts.ts | 4 +- src/ui/components/InputAlert.tsx | 68 +++++++++++++++----------------- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/def.d.ts b/src/def.d.ts index 9d4c574..77bc58f 100644 --- a/src/def.d.ts +++ b/src/def.d.ts @@ -95,6 +95,7 @@ interface InputAlertProps { cancelText?: string; placeholder?: string; initialValue?: string; + secureTextEntry?: boolean; } interface Author { diff --git a/src/lib/plugins.ts b/src/lib/plugins.ts index bc81bce..a73c20b 100644 --- a/src/lib/plugins.ts +++ b/src/lib/plugins.ts @@ -1,7 +1,6 @@ import { PluginManifest, Plugin } from "@types"; import { safeFetch } from "@lib/utils"; import { awaitSyncWrapper, createMMKVBackend, createStorage, purgeStorage, wrapSync } from "@lib/storage"; -import { MMKVManager } from "@lib/native"; import { allSettled } from "@lib/polyfills"; import logger, { logModule } from "@lib/logger"; import settings from "@lib/settings"; diff --git a/src/ui/alerts.ts b/src/ui/alerts.ts index efb5a6f..b35a3fe 100644 --- a/src/ui/alerts.ts +++ b/src/ui/alerts.ts @@ -20,8 +20,8 @@ export function showConfirmationAlert(options: ConfirmationAlertOptions) { return Alerts.show(internalOptions); }; -export const showCustomAlert = (component: React.ComponentType, props: any) => Alerts.openLazy({ +export const showCustomAlert = (component: React.ComponentType, props: any) => Alerts.openLazy({ importer: async () => () => React.createElement(component, props), }); -export const showInputAlert = (options: InputAlertProps) => showCustomAlert(InputAlert as React.ComponentType, options); +export const showInputAlert = (options: InputAlertProps) => showCustomAlert(InputAlert, options); diff --git a/src/ui/components/InputAlert.tsx b/src/ui/components/InputAlert.tsx index 98f50f7..d425c3c 100644 --- a/src/ui/components/InputAlert.tsx +++ b/src/ui/components/InputAlert.tsx @@ -1,48 +1,42 @@ import { InputAlertProps } from "@types"; -import { findByProps } from "@metro/filters"; -import { Forms, Alert } from "@ui/components"; +import { findByName, findByProps } from "@metro/filters"; + +interface InternalInputAlertProps extends InputAlertProps { + onClose?: () => void; + onSubmit?: (input: string) => void; + isLoading?: boolean; +} -const { FormInput } = Forms; const Alerts = findByProps("openLazy", "close"); +const UserSettingsInputAlert = findByName("UserSettingsInputAlert") as React.ComponentClass; -export default function InputAlert({ title, confirmText, confirmColor, onConfirm, cancelText, placeholder, initialValue = "" }: InputAlertProps) { - const [value, setValue] = React.useState(initialValue); - const [error, setError] = React.useState(""); +// TODO: Moving to Discord's UserSettingsInputAlert here has some issues: +//* - Errors are not cleared upon typing +//* - The confirmText does not apply +//* - The confirm button is not disabled when there is an error - function onConfirmWrapper() { - const asyncOnConfirm = Promise.resolve(onConfirm(value)) +export default class InputAlert extends UserSettingsInputAlert { + constructor(props: InternalInputAlertProps) { + super(props); + props.secureTextEntry = false; + props.onClose = Alerts.close; + props.onSubmit = (i: string) => this.onConfirmWrapper(i); + this.state = { input: props.initialValue }; + } + + onConfirmWrapper(input: string) { + // @ts-expect-error + this.props.isLoading = true; + + const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input)); asyncOnConfirm.then(() => { Alerts.close(); }).catch((e: Error) => { - setError(e.message); + this.setState({ error: e.message }); }); - }; - return ( - Alerts.close()} - > - { - setValue(v); - if (error) setError(""); - }} - returnKeyType="done" - onSubmitEditing={onConfirmWrapper} - error={error} - autoFocus={true} - showBorder={true} - style={{ paddingVertical: 5, alignSelf: "stretch", paddingHorizontal: 0 }} - /> - - ); -}; \ No newline at end of file + // @ts-expect-error + this.props.isLoading = false; + }; +}