import { useSecondaryPage } from '@/PageManager' import { Button } from '@/components/ui/button' import { Drawer, DrawerContent, DrawerOverlay } from '@/components/ui/drawer' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' import { toExternalContent } from '@/lib/link' import { truncateUrl } from '@/lib/url' import { cn } from '@/lib/utils' import { useScreenSize } from '@/providers/ScreenSizeProvider' import { ExternalLink as ExternalLinkIcon, MessageSquare } from 'lucide-react' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' export default function ExternalLink({ url, className, justOpenLink }: { url: string className?: string justOpenLink?: boolean }) { const { t } = useTranslation() const { isSmallScreen } = useScreenSize() const { push } = useSecondaryPage() const [isDrawerOpen, setIsDrawerOpen] = useState(false) const displayUrl = useMemo(() => truncateUrl(url), [url]) const handleOpenLink = (e: React.MouseEvent) => { e.stopPropagation() if (isSmallScreen) { setIsDrawerOpen(false) } window.open(url, '_blank', 'noreferrer') } const handleViewDiscussions = (e: React.MouseEvent) => { e.stopPropagation() if (isSmallScreen) { setIsDrawerOpen(false) setTimeout(() => push(toExternalContent(url)), 100) // wait for drawer to close return } push(toExternalContent(url)) } if (justOpenLink) { return ( e.stopPropagation()} > {displayUrl} ) } const trigger = ( { e.stopPropagation() if (isSmallScreen) { setIsDrawerOpen(true) } }} title={url} > {displayUrl} ) if (isSmallScreen) { return ( <> {trigger} { e.stopPropagation() setIsDrawerOpen(false) }} />
) } return ( {displayUrl} e.stopPropagation()}> {t('Open link')} {t('View Nostr discussions')} ) }