import { useFetchWebMetadata } from '@/hooks/useFetchWebMetadata' import { isInsecureUrl } from '@/lib/url' import { cn } from '@/lib/utils' import { useContentPolicy } from '@/providers/ContentPolicyProvider' import { useScreenSize } from '@/providers/ScreenSizeProvider' import { useUserPreferences } from '@/providers/UserPreferencesProvider' import { useMemo } from 'react' import ExternalLink from '../ExternalLink' import Image from '../Image' export default function WebPreview({ url, className, mustLoad }: { url: string className?: string mustLoad?: boolean }) { const { autoLoadMedia } = useContentPolicy() const { allowInsecureConnection } = useUserPreferences() const { isSmallScreen } = useScreenSize() const { title, description, image } = useFetchWebMetadata(url) const hostname = useMemo(() => { try { return new URL(url).hostname } catch { return '' } }, [url]) if (!allowInsecureConnection && isInsecureUrl(url)) { return null } if (!autoLoadMedia && !mustLoad) { return null } if (!title) { if (mustLoad) { return } else { return null } } if (isSmallScreen && image) { return (
{ e.stopPropagation() window.open(url, '_blank') }} >
{hostname}
{title}
) } return (
{ e.stopPropagation() window.open(url, '_blank') }} > {image && ( )}
{hostname}
{title}
{description}
) }