'use client'; /** * `` — a compact, clickable "URL chip" (Chrome-omnibox style): * a favicon + the domain with a middle-ellipsised path * (`github.com/wailsapp/…/README.md`), monospace, with the full href on * hover. Opens in a new tab. * * This is the SURFACE-AGNOSTIC, React counterpart of the TipTap composer's * `editorChip` (`kind: 'url'`, see `forms/MarkdownEditor/chip/ChipNode.ts`). * It matches that chip's look pixel-for-pixel using semantic tokens, so a * URL reads identically whether it sits in the composer (being typed) or in * a rendered chat bubble (`MarkdownMessage`'s `urlChipRule`). * * Presentational + self-contained: it imports only the pure URL detector * helpers (`url/detect`) — no TipTap / ProseMirror — so it can render a * known URL anywhere (chat messages, link lists, etc). * * Favicon degradation: the favicon `` falls back to a lucide `Globe` * glyph on `onError` (offline / blocked / unknown host) so the chip never * shows a broken-image marker — mirroring the composer chip's behaviour. */ import { useState } from 'react'; import { Globe } from 'lucide-react'; import { cn } from '@djangocfg/ui-core/lib'; import { splitUrl, truncateUrlLabel, faviconUrl, } from '../../tools/forms/MarkdownEditor/url/detect'; export interface UrlChipProps { /** The full, navigable href (`https://…`). */ href: string; /** * Approximate max characters for the truncated label. The domain is * always preserved; only the path's middle collapses. Default 40. */ maxChars?: number; className?: string; } /** * Render a URL as an inline, clickable chip. Favicon → globe glyph on * favicon load error. */ export function UrlChip({ href, maxChars = 40, className }: UrlChipProps) { const { host } = splitUrl(href); const favicon = faviconUrl(host); const label = truncateUrlLabel(href, { maxChars }); // Favicon load failure → swap to the globe glyph. Also covers the empty- // favicon case (unparseable host → faviconUrl returns ''). const [imgFailed, setImgFailed] = useState(false); const showGlobe = imgFailed || !favicon; return ( {label} ); } export default UrlChip;