import { SubmitHandler, useForm } from "react-hook-form"; import { API } from "revolt.js"; import { Text } from "preact-i18n"; import { useContext, useState } from "preact/hooks"; import { Category, Modal } from "@revoltchat/ui"; import FormField from "../../../pages/login/FormField"; import { I18nError } from "../../Locale"; import { AppContext } from "../../revoltjs/RevoltClient"; import { takeError } from "../../revoltjs/util"; interface Props { onClose: () => void; onCreate: (bot: API.Bot) => void; } interface FormInputs { name: string; } export function CreateBotModal({ onClose, onCreate }: Props) { const client = useContext(AppContext); const { handleSubmit, register, errors } = useForm(); const [error, setError] = useState(undefined); const onSubmit: SubmitHandler = async ({ name }) => { try { const { bot } = await client.bots.create({ name }); onCreate(bot); } catch (err) { setError(takeError(err)); } }; return ( } actions={[ { confirmation: true, palette: "accent", onClick: async () => { await handleSubmit(onSubmit); return true; }, children: , }, { palette: "plain", onClick: onClose, children: , }, ]}> {/* Preact / React typing incompatabilities */}
{ e.preventDefault(); handleSubmit( onSubmit, // eslint-disable-next-line @typescript-eslint/no-explicit-any )(e as any); }}> {error && ( {" "} · )}
); }