"use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; type ResourceSection = { title: string; level: number; }; type ResourceLink = { id: string; title: string; url: string; category: string; status: string; createdAt: string; }; interface ResourceLinkCaptureProps { date: string; } export function ResourceLinkCapture({ date }: ResourceLinkCaptureProps) { const [open, setOpen] = useState(false); const [title, setTitle] = useState(""); const [url, setUrl] = useState(""); const [category, setCategory] = useState("Interesting things to read up:"); const qc = useQueryClient(); const { data } = useQuery({ queryKey: ["resource-links", "capture"], queryFn: async () => { const res = await fetch("/api/resource-links?status=inbox&includeSections=1"); if (!res.ok) throw new Error("Failed to load resource links"); return res.json() as Promise<{ links: ResourceLink[]; sections: ResourceSection[] }>; }, enabled: open, staleTime: 60_000, }); const sections = data?.sections?.length ? data.sections : [{ title: "Interesting things to read up:", level: 1 }]; const save = useMutation({ mutationFn: async () => { const res = await fetch("/api/resource-links", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ date, title, url, category }), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error ?? "Failed to save link"); } return res.json(); }, onSuccess: () => { setTitle(""); setUrl(""); qc.invalidateQueries({ queryKey: ["resource-links"] }); }, }); if (!open) { return ( ); } return (
Resource Inbox
setTitle(event.target.value)} /> setUrl(event.target.value)} /> {save.error && (

{save.error.message}

)}
{data?.links?.length ? (

Inbox

    {data.links.slice(0, 4).map((link) => (
  • {link.title}
  • ))}
) : null}
); }