paper-ai-release-24-07-21/components/ReferenceList.tsx

238 lines
7.7 KiB
TypeScript
Raw Normal View History

2024-01-18 15:46:18 +08:00
import React, { useState } from "react";
import { Reference } from "@/utils/global";
2024-01-19 15:36:41 +08:00
import {
copyToClipboard,
formatReferenceForCopy,
2024-01-19 16:27:34 +08:00
formatAllReferencesForCopy,
2024-01-20 11:13:12 +08:00
delteIndexUpdateBracketNumbersInDeltaKeepSelection,
2024-01-19 15:36:41 +08:00
} from "@/utils/others/quillutils";
//删除文献按钮
import ParagraphDeleteButton from "@/components/ParagraphDeleteInterface";
//redux
import { useAppDispatch, useAppSelector } from "@/app/store";
import {
addReferenceRedux,
removeReferenceRedux,
clearReferencesRedux,
2024-01-29 15:01:43 +08:00
swapReferencesRedux,
} from "@/app/store/slices/authSlice";
2024-02-09 23:01:05 +08:00
//supabase
import { submitPaper } from "@/utils/supabase/supabaseutils";
import { createClient } from "@/utils/supabase/client";
2024-01-18 15:46:18 +08:00
type ReferenceListProps = {
2024-01-20 11:13:12 +08:00
editor: any;
2024-01-18 15:46:18 +08:00
};
function ReferenceList({ editor }: ReferenceListProps) {
// console.log("editor in ReferenceList", editor);
2024-01-18 15:46:18 +08:00
const [newTitle, setNewTitle] = useState("");
const [newAuthor, setNewAuthor] = useState("");
2024-01-20 13:43:31 +08:00
const [newYear, setNewYear] = useState("");
2024-01-18 15:46:18 +08:00
const [newPublisher, setNewPublisher] = useState("");
const [newUrl, setNewUrl] = useState("");
//redux
const dispatch = useAppDispatch();
const references = useAppSelector((state) => state.auth.referencesRedux);
2024-02-09 23:01:05 +08:00
const paperNumberRedux = useAppSelector(
(state) => state.state.paperNumberRedux
);
//supabase
const supabase = createClient();
2024-01-20 11:13:12 +08:00
function moveReferenceUp(index: number) {
2024-01-29 15:01:43 +08:00
console.log("index", index);
2024-01-20 11:13:12 +08:00
2024-01-29 15:01:43 +08:00
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 }));
2024-01-20 11:13:12 +08:00
}
function moveReferenceDown(index: number) {
2024-01-29 15:01:43 +08:00
console.log("index", index);
if (index < 0 || index >= references.length - 1) {
console.log("index", index);
return; // Index out of bounds or last element
}
2024-01-20 11:13:12 +08:00
2024-01-29 15:01:43 +08:00
dispatch(swapReferencesRedux({ indexA: index, indexB: index + 1 }));
2024-01-20 11:13:12 +08:00
}
function removeReferenceUpdateIndex(index: number, rmPg = false) {
handleRemoveReference(index);
delteIndexUpdateBracketNumbersInDeltaKeepSelection(editor, index, rmPg);
2024-01-20 11:13:12 +08:00
}
const handleAddReference = (newReference: Reference) => {
dispatch(addReferenceRedux(newReference));
};
const handleRemoveReference = (index: number) => {
dispatch(removeReferenceRedux(index));
};
const handleClearReferences = () => {
dispatch(clearReferencesRedux());
};
2024-02-09 23:01:05 +08:00
//监听references如果发生变化就提交到服务器
React.useEffect(() => {
submitPaper(supabase, undefined, references, paperNumberRedux);
}, [references]);
2024-01-18 15:46:18 +08:00
return (
2024-01-19 15:36:41 +08:00
<div className="container mx-auto p-4">
{/* 表单区域 */}
2024-01-18 15:46:18 +08:00
<form
2024-02-09 23:01:05 +08:00
onSubmit={async (e) => {
2024-01-18 15:46:18 +08:00
e.preventDefault();
handleAddReference({
2024-01-18 15:46:18 +08:00
title: newTitle,
author: newAuthor,
year: newYear,
2024-01-18 23:22:23 +08:00
venue: newPublisher,
2024-01-18 15:46:18 +08:00
url: newUrl,
});
2024-01-19 15:36:41 +08:00
// 清空表单
2024-01-18 15:46:18 +08:00
setNewTitle("");
setNewAuthor("");
2024-01-20 13:43:31 +08:00
setNewYear("");
2024-01-19 15:36:41 +08:00
setNewPublisher("");
setNewUrl("");
2024-02-09 23:01:05 +08:00
// submitPaper(supabase, undefined, references, paperNumberRedux);
2024-01-18 15:46:18 +08:00
}}
2024-01-19 15:36:41 +08:00
className="mb-6"
2024-01-18 15:46:18 +08:00
>
2024-01-19 15:36:41 +08:00
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3">
<input
className="border p-2 rounded"
type="text"
value={newTitle}
onChange={(e) => setNewTitle(e.target.value)}
placeholder="Title"
/>
<input
className="border p-2 rounded"
type="text"
value={newAuthor}
onChange={(e) => setNewAuthor(e.target.value)}
placeholder="Author"
/>
<input
className="border p-2 rounded"
type="text"
value={newYear}
2024-01-20 13:43:31 +08:00
onChange={(e) => setNewYear(e.target.value)}
2024-01-19 15:36:41 +08:00
placeholder="Year"
/>
<input
className="border p-2 rounded"
type="text"
value={newPublisher}
onChange={(e) => setNewPublisher(e.target.value)}
placeholder="Publisher"
/>
<input
className="border p-2 rounded"
type="text"
value={newUrl}
onChange={(e) => setNewUrl(e.target.value)}
placeholder="URL"
/>
</div>
2024-01-19 16:27:34 +08:00
<div className="container mx-auto p-4">
<div className="flex justify-between items-center mb-4">
<button
className="bg-gray-300 hover:bg-gray-400 text-black font-bold py-2 px-4 rounded "
2024-01-19 16:27:34 +08:00
type="submit"
>
2024-01-21 23:08:25 +08:00
2024-01-19 16:27:34 +08:00
</button>
2024-01-19 15:36:41 +08:00
2024-01-19 16:27:34 +08:00
<button
className="bg-gray-300 hover:bg-gray-400 text-black font-bold py-2 px-4 rounded "
type="button"
2024-01-19 16:27:34 +08:00
onClick={() =>
copyToClipboard(formatAllReferencesForCopy(references))
}
>
</button>
<button
2024-01-20 13:43:31 +08:00
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded "
type="button"
// onClick={() => setReferences([])} // 设置引用列表为空数组
onClick={() => handleClearReferences()}
2024-01-19 16:27:34 +08:00
>
</button>
</div>
</div>
</form>
2024-01-19 15:36:41 +08:00
{/* 引用列表显示区域 */}
2024-01-18 15:46:18 +08:00
<ul>
{references &&
references.map(
(reference, index) =>
reference && (
<li key={index} className="mb-3 p-2 border-b">
{/* 显示序号 */}
<span className="font-bold mr-2">[{index + 1}].</span>
{reference.author}. {reference.title}.{" "}
{/* 判断 journal 字段是否存在 */}
2024-01-26 23:41:41 +08:00
{reference.journal ? (
<span>reference.journal. </span>
) : (
<span>
{reference.venue}, {reference.year}.
</span>
)}
{reference.url && (
<a
href={reference.url}
className="text-blue-500 hover:underline"
target="_blank"
rel="noopener noreferrer"
id={`[${(index + 1).toString()}]`}
>
{" "}
({reference.url})
</a>
)}
2024-01-29 15:01:43 +08:00
<button
className="bg-gray-500 hover:bg-gray-700 text-white font-bold py-1 px-2 ml-2 rounded"
onClick={() => moveReferenceUp(index)}
>
</button>
<button
className="bg-gray-500 hover:bg-gray-700 text-white font-bold py-1 px-2 ml-2 rounded"
onClick={() => moveReferenceDown(index)}
>
</button>
<button
className="bg-gray-500 hover:bg-gray-700 text-white font-bold py-1 px-2 ml-2 rounded"
onClick={() =>
copyToClipboard(formatReferenceForCopy(reference))
}
>
</button>
<ParagraphDeleteButton
index={index}
2024-02-09 23:01:05 +08:00
isRemovePaper={true}
removeReferenceUpdateIndex={removeReferenceUpdateIndex}
></ParagraphDeleteButton>
</li>
)
)}
2024-01-18 15:46:18 +08:00
</ul>
</div>
);
}
export default ReferenceList;