From b3c6ffd306b5cb2363ae391b56da2af588e11753 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 2 Sep 2024 23:48:08 +0800 Subject: [PATCH] refactor: drop `SiteIcon` Signed-off-by: Innei --- src/renderer/src/components/feed-icon.tsx | 200 ++++++++++++------ src/renderer/src/components/site-icon.tsx | 62 ++++-- src/renderer/src/lib/link-parser.ts | 2 +- .../modules/discover/recommendations-card.tsx | 8 +- src/renderer/src/modules/panel/cmdk.tsx | 22 +- 5 files changed, 202 insertions(+), 92 deletions(-) diff --git a/src/renderer/src/components/feed-icon.tsx b/src/renderer/src/components/feed-icon.tsx index a57f73759..da4426a72 100644 --- a/src/renderer/src/components/feed-icon.tsx +++ b/src/renderer/src/components/feed-icon.tsx @@ -1,104 +1,177 @@ import { Avatar, AvatarFallback, AvatarImage } from "@radix-ui/react-avatar" -import { SiteIcon } from "@renderer/components/site-icon" -import { Media } from "@renderer/components/ui/media" import { getColorScheme, stringToHue } from "@renderer/lib/color" -import { cn } from "@renderer/lib/utils" +import { getProxyUrl } from "@renderer/lib/img-proxy" +import { cn, getUrlIcon } from "@renderer/lib/utils" import type { CombinedEntryModel, FeedModel } from "@renderer/models" import type { ReactNode } from "react" -import { useMemo } from "react" +import { forwardRef, useMemo } from "react" import { PlatformIcon } from "./ui/platform-icon" +const getFeedIconSrc = ({ + src, + siteUrl, + fallback, + proxy, +}: { + src?: string + siteUrl?: string + fallback?: boolean + proxy?: { height: number, width: number } +} = {}) => { + // src ? getProxyUrl(src) : ""; + if (src) { + if (proxy) { + return [ + getProxyUrl({ + url: src, + width: proxy.width, + height: proxy.height, + }), + "", + ] + } + + return [src, ""] + } + if (!siteUrl) return ["", ""] + const ret = getUrlIcon(siteUrl, fallback) + + return [ret.src, ret.fallbackUrl] +} + +const FallbackableImage = forwardRef< + HTMLImageElement, + { + fallbackUrl: string + } & React.ImgHTMLAttributes +>(function FallbackableImage({ fallbackUrl, ...rest }, ref) { + return ( + { + if (fallbackUrl && e.currentTarget.src !== fallbackUrl) { + e.currentTarget.src = fallbackUrl + } else { + rest.onError?.(e) + // Empty svg + e.currentTarget.src = + "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3C/svg%3E" + } + }} + {...rest} + ref={ref} + /> + ) +}) + export function FeedIcon({ feed, entry, fallbackUrl, className, size = 20, - fallback = false, + fallback = true, + siteUrl, }: { - feed: FeedModel + feed?: FeedModel entry?: CombinedEntryModel["entries"] fallbackUrl?: string className?: string size?: number + siteUrl?: string /** * Image loading error fallback to site icon */ fallback?: boolean }) { - const image = entry?.authorAvatar || feed.image + const image = entry?.authorAvatar || feed?.image + if (!feed && !siteUrl) { + throw new Error("You must provide either a feed or a siteUrl") + } + + const colors = useMemo( + () => + getColorScheme(stringToHue(feed?.title || feed?.url || siteUrl!), true), + [feed?.title, feed?.url, siteUrl], + ) let ImageElement: ReactNode + let finalSrc = "" + + const sizeStyle = { + width: size, + height: size, + } switch (true) { - case !!image: { + case !feed && !!siteUrl: { + const [src] = getFeedIconSrc({ + siteUrl, + }) + finalSrc = src + ImageElement = ( - - + + + + ) + break + } + case !!image: { + finalSrc = getProxyUrl({ + url: image, + width: size * 2, + height: size * 2, + }) + ImageElement = ( + + ) break } case !!fallbackUrl: - case !!feed.siteUrl: { + case !!feed?.siteUrl: { + const [src, fallbackSrc] = getFeedIconSrc({ + siteUrl: feed?.siteUrl || fallbackUrl, + fallback, + proxy: { + width: size * 2, + height: size * 2, + }, + }) + finalSrc = src + ImageElement = ( - + + + ) break } default: { ImageElement = ( - + ) break } } - const colors = useMemo( - () => getColorScheme(stringToHue(feed.title || feed.url), true), - [feed.title, feed.url], - ) - if (!ImageElement) { return null } - if (fallback && !!image) { + if (fallback && !!finalSrc) { return ( - + {ImageElement} @@ -106,28 +179,37 @@ export function FeedIcon({ - - {!!feed.title && feed.title[0]} - + {!!feed?.title && feed.title[0]} ) } - return ImageElement + // Is Icon + if (!finalSrc) return ImageElement + // Else + return ( + + + {ImageElement} + + + ) } diff --git a/src/renderer/src/components/site-icon.tsx b/src/renderer/src/components/site-icon.tsx index dba163ca7..5b7f4b467 100644 --- a/src/renderer/src/components/site-icon.tsx +++ b/src/renderer/src/components/site-icon.tsx @@ -1,15 +1,22 @@ import { getColorScheme, stringToHue } from "@renderer/lib/color" +import { getProxyUrl } from "@renderer/lib/img-proxy" import { cn, getUrlIcon } from "@renderer/lib/utils" import { useEffect, useMemo, useState } from "react" import { PlatformIcon } from "./ui/platform-icon" - +/** + * + * @deprecated please use `FeedIcon` instead + * @returns + */ export function SiteIcon({ url, className, style, fallback, fallbackText, + proxy, + src, }: { url?: string className?: string @@ -19,36 +26,59 @@ export function SiteIcon({ */ fallback?: boolean fallbackText?: string -}) { - let src = "" - let fallbackUrl = "" - const colors = useMemo( - () => (url && fallback ? getColorScheme(stringToHue(url), true) : null), - [fallback, url], - ) + src?: string + proxy?: { + width: number + height: number + } +}) { + const colors = useMemo(() => { + const id = src || url + return id && fallback ? getColorScheme(stringToHue(id), true) : null + }, [fallback, src, url]) const [showFallback, setShowFallback] = useState(false) useEffect(() => { setShowFallback(false) }, [url]) - if (!url) return null + const stableProxy = useState(proxy)[0] - const ret = getUrlIcon(url, fallback) - src = ret.src - fallbackUrl = ret.fallbackUrl + const [nextSrc, fallbackUrl] = useMemo((): [string, string] => { + // src ? getProxyUrl(src) : ""; + if (src) { + if (stableProxy) { + return [ + getProxyUrl({ + url: src, + width: stableProxy.width, + height: stableProxy.height, + }), + "", + ] + } + + return [src, ""] + } + if (!url) return ["", ""] + const ret = getUrlIcon(url, fallback) + + return [ret.src, ret.fallbackUrl] + }, [fallback, stableProxy, src, url]) + + if (!nextSrc) return null return ( {showFallback && fallback ? (
) : ( { if (fallbackUrl && e.currentTarget.src !== fallbackUrl) { diff --git a/src/renderer/src/lib/link-parser.ts b/src/renderer/src/lib/link-parser.ts index fc3986ae1..63f2076cd 100644 --- a/src/renderer/src/lib/link-parser.ts +++ b/src/renderer/src/lib/link-parser.ts @@ -3,7 +3,7 @@ const GITHUB_HOST = "github.com" export const isYoutubeUrl = (url: URL) => url.hostname === "www.youtube.com" && url.pathname.startsWith("/watch") -export const isGithubUrl = (url: URL) => url.hostname === GITHUB_HOST +export const isGithubUrl = (url: URL) => url.hostname === GITHUB_HOST || url.hostname === "github.blog" export const isTwitterUrl = (url: URL) => url.hostname === "twitter.com" export const isXUrl = (url: URL) => url.hostname === "x.com" diff --git a/src/renderer/src/modules/discover/recommendations-card.tsx b/src/renderer/src/modules/discover/recommendations-card.tsx index 613db7f57..c316e10b9 100644 --- a/src/renderer/src/modules/discover/recommendations-card.tsx +++ b/src/renderer/src/modules/discover/recommendations-card.tsx @@ -1,4 +1,4 @@ -import { SiteIcon } from "@renderer/components/site-icon" +import { FeedIcon } from "@renderer/components/feed-icon" import { Card, CardContent, @@ -23,7 +23,7 @@ export const RecommendationCard: FC = memo(({ data, rou - + {data.name} @@ -40,9 +40,9 @@ export const RecommendationCard: FC = memo(({ data, rou ), icon: ( - ), title: `${data.name} - ${data.routes[route].name}`, diff --git a/src/renderer/src/modules/panel/cmdk.tsx b/src/renderer/src/modules/panel/cmdk.tsx index 074522be7..0dd94ce01 100644 --- a/src/renderer/src/modules/panel/cmdk.tsx +++ b/src/renderer/src/modules/panel/cmdk.tsx @@ -1,9 +1,9 @@ import { setAppSearchOpen, useAppSearchOpen } from "@renderer/atoms/app" import { ExPromise } from "@renderer/components/common/ExPromise" import { LoadMoreIndicator } from "@renderer/components/common/LoadMoreIndicator" +import { FeedIcon } from "@renderer/components/feed-icon" import { EmptyIcon } from "@renderer/components/icons/empty" import { Logo } from "@renderer/components/icons/logo" -import { SiteIcon } from "@renderer/components/site-icon" import { ScrollArea } from "@renderer/components/ui/scroll-area" import { Select, @@ -247,12 +247,14 @@ const SearchItem = memo(function Item({ title, entryId, feedId, - icon, + subtitle, view, }: {} & SearchListType) { const navigateEntry = useNavigateEntry() + const feed = getFeedById(feedId!) + return (
- {icon && } + {feed && } {title} {subtitle} @@ -299,6 +301,7 @@ const SearchResultCount: FC<{ const searchInstance = React.useContext(SearchCmdKContext) const hasKeyword = useSearchStore((s) => !!s.keyword) const searchType = useSearchStore((s) => s.searchType) + // eslint-disable-next-line react-compiler/react-compiler const recordCountPromise = useMemo(async () => { let count = 0 const counts = await searchInstance?.then((s) => s.counts) @@ -321,19 +324,13 @@ const SearchResultCount: FC<{ {hasKeyword ? ( - {count} - {" "} - {pluralize("result", count || 0)} + {count} {pluralize("result", count || 0)} ) : ( {(count) => ( <> - {count} - {" "} - local - {" "} - {pluralize("record", count)} + {count} local {pluralize("record", count)} )} @@ -344,7 +341,8 @@ const SearchResultCount: FC<{ - This search covers locally available data. Try a Refetch to include the latest data. + This search covers locally available data. Try a Refetch to include the + latest data. )