Skip to content

Commit

Permalink
fix: read history delay to polling
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 27, 2024
1 parent e2fec9c commit f6f8ec8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 27 deletions.
22 changes: 5 additions & 17 deletions src/renderer/src/components/site-icon.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getColorScheme, stringToHue } from "@renderer/lib/color"
import { cn } from "@renderer/lib/utils"
import { cn, getUrlIcon } from "@renderer/lib/utils"
import { useEffect, useMemo, useState } from "react"
import { parse } from "tldts"

import { PlatformIcon } from "./ui/platform-icon"

Expand All @@ -21,8 +20,7 @@ export function SiteIcon({
fallback?: boolean
fallbackText?: string
}) {
let host: string
let src: string
let src = ""
let fallbackUrl = ""

const colors = useMemo(
Expand All @@ -37,19 +35,9 @@ export function SiteIcon({

if (!url) return null

try {
host = new URL(url).host
const pureDomain = parse(host).domainWithoutSuffix
fallbackUrl = `https://avatar.vercel.sh/${pureDomain}.svg?text=${pureDomain
?.slice(0, 2)
.toUpperCase()}`
src = `https://unavatar.follow.is/${host}?fallback=${fallback}`
} catch {
const pureDomain = parse(url).domainWithoutSuffix
src = `https://avatar.vercel.sh/${pureDomain}.svg?text=${pureDomain
?.slice(0, 2)
.toUpperCase()}`
}
const ret = getUrlIcon(url, fallback)
src = ret.src
fallbackUrl = ret.fallbackUrl

return (
<PlatformIcon
Expand Down
31 changes: 27 additions & 4 deletions src/renderer/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ClassValue } from "clsx"
import { clsx } from "clsx"
import { memoize } from "lodash-es"
import { twMerge } from "tailwind-merge"
import { parse } from "tldts"

import { FEED_COLLECTION_LIST, ROUTE_FEED_PENDING } from "../constants/app"
import { FeedViewType } from "./enum"
Expand Down Expand Up @@ -88,10 +89,7 @@ export function detectBrowser() {
return "Safari"
} else if (userAgent.includes("Opera")) {
return "Opera"
} else if (
userAgent.includes("Trident") ||
userAgent.includes("MSIE")
) {
} else if (userAgent.includes("Trident") || userAgent.includes("MSIE")) {
return "Internet Explorer"
}

Expand Down Expand Up @@ -220,3 +218,28 @@ export const sortByAlphabet = (a: string, b: string) => {

return a.localeCompare(b, "zh-CN")
}

export const getUrlIcon = (url: string, fallback?: boolean | undefined) => {
let src: string
let fallbackUrl = ""

try {
const { host } = new URL(url)
const pureDomain = parse(host).domainWithoutSuffix
fallbackUrl = `https://avatar.vercel.sh/${pureDomain}.svg?text=${pureDomain
?.slice(0, 2)
.toUpperCase()}`
src = `https://unavatar.follow.is/${host}?fallback=${fallback || false}`
} catch {
const pureDomain = parse(url).domainWithoutSuffix
src = `https://avatar.vercel.sh/${pureDomain}.svg?text=${pureDomain
?.slice(0, 2)
.toUpperCase()}`
}
const ret = {
src,
fallbackUrl,
}

return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Queries } from "@renderer/queries"
import { useEntryReadHistory } from "@renderer/store/entry"
import { useUserById } from "@renderer/store/user"
import { LayoutGroup, m } from "framer-motion"
import { Fragment } from "react"
import { Fragment, useEffect, useState } from "react"

import { usePresentUserProfileModal } from "../../profile/hooks"

Expand All @@ -24,9 +24,20 @@ export const EntryReadHistory: Component<{ entryId: string }> = ({
const me = useWhoami()
const entryHistory = useEntryReadHistory(entryId)

const [isEnabledPolling, setIsEnabledPolling] = useState(false)
useAuthQuery(Queries.entries.entryReadingHistory(entryId), {
refetchInterval: 1000 * 60,
enabled: isEnabledPolling,
})
useEffect(() => {
const timer = setTimeout(() => {
setIsEnabledPolling(true)
}, 1000 * 60)

return () => {
clearTimeout(timer)
}
}, [entryId])

if (!entryHistory) return null
if (!me) return null
Expand Down
12 changes: 7 additions & 5 deletions src/renderer/src/modules/entry-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { EntryTranslation } from "../entry-column/translation"
import { setEntryContentScrollToTop, setEntryTitleMeta } from "./atoms"
import { EntryPlaceholderLogo } from "./components/EntryPlaceholderLogo"
import { EntryHeader } from "./header"
import { EntryContentLoading } from "./loading"
import { EntryContentProvider } from "./provider"

export const EntryContent = ({ entryId }: { entryId: ActiveEntryId }) => {
Expand Down Expand Up @@ -242,13 +243,11 @@ export const EntryContentRender: Component<{ entryId: string }> = ({
</ErrorBoundary>
</div>
</WrappedElementProvider>

{!content && (
<div className="center mt-16">
{isPending ? (
<LoadingWithIcon
size="large"
icon={<i className="i-mgc-rss-cute-fi text-accent" />}
/>
<EntryContentLoading icon={feed?.siteUrl!} />
) : error ?
(
<div className="center flex flex-col gap-2">
Expand Down Expand Up @@ -331,7 +330,10 @@ const ReadabilityContent = ({ entryId }: { entryId: string }) => {
</div>
)}

<HTML as="article" className="prose dark:prose-invert prose-h1:text-[1.6em]">
<HTML
as="article"
className="prose dark:prose-invert prose-h1:text-[1.6em]"
>
{result?.content ?? ""}
</HTML>
</div>
Expand Down
25 changes: 25 additions & 0 deletions src/renderer/src/modules/entry-content/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Avatar, AvatarImage } from "@renderer/components/ui/avatar"
import {
LoadingCircle,
LoadingWithIcon,
} from "@renderer/components/ui/loading"
import { getUrlIcon } from "@renderer/lib/utils"

export const EntryContentLoading = (props: { icon?: string }) => {
if (!props.icon) {
return (
<LoadingWithIcon
size="large"
icon={<i className="i-mgc-sparkles-2-cute-re" />}
/>
)
}
return (
<div className="center flex flex-col gap-4">
<Avatar className="animate-pulse rounded-sm">
<AvatarImage src={getUrlIcon(props.icon).src} />
</Avatar>
<LoadingCircle size="medium" />
</div>
)
}

0 comments on commit f6f8ec8

Please sign in to comment.