| // src/components/article/CitationGenerator.tsx | |
| "use client"; | |
| import { useState, useEffect, useRef } from "react"; | |
| import api from "@/lib/api"; | |
| import { Copy, Check, Loader2, FileText, Download, RefreshCw } from "lucide-react"; | |
| interface CitationGeneratorProps { | |
| articleId: number; | |
| autoGenerate?: boolean; | |
| } | |
| const FORMATS = [ | |
| { value: "apa", label: "APA (7th ed.)" }, | |
| { value: "mla", label: "MLA (9th ed.)" }, | |
| { value: "chicago", label: "Chicago (17th ed.)" }, | |
| { value: "ieee", label: "IEEE" }, | |
| { value: "harvard", label: "Harvard" }, | |
| { value: "bibtex", label: "BibTeX" }, | |
| ]; | |
| export function CitationGenerator({ articleId, autoGenerate = true }: CitationGeneratorProps) { | |
| const [format, setFormat] = useState("apa"); | |
| const [citationText, setCitationText] = useState(""); | |
| const [loading, setLoading] = useState(false); | |
| const [copied, setCopied] = useState(false); | |
| const [error, setError] = useState(""); | |
| const textareaRef = useRef<HTMLTextAreaElement>(null); | |
| const generateCitation = async () => { | |
| if (!articleId) return; | |
| setLoading(true); | |
| setError(""); | |
| try { | |
| const res = await api.get(`/articles/${articleId}/citation?format=${format}`); | |
| setCitationText(res.data || ""); | |
| } catch (err) { | |
| console.error("Failed to generate citation", err); | |
| setError("Could not generate citation. Please try again."); | |
| setCitationText(""); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }; | |
| // Auto‑generate on mount or when format changes | |
| useEffect(() => { | |
| if (autoGenerate) generateCitation(); | |
| }, [format, articleId]); // eslint-disable-line react-hooks/exhaustive-deps | |
| const handleCopy = async () => { | |
| if (!citationText) return; | |
| try { | |
| await navigator.clipboard.writeText(citationText); | |
| setCopied(true); | |
| setTimeout(() => setCopied(false), 2000); | |
| } catch { | |
| // fallback for older browsers | |
| textareaRef.current?.select(); | |
| document.execCommand("copy"); | |
| setCopied(true); | |
| setTimeout(() => setCopied(false), 2000); | |
| } | |
| }; | |
| const downloadBibTeX = () => { | |
| if (format !== "bibtex" || !citationText) return; | |
| const blob = new Blob([citationText], { type: "text/plain" }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement("a"); | |
| a.href = url; | |
| a.download = `article-${articleId}.bib`; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| }; | |
| // Keyboard shortcut: Ctrl/Cmd + C when textarea focused | |
| useEffect(() => { | |
| const handleKeyDown = (e: KeyboardEvent) => { | |
| if ((e.ctrlKey || e.metaKey) && e.key === "c" && document.activeElement === textareaRef.current) { | |
| e.preventDefault(); | |
| handleCopy(); | |
| } | |
| }; | |
| document.addEventListener("keydown", handleKeyDown); | |
| return () => document.removeEventListener("keydown", handleKeyDown); | |
| }, [citationText]); | |
| return ( | |
| <div className="mt-8 bg-white dark:bg-gray-800 rounded-2xl shadow-md p-6 border border-gray-100 dark:border-gray-700"> | |
| <div className="flex items-center justify-between mb-4 flex-wrap gap-2"> | |
| <h3 className="text-lg font-semibold flex items-center gap-2 text-gray-800 dark:text-gray-200"> | |
| <FileText className="w-5 h-5 text-primary" /> | |
| Cite this article | |
| </h3> | |
| {citationText && format === "bibtex" && ( | |
| <button | |
| onClick={downloadBibTeX} | |
| className="inline-flex items-center gap-1 text-sm text-primary hover:underline" | |
| > | |
| <Download className="w-4 h-4" /> Download .bib | |
| </button> | |
| )} | |
| </div> | |
| <div className="flex flex-col sm:flex-row gap-3"> | |
| <select | |
| value={format} | |
| onChange={(e) => setFormat(e.target.value)} | |
| className="px-4 py-2 rounded-full border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800 text-gray-700 dark:text-gray-200 text-sm focus:outline-none focus:ring-2 focus:ring-primary" | |
| aria-label="Citation format" | |
| > | |
| {FORMATS.map((f) => ( | |
| <option key={f.value} value={f.value}> | |
| {f.label} | |
| </option> | |
| ))} | |
| </select> | |
| <button | |
| onClick={generateCitation} | |
| disabled={loading} | |
| className="inline-flex items-center gap-2 px-5 py-2 rounded-full bg-primary text-white hover:bg-primary/90 transition text-sm font-medium disabled:opacity-50" | |
| > | |
| {loading ? ( | |
| <Loader2 className="animate-spin w-4 h-4" /> | |
| ) : ( | |
| <RefreshCw className="w-4 h-4" /> | |
| )} | |
| {loading ? "Generating..." : "Generate"} | |
| </button> | |
| </div> | |
| {error && ( | |
| <div className="mt-3 text-sm text-red-500 dark:text-red-400">{error}</div> | |
| )} | |
| {citationText && ( | |
| <div className="mt-4 relative"> | |
| <textarea | |
| ref={textareaRef} | |
| readOnly | |
| value={citationText} | |
| className="w-full h-36 p-3 border border-gray-200 dark:border-gray-700 rounded-lg bg-gray-50 dark:bg-gray-900 text-sm font-mono text-gray-700 dark:text-gray-300 resize-none focus:outline-none focus:ring-1 focus:ring-primary" | |
| aria-label="Generated citation text" | |
| /> | |
| <button | |
| onClick={handleCopy} | |
| className="absolute top-2 right-2 p-2 rounded-full bg-white dark:bg-gray-700 shadow hover:bg-gray-100 dark:hover:bg-gray-600 transition" | |
| title="Copy citation (Ctrl+C)" | |
| aria-label="Copy citation to clipboard" | |
| > | |
| {copied ? ( | |
| <Check className="w-4 h-4 text-green-500" /> | |
| ) : ( | |
| <Copy className="w-4 h-4 text-gray-500" /> | |
| )} | |
| </button> | |
| </div> | |
| )} | |
| <p className="text-xs text-gray-400 mt-3"> | |
| Citations are generated automatically. Always verify against official style guides. | |
| </p> | |
| </div> | |
| ); | |
| } |
Xet Storage Details
- Size:
- 6.01 kB
- Xet hash:
- f59125fc7cc31051ddd065694cbde85ca0bc5ee6be146a67776965ae5dd163c2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.