From 6632c839e4d4d45b8755cfb67e8346421afea4f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E8=B6=A3=E4=BF=9D=E7=BD=97?= Date: Wed, 24 Apr 2024 14:17:42 +0800 Subject: [PATCH] Add say form submit & popup entry --- assets/icons.tsx | 6 +++ background/messages/say.ts | 26 +++++++++++++ components/biz/say/index.tsx | 74 ++++++++++++++++++++++++++++++++++++ popup/menu.tsx | 9 ++++- sidepanel/index.tsx | 7 +++- 5 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 background/messages/say.ts create mode 100644 components/biz/say/index.tsx diff --git a/assets/icons.tsx b/assets/icons.tsx index 2f50bbc..ebd15e7 100644 --- a/assets/icons.tsx +++ b/assets/icons.tsx @@ -16,6 +16,12 @@ export function IconBili() { ); } +export function IconSay() { + return ( + + ); +} + export function IconBack() { return ( diff --git a/background/messages/say.ts b/background/messages/say.ts new file mode 100644 index 0000000..f4ddf61 --- /dev/null +++ b/background/messages/say.ts @@ -0,0 +1,26 @@ +import type { PlasmoMessaging } from "@plasmohq/messaging"; + +const handler: PlasmoMessaging.MessageHandler = async (req, res) => { + const { token, siteUrl } = await chrome.storage.local.get(["token", "siteUrl"]); + + if (req.body.action === "addSay") { + const { values } = req.body; + const formData = new FormData(); + + Object.keys(values).forEach((item) => { + formData.append(item, String(values[item])); + }); + + const addReq = await fetch(`${siteUrl}/api/say/add`, { + method: "POST", + body: formData, + headers: { + "paul-token-code": token, + }, + }).then((res) => res.json()); + + res.send(addReq); + } +} + +export default handler; diff --git a/components/biz/say/index.tsx b/components/biz/say/index.tsx new file mode 100644 index 0000000..3cc4ade --- /dev/null +++ b/components/biz/say/index.tsx @@ -0,0 +1,74 @@ +import { useEffect, useRef } from "react"; +import { sendToBackground } from "@plasmohq/messaging"; +import useForm from "~hooks/useForm"; +import Tab from "~components/ui/tab"; +import Form from "~components/ui/form"; +import { add } from "~components/ui/message/utils"; + +interface FormValue { + content: string; + author: string; + origin: string; + links: string; + is_comment: boolean; +} + +const submitForm = (body: FormValue) => { + sendToBackground({ + name: "say", + body: { + action: "addSay", + values: body, + }, + }).then((res) => { + add({ + content: res.msg, + }); + }).catch((e) => { + if (e instanceof Error) { + add({ + content: e.message, + }); + } + }); +} + +function Say() { + const formRef = useRef(); + + const { bindInput, onSubmit } = useForm({ + initialValues: { + is_comment: true, + }, + }); + + return ( + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + + +
+ ); +} + +export default Say; diff --git a/popup/menu.tsx b/popup/menu.tsx index 2bee551..89d9fd2 100644 --- a/popup/menu.tsx +++ b/popup/menu.tsx @@ -1,4 +1,4 @@ -import { IconBili, IconPaul, IconRead, IconSetting } from "~assets/icons"; +import { IconBili, IconPaul, IconRead, IconSay, IconSetting } from "~assets/icons"; import styles from "./popup.module.less"; const items = [ @@ -8,10 +8,15 @@ const items = [ value: "read", }, { - name: "增加手办", + name: "手办", icon: , value: "toy", }, + { + name: "语录", + icon: , + value: "say", + }, { name: "访问网站", icon: , diff --git a/sidepanel/index.tsx b/sidepanel/index.tsx index 12ab15d..fb1db06 100644 --- a/sidepanel/index.tsx +++ b/sidepanel/index.tsx @@ -1,6 +1,7 @@ import { useState } from "react"; import Read from "~components/biz/read"; import Toy from "~components/biz/toy"; +import Say from "~components/biz/say"; import Message from "~components/ui/message"; import { clsn } from "~utils"; @@ -10,7 +11,7 @@ import styles from "./sidepanel.module.less"; const initialType = new URLSearchParams(location.search).get("type") || "read"; const tabItems = [ - { title: "在读", value: "read" }, + { title: "在看", value: "read" }, { title: "手办", value: "toy" }, { title: "语录", value: "say" }, ]; @@ -22,7 +23,8 @@ function SidePanel() { );