fix(web): readability support
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
import { tracker } from "@follow/tracker"
|
||||
import { createElement, useCallback } from "react"
|
||||
import { createElement, useCallback, useMemo } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import {
|
||||
useEntryIsInReadability,
|
||||
useEntryIsInReadabilitySuccess,
|
||||
useEntryReadabilityContent,
|
||||
} from "~/atoms/readability"
|
||||
import { useModalStack } from "~/components/ui/modal/stacked/hooks"
|
||||
import { useAuthQuery } from "~/hooks/common/useBizQuery"
|
||||
import { Queries } from "~/queries"
|
||||
import { useEntryTranslation } from "~/store/ai/hook"
|
||||
import { useEntry } from "~/store/entry/hooks"
|
||||
import { useInboxById } from "~/store/inbox/hooks"
|
||||
|
||||
import { ImageGalleryContent } from "./components/ImageGalleryContent"
|
||||
|
||||
@@ -30,3 +40,62 @@ export const useGalleryModal = () => {
|
||||
[present, t],
|
||||
)
|
||||
}
|
||||
|
||||
export const useEntryContent = (entryId: string) => {
|
||||
const entry = useEntry(entryId)
|
||||
const isInbox = useInboxById(entry?.inboxId, (inbox) => inbox !== null)
|
||||
const { error, data, isPending } = useAuthQuery(
|
||||
isInbox ? Queries.entries.byInboxId(entryId) : Queries.entries.byId(entryId),
|
||||
{
|
||||
staleTime: 300_000,
|
||||
},
|
||||
)
|
||||
|
||||
const isInReadabilityMode = useEntryIsInReadability(entryId)
|
||||
const isReadabilitySuccess = useEntryIsInReadabilitySuccess(entryId)
|
||||
const readabilityContent = useEntryReadabilityContent(entryId)
|
||||
const contentTranslated = useEntryTranslation({
|
||||
entry,
|
||||
extraFields: isReadabilitySuccess ? ["readabilityContent"] : ["content"],
|
||||
})
|
||||
|
||||
return useMemo(() => {
|
||||
const entryContent = isInReadabilityMode
|
||||
? readabilityContent?.content
|
||||
: (entry?.entries.content ?? data?.entries.content)
|
||||
const translatedContent = isInReadabilityMode
|
||||
? contentTranslated.data?.readabilityContent
|
||||
: contentTranslated.data?.content
|
||||
const content = translatedContent || entryContent
|
||||
return {
|
||||
content,
|
||||
error,
|
||||
isPending,
|
||||
}
|
||||
}, [
|
||||
contentTranslated.data?.content,
|
||||
contentTranslated.data?.readabilityContent,
|
||||
data?.entries.content,
|
||||
entry?.entries.content,
|
||||
error,
|
||||
isInReadabilityMode,
|
||||
isPending,
|
||||
readabilityContent?.content,
|
||||
])
|
||||
}
|
||||
|
||||
export const useEntryMediaInfo = (entryId: string) => {
|
||||
return useEntry(entryId, (entry) =>
|
||||
Object.fromEntries(
|
||||
entry?.entries.media
|
||||
?.filter((m) => m.type === "photo")
|
||||
.map((cur) => [
|
||||
cur.url,
|
||||
{
|
||||
width: cur.width,
|
||||
height: cur.height,
|
||||
},
|
||||
]) ?? [],
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -17,11 +17,7 @@ import { ErrorBoundary } from "@sentry/react"
|
||||
import * as React from "react"
|
||||
import { useEffect, useMemo, useRef, useState } from "react"
|
||||
|
||||
import {
|
||||
useEntryIsInReadability,
|
||||
useEntryIsInReadabilitySuccess,
|
||||
useEntryReadabilityContent,
|
||||
} from "~/atoms/readability"
|
||||
import { useEntryIsInReadability } from "~/atoms/readability"
|
||||
import { useIsZenMode, useUISettingKey } from "~/atoms/settings/ui"
|
||||
import { ShadowDOM } from "~/components/common/ShadowDOM"
|
||||
import type { TocRef } from "~/components/ui/markdown/components/Toc"
|
||||
@@ -29,12 +25,10 @@ import { useInPeekModal } from "~/components/ui/modal/inspire/InPeekModal"
|
||||
import { HotkeyScope } from "~/constants"
|
||||
import { useRenderStyle } from "~/hooks/biz/useRenderStyle"
|
||||
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
|
||||
import { useAuthQuery, useConditionalHotkeyScope } from "~/hooks/common"
|
||||
import { useConditionalHotkeyScope } from "~/hooks/common"
|
||||
import { useFeedSafeUrl } from "~/hooks/common/useFeedSafeUrl"
|
||||
import { useHotkeyScope } from "~/providers/hotkey-provider"
|
||||
import { WrappedElementProvider } from "~/providers/wrapped-element-provider"
|
||||
import { Queries } from "~/queries"
|
||||
import { useEntryTranslation } from "~/store/ai/hook"
|
||||
import { useEntry } from "~/store/entry"
|
||||
import { useFeedById } from "~/store/feed"
|
||||
import { useInboxById } from "~/store/inbox"
|
||||
@@ -48,6 +42,7 @@ import { EntryTitle } from "./components/EntryTitle"
|
||||
import { SourceContentPanel } from "./components/SourceContentView"
|
||||
import { SupportCreator } from "./components/SupportCreator"
|
||||
import { EntryHeader } from "./header"
|
||||
import { useEntryContent, useEntryMediaInfo } from "./hooks"
|
||||
import type { EntryContentProps } from "./index.shared"
|
||||
import {
|
||||
ContainerToc,
|
||||
@@ -73,20 +68,13 @@ export const EntryContent: Component<EntryContentProps> = ({
|
||||
const feed = useFeedById(entry?.feedId) as FeedModel | InboxModel
|
||||
|
||||
const inbox = useInboxById(entry?.inboxId, (inbox) => inbox !== null)
|
||||
const isInbox = !!inbox
|
||||
const isInReadabilityMode = useEntryIsInReadability(entryId)
|
||||
|
||||
const { error, data, isPending } = useAuthQuery(
|
||||
inbox ? Queries.entries.byInboxId(entryId) : Queries.entries.byId(entryId),
|
||||
{
|
||||
staleTime: 300_000,
|
||||
},
|
||||
)
|
||||
const readabilityContent = useEntryReadabilityContent(entryId)
|
||||
const { error, content, isPending } = useEntryContent(entryId)
|
||||
|
||||
const view = useRouteParamsSelector((route) => route.view)
|
||||
|
||||
const isInReadabilityMode = useEntryIsInReadability(entryId)
|
||||
const isReadabilitySuccess = useEntryIsInReadabilitySuccess(entryId)
|
||||
|
||||
const scrollerRef = useRef<HTMLDivElement | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -105,27 +93,13 @@ export const EntryContent: Component<EntryContentProps> = ({
|
||||
|
||||
const customCSS = useUISettingKey("customCSS")
|
||||
|
||||
const contentTranslated = useEntryTranslation({
|
||||
entry,
|
||||
extraFields: isReadabilitySuccess ? ["readabilityContent"] : ["content"],
|
||||
})
|
||||
|
||||
const isInPeekModal = useInPeekModal()
|
||||
|
||||
const [isUserInteraction, setIsUserInteraction] = useState(false)
|
||||
const isZenMode = useIsZenMode()
|
||||
|
||||
if (!entry) return null
|
||||
|
||||
const entryContent = isInReadabilityMode
|
||||
? readabilityContent?.content
|
||||
: (entry?.entries.content ?? data?.entries.content)
|
||||
const translatedContent = isInReadabilityMode
|
||||
? contentTranslated.data?.readabilityContent
|
||||
: contentTranslated.data?.content
|
||||
const content = translatedContent || entryContent
|
||||
|
||||
const isInbox = !!inbox
|
||||
|
||||
return (
|
||||
<>
|
||||
{!isInPeekModal && (
|
||||
@@ -282,19 +256,7 @@ const Renderer: React.FC<{
|
||||
noMedia?: boolean
|
||||
content?: Nullable<string>
|
||||
}> = React.memo(({ entryId, view, feedId, noMedia = false, content = "" }) => {
|
||||
const mediaInfo = useEntry(entryId, (entry) =>
|
||||
Object.fromEntries(
|
||||
entry?.entries.media
|
||||
?.filter((m) => m.type === "photo")
|
||||
.map((cur) => [
|
||||
cur.url,
|
||||
{
|
||||
width: cur.width,
|
||||
height: cur.height,
|
||||
},
|
||||
]) ?? [],
|
||||
),
|
||||
)
|
||||
const mediaInfo = useEntryMediaInfo(entryId)
|
||||
|
||||
const readerRenderInlineStyle = useUISettingKey("readerRenderInlineStyle")
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { FeedModel, InboxModel } from "@follow/models/types"
|
||||
import { nextFrame, stopPropagation } from "@follow/utils/dom"
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import { ErrorBoundary } from "@sentry/react"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import { useAudioPlayerAtomSelector } from "~/atoms/player"
|
||||
import { useUISettingKey } from "~/atoms/settings/ui"
|
||||
@@ -13,10 +13,8 @@ import { ShadowDOM } from "~/components/common/ShadowDOM"
|
||||
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
|
||||
import { useRenderStyle } from "~/hooks/biz/useRenderStyle"
|
||||
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
|
||||
import { useAuthQuery, usePreventOverscrollBounce } from "~/hooks/common"
|
||||
import { usePreventOverscrollBounce } from "~/hooks/common"
|
||||
import { WrappedElementProvider } from "~/providers/wrapped-element-provider"
|
||||
import { Queries } from "~/queries"
|
||||
import { useEntryTranslation } from "~/store/ai/hook"
|
||||
import { useEntry } from "~/store/entry"
|
||||
import { useFeedById } from "~/store/feed"
|
||||
import { useInboxById } from "~/store/inbox"
|
||||
@@ -28,7 +26,14 @@ import { EntryReadHistory } from "./components/EntryReadHistory"
|
||||
import { EntryTitle } from "./components/EntryTitle"
|
||||
import { SupportCreator } from "./components/SupportCreator"
|
||||
import { EntryHeader } from "./header"
|
||||
import { NoContent, RenderError, TitleMetaHandler } from "./index.shared"
|
||||
import { useEntryContent, useEntryMediaInfo } from "./hooks"
|
||||
import {
|
||||
NoContent,
|
||||
ReadabilityAutoToggleEffect,
|
||||
ReadabilityNotice,
|
||||
RenderError,
|
||||
TitleMetaHandler,
|
||||
} from "./index.shared"
|
||||
import { EntryContentLoading } from "./loading"
|
||||
|
||||
export interface EntryContentClassNames {
|
||||
@@ -63,37 +68,19 @@ export const EntryContent: Component<{
|
||||
}, [navigateEntry, params])
|
||||
|
||||
const entry = useEntry(entryId)
|
||||
const mediaInfo = useEntryMediaInfo(entryId)
|
||||
|
||||
useTitle(entry?.entries.title)
|
||||
|
||||
const feed = useFeedById(entry?.feedId) as FeedModel | InboxModel
|
||||
const readerRenderInlineStyle = useUISettingKey("readerRenderInlineStyle")
|
||||
const inbox = useInboxById(entry?.inboxId, (inbox) => inbox !== null)
|
||||
const isInbox = !!inbox
|
||||
|
||||
const { error, data, isPending } = useAuthQuery(
|
||||
inbox ? Queries.entries.byInboxId(entryId) : Queries.entries.byId(entryId),
|
||||
{
|
||||
enabled: !!entryId,
|
||||
staleTime: 300_000,
|
||||
},
|
||||
)
|
||||
const { error, content, isPending } = useEntryContent(entryId)
|
||||
|
||||
const view = useRouteParamsSelector((route) => route.view)
|
||||
|
||||
const mediaInfo = useMemo(
|
||||
() =>
|
||||
Object.fromEntries(
|
||||
(entry?.entries.media ?? data?.entries.media)
|
||||
?.filter((m) => m.type === "photo")
|
||||
.map((cur) => [
|
||||
cur.url,
|
||||
{
|
||||
width: cur.width,
|
||||
height: cur.height,
|
||||
},
|
||||
]) ?? [],
|
||||
),
|
||||
[entry?.entries.media, data?.entries.media],
|
||||
)
|
||||
const hideRecentReader = useUISettingKey("hideRecentReader")
|
||||
|
||||
const { entryId: audioEntryId } = useAudioPlayerAtomSelector((state) => state)
|
||||
@@ -102,19 +89,10 @@ export const EntryContent: Component<{
|
||||
const [scrollElement, setScrollElement] = useState<HTMLElement | null>(null)
|
||||
|
||||
const customCSS = useUISettingKey("customCSS")
|
||||
|
||||
const contentTranslated = useEntryTranslation({ entry, extraFields: ["content"] })
|
||||
|
||||
const stableRenderStyle = useRenderStyle()
|
||||
|
||||
if (!entry) return null
|
||||
|
||||
const entryContent = entry?.entries.content ?? data?.entries.content
|
||||
const translatedContent = contentTranslated.data?.content
|
||||
const content = translatedContent || entryContent
|
||||
|
||||
const isInbox = !!inbox
|
||||
|
||||
return (
|
||||
<WrappedElementProvider>
|
||||
<ScrollElementContext value={scrollElement}>
|
||||
@@ -162,6 +140,7 @@ export const EntryContent: Component<{
|
||||
<TitleMetaHandler entryId={entry.entries.id} />
|
||||
<AISummary entryId={entry.entries.id} />
|
||||
<ErrorBoundary fallback={RenderError}>
|
||||
<ReadabilityNotice entryId={entryId} />
|
||||
<ShadowDOM injectHostStyles={!isInbox}>
|
||||
{!!customCSS && (
|
||||
<MemoedDangerousHTMLStyle>{customCSS}</MemoedDangerousHTMLStyle>
|
||||
@@ -184,6 +163,13 @@ export const EntryContent: Component<{
|
||||
</div>
|
||||
</WrappedElementProvider>
|
||||
|
||||
{entry.settings?.readability && (
|
||||
<ReadabilityAutoToggleEffect
|
||||
id={entry.entries.id}
|
||||
url={entry.entries.url ?? ""}
|
||||
/>
|
||||
)}
|
||||
|
||||
{!content && (
|
||||
<div className="center mt-16 min-w-0">
|
||||
{isPending ? (
|
||||
|
||||
Reference in New Issue
Block a user