tahamajs/IE / Projects /front /src /components /article /BookmarksList.tsx
tahamajs's picture
download
raw
5.13 kB
// src/components/article/BookmarksList.tsx
import { useState } from "react";
import { Link } from "react-router-dom";
import { Trash2, Bookmark, AlertCircle } from "lucide-react";
interface Bookmark {
articleId: number;
title: string;
authorUsername: string;
savedAt?: string;
}
interface BookmarksListProps {
bookmarks: Bookmark[];
removing: number | null;
onRemove: (articleId: number) => void;
showConfirmDialog?: boolean;
isLoading?: boolean;
}
// Skeleton loader for bookmarks
const SkeletonItem = () => (
<div className="bg-white dark:bg-gray-800 rounded-xl shadow p-4 animate-pulse">
<div className="flex justify-between items-center">
<div className="space-y-2 flex-1">
<div className="h-5 w-3/4 bg-gray-200 dark:bg-gray-700 rounded" />
<div className="h-4 w-1/3 bg-gray-200 dark:bg-gray-700 rounded" />
</div>
<div className="w-20 h-8 bg-gray-200 dark:bg-gray-700 rounded-lg" />
</div>
</div>
);
export function BookmarksList({
bookmarks,
removing,
onRemove,
showConfirmDialog = true,
isLoading = false,
}: BookmarksListProps) {
const [confirmingId, setConfirmingId] = useState<number | null>(null);
const handleRemoveClick = (articleId: number) => {
if (showConfirmDialog) {
setConfirmingId(articleId);
} else {
onRemove(articleId);
}
};
const confirmRemove = (articleId: number) => {
setConfirmingId(null);
onRemove(articleId);
};
const cancelRemove = () => {
setConfirmingId(null);
};
const formatSavedDate = (dateStr?: string) => {
if (!dateStr) return "";
const date = new Date(dateStr);
if (isNaN(date.getTime())) return "";
return date.toLocaleDateString(undefined, {
month: "short",
day: "numeric",
year: "numeric",
});
};
// Loading state
if (isLoading) {
return (
<div className="space-y-3">
{[...Array(3)].map((_, i) => (
<SkeletonItem key={i} />
))}
</div>
);
}
// Empty state
if (bookmarks.length === 0) {
return (
<div className="text-center py-12 bg-white dark:bg-gray-800 rounded-2xl shadow-sm border border-gray-100 dark:border-gray-700">
<div className="text-5xl mb-3">📚</div>
<Bookmark className="w-12 h-12 text-gray-300 mx-auto mb-2" />
<p className="text-lg font-medium text-gray-600 dark:text-gray-400">
No bookmarks yet
</p>
<p className="text-sm text-gray-400 mt-1">
Save interesting articles to read them later.
</p>
<Link
to="/articles"
className="inline-block mt-4 text-primary hover:underline text-sm"
>
Explore articles →
</Link>
</div>
);
}
return (
<div className="space-y-3">
{bookmarks.map((bookmark) => (
<div
key={bookmark.articleId}
className="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 p-4 hover:shadow-md transition"
>
<div className="flex justify-between items-start gap-4">
<div className="flex-1 min-w-0">
<Link
to={`/articles/${bookmark.articleId}`}
className="font-semibold text-gray-900 dark:text-white hover:text-primary transition line-clamp-1"
>
{bookmark.title || `Article #${bookmark.articleId}`}
</Link>
<p className="text-sm text-gray-500 dark:text-gray-400 mt-0.5">
by {bookmark.authorUsername}
</p>
{bookmark.savedAt && (
<p className="text-xs text-gray-400 mt-1">
Saved {formatSavedDate(bookmark.savedAt)}
</p>
)}
</div>
{confirmingId === bookmark.articleId ? (
<div className="flex gap-2">
<button
onClick={() => confirmRemove(bookmark.articleId)}
className="text-xs bg-red-500 text-white px-2 py-1 rounded-md hover:bg-red-600"
>
Yes
</button>
<button
onClick={cancelRemove}
className="text-xs bg-gray-200 dark:bg-gray-700 px-2 py-1 rounded-md hover:bg-gray-300"
>
No
</button>
</div>
) : (
<button
onClick={() => handleRemoveClick(bookmark.articleId)}
disabled={removing === bookmark.articleId}
className="text-gray-400 hover:text-red-500 transition disabled:opacity-50"
aria-label={`Remove ${bookmark.title} from bookmarks`}
>
{removing === bookmark.articleId ? (
<div className="w-5 h-5 border-2 border-red-500 border-t-transparent rounded-full animate-spin" />
) : (
<Trash2 className="w-5 h-5" />
)}
</button>
)}
</div>
</div>
))}
</div>
);
}

Xet Storage Details

Size:
5.13 kB
·
Xet hash:
add824505c3f88a73bb8a9b3fbec4c105fb34276193f2f39402b025da4153954

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