tahamajs/IE / Projects /front /src /components /collections /CollectionLinkModal.tsx
tahamajs's picture
download
raw
7.82 kB
import { useState, useEffect } from "react";
import { X, FolderOpen, Loader2, Search } from "lucide-react";
import api from "@/lib/api";
import { toast } from "react-toastify";
interface Collection {
id: number;
name: string;
description: string;
articleCount: number;
isPublic: boolean;
createdAt: string;
}
interface Props {
isOpen: boolean;
onClose: () => void;
onInsert: (url: string, title: string) => void;
}
export function CollectionLinkModal({ isOpen, onClose, onInsert }: Props) {
const [collections, setCollections] = useState<Collection[]>([]);
const [loading, setLoading] = useState(true);
const [searchQuery, setSearchQuery] = useState("");
const [selectedCollection, setSelectedCollection] = useState<Collection | null>(null);
useEffect(() => {
if (isOpen) {
fetchCollections();
}
}, [isOpen]);
const fetchCollections = async () => {
setLoading(true);
try {
const response = await api.get("/collections");
setCollections(response.data || []);
} catch (err: any) {
console.error("Failed to fetch collections:", err);
toast.error("Could not load collections");
} finally {
setLoading(false);
}
};
const handleInsert = () => {
if (selectedCollection) {
const url = `/collections/${selectedCollection.id}`;
const title = `Collection: ${selectedCollection.name}`;
onInsert(url, title);
toast.success(`Link to "${selectedCollection.name}" inserted`);
handleClose();
} else {
toast.warning("Please select a collection first");
}
};
const handleClose = () => {
setSelectedCollection(null);
setSearchQuery("");
onClose();
};
const filteredCollections = collections.filter((collection) =>
collection.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
collection.description?.toLowerCase().includes(searchQuery.toLowerCase())
);
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-4">
<div className="relative w-full max-w-lg rounded-2xl bg-white dark:bg-gray-800 shadow-2xl">
{/* Header */}
<div className="flex items-center justify-between border-b border-gray-200 dark:border-gray-700 p-5">
<div>
<h2 className="text-xl font-bold bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">
Insert Collection Link
</h2>
<p className="text-sm text-gray-500 mt-1">
Select a collection to insert a link to it
</p>
</div>
<button
onClick={handleClose}
className="p-1 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition"
>
<X className="w-5 h-5 text-gray-500" />
</button>
</div>
{/* Search */}
<div className="p-5 border-b border-gray-200 dark:border-gray-700">
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
<input
type="text"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search collections..."
className="w-full pl-9 pr-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary dark:bg-gray-700 transition"
/>
</div>
</div>
{/* Collections List */}
<div className="max-h-96 overflow-y-auto p-2">
{loading ? (
<div className="flex items-center justify-center py-12">
<Loader2 className="w-8 h-8 animate-spin text-primary" />
<span className="ml-3 text-gray-500">Loading collections...</span>
</div>
) : filteredCollections.length === 0 ? (
<div className="text-center py-12">
<FolderOpen className="w-12 h-12 text-gray-300 mx-auto mb-3" />
<p className="text-gray-500">
{searchQuery ? "No collections match your search" : "No collections yet"}
</p>
{!searchQuery && (
<button
onClick={handleClose}
className="mt-3 text-sm text-primary hover:underline"
>
Create a collection first
</button>
)}
</div>
) : (
<div className="space-y-2">
{filteredCollections.map((collection) => (
<div
key={collection.id}
onClick={() => setSelectedCollection(collection)}
className={`
flex items-start gap-3 p-3 rounded-lg cursor-pointer transition-all
${selectedCollection?.id === collection.id
? "bg-primary/10 border-2 border-primary"
: "hover:bg-gray-50 dark:hover:bg-gray-700/50 border-2 border-transparent"
}
`}
>
<div className="flex-shrink-0">
<div className="p-2 rounded-lg bg-primary/10">
<FolderOpen className="w-5 h-5 text-primary" />
</div>
</div>
<div className="flex-1 min-w-0">
<h3 className="font-medium text-gray-900 dark:text-gray-100">
{collection.name}
</h3>
{collection.description && (
<p className="text-sm text-gray-500 dark:text-gray-400 line-clamp-1">
{collection.description}
</p>
)}
<div className="flex items-center gap-3 mt-1 text-xs text-gray-400">
<span>{collection.articleCount} paper{collection.articleCount !== 1 ? "s" : ""}</span>
{collection.isPublic && (
<span className="px-1.5 py-0.5 bg-green-100 dark:bg-green-900/30 text-green-600 dark:text-green-400 rounded">
Public
</span>
)}
</div>
</div>
{selectedCollection?.id === collection.id && (
<div className="flex-shrink-0">
<div className="w-5 h-5 rounded-full bg-primary text-white flex items-center justify-center text-xs">
✓
</div>
</div>
)}
</div>
))}
</div>
)}
</div>
{/* Footer */}
<div className="flex gap-3 p-5 border-t border-gray-200 dark:border-gray-700">
<button
onClick={handleClose}
className="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 transition"
>
Cancel
</button>
<button
onClick={handleInsert}
disabled={!selectedCollection}
className={`
flex-1 px-4 py-2 rounded-lg font-medium transition flex items-center justify-center gap-2
${selectedCollection
? "bg-gradient-to-r from-primary to-secondary text-white hover:shadow-lg"
: "bg-gray-200 dark:bg-gray-700 text-gray-400 dark:text-gray-500 cursor-not-allowed"
}
`}
>
<FolderOpen className="w-4 h-4" />
Insert Link
</button>
</div>
</div>
</div>
);
}

Xet Storage Details

Size:
7.82 kB
·
Xet hash:
f22b5c7c28383edf5bb34debc8b4ac0695c28011e3b3029c764258f6768763e9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.