import React, { useState, useEffect } from "react"; import { useLocalStorage } from "react-use"; import { Reference } from "@/utils/global"; import { copyToClipboard, getFullReference, renderCitation, getAllFullReferences, delteIndexUpdateBracketNumbersInDeltaKeepSelection, } from "@/utils/others/quillutils"; //删除文献按钮 import ParagraphDeleteButton from "@/components/ParagraphDeleteInterface"; //redux import { useAppDispatch, useAppSelector } from "@/app/store"; import { addReferenceRedux, removeReferenceRedux, clearReferencesRedux, swapReferencesRedux, setReferencesRedux, } from "@/app/store/slices/authSlice"; import { setCitationStyle } from "@/app/store/slices/stateSlice"; //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; }; //引用转换 import Cite from "citation-js"; const citationStyles = [ { name: "中文", template: "custom-chinese" }, // 假设你有一个自定义的“中文”格式 { name: "APA", template: "apa" }, { name: "MLA", template: "mla" }, { name: "Chicago", template: "chicago" }, { name: "Harvard", template: "harvard" }, { name: "Vancouver", template: "vancouver" }, { name: "IEEE", template: "ieee" }, ]; 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); const citationStyle = useAppSelector((state) => state.state.citationStyle); //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]); async function generateCitation(doi, style) { try { const citation = await Cite.async(doi); const output = citation.format("bibliography", { format: "text", template: style, lang: "en-US", }); return output; } catch (error) { console.error("Error generating citation:", error); return ""; // Return an empty string in case of error } } useEffect(() => { const fetchCitations = async () => { const updatedReferences = await Promise.all( references.map(async (ref) => { // 检查是否已经有当前风格的引用 if (!ref[citationStyle]) { // 如果没有,则生成新的引用 const citationText = await generateCitation(ref.doi, citationStyle); return { ...ref, [citationStyle]: citationText }; // 添加新的引用到对象 } return ref; // 如果已有引用,则不做改变 }) ); dispatch(setReferencesRedux(updatedReferences)); }; fetchCitations(); }, [citationStyle]); const handleStyleChange = (event) => { dispatch(setCitationStyle(event.target.value)); }; return (