import { useEffect, useRef, useState } 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 Article from "~components/ui/article"; import { add } from "~components/ui/message/utils"; import styles from "./read.module.less"; interface FormValue { title: string; link: string; desc: string; tips: string; author: string; image: string; sitename: string; tags: string; raw_content?: string; } const getInfo = async () => { const [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true }); if (tab) { return await chrome.tabs.sendMessage(tab.id, { type: 'toolbox:getInfo' }); } } const submitForm = (body: FormValue) => { sendToBackground({ name: "read", body: { action: "addRead", values: body, }, }).then((res) => { add({ content: res.msg, }); }).catch((e) => { if (e instanceof Error) { add({ content: e.message, }); } }); } function Read() { const formRef = useRef(); const [htmlContent, setHtmlContent] = useState(""); const { bindInput, setValues, onSubmit } = useForm({ initialValues: { tags: "", }, }); useEffect(() => { getInfo().then((res) => { if (!res) { return; } setValues(res); res.raw_content && setHtmlContent(res.raw_content); }) }, []); return (
submitForm({ ...values, raw_content: htmlContent }))}>
); } export default Read;