(READ ONLY) Margin is an open annotation layer for the internet. Powered by the AT Protocol.
Something went wrong. Try again.
JSX
at ui-refactor
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334import { useState, useEffect, useCallback } from "react";import { Link } from "react-router-dom";import { Plus } from "lucide-react";import { useAuth } from "../context/AuthContext";import { getUserBookmarks, deleteBookmark, createBookmark, getURLMetadata,} from "../api/client";import { BookmarkIcon } from "../components/Icons";import BookmarkCard from "../components/BookmarkCard";import CollectionItemCard from "../components/CollectionItemCard";import AddToCollectionModal from "../components/AddToCollectionModal";
export default function Bookmarks() { const { user, isAuthenticated, loading } = useAuth(); const [bookmarks, setBookmarks] = useState([]); const [loadingBookmarks, setLoadingBookmarks] = useState(true); const [error, setError] = useState(null); const [showAddForm, setShowAddForm] = useState(false); const [newUrl, setNewUrl] = useState(""); const [newTitle, setNewTitle] = useState(""); const [submitting, setSubmitting] = useState(false); const [fetchingTitle, setFetchingTitle] = useState(false); const [collectionModalState, setCollectionModalState] = useState({ isOpen: false, uri: null, });
const loadBookmarks = useCallback(async () => { if (!user?.did) return;
try { setLoadingBookmarks(true); const data = await getUserBookmarks(user.did); setBookmarks(data.items || []); } catch (err) { console.error("Failed to load bookmarks:", err); setError(err.message); } finally { setLoadingBookmarks(false); } }, [user]);
useEffect(() => { if (isAuthenticated && user) { loadBookmarks(); } }, [isAuthenticated, user, loadBookmarks]);
const handleDelete = async (uri) => { if (!confirm("Delete this bookmark?")) return;
try { const parts = uri.split("/"); const rkey = parts[parts.length - 1]; await deleteBookmark(rkey); setBookmarks((prev) => prev.filter((b) => (b.id || b.uri) !== uri)); } catch (err) { alert("Failed to delete: " + err.message); } };
const handleUrlBlur = async () => { if (!newUrl.trim() || newTitle.trim()) return; try { new URL(newUrl); } catch { return; } try { setFetchingTitle(true); const data = await getURLMetadata(newUrl.trim()); if (data.title && !newTitle) { setNewTitle(data.title); } } catch (err) { console.error("Failed to fetch title:", err); } finally { setFetchingTitle(false); } };
const handleAddBookmark = async (e) => { e.preventDefault(); if (!newUrl.trim()) return;
try { setSubmitting(true); await createBookmark(newUrl.trim(), newTitle.trim() || undefined); setNewUrl(""); setNewTitle(""); setShowAddForm(false); await loadBookmarks(); } catch (err) { alert("Failed to add bookmark: " + err.message); } finally { setSubmitting(false); } };
if (loading) return ( <div className="page-loading"> <div className="spinner"></div> </div> );
if (!isAuthenticated) { return ( <div className="new-page"> <div className="card" style={{ textAlign: "center", padding: "48px" }}> <h2>Sign in to view your bookmarks</h2> <p style={{ color: "var(--text-secondary)", marginTop: "8px" }}> You need to be logged in with your Bluesky account </p> <Link to="/login" className="btn btn-primary" style={{ marginTop: "24px" }} > Sign in with Bluesky </Link> </div> </div> ); }
return ( <div className="feed-page"> <div className="page-header" style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", }} > <div> <h1 className="page-title">My Bookmarks</h1> <p className="page-description">Pages you've saved for later</p> </div> <button onClick={() => setShowAddForm(!showAddForm)} className="btn btn-primary" > <Plus size={20} /> Add Bookmark </button> </div>
{showAddForm && ( <div className="card" style={{ marginBottom: "20px", padding: "24px" }}> <h3 style={{ marginBottom: "16px", fontSize: "1.1rem", color: "var(--text-primary)", }} > Add a Bookmark </h3> <form onSubmit={handleAddBookmark}> <div style={{ display: "flex", flexDirection: "column", gap: "16px" }} > <div> <label style={{ display: "block", marginBottom: "6px", fontSize: "0.85rem", color: "var(--text-secondary)", }} > URL * </label> <input type="url" placeholder="https://example.com/article" value={newUrl} onChange={(e) => setNewUrl(e.target.value)} onBlur={handleUrlBlur} className="input" style={{ width: "100%" }} required autoFocus /> </div> <div> <label style={{ display: "block", marginBottom: "6px", fontSize: "0.85rem", color: "var(--text-secondary)", }} > Title{" "} {fetchingTitle ? ( <span style={{ color: "var(--accent)" }}>Fetching...</span> ) : ( <span style={{ color: "var(--text-tertiary)" }}> (auto-fetched) </span> )} </label> <input type="text" placeholder={ fetchingTitle ? "Fetching title..." : "Page title will be fetched automatically" } value={newTitle} onChange={(e) => setNewTitle(e.target.value)} className="input" style={{ width: "100%" }} /> </div> <div style={{ display: "flex", gap: "10px", justifyContent: "flex-end", marginTop: "8px", }} > <button type="button" onClick={() => { setShowAddForm(false); setNewUrl(""); setNewTitle(""); }} className="btn btn-secondary" > Cancel </button> <button type="submit" className="btn btn-primary" disabled={submitting || !newUrl.trim()} > {submitting ? "Adding..." : "Save Bookmark"} </button> </div> </div> </form> </div> )}
{loadingBookmarks ? ( <div className="feed-container"> <div className="feed"> {[1, 2, 3].map((i) => ( <div key={i} className="card"> <div className="skeleton skeleton-text" style={{ width: "40%" }} ></div> <div className="skeleton skeleton-text"></div> <div className="skeleton skeleton-text" style={{ width: "60%" }} ></div> </div> ))} </div> </div> ) : error ? ( <div className="empty-state"> <div className="empty-state-icon">⚠️</div> <h3 className="empty-state-title">Error loading bookmarks</h3> <p className="empty-state-text">{error}</p> </div> ) : bookmarks.length === 0 ? ( <div className="empty-state"> <div className="empty-state-icon"> <BookmarkIcon size={32} /> </div> <h3 className="empty-state-title">No bookmarks yet</h3> <p className="empty-state-text"> Click "Add Bookmark" above to save a page, or use the browser extension. </p> </div> ) : ( <div className="feed-container"> <div className="feed"> {bookmarks.map((bookmark) => { if (bookmark.type === "CollectionItem") { return ( <CollectionItemCard key={bookmark.id} item={bookmark} onAddToCollection={(uri) => setCollectionModalState({ isOpen: true, uri: uri, }) } /> ); } return ( <BookmarkCard key={bookmark.id} bookmark={bookmark} onDelete={handleDelete} onAddToCollection={() => setCollectionModalState({ isOpen: true, uri: bookmark.uri || bookmark.id, }) } /> ); })} </div> </div> )} {collectionModalState.isOpen && ( <AddToCollectionModal isOpen={collectionModalState.isOpen} onClose={() => setCollectionModalState({ isOpen: false, uri: null })} annotationUri={collectionModalState.uri} /> )} </div> );}