From 28dbd4995788f329ec432b74a99ae728378b5d81 Mon Sep 17 00:00:00 2001 From: Snazzah <7025343+Snazzah@users.noreply.github.com> Date: Tue, 31 Aug 2021 04:00:45 +0000 Subject: [PATCH] Move creating bots to a popover --- src/context/intermediate/Intermediate.tsx | 2 + src/context/intermediate/Popovers.tsx | 4 + .../intermediate/popovers/CreateBot.tsx | 80 +++++++++++++++++++ src/pages/settings/panes/MyBots.tsx | 38 ++++----- src/pages/settings/panes/Panes.module.scss | 1 + 5 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 src/context/intermediate/popovers/CreateBot.tsx diff --git a/src/context/intermediate/Intermediate.tsx b/src/context/intermediate/Intermediate.tsx index a2e5b1ec..05211969 100644 --- a/src/context/intermediate/Intermediate.tsx +++ b/src/context/intermediate/Intermediate.tsx @@ -16,6 +16,7 @@ import { Action } from "../../components/ui/Modal"; import { Children } from "../../types/Preact"; import Modals from "./Modals"; +import { Bot } from "revolt-api/types/Bots"; export type Screen = | { id: "none" } @@ -90,6 +91,7 @@ export type Screen = | { id: "channel_info"; channel: Channel } | { id: "pending_requests"; users: User[] } | { id: "modify_account"; field: "username" | "email" | "password" } + | { id: "create_bot"; onCreate: (bot: Bot) => void } | { id: "server_identity"; server: Server; diff --git a/src/context/intermediate/Popovers.tsx b/src/context/intermediate/Popovers.tsx index 0834a1f8..2d0d8996 100644 --- a/src/context/intermediate/Popovers.tsx +++ b/src/context/intermediate/Popovers.tsx @@ -8,6 +8,7 @@ import { IntermediateContext, useIntermediate } from "./Intermediate"; import { SpecialInputModal } from "./modals/Input"; import { SpecialPromptModal } from "./modals/Prompt"; import { ChannelInfo } from "./popovers/ChannelInfo"; +import { CreateBotModal } from "./popovers/CreateBot"; import { ImageViewer } from "./popovers/ImageViewer"; import { ModifyAccountModal } from "./popovers/ModifyAccount"; import { PendingRequests } from "./popovers/PendingRequests"; @@ -42,6 +43,9 @@ export default function Popovers() { case "modify_account": // @ts-expect-error someone figure this out :) return ; + case "create_bot": + // @ts-expect-error someone figure this out :) + return ; case "special_prompt": // @ts-expect-error someone figure this out :) return ; diff --git a/src/context/intermediate/popovers/CreateBot.tsx b/src/context/intermediate/popovers/CreateBot.tsx new file mode 100644 index 00000000..1f63940b --- /dev/null +++ b/src/context/intermediate/popovers/CreateBot.tsx @@ -0,0 +1,80 @@ +import { SubmitHandler, useForm } from "react-hook-form"; +import { Bot } from "revolt-api/types/Bots"; + +import { useContext, useState } from "preact/hooks"; + +import Modal from "../../../components/ui/Modal"; +import Overline from "../../../components/ui/Overline"; + +import FormField from "../../../pages/login/FormField"; +import { AppContext } from "../../revoltjs/RevoltClient"; +import { takeError } from "../../revoltjs/util"; + +interface Props { + onClose: () => void; + onCreate: (bot: 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); + onClose(); + } catch (err) { + setError(takeError(err)); + } + }; + + return ( + + {/* Preact / React typing incompatabilities */} +
{ + e.preventDefault(); + handleSubmit( + onSubmit, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + )(e as any); + }}> + + {error && ( + + Failed to create a bot! + + )} + +
+ ); +} diff --git a/src/pages/settings/panes/MyBots.tsx b/src/pages/settings/panes/MyBots.tsx index 22d5d125..0c8cc734 100644 --- a/src/pages/settings/panes/MyBots.tsx +++ b/src/pages/settings/panes/MyBots.tsx @@ -1,4 +1,4 @@ -import { Key, Clipboard, Globe } from "@styled-icons/boxicons-regular"; +import { Key, Clipboard, Globe, Plus } from "@styled-icons/boxicons-regular"; import { LockAlt } from "@styled-icons/boxicons-solid"; import { observer } from "mobx-react-lite"; import { Bot } from "revolt-api/types/Bots"; @@ -21,7 +21,6 @@ import UserIcon from "../../../components/common/user/UserIcon"; import Button from "../../../components/ui/Button"; import Checkbox from "../../../components/ui/Checkbox"; import InputBox from "../../../components/ui/InputBox"; -import Overline from "../../../components/ui/Overline"; import Tip from "../../../components/ui/Tip"; import CategoryButton from "../../../components/ui/fluent/CategoryButton"; @@ -349,34 +348,25 @@ export const MyBots = observer(() => { // eslint-disable-next-line }, []); - const [name, setName] = useState(""); + const { openScreen } = useIntermediate(); return (
This section is under construction. - create a new bot -

- setName(e.currentTarget.value)} - /> -

-

- -

- my bots + } + onClick={() => + openScreen({ + id: "create_bot", + onCreate: (bot) => setBots([...(bots ?? []), bot]), + }) + } + action="chevron"> + Create a Bot + {bots?.map((bot) => { return (