From ec0d861eb1cdac2a45ae72c4f7e30149c7ea257b Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 13 Jun 2025 19:57:21 +0800 Subject: [PATCH] refactor: optimize entry selection in ListItem component Signed-off-by: Innei --- .../templates/list-item-template.tsx | 56 ++--- .../renderer/src/modules/feed/feed-icon.tsx | 193 ++++++++++-------- 2 files changed, 137 insertions(+), 112 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/templates/list-item-template.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/templates/list-item-template.tsx index 946992df1..eab295e79 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/templates/list-item-template.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/templates/list-item-template.tsx @@ -2,6 +2,7 @@ import { useMobile } from "@follow/components/hooks/useMobile.js" import { EllipsisHorizontalTextWithTooltip } from "@follow/components/ui/typography/index.js" import { useCollectionEntry, useIsEntryStarred } from "@follow/store/collection/hooks" import { useEntry } from "@follow/store/entry/hooks" +import type { EntryModel } from "@follow/store/entry/types" import { useFeedById } from "@follow/store/feed/hooks" import { useInboxById } from "@follow/store/inbox/hooks" import { clsx, cn, formatEstimatedMins, formatTimeToSeconds, isSafari } from "@follow/utils/utils" @@ -25,6 +26,33 @@ import { getPreferredTitle } from "~/store/feed/hooks" import { StarIcon } from "../star-icon" import type { UniversalItemProps } from "../types" +const entrySelector = (state: EntryModel) => { + const { feedId, inboxHandle, read } = state + const { authorAvatar, authorUrl, description, publishedAt, title } = state + + const audios = state.attachments?.filter((a) => a.mime_type?.startsWith("audio") && a.url) + const firstAudio = audios?.[0] + const media = state.media || [] + const firstMedia = media?.[0] + const photo = media.find((a) => a.type === "photo") + const firstPhotoUrl = photo?.url + const iconEntry: FeedIconEntry = { firstPhotoUrl, authorAvatar } + + const titleEntry = { authorUrl } + + return { + description, + feedId, + firstAudio, + firstMedia, + iconEntry, + inboxId: inboxHandle, + publishedAt, + read, + title, + titleEntry, + } +} export function ListItem({ entryId, entryPreview, @@ -34,33 +62,7 @@ export function ListItem({ simple?: boolean }) { const isMobile = useMobile() - const entry = useEntry(entryId, (state) => { - const { feedId, inboxHandle, read } = state - const { authorAvatar, authorUrl, description, publishedAt, title } = state - - const audios = state.attachments?.filter((a) => a.mime_type?.startsWith("audio") && a.url) - const firstAudio = audios?.[0] - const media = state.media || [] - const firstMedia = media?.[0] - const photo = media.find((a) => a.type === "photo") - const firstPhotoUrl = photo?.url - const iconEntry: FeedIconEntry = { firstPhotoUrl, authorAvatar } - - const titleEntry = { authorUrl } - - return { - description, - feedId, - firstAudio, - firstMedia, - iconEntry, - inboxId: inboxHandle, - publishedAt, - read, - title, - titleEntry, - } - }) + const entry = useEntry(entryId, entrySelector) const isInCollection = useIsEntryStarred(entryId) const collectionCreatedAt = useCollectionEntry(entryId)?.createdAt diff --git a/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx b/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx index 03c3dcb59..8d1af2c0e 100644 --- a/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx +++ b/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx @@ -1,4 +1,4 @@ -import { Avatar, AvatarFallback, AvatarImage } from "@follow/components/ui/avatar/index.jsx" +// import { Avatar, AvatarFallback, AvatarImage } from "@follow/components/ui/avatar/index.jsx" import { PlatformIcon } from "@follow/components/ui/platform-icon/index.jsx" import type { FeedOrListRespModel } from "@follow/models/types" import type { FeedModel } from "@follow/store/feed/types" @@ -6,10 +6,81 @@ import type { ListModel } from "@follow/store/list/types" import { getBackgroundGradient } from "@follow/utils/color" import { getImageProxyUrl } from "@follow/utils/img-proxy" import { cn, getUrlIcon } from "@follow/utils/utils" +import * as AvatarPrimitive from "@radix-ui/react-avatar" import { m } from "motion/react" import type { ReactNode } from "react" import { useMemo } from "react" +const { Avatar, AvatarFallback, AvatarImage } = AvatarPrimitive + +function getIconProps( + props: Pick< + Parameters[0], + "feed" | "entry" | "useMedia" | "siteUrl" | "fallbackUrl" | "fallback" | "size" + >, +) { + const { feed, entry, useMedia, siteUrl: propSiteUrl, fallbackUrl, fallback, size = 20 } = props + const image = + (useMedia ? entry?.firstPhotoUrl || entry?.authorAvatar : entry?.authorAvatar) || feed?.image + const siteUrl = (feed as FeedModel)?.siteUrl || fallbackUrl + + if (propSiteUrl && !feed) { + const [src] = getFeedIconSrc({ + siteUrl: propSiteUrl, + }) + return { + type: "image" as const, + src, + platformUrl: propSiteUrl, + fallbackSrc: "", + } + } + if (image) { + return { + type: "image" as const, + src: getImageProxyUrl({ + url: image, + width: size * 2, + height: size * 2, + }), + platformUrl: image, + fallbackSrc: "", + } + } + + if (siteUrl) { + const [src, fallbackSrc] = getFeedIconSrc({ + siteUrl, + fallback, + proxy: { + width: size * 2, + height: size * 2, + }, + }) + return { + type: "image" as const, + src, + platformUrl: siteUrl, + fallbackSrc, + } + } + if (feed?.type === "inbox") { + return { + type: "inbox" as const, + } + } + + if (feed?.title) { + return { + type: "text" as const, + } + } + + return { + type: "default" as const, + } +} + const getFeedIconSrc = ({ src, siteUrl, @@ -111,15 +182,12 @@ export function FeedIcon({ noMargin?: boolean }) { const marginClassName = noMargin ? "" : "mr-2" - const image = - (useMedia ? entry?.firstPhotoUrl || entry?.authorAvatar : entry?.authorAvatar) || feed?.image + const iconProps = getIconProps({ feed, entry, useMedia, siteUrl, fallbackUrl, fallback, size }) const colors = useMemo( () => getBackgroundGradient(feed?.title || (feed as FeedModel)?.url || siteUrl || ""), [feed?.title, (feed as FeedModel)?.url, siteUrl], ) - let ImageElement: ReactNode - let finalSrc = "" const sizeStyle: React.CSSProperties = useMemo( () => ({ @@ -137,7 +205,7 @@ export function FeedIcon({ } }, [colors, sizeStyle]) - const fallbackIcon = ( + const textFallbackIcon = ( ) - switch (true) { - case !feed && !!siteUrl: { - const [src] = getFeedIconSrc({ - siteUrl, - }) - finalSrc = src! + let imageElement: ReactNode + let finalSrc = "" - const isIconLoaded = isIconLoadedSet.has(src!) - isIconLoadedSet.add(src!) - - ImageElement = ( - - - - ) - break - } - case !!image: { - finalSrc = getImageProxyUrl({ - url: image, - width: size * 2, - height: size * 2, - }) + switch (iconProps.type) { + case "image": { + finalSrc = iconProps.src! const isIconLoaded = isIconLoadedSet.has(finalSrc) isIconLoadedSet.add(finalSrc) + const { fallbackSrc } = iconProps - ImageElement = ( - - + imageElement = ( + + {fallbackSrc ? ( + + ) : ( + + )} ) break } - case !!fallbackUrl: - case !!(feed as FeedModel)?.siteUrl: { - const [src, fallbackSrc] = getFeedIconSrc({ - siteUrl: (feed as FeedModel)?.siteUrl || fallbackUrl, - fallback, - proxy: { - width: size * 2, - height: size * 2, - }, - }) - finalSrc = src! - - ImageElement = ( - - - - ) - break - } - case feed?.type === "inbox": { - ImageElement = ( + case "inbox": { + imageElement = ( ) break } - case !!feed?.title && !!feed.title[0]: { - ImageElement = fallbackIcon + case "text": { + imageElement = textFallbackIcon break } - default: { - ImageElement = ( + case "default": { + imageElement = ( ) break } } - if (!ImageElement) { + if (!imageElement) { return null } - if (fallback && !!finalSrc) { + const fallbackIcon = fallbackElement || textFallbackIcon + + if (finalSrc) { return ( - {ImageElement} + {imageElement} - {fallbackElement || fallbackIcon} + {fallback ? fallbackIcon :
} ) } - // Is Icon - if (!finalSrc) return ImageElement - // Else - return ( - - - {ImageElement} - - -
- - - ) + return imageElement }