({ defaultUrl }: { defaultUrl?: string })
| 10 | import { LoadingSpinner, Photo } from "./icons"; |
| 11 | |
| 12 | export function LinkPreview({ defaultUrl }: { defaultUrl?: string }) { |
| 13 | const searchParams = useSearchParams(); |
| 14 | const url = |
| 15 | defaultUrl || searchParams?.get("url") || "https://github.com/dubinc/dub"; |
| 16 | const [debouncedUrl] = useDebounce(getUrlFromString(url), 500); |
| 17 | const hostname = useMemo(() => { |
| 18 | return getDomainWithoutWWW(debouncedUrl || ""); |
| 19 | }, [debouncedUrl]); |
| 20 | |
| 21 | const { data, isValidating } = useSWR<{ |
| 22 | title: string | null; |
| 23 | description: string | null; |
| 24 | image: string | null; |
| 25 | }>( |
| 26 | debouncedUrl && |
| 27 | `/api/links/metatags?url=${encodeURIComponent(debouncedUrl)}`, |
| 28 | fetcher, |
| 29 | { |
| 30 | revalidateOnFocus: false, |
| 31 | }, |
| 32 | ); |
| 33 | |
| 34 | const { title, description, image } = data || {}; |
| 35 | |
| 36 | const inputRef = useRef<HTMLInputElement>(null); |
| 37 | useEffect(() => { |
| 38 | if (!defaultUrl && inputRef.current) { |
| 39 | inputRef.current.select(); |
| 40 | } |
| 41 | }, [defaultUrl]); |
| 42 | |
| 43 | const { isMobile } = useMediaQuery(); |
| 44 | |
| 45 | return ( |
| 46 | <> |
| 47 | {!defaultUrl && ( |
| 48 | <div className="relative flex items-center"> |
| 49 | <Link2 className="absolute inset-y-0 left-0 my-2 ml-3 w-5 text-neutral-400" /> |
| 50 | <input |
| 51 | ref={inputRef} |
| 52 | name="url" |
| 53 | id="url" |
| 54 | type="url" |
| 55 | autoFocus={!isMobile} |
| 56 | className="block w-full rounded-md border-neutral-200 pl-10 text-sm text-neutral-900 placeholder-neutral-400 shadow-lg focus:border-neutral-500 focus:outline-none focus:ring-neutral-500" |
| 57 | placeholder="Enter your URL" |
| 58 | defaultValue={url} |
| 59 | aria-invalid="true" |
| 60 | /> |
| 61 | </div> |
| 62 | )} |
| 63 | |
| 64 | <div className="relative w-full overflow-hidden rounded-lg border border-neutral-200 bg-neutral-50 shadow-sm"> |
| 65 | {isValidating && ( |
| 66 | <div className="absolute inset-x-0 flex h-[250px] flex-col items-center justify-center space-y-4 border-b border-neutral-300 bg-neutral-50"> |
| 67 | <LoadingSpinner /> |
| 68 | </div> |
| 69 | )} |
nothing calls this directly
no test coverage detected
searching dependent graphs…