From fbde6f3d9216a7054c328580bc17f0adbeaa6e90 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 29 Dec 2022 20:47:22 +0800 Subject: [PATCH] feat: test api key modal --- src/app/services/server.ts | 4 +- .../setting/BotConfig/TestAPIKeyModal.tsx | 86 +++++++++++++++++++ src/routes/setting/BotConfig/index.tsx | 14 ++- 3 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 src/routes/setting/BotConfig/TestAPIKeyModal.tsx diff --git a/src/app/services/server.ts b/src/app/services/server.ts index 467c12a2..9fba430b 100644 --- a/src/app/services/server.ts +++ b/src/app/services/server.ts @@ -283,5 +283,7 @@ export const { useCheckLicenseMutation, useGetLicenseQuery, useGetLicensePaymentUrlMutation, - useLazyGetGeneratedLicenseQuery + useLazyGetGeneratedLicenseQuery, + useLazyGetBotRelatedChannelsQuery, + useSendMessageByBotMutation } = serverApi; diff --git a/src/routes/setting/BotConfig/TestAPIKeyModal.tsx b/src/routes/setting/BotConfig/TestAPIKeyModal.tsx new file mode 100644 index 00000000..a8afe27a --- /dev/null +++ b/src/routes/setting/BotConfig/TestAPIKeyModal.tsx @@ -0,0 +1,86 @@ +import { useEffect, useRef, useState } from 'react'; +// import { toast } from 'react-hot-toast'; +import { useTranslation } from 'react-i18next'; +import Modal from '../../../common/component/Modal'; +import Button from '../../../common/component/styled/Button'; +import Textarea from '../../../common/component/styled/Textarea'; +import StyledModal from '../../../common/component/styled/Modal'; +import { useLazyGetBotRelatedChannelsQuery, useSendMessageByBotMutation } from '../../../app/services/server'; +import clsx from 'clsx'; +import { MessageTypes } from '../../../app/config'; + +type Props = { + closeModal: () => void +} +const TestAPIKeyModal = ({ closeModal }: Props) => { + const [currCid, setCurrCid] = useState(null); + const [msgType, setMsgType] = useState("text"); + const [getChannels, { data }] = useLazyGetBotRelatedChannelsQuery(); + const [sendMessage] = useSendMessageByBotMutation(); + const inputRef = useRef(); + const msgInputRef = useRef(); + const [key, setKey] = useState(""); + // const { t } = useTranslation("setting", { keyPrefix: "bot" }); + const { t: ct } = useTranslation(); + const handleSetKey = () => { + const input = inputRef?.current; + if (input && input.value) { + setKey(input.value); + } + }; + const handleSetChannel = (cid: number) => { + setCurrCid(cid); + }; + const handleSetMsgType = (type: string) => { + setMsgType(type); + }; + const handleSend = () => { + const input = msgInputRef?.current; + if (input && input.value && currCid) { + sendMessage({ cid: currCid, api_key: key, type: msgType, content: input.value }); + } + }; + useEffect(() => { + if (key) { + getChannels({ api_key: key }); + } + }, [key]); + + + return ( + + + + + + } + > + {key ? (data ?
    + {data.map(({ gid, name, is_public }) => { + return
  • + # {name} {!is_public ? "🔒" : ""} +
  • ; + })} +
: null) + :