import { useState, 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"; import Placeholder from "~components/ui/placeholder"; import styles from "./toy.module.less"; interface FormValue { name: string; ename: string; project: string; made: string; sale: string; type: number; desc: string; } const getInfo = async () => { let tab: chrome.tabs.Tab; [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true }); if (tab && !tab.url.includes("chrome")) { return await chrome.tabs.sendMessage(tab.id, { type: 'toolbox:getBiliToy' }); } } function Toy() { const formRef = useRef(); const { bindInput, setValues, onSubmit } = useForm({ initialValues: { type: 3, }, }); const [imgs, setImgs] = useState([]); const [currentImg, setCurrentImg] = useState(0); const submitForm = (body: FormValue) => { sendToBackground({ name: "bili", body: { action: "submitAddForm", values: { ...body, imageUrl: imgs[0], }, }, }).then((res) => { if (res.status === "Success") { add({ content: "提交成功", }); } else { add({ content: res.msg, }); } }); } useEffect(() => { getInfo().then((res) => { if (!res) { return; } const { images, ...others } = res; setImgs(images); setCurrentImg(0); // ! 必须等组件渲染出来才能设置值,这里是个问题 window.requestAnimationFrame(() => { setValues(others); }); }) }, []); return (
{imgs.map((item, index) => ( setCurrentImg(index)} /> ))}
); } export default Toy;