From a45cdf5ccefc6ddb35bb2092663cfd9bb321a188 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:26:32 +0800 Subject: [PATCH] refactor: avoid circular import in entry content --- .../src/components/ui/media/hooks.tsx | 8 ++-- .../src/components/ui/media/preview-media.tsx | 29 +++++++----- .../entry-column/Items/picture-item.tsx | 8 ++-- .../src/modules/entry-content/index.tsx | 33 ++------------ .../feeds/[feedId]/[entryId]/index.tsx | 45 +++++++++++++------ 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/apps/renderer/src/components/ui/media/hooks.tsx b/apps/renderer/src/components/ui/media/hooks.tsx index 58fac6d56..5692a25c5 100644 --- a/apps/renderer/src/components/ui/media/hooks.tsx +++ b/apps/renderer/src/components/ui/media/hooks.tsx @@ -5,14 +5,16 @@ import { useModalStack } from "../modal/stacked/hooks" import type { PreviewMediaProps } from "./preview-media" import { PreviewMediaContent } from "./preview-media" -export const usePreviewMedia = (entryId?: string) => { +export const usePreviewMedia = (children?: React.ReactNode) => { const { present } = useModalStack() return useCallback( (media: PreviewMediaProps[], initialIndex = 0) => { present({ content: () => (
- + + {children} +
), title: "Media Preview", @@ -25,6 +27,6 @@ export const usePreviewMedia = (entryId?: string) => { clickOutsideToDismiss: true, }) }, - [entryId, present], + [children, present], ) } diff --git a/apps/renderer/src/components/ui/media/preview-media.tsx b/apps/renderer/src/components/ui/media/preview-media.tsx index 493e6ee72..2df886427 100644 --- a/apps/renderer/src/components/ui/media/preview-media.tsx +++ b/apps/renderer/src/components/ui/media/preview-media.tsx @@ -17,7 +17,6 @@ import { m } from "~/components/common/Motion" import { COPY_MAP } from "~/constants" import { tipcClient } from "~/lib/client" import { replaceImgUrlIfNeed } from "~/lib/img-proxy" -import { EntryContent } from "~/modules/entry-content" import { microReboundPreset } from "../constants/spring" import { FixedModalCloseButton } from "../modal/components/close" @@ -27,8 +26,8 @@ import { VideoPlayer } from "./VideoPlayer" const Wrapper: Component<{ src: string showActions?: boolean - entryId?: string -}> = ({ children, src, showActions, entryId }) => { + sideContent?: React.ReactNode +}> = ({ children, src, showActions, sideContent }) => { const { dismiss } = useCurrentModal() const { t } = useTranslation(["shortcuts", "external"]) @@ -55,11 +54,13 @@ const Wrapper: Component<{
{children} - +
- {entryId && ( + {!!sideContent && (
- + {sideContent}
)} @@ -108,8 +109,8 @@ export interface PreviewMediaProps extends MediaModel { export const PreviewMediaContent: FC<{ media: PreviewMediaProps[] initialIndex?: number - entryId?: string -}> = ({ media, initialIndex = 0, entryId }) => { + children?: React.ReactNode +}> = ({ media, initialIndex = 0, children }) => { const [currentMedia, setCurrentMedia] = useState(media[initialIndex]) const [currentSlideIndex, setCurrentSlideIndex] = useState(initialIndex) const swiperRef = useRef(null) @@ -128,14 +129,14 @@ export const PreviewMediaContent: FC<{ const src = media[0].url const { type } = media[0] return ( - + {type === "video" ? ( ) : ( @@ -154,7 +155,11 @@ export const PreviewMediaContent: FC<{ ) } return ( - + entryId === entry?.entries.id) const { t } = useTranslation() - const previewMedia = usePreviewMedia(entryId) + const entryContent = useMemo(() => , [entryId]) + const previewMedia = usePreviewMedia(entryContent) if (!entry) return return ( @@ -76,8 +78,8 @@ export const PictureWaterFallItem = memo(function PictureWaterFallItem({ const entry = useEntry(entryId) || entryPreview const isActive = useRouteParamsSelector(({ entryId }) => entryId === entry?.entries.id) - - const previewMedia = usePreviewMedia(entryId) + const entryContent = useMemo(() => , [entryId]) + const previewMedia = usePreviewMedia(entryContent) const itemWidth = useMasonryItemWidth() const [ref, setRef] = useState(null) diff --git a/apps/renderer/src/modules/entry-content/index.tsx b/apps/renderer/src/modules/entry-content/index.tsx index 775efc22f..c0442958a 100644 --- a/apps/renderer/src/modules/entry-content/index.tsx +++ b/apps/renderer/src/modules/entry-content/index.tsx @@ -2,9 +2,8 @@ import { AutoResizeHeight } from "@follow/components/ui/auto-resize-height/index import { LoadingWithIcon } from "@follow/components/ui/loading/index.jsx" import { RootPortal } from "@follow/components/ui/portal/index.jsx" import { ScrollArea } from "@follow/components/ui/scroll-area/index.js" -import { FeedViewType } from "@follow/constants" import { useTitle } from "@follow/hooks" -import type { ActiveEntryId, FeedModel, InboxModel } from "@follow/models/types" +import type { FeedModel, InboxModel } from "@follow/models/types" import { IN_ELECTRON } from "@follow/shared/constants" import { stopPropagation } from "@follow/utils/dom" import { cn } from "@follow/utils/utils" @@ -24,14 +23,13 @@ import { } from "~/atoms/readability" import { useUISettingKey } from "~/atoms/settings/ui" import { enableShowSourceContent } from "~/atoms/source-content" -import { m } from "~/components/common/Motion" import { ShadowDOM } from "~/components/common/ShadowDOM" import { Toc } from "~/components/ui/markdown/components/Toc" import { useInPeekModal } from "~/components/ui/modal/inspire/PeekModal" -import { isWebBuild, ROUTE_FEED_PENDING } from "~/constants" +import { isWebBuild } from "~/constants" import { shortcuts } from "~/constants/shortcuts" import { useEntryReadabilityToggle } from "~/hooks/biz/useEntryActions" -import { useRouteParams, useRouteParamsSelector } from "~/hooks/biz/useRouteParams" +import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useAuthQuery } from "~/hooks/common" import { getNewIssueUrl } from "~/lib/issues" import { LanguageMap } from "~/lib/translate" @@ -44,7 +42,6 @@ import { Queries } from "~/queries" import { useEntry } from "~/store/entry" import { useFeedById } from "~/store/feed" -import { EntryPlaceholderDaily } from "../ai/ai-daily/EntryPlaceholderDaily" import { EntryContentHTMLRenderer } from "../renderer/html" import { getTranslationCache, @@ -52,7 +49,6 @@ import { setEntryTitleMeta, setTranslationCache, } from "./atoms" -import { EntryPlaceholderLogo } from "./components/EntryPlaceholderLogo" import { EntryTitle } from "./components/EntryTitle" import { SourceContentPanel } from "./components/SourceContentView" import { SupportCreator } from "./components/SupportCreator" @@ -68,32 +64,11 @@ export const EntryContent = ({ compact, classNames, }: { - entryId: ActiveEntryId + entryId: string noMedia?: boolean compact?: boolean classNames?: EntryContentClassNames }) => { - const { feedId, view } = useRouteParams() - const enableEntryWideMode = useUISettingKey("wideMode") - - if (!entryId) { - if (enableEntryWideMode) { - return null - } - return ( - - - {feedId === ROUTE_FEED_PENDING && view === FeedViewType.Articles && ( - - )} - - ) - } - return ( { @@ -44,6 +46,9 @@ export const Component = () => { }, ) + const { feedId } = useRouteParams() + const enableEntryWideMode = useUISettingKey("wideMode") + if (!showEntryContent) { return null } @@ -68,17 +73,29 @@ export const Component = () => { )} - - + {realEntryId ? ( + + ) : !enableEntryWideMode ? ( + + + {feedId === ROUTE_FEED_PENDING && view === FeedViewType.Articles && ( + + )} + + ) : null} )