refactor: avoid circular import in entry content

This commit is contained in:
Stephen Zhou
2024-10-25 14:26:32 +08:00
parent 6edbf10fcd
commit a45cdf5cce
5 changed files with 62 additions and 61 deletions
@@ -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: () => (
<div className="relative size-full">
<PreviewMediaContent initialIndex={initialIndex} media={media} entryId={entryId} />
<PreviewMediaContent initialIndex={initialIndex} media={media}>
{children}
</PreviewMediaContent>
</div>
),
title: "Media Preview",
@@ -25,6 +27,6 @@ export const usePreviewMedia = (entryId?: string) => {
clickOutsideToDismiss: true,
})
},
[entryId, present],
[children, present],
)
}
@@ -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<{
<div
className={cn(
"relative flex h-full w-auto overflow-hidden",
entryId ? "min-w-96 items-center justify-center rounded-l-xl bg-native" : "rounded-xl",
sideContent
? "min-w-96 items-center justify-center rounded-l-xl bg-native"
: "rounded-xl",
)}
>
{children}
<RootPortal to={entryId ? null : undefined}>
<RootPortal to={sideContent ? null : undefined}>
<div
className="pointer-events-auto absolute bottom-4 right-4 z-[99] flex gap-3 text-theme-vibrancyFg dark:text-white/70 [&_button]:hover:text-theme-vibrancyFg dark:[&_button]:hover:text-white"
onClick={stopPropagation}
@@ -89,12 +90,12 @@ const Wrapper: Component<{
</div>
</RootPortal>
</div>
{entryId && (
{!!sideContent && (
<div
className="box-border flex h-full w-[400px] min-w-0 shrink-0 flex-col rounded-r-xl bg-theme-background px-2 pt-1"
onClick={stopPropagation}
>
<EntryContent entryId={entryId} noMedia compact />
{sideContent}
</div>
)}
</m.div>
@@ -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<SwiperRef>(null)
@@ -128,14 +129,14 @@ export const PreviewMediaContent: FC<{
const src = media[0].url
const { type } = media[0]
return (
<Wrapper src={src} showActions={type !== "video"} entryId={entryId}>
<Wrapper src={src} showActions={type !== "video"} sideContent={children}>
{type === "video" ? (
<VideoPlayer
src={src}
controls
autoPlay
muted
className={cn("h-full w-auto object-contain", entryId && "rounded-l-xl")}
className={cn("h-full w-auto object-contain", !!children && "rounded-l-xl")}
onClick={stopPropagation}
/>
) : (
@@ -154,7 +155,11 @@ export const PreviewMediaContent: FC<{
)
}
return (
<Wrapper src={currentMedia.url} showActions={currentMedia.type !== "video"} entryId={entryId}>
<Wrapper
src={currentMedia.url}
showActions={currentMedia.type !== "video"}
sideContent={children}
>
<Swiper
ref={swiperRef}
loop
@@ -12,6 +12,7 @@ import { SwipeMedia } from "~/components/ui/media/SwipeMedia"
import { ReactVirtuosoItemPlaceholder } from "~/components/ui/placeholder"
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
import { filterSmallMedia } from "~/lib/utils"
import { EntryContent } from "~/modules/entry-content"
import { FeedIcon } from "~/modules/feed/feed-icon"
import { useEntry } from "~/store/entry/hooks"
import { useImageDimensions } from "~/store/image"
@@ -33,7 +34,8 @@ export function PictureItem({ entryId, entryPreview, translation }: UniversalIte
const isActive = useRouteParamsSelector(({ entryId }) => entryId === entry?.entries.id)
const { t } = useTranslation()
const previewMedia = usePreviewMedia(entryId)
const entryContent = useMemo(() => <EntryContent entryId={entryId} noMedia compact />, [entryId])
const previewMedia = usePreviewMedia(entryContent)
if (!entry) return <ReactVirtuosoItemPlaceholder />
return (
<GridItem entryId={entryId} entryPreview={entryPreview} translation={translation}>
@@ -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(() => <EntryContent entryId={entryId} noMedia compact />, [entryId])
const previewMedia = usePreviewMedia(entryContent)
const itemWidth = useMasonryItemWidth()
const [ref, setRef] = useState<HTMLDivElement | null>(null)
@@ -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 (
<m.div
className="center size-full flex-col"
initial={{ opacity: 0.01, y: 300 }}
animate={{ opacity: 1, y: 0 }}
>
<EntryPlaceholderLogo />
{feedId === ROUTE_FEED_PENDING && view === FeedViewType.Articles && (
<EntryPlaceholderDaily view={view} />
)}
</m.div>
)
}
return (
<EntryContentRender
entryId={entryId}
@@ -1,5 +1,5 @@
import { ActionButton } from "@follow/components/ui/button/index.js"
import { views } from "@follow/constants"
import { FeedViewType, views } from "@follow/constants"
import { cn } from "@follow/utils/utils"
import { useWheel } from "@use-gesture/react"
import { easeOut } from "framer-motion"
@@ -11,10 +11,12 @@ import { useParams } from "react-router-dom"
import { useUISettingKey } from "~/atoms/settings/ui"
import { useFeedColumnShow, useFeedColumnTempShow } from "~/atoms/sidebar"
import { m } from "~/components/common/Motion"
import { HotKeyScopeMap, ROUTE_ENTRY_PENDING } from "~/constants"
import { HotKeyScopeMap, ROUTE_ENTRY_PENDING, ROUTE_FEED_PENDING } from "~/constants"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useRouteView } from "~/hooks/biz/useRouteParams"
import { useRouteParams, useRouteView } from "~/hooks/biz/useRouteParams"
import { EntryPlaceholderDaily } from "~/modules/ai/ai-daily/EntryPlaceholderDaily"
import { EntryContent } from "~/modules/entry-content"
import { EntryPlaceholderLogo } from "~/modules/entry-content/components/EntryPlaceholderLogo"
import { AppLayoutGridContainerProvider } from "~/providers/app-grid-layout-container-provider"
export const Component = () => {
@@ -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 = () => {
<i className="i-mgc-close-cute-re size-5" />
</ActionButton>
)}
<EntryContent
entryId={realEntryId}
classNames={{
header: shouldHeaderPaddingLeft
? "ml-[calc(theme(width.feed-col)+theme(width.8))]"
: wideMode
? "ml-8"
: "",
}}
/>
{realEntryId ? (
<EntryContent
entryId={realEntryId}
classNames={{
header: shouldHeaderPaddingLeft
? "ml-[calc(theme(width.feed-col)+theme(width.8))]"
: wideMode
? "ml-8"
: "",
}}
/>
) : !enableEntryWideMode ? (
<m.div
className="center size-full flex-col"
initial={{ opacity: 0.01, y: 300 }}
animate={{ opacity: 1, y: 0 }}
>
<EntryPlaceholderLogo />
{feedId === ROUTE_FEED_PENDING && view === FeedViewType.Articles && (
<EntryPlaceholderDaily view={view} />
)}
</m.div>
) : null}
</EntryGridContainer>
</AppLayoutGridContainerProvider>
)