Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
[UI > InputAlert] Fix for new UI
Browse files Browse the repository at this point in the history
  • Loading branch information
maisymoe committed Dec 17, 2023
1 parent 80efe70 commit 963ddc5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/def.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ interface InputAlertProps {
cancelText?: string;
placeholder?: string;
initialValue?: string;
secureTextEntry?: boolean;
}

interface Author {
Expand Down
1 change: 0 additions & 1 deletion src/lib/plugins.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 2 additions & 2 deletions src/ui/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>, 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);
68 changes: 31 additions & 37 deletions src/ui/components/InputAlert.tsx
Original file line number Diff line number Diff line change
@@ -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<InternalInputAlertProps>;

// 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

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 };
}

export default function InputAlert({ title, confirmText, confirmColor, onConfirm, cancelText, placeholder, initialValue = "" }: InputAlertProps) {
const [value, setValue] = React.useState(initialValue);
const [error, setError] = React.useState("");
onConfirmWrapper(input: string) {
// @ts-expect-error
this.props.isLoading = true;

function onConfirmWrapper() {
const asyncOnConfirm = Promise.resolve(onConfirm(value))
const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input));

asyncOnConfirm.then(() => {
Alerts.close();
}).catch((e: Error) => {
setError(e.message);
this.setState({ error: e.message });
});
};

return (
<Alert
title={title}
confirmText={confirmText}
confirmColor={confirmColor}
isConfirmButtonDisabled={error.length !== 0}
onConfirm={onConfirmWrapper}
cancelText={cancelText}
onCancel={() => Alerts.close()}
>
<FormInput
placeholder={placeholder}
value={value}
onChangeText={(v: string) => {
setValue(v);
if (error) setError("");
}}
returnKeyType="done"
onSubmitEditing={onConfirmWrapper}
error={error}
autoFocus={true}
showBorder={true}
style={{ paddingVertical: 5, alignSelf: "stretch", paddingHorizontal: 0 }}
/>
</Alert>
);
};
// @ts-expect-error
this.props.isLoading = false;
};
}

0 comments on commit 963ddc5

Please sign in to comment.