feat: 💨

This commit is contained in:
codytseng 2025-11-15 21:06:36 +08:00
parent 0bb62dd3fb
commit 9d4eec350c
4 changed files with 101 additions and 13 deletions

View file

@ -45,7 +45,7 @@ export default function ExternalContent({
} }
if (node.type === 'url') { if (node.type === 'url') {
return <WebPreview url={node.data} className={className} /> return <WebPreview url={node.data} className={className} mustLoad={mustLoadMedia} />
} }
if (node.type === 'x-post') { if (node.type === 'x-post') {

View file

@ -1,4 +1,6 @@
import { useSecondaryPage } from '@/PageManager' import { useSecondaryPage } from '@/PageManager'
import { Button } from '@/components/ui/button'
import { Drawer, DrawerContent, DrawerOverlay } from '@/components/ui/drawer'
import { import {
DropdownMenu, DropdownMenu,
DropdownMenuContent, DropdownMenuContent,
@ -8,33 +10,106 @@ import {
import { toExternalContent } from '@/lib/link' import { toExternalContent } from '@/lib/link'
import { truncateUrl } from '@/lib/url' import { truncateUrl } from '@/lib/url'
import { cn } from '@/lib/utils' import { cn } from '@/lib/utils'
import { useScreenSize } from '@/providers/ScreenSizeProvider'
import { ExternalLink as ExternalLinkIcon, MessageSquare } from 'lucide-react' import { ExternalLink as ExternalLinkIcon, MessageSquare } from 'lucide-react'
import { useMemo } from 'react' import { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
export default function ExternalLink({ url, className }: { url: string; className?: string }) { export default function ExternalLink({
url,
className,
justOpenLink
}: {
url: string
className?: string
justOpenLink?: boolean
}) {
const { t } = useTranslation() const { t } = useTranslation()
const { isSmallScreen } = useScreenSize()
const { push } = useSecondaryPage() const { push } = useSecondaryPage()
const [isDrawerOpen, setIsDrawerOpen] = useState(false)
const displayUrl = useMemo(() => truncateUrl(url), [url]) const displayUrl = useMemo(() => truncateUrl(url), [url])
const handleOpenLink = (e: React.MouseEvent) => { const handleOpenLink = (e: React.MouseEvent) => {
e.stopPropagation() e.stopPropagation()
if (isSmallScreen) {
setIsDrawerOpen(false)
}
window.open(url, '_blank', 'noreferrer') window.open(url, '_blank', 'noreferrer')
} }
const handleViewDiscussions = (e: React.MouseEvent) => { const handleViewDiscussions = (e: React.MouseEvent) => {
e.stopPropagation() e.stopPropagation()
if (isSmallScreen) {
setIsDrawerOpen(false)
}
push(toExternalContent(url)) push(toExternalContent(url))
} }
if (justOpenLink) {
return (
<a
href={url}
target="_blank"
rel="noreferrer"
className={cn('cursor-pointer text-primary hover:underline', className)}
onClick={(e) => e.stopPropagation()}
>
{displayUrl}
</a>
)
}
const trigger = (
<span
className={cn('cursor-pointer text-primary hover:underline', className)}
onClick={(e) => {
e.stopPropagation()
if (isSmallScreen) {
setIsDrawerOpen(true)
}
}}
title={url}
>
{displayUrl}
</span>
)
if (isSmallScreen) {
return (
<>
{trigger}
<Drawer open={isDrawerOpen} onOpenChange={setIsDrawerOpen}>
<DrawerOverlay onClick={() => setIsDrawerOpen(false)} />
<DrawerContent hideOverlay>
<div className="py-2">
<Button
onClick={handleOpenLink}
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
variant="ghost"
>
<ExternalLinkIcon />
{t('Open link')}
</Button>
<Button
onClick={handleViewDiscussions}
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
variant="ghost"
>
<MessageSquare />
{t('View Nostr discussions')}
</Button>
</div>
</DrawerContent>
</Drawer>
</>
)
}
return ( return (
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger>
<span <span className={cn('cursor-pointer text-primary hover:underline', className)} title={url}>
className={cn('cursor-pointer text-primary hover:underline', className)}
onClick={(e) => e.stopPropagation()}
title={url}
>
{displayUrl} {displayUrl}
</span> </span>
</DropdownMenuTrigger> </DropdownMenuTrigger>

View file

@ -4,8 +4,17 @@ import { useContentPolicy } from '@/providers/ContentPolicyProvider'
import { useScreenSize } from '@/providers/ScreenSizeProvider' import { useScreenSize } from '@/providers/ScreenSizeProvider'
import { useMemo } from 'react' import { useMemo } from 'react'
import Image from '../Image' import Image from '../Image'
import ExternalLink from '../ExternalLink'
export default function WebPreview({ url, className }: { url: string; className?: string }) { export default function WebPreview({
url,
className,
mustLoad
}: {
url: string
className?: string
mustLoad?: boolean
}) {
const { autoLoadMedia } = useContentPolicy() const { autoLoadMedia } = useContentPolicy()
const { isSmallScreen } = useScreenSize() const { isSmallScreen } = useScreenSize()
const { title, description, image } = useFetchWebMetadata(url) const { title, description, image } = useFetchWebMetadata(url)
@ -18,13 +27,17 @@ export default function WebPreview({ url, className }: { url: string; className?
} }
}, [url]) }, [url])
if (!autoLoadMedia) { if (!autoLoadMedia && !mustLoad) {
return null return null
} }
if (!title) { if (!title) {
if (mustLoad) {
return <ExternalLink url={url} justOpenLink />
} else {
return null return null
} }
}
if (isSmallScreen && image) { if (isSmallScreen && image) {
return ( return (

View file

@ -29,7 +29,7 @@ const ExternalContentPage = forwardRef(({ index }: { index?: number }, ref) => {
displayScrollToTopButton displayScrollToTopButton
> >
<div className="px-4 mt-3"> <div className="px-4 mt-3">
<ExternalContent content={id} /> <ExternalContent content={id} mustLoadMedia />
<StuffStats className="mt-3" stuff={id} fetchIfNotExisting displayTopZapsAndLikes /> <StuffStats className="mt-3" stuff={id} fetchIfNotExisting displayTopZapsAndLikes />
</div> </div>
<Separator className="mt-4" /> <Separator className="mt-4" />