refactor: drop SiteIcon

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-02 23:48:08 +08:00
parent 93e2ccfd58
commit b3c6ffd306
5 changed files with 202 additions and 92 deletions
+141 -59
View File
@@ -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<HTMLImageElement>
>(function FallbackableImage({ fallbackUrl, ...rest }, ref) {
return (
<img
onError={(e) => {
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 = (
<PlatformIcon className="mr-2 shrink-0 rounded-sm" url={image}>
<Media
src={feed.siteUrl || image}
type="photo"
loading="lazy"
className={cn("rounded-sm", className)}
style={{
width: size,
height: size,
}}
proxy={{
width: size * 2,
height: size * 2,
}}
/>
<PlatformIcon url={siteUrl} style={sizeStyle} className="center">
<img className={className} style={sizeStyle} />
</PlatformIcon>
)
break
}
case !!image: {
finalSrc = getProxyUrl({
url: image,
width: size * 2,
height: size * 2,
})
ImageElement = (
<PlatformIcon url={image} style={sizeStyle} className="center">
<img className={className} style={sizeStyle} />
</PlatformIcon>
)
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 = (
<SiteIcon
fallbackText={feed.title!}
fallback={fallback}
url={feed.siteUrl || fallbackUrl}
className={className}
style={{
width: size,
height: size,
}}
/>
<PlatformIcon url={feed?.siteUrl!} style={sizeStyle} className="center">
<FallbackableImage
className={className}
style={sizeStyle}
fallbackUrl={fallbackSrc}
/>
</PlatformIcon>
)
break
}
default: {
ImageElement = (
<i
className="i-mgc-link-cute-re mr-2 shrink-0"
style={{
width: size,
height: size,
}}
/>
<i className="i-mgc-link-cute-re mr-2 shrink-0" style={sizeStyle} />
)
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 (
<Avatar className="shrink-0">
<Avatar className="mr-2 shrink-0">
<AvatarImage
className="duration-200 animate-in fade-in-0"
src={image || ""}
className="rounded-sm duration-200 animate-in fade-in-0"
asChild
src={finalSrc}
>
{ImageElement}
</AvatarImage>
@@ -106,28 +179,37 @@ export function FeedIcon({
<span
style={
{
"width": size,
"height": size,
...sizeStyle,
"--fo-light-background": colors.light.background,
"--fo-dark-background": colors.dark.background,
} as any
}
className={cn(
"mr-2 flex shrink-0 items-center justify-center rounded-sm",
"bg-[var(--fo-light-background)] text-white dark:bg-[var(--fo-dark-background)] dark:text-black",
"flex shrink-0 items-center justify-center rounded-sm",
"bg-[var(--fo-light-background)] text-white dark:bg-[var(--fo-dark-background)]",
className,
)}
>
<span className="text-xs font-medium">
{!!feed.title && feed.title[0]}
</span>
<span className="text-[9px]">{!!feed?.title && feed.title[0]}</span>
</span>
</AvatarFallback>
</Avatar>
)
}
return ImageElement
// Is Icon
if (!finalSrc) return ImageElement
// Else
return (
<Avatar className="shrink-0">
<AvatarImage
className="duration-200 animate-in fade-in-0"
asChild
src={finalSrc}
>
{ImageElement}
</AvatarImage>
</Avatar>
)
}
+46 -16
View File
@@ -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 (
<PlatformIcon
url={url}
url={nextSrc}
style={style}
className={cn("relative mr-2 size-5 shrink-0 rounded-sm", className)}
>
{showFallback && fallback ? (
<div
data-error-image={src || fallbackUrl}
className="center absolute inset-0 bg-[var(--fo-light-background)] text-[9px] text-white dark:bg-[var(--fo-dark-background)] dark:text-black"
data-error-image={nextSrc || fallbackUrl}
className="center absolute inset-0 bg-[var(--fo-light-background)] text-[9px] text-white dark:bg-[var(--fo-dark-background)]"
style={
{
"--fo-light-background": colors.light.background,
@@ -60,7 +90,7 @@ export function SiteIcon({
</div>
) : (
<img
src={src}
src={nextSrc}
loading="lazy"
onError={(e) => {
if (fallbackUrl && e.currentTarget.src !== fallbackUrl) {
+1 -1
View File
@@ -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"
@@ -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<RecommendationCardProps> = memo(({ data, rou
<Card className="shadow-none">
<CardHeader className="p-5 pb-3">
<CardTitle className="flex items-center text-base">
<SiteIcon url={`https://${data.url}`} />
<FeedIcon siteUrl={`https://${data.url}`} />
{data.name}
</CardTitle>
</CardHeader>
@@ -40,9 +40,9 @@ export const RecommendationCard: FC<RecommendationCardProps> = memo(({ data, rou
<RecommendationContent routePrefix={routePrefix} route={data.routes[route]} />
),
icon: (
<SiteIcon
<FeedIcon
className="size-4"
url={`https://${data.url}`}
siteUrl={`https://${data.url}`}
/>
),
title: `${data.name} - ${data.routes[route].name}`,
+10 -12
View File
@@ -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 (
<Command.Item
className={clsx(
@@ -273,7 +275,7 @@ const SearchItem = memo(function Item({
}}
>
<div className="relative z-10 flex w-full items-center justify-between px-1 py-2">
{icon && <SiteIcon className="mr-2 size-5 shrink-0" url={icon} />}
{feed && <FeedIcon className="mr-2 size-5 shrink-0 rounded" feed={feed} />}
<span className="block min-w-0 flex-1 shrink-0 truncate">{title}</span>
<span className="block min-w-0 shrink-0 grow-0 text-xs font-medium text-zinc-800 opacity-60 dark:text-slate-200/80">
{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<{
<small className="center absolute bottom-3 right-3 shrink-0 gap-1 opacity-80">
{hasKeyword ? (
<span>
{count}
{" "}
{pluralize("result", count || 0)}
{count} {pluralize("result", count || 0)}
</span>
) : (
<ExPromise promise={recordCountPromise}>
{(count) => (
<>
{count}
{" "}
local
{" "}
{pluralize("record", count)}
{count} local {pluralize("record", count)}
</>
)}
</ExPromise>
@@ -344,7 +341,8 @@ const SearchResultCount: FC<{
</small>
</TooltipTrigger>
<TooltipContent>
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.
</TooltipContent>
</Tooltip>
)