| import { useState, useEffect, useCallback } from "react"; | |
| import api from "@/lib/api"; | |
| import { toast } from "react-toastify"; | |
| import { BookOpen, Plus, ExternalLink, Trash2, Search, Filter, Users, Copy, Check, FileText } from "lucide-react"; | |
| interface Citation { | |
| id: string; | |
| title: string; | |
| authors: string[]; | |
| year: number; | |
| journal: string; | |
| doi: string; | |
| addedBy: string; | |
| addedAt: string; | |
| tags: string[]; | |
| } | |
| interface SharedLibraryProps { | |
| groupId?: string; // if part of a project/group | |
| userId?: string; // personal library | |
| } | |
| export function SharedLibrary({ groupId, userId }: SharedLibraryProps) { | |
| const [citations, setCitations] = useState<Citation[]>([]); | |
| const [loading, setLoading] = useState(true); | |
| const [search, setSearch] = useState(""); | |
| const [newDoi, setNewDoi] = useState(""); | |
| const [adding, setAdding] = useState(false); | |
| const fetchCitations = useCallback(async () => { | |
| setLoading(true); | |
| const endpoint = groupId ? `/groups/${groupId}/citations` : `/users/me/citations`; | |
| try { | |
| const res = await api.get(endpoint); | |
| setCitations(res.data ?? []); | |
| } catch { /* silent */ } | |
| finally { setLoading(false); } | |
| }, [groupId]); | |
| useEffect(() => { fetchCitations(); }, [fetchCitations]); | |
| const addByDOI = async () => { | |
| if (!newDoi.trim()) return; | |
| setAdding(true); | |
| try { | |
| await api.post(`/citations/add`, { doi: newDoi.trim(), groupId }); | |
| setNewDoi(""); | |
| fetchCitations(); | |
| toast.success("Citation added"); | |
| } catch { toast.error("Could not add citation"); } | |
| finally { setAdding(false); } | |
| }; | |
| const removeCitation = async (id: string) => { | |
| await api.delete(`/citations/${id}`); | |
| setCitations(prev => prev.filter(c => c.id !== id)); | |
| toast.success("Citation removed"); | |
| }; | |
| const copyCitation = (citation: Citation) => { | |
| const text = `${citation.authors.join(", ")} (${citation.year}). ${citation.title}. ${citation.journal}. DOI: ${citation.doi}`; | |
| navigator.clipboard.writeText(text); | |
| toast.success("Citation copied!"); | |
| }; | |
| const filtered = search ? citations.filter(c => | |
| c.title.toLowerCase().includes(search.toLowerCase()) || | |
| c.authors.some(a => a.toLowerCase().includes(search.toLowerCase())) | |
| ) : citations; | |
| return ( | |
| <div className="bg-white dark:bg-gray-800 rounded-2xl border border-gray-200 dark:border-gray-700 p-4"> | |
| <div className="flex items-center justify-between mb-4"> | |
| <h3 className="font-bold text-lg flex items-center gap-2"> | |
| <BookOpen className="w-5 h-5 text-primary" /> | |
| {groupId ? "Group Library" : "My Library"} | |
| <span className="text-sm font-normal text-gray-400">({citations.length} refs)</span> | |
| </h3> | |
| </div> | |
| {/* Add by DOI */} | |
| <div className="flex gap-2 mb-4"> | |
| <input type="text" value={newDoi} onChange={e => setNewDoi(e.target.value)} placeholder="Add by DOI (e.g., 10.1038/nature12373)" | |
| className="flex-1 px-3 py-1.5 text-sm border rounded-lg dark:bg-gray-700" /> | |
| <button onClick={addByDOI} disabled={adding} className="btn btn-primary btn-sm gap-1"> | |
| <Plus className="w-4 h-4"/> Add | |
| </button> | |
| </div> | |
| {/* Search */} | |
| <div className="relative mb-3"> | |
| <Search className="absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" /> | |
| <input type="text" value={search} onChange={e => setSearch(e.target.value)} placeholder="Search citations..." | |
| className="w-full pl-8 pr-3 py-1.5 text-sm border rounded-lg dark:bg-gray-700" /> | |
| </div> | |
| {/* List */} | |
| <div className="space-y-2 max-h-96 overflow-y-auto"> | |
| {loading ? ( | |
| <div className="text-center py-8 text-gray-400">Loading...</div> | |
| ) : filtered.length === 0 ? ( | |
| <div className="text-center py-8 text-gray-400">No citations yet. Add one by DOI.</div> | |
| ) : ( | |
| filtered.map(citation => ( | |
| <div key={citation.id} className="flex items-start justify-between p-3 rounded-lg border border-gray-100 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700/50"> | |
| <div className="flex-1 min-w-0"> | |
| <p className="text-sm font-semibold truncate">{citation.title}</p> | |
| <p className="text-xs text-gray-500">{citation.authors.slice(0,3).join(", ")} ({citation.year}) – {citation.journal}</p> | |
| <p className="text-[10px] text-gray-400 mt-1">Added by {citation.addedBy}</p> | |
| </div> | |
| <div className="flex items-center gap-1 shrink-0 ml-2"> | |
| <button onClick={() => copyCitation(citation)} className="p-1 text-gray-400 hover:text-primary"><Copy className="w-3.5 h-3.5"/></button> | |
| <a href={`https://doi.org/${citation.doi}`} target="_blank" className="p-1 text-gray-400 hover:text-primary"><ExternalLink className="w-3.5 h-3.5"/></a> | |
| <button onClick={() => removeCitation(citation.id)} className="p-1 text-gray-400 hover:text-red-500"><Trash2 className="w-3.5 h-3.5"/></button> | |
| </div> | |
| </div> | |
| )) | |
| )} | |
| </div> | |
| </div> | |
| ); | |
| } |
Xet Storage Details
- Size:
- 5.18 kB
- Xet hash:
- 2c0e53e2737721afba80a960e29c9be80274d44a2501f17039a431e5a1270df8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.