import { useContext, useLayoutEffect, useRef, useState, type RefObject } from "react"; import { ArrowLeft, ChevronRight, Link2 } from "lucide-react"; import { useTranslation } from "react-i18next"; import { WebPreviewContext } from "@/components/WebLink"; import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface"; import { copyTextToClipboard } from "@/lib/clipboard"; import { currentWebPreviewRestriction, parseWebLink } from "@/lib/web-preview"; interface MessageWebLink { href: string; title: string } /** Read only links actually rendered in this message, not code samples or file references. */ export function useMessageWebLinks(root: RefObject, open: boolean) { const [links, setLinks] = useState([]); useLayoutEffect(() => { const element = root.current; if (!open || !element) return; const collect = () => { const unique = new Map(); element.querySelectorAll("[data-assistant-selectable] a[data-web-link]").forEach((anchor) => { const href = anchor.getAttribute("href") ?? ""; const url = parseWebLink(href); if (url && !unique.has(url.href)) { unique.set(url.href, { href, title: anchor.textContent?.trim() || href }); } }); setLinks(Array.from(unique.values())); }; collect(); // Markdown is lazy-loaded; collect again when its real links appear/change. const observer = new MutationObserver(collect); observer.observe(element, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ["href"] }); return () => observer.disconnect(); }, [root, open]); return links; } const itemClassName = `${floatingItemClassName} ${floatingItemFocusClassName} w-full min-h-11 text-start hover:bg-muted/70`; /** A drill-in inside the message's action surface, not another floating layer. */ export function MessageLinksMenu({ links, onBack, onClose, expanded = false }: { links: MessageWebLink[]; onBack: () => void; onClose: () => void; expanded?: boolean; }) { const { t } = useTranslation(); const openPreview = useContext(WebPreviewContext); const [selected, setSelected] = useState(links.length === 1 ? links[0].href : null); const active = links.find((link) => link.href === selected); const backRef = useRef(null); useLayoutEffect(() => { backRef.current?.focus({ preventScroll: true }); }, [selected]); return
{active ? { onClose(); openPreview(active.href); } : undefined} /> :
{links.map((link) => )}
}
; } function MessageLinkActions({ link, onClose, onPreview }: { link: MessageWebLink; onClose: () => void; onPreview?: () => void; }) { const { t } = useTranslation(); const restriction = onPreview ? currentWebPreviewRestriction(parseWebLink(link.href)) : null; const [feedback, setFeedback] = useState<"copied" | "copyFailed" | null>(null); const mounted = useRef(false); useLayoutEffect(() => { mounted.current = true; return () => { mounted.current = false; }; }, []); const copy = async () => { const success = await copyTextToClipboard(link.href); if (mounted.current) setFeedback(success ? "copied" : "copyFailed"); }; return

{link.href}

{onPreview && !restriction ? : null} {t("webPreview.external")} {restriction ?

{t(`webPreview.${restriction}`)}

: null} {feedback ?

{t(`webPreview.${feedback}`)}

: null}
; }