diff --git a/src/renderer/src/components/ui/media.tsx b/src/renderer/src/components/ui/media.tsx index 06b37a646..049bea017 100644 --- a/src/renderer/src/components/ui/media.tsx +++ b/src/renderer/src/components/ui/media.tsx @@ -3,32 +3,34 @@ import { getProxyUrl } from "@renderer/lib/img-proxy" import { showNativeMenu } from "@renderer/lib/native-menu" import { cn } from "@renderer/lib/utils" import type { FC, ImgHTMLAttributes, VideoHTMLAttributes } from "react" -import { memo, useState } from "react" +import { memo, useMemo, useState } from "react" import { toast } from "sonner" import { useEventCallback } from "usehooks-ts" import { usePreviewMedia } from "./media/hooks" const failedList = new Set() -export type ImageProps = (ImgHTMLAttributes & { - proxy?: { - width: number - height: number - } - disableContextMenu?: boolean - popper?: boolean - type: "photo" - previewImageUrl?: string -}) | (VideoHTMLAttributes & { - proxy?: { - width: number - height: number - } - disableContextMenu?: boolean - popper?: boolean - type: "video" - previewImageUrl?: string -}) +export type ImageProps = + | (ImgHTMLAttributes & { + proxy?: { + width: number + height: number + } + disableContextMenu?: boolean + popper?: boolean + type: "photo" + previewImageUrl?: string + }) + | (VideoHTMLAttributes & { + proxy?: { + width: number + height: number + } + disableContextMenu?: boolean + popper?: boolean + type: "video" + previewImageUrl?: string + }) const MediaImpl: FC = ({ className, proxy, @@ -59,95 +61,120 @@ const MediaImpl: FC = ({ } }) const previewMedia = usePreviewMedia() - const handleClick = useEventCallback( - (e: React.MouseEvent) => { - if (popper && src) { - e.stopPropagation() - previewMedia([{ - url: src, - type, - }], 0) + const handleClick = useEventCallback((e: React.MouseEvent) => { + if (popper && src) { + e.stopPropagation() + previewMedia( + [ + { + url: src, + type, + }, + ], + 0, + ) + } + props.onClick?.(e as any) + }) + + const InnerContent = useMemo(() => { + switch (type) { + case "photo": { + return ( + )} + onError={errorHandle} + className={cn( + hidden && "hidden", + !(props.width || props.height) && "size-full", + "bg-stone-100 object-cover", + popper && "cursor-zoom-in", + )} + src={imgSrc} + onClick={handleClick} + {...(!disableContextMenu ? + { + onContextMenu: (e) => { + e.stopPropagation() + props.onContextMenu?.(e) + showNativeMenu( + [ + { + type: "text", + label: "Open Image in New Window", + click: () => { + if (props.src && imgSrc && tipcClient) { + window.open(props.src, "_blank") + } + }, + }, + { + type: "text", + label: "Copy Image Address", + click: () => { + if (props.src) { + navigator.clipboard.writeText(props.src) + toast("Address copied to clipboard.", { + duration: 1000, + }) + } + }, + }, + ], + e, + ) + }, + } : + {})} + /> + ) } - props.onClick?.(e as any) - }, - ) + case "video": { + return ( + + ) + } + default: { + throw new Error("Invalid type") + } + } + }, [ + disableContextMenu, + errorHandle, + handleClick, + hidden, + imgSrc, + popper, + previewImageUrl, + props, + rest, + src, + type, + ]) return (
- {(type === "photo") && ( - } - onError={errorHandle} - className={cn( - hidden && "hidden", - !(props.width || props.height) && "size-full", - "bg-stone-100 object-cover", - popper && "cursor-zoom-in", - )} - src={imgSrc} - onClick={handleClick} - {...(!disableContextMenu ? - { - onContextMenu: (e) => { - e.stopPropagation() - props.onContextMenu?.(e) - showNativeMenu( - [ - { - type: "text", - label: "Open Image in New Window", - click: () => { - if (props.src && imgSrc && tipcClient) { - window.open(props.src, "_blank") - } - }, - }, - { - type: "text", - label: "Copy Image Address", - click: () => { - if (props.src) { - navigator.clipboard.writeText(props.src) - toast("Address copied to clipboard.", { - duration: 1000, - }) - } - }, - }, - ], - e, - ) - }, - } : - {})} - /> - )} - {(type === "video") && ( - - )} + {InnerContent}
) } diff --git a/src/renderer/src/modules/entry-column/social-media-item.tsx b/src/renderer/src/modules/entry-column/social-media-item.tsx index 8814bb765..a706f9b53 100644 --- a/src/renderer/src/modules/entry-column/social-media-item.tsx +++ b/src/renderer/src/modules/entry-column/social-media-item.tsx @@ -22,7 +22,6 @@ export const SocialMediaItem: EntryListItemFC = ({ const previewMedia = usePreviewMedia() const asRead = useAsRead(entry) const feed = useFeedById(entry?.feedId) - // NOTE: prevent 0 height element, react virtuoso will not stop render any more if (!entry || !feed) return diff --git a/src/renderer/src/modules/entry-column/video-item.tsx b/src/renderer/src/modules/entry-column/video-item.tsx index 9c5d9885c..0bf2dfb26 100644 --- a/src/renderer/src/modules/entry-column/video-item.tsx +++ b/src/renderer/src/modules/entry-column/video-item.tsx @@ -15,12 +15,24 @@ import type { UniversalItemProps } from "./types" const ViewTag = window.electron ? "webview" : "iframe" -export function VideoItem({ entryId, entryPreview, translation }: UniversalItemProps) { +export function VideoItem({ + entryId, + entryPreview, + translation, +}: UniversalItemProps) { const entry = useEntry(entryId) || entryPreview - const isActive = useRouteParamsSelector(({ entryId }) => entryId === entry?.entries.id) + const isActive = useRouteParamsSelector( + ({ entryId }) => entryId === entry?.entries.id, + ) - const [miniIframeSrc, iframeSrc] = useMemo(() => [urlToIframe(entry?.entries.url, true), urlToIframe(entry?.entries.url)], [entry?.entries.url]) + const [miniIframeSrc, iframeSrc] = useMemo( + () => [ + urlToIframe(entry?.entries.url, true), + urlToIframe(entry?.entries.url), + ], + [entry?.entries.url], + ) const modalStack = useModalStack() const ref = useRef(null) @@ -36,7 +48,11 @@ export function VideoItem({ entryId, entryPreview, translation }: UniversalItemP if (!entry) return return ( - +
{ @@ -45,11 +61,12 @@ export function VideoItem({ entryId, entryPreview, translation }: UniversalItemP modalStack.present({ title: "", content: ({ dismiss }) => ( - dismiss()}> - + dismiss()} + > + ), clickOutsideToDismiss: true, @@ -63,25 +80,35 @@ export function VideoItem({ entryId, entryPreview, translation }: UniversalItemP {miniIframeSrc && hovered ? ( - ) : ( - entry.entries.media && ( - - ) - )} + ) : entry.entries.media ? + ( + + ) : + ( +
+ No video available +
+ )}