import { useEffect, useState } from "react";
import { ArrowDown, ArrowUp, Check, Copy, ExternalLink, Plus, Save, Trash2 } from "lucide-react";
import { api } from "../api.js";
import { useAuth } from "../App.jsx";
import { Button, ErrorNote, Field, fmt, Panel, Spinner } from "../ui.jsx";
/** Client page tab: the client's link-in-bio page, its links and click counts. */
export default function LinkInBio({ slug }) {
const { isAdmin } = useAuth();
const [page, setPage] = useState(null);
const [stats, setStats] = useState(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState(null);
const [saved, setSaved] = useState(false);
const [copied, setCopied] = useState(false);
useEffect(() => { api(`/api/bio/${slug}`).then((r) => { setPage(r.page); setStats(r.stats); }).catch(setError); }, [slug]);
if (!page) return error ? : ;
const set = (k, v) => { setSaved(false); setPage((p) => ({ ...p, [k]: v })); };
const setLink = (i, k, v) => set("links", page.links.map((l, j) => (j === i ? { ...l, [k]: v } : l)));
const move = (i, d) => { const ls = [...page.links]; const [x] = ls.splice(i, 1); ls.splice(i + d, 0, x); set("links", ls); };
const save = async () => {
setBusy(true); setError(null);
try { const r = await api(`/api/bio/${slug}`, { method: "PUT", body: page }); setPage(r.page); setStats(r.stats); setSaved(true); } catch (e) { setError(e); }
setBusy(false);
};
const url = `${location.origin}/l/${page.path}`;
const clicks = Object.fromEntries((stats?.links || []).map((l) => [l.id, l.clicks]));
return (
{page.links.map((l, i) => (
))}
{isAdmin &&
}
{[["bg", "Background"], ["fg", "Text"], ["button", "Buttons"], ["buttonText", "Button text"]].map(([k, l]) => (
))}
{isAdmin && (
{saved && Saved.{page.enabled ? " The page is live." : ""}}
)}
);
}