"use client"; import React from "react"; import { NodeViewProps, NodeViewWrapper } from "@tiptap/react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { ArrowRight } from "lucide-react"; import { Skeleton } from "@/components/ui/skeleton"; export type LinkPreviewData = { href: string; title: string; description: string; image: string; }; const InputLink = ({ onSubmit }: { onSubmit: (link: string) => void }) => { const [link, setLink] = React.useState(""); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // Prevent page reload if (link.trim()) { onSubmit(link); // Pass link to parent function setLink(""); // Clear input field after submission } }; const inputRef = React.useRef(null); React.useEffect(() => { inputRef.current?.focus(); }, []); return (
setLink(e.target.value)} className="flex-1 focus-visible:ring-transparent border-0" />
); }; const Preview = ({ href, title, description: _description, image, }: LinkPreviewData) => { const description = _description?.length > 100 ? `${_description?.slice(0, 150)}...` : _description; return (

{title}

{description}

{href}
{image?.length ? ( {title} ) : (
)}
); }; export const LinkPreviewComponent: React.FC = ({ node, updateAttributes, extension, }) => { const [preview, setPreview] = React.useState( (node.attrs?.href?.length && (node.attrs as LinkPreviewData)) ?? undefined ); const [loading, setLoading] = React.useState(false); return ( {loading ? ( ) : preview ? ( ) : ( { setLoading(true); try { const metadata = await extension.options.fetchMetadata(url); const newAttrs = { href: url, title: metadata?.title, description: metadata?.description, image: metadata?.image, }; updateAttributes(newAttrs); setPreview(newAttrs); setLoading(false); } catch (error) { console.error("Error fetching metadata:", error); setLoading(false); } }} /> )} ); };