import React, { useState } from "react"; import { Reference } from "@/utils/global"; import { copyToClipboard, getFullReference, getAllFullReferences, delteIndexUpdateBracketNumbersInDeltaKeepSelection, } from "@/utils/others/quillutils"; //删除文献按钮 import ParagraphDeleteButton from "@/components/ParagraphDeleteInterface"; //redux import { useAppDispatch, useAppSelector } from "@/app/store"; import { addReferenceRedux, removeReferenceRedux, clearReferencesRedux, swapReferencesRedux, } from "@/app/store/slices/authSlice"; //supabase import { submitPaper } from "@/utils/supabase/supabaseutils"; import { createClient } from "@/utils/supabase/client"; //i18n import { useTranslation } from "@/app/i18n/client"; type ReferenceListProps = { editor: any; lng: string; }; function ReferenceList({ editor, lng }: ReferenceListProps) { //i18n const { t } = useTranslation(lng); const [newTitle, setNewTitle] = useState(""); const [newAuthor, setNewAuthor] = useState(""); const [newYear, setNewYear] = useState(""); const [newPublisher, setNewPublisher] = useState(""); const [newUrl, setNewUrl] = useState(""); //redux const dispatch = useAppDispatch(); const references = useAppSelector((state) => state.auth.referencesRedux); const paperNumberRedux = useAppSelector( (state) => state.state.paperNumberRedux ); const isVip = useAppSelector((state) => state.state.isVip); //supabase const supabase = createClient(); function moveReferenceUp(index: number) { console.log("index", index); if (index <= 0 || index >= references.length) { console.log("index", index); return; // Index out of bounds or first element } dispatch(swapReferencesRedux({ indexA: index, indexB: index - 1 })); } function moveReferenceDown(index: number) { console.log("index", index); if (index < 0 || index >= references.length - 1) { console.log("index", index); return; // Index out of bounds or last element } dispatch(swapReferencesRedux({ indexA: index, indexB: index + 1 })); } function removeReferenceUpdateIndex(index: number, rmPg = false) { handleRemoveReference(index); delteIndexUpdateBracketNumbersInDeltaKeepSelection(editor, index, rmPg); } const handleAddReference = (newReference: Reference) => { dispatch(addReferenceRedux(newReference)); }; const handleRemoveReference = (index: number) => { dispatch(removeReferenceRedux(index)); }; const handleClearReferences = () => { dispatch(clearReferencesRedux()); }; // 状态标志,用于跟踪组件是否首次渲染 const [isFirstRender, setIsFirstRender] = useState(true); React.useEffect(() => { // 当组件首次渲染后,设置 isFirstRender 为 false setIsFirstRender(false); }, []); // 这个 useEffect 依赖数组为空,所以只会在组件首次渲染后运行 //监听references,如果发生变化,就提交到服务器 React.useEffect(() => { if (!isFirstRender && isVip) { submitPaper(supabase, undefined, references, paperNumberRedux); } }, [references]); return (
{/* 引用列表显示区域 */} {/* 表单区域 */}
{ e.preventDefault(); handleAddReference({ title: newTitle, author: newAuthor, year: newYear, venue: newPublisher, url: newUrl, }); // 清空表单 setNewTitle(""); setNewAuthor(""); setNewYear(""); setNewPublisher(""); setNewUrl(""); // submitPaper(supabase, undefined, references, paperNumberRedux); }} className="mb-6" >
setNewTitle(e.target.value)} placeholder={t("Title")} /> setNewAuthor(e.target.value)} placeholder={t("Author")} /> setNewYear(e.target.value)} placeholder={t("Year")} /> setNewPublisher(e.target.value)} placeholder={t("Publisher")} /> setNewUrl(e.target.value)} placeholder={t("Url")} />
); } export default ReferenceList;