From dac1a47b0eedc083b8a94291373873f4a2315894 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 9 Dec 2024 22:25:57 +0800 Subject: [PATCH] feat: support read indicator and back to top, fixed #2040 Signed-off-by: Innei --- .../src/components/ux/transition/icon.tsx | 41 ++++-- .../src/hooks/biz/useNavigateEntry.ts | 6 +- .../modules/entry-content/index.shared.tsx | 123 +++++++++++++++--- packages/components/src/icons/Progress.tsx | 63 +++++++++ packages/utils/src/scroller.ts | 4 +- packages/utils/tsconfig.json | 2 +- 6 files changed, 203 insertions(+), 36 deletions(-) create mode 100644 packages/components/src/icons/Progress.tsx diff --git a/apps/renderer/src/components/ux/transition/icon.tsx b/apps/renderer/src/components/ux/transition/icon.tsx index 7a20bbd4e..f9045c8ec 100644 --- a/apps/renderer/src/components/ux/transition/icon.tsx +++ b/apps/renderer/src/components/ux/transition/icon.tsx @@ -1,7 +1,8 @@ import { cn } from "@follow/utils/utils" import type { Target } from "framer-motion" import { AnimatePresence, m } from "framer-motion" -import { useEffect, useState } from "react" +import * as React from "react" +import { cloneElement, useEffect, useState } from "react" type TransitionType = { initial: Target | boolean @@ -10,8 +11,8 @@ type TransitionType = { } type IconTransitionProps = { - icon1: string - icon2: string + icon1: string | React.JSX.Element + icon2: string | React.JSX.Element status: "init" | "done" className?: string icon1ClassName?: string @@ -34,14 +35,24 @@ const createIconTransition = return ( {status === "init" ? ( - - ) : ( + typeof icon1 === "string" ? ( + + ) : ( + cloneElement(icon1, { + className: cn(icon1ClassName, className), + key: "1", + initial, + animate, + exit, + }) + ) + ) : typeof icon2 === "string" ? ( + ) : ( + cloneElement(icon2, { + className: cn(icon2ClassName, className), + key: "2", + initial, + animate, + exit, + }) )} ) diff --git a/apps/renderer/src/hooks/biz/useNavigateEntry.ts b/apps/renderer/src/hooks/biz/useNavigateEntry.ts index 584aada0a..dc1697ba0 100644 --- a/apps/renderer/src/hooks/biz/useNavigateEntry.ts +++ b/apps/renderer/src/hooks/biz/useNavigateEntry.ts @@ -93,5 +93,9 @@ export const navigateEntry = (options: NavigateEntryOptions) => { } else { if (!isMobile()) path += `/${ROUTE_ENTRY_PENDING}` } - return getStableRouterNavigate()?.(`${path}?${nextSearchParams.toString()}`) + + const finalPath = `${path}?${nextSearchParams.toString()}` + const currentPath = getReadonlyRoute().location.pathname + getReadonlyRoute().location.search + if (finalPath === currentPath) return + return getStableRouterNavigate()?.(finalPath) } diff --git a/apps/renderer/src/modules/entry-content/index.shared.tsx b/apps/renderer/src/modules/entry-content/index.shared.tsx index 4bdc50f29..8b224e9cd 100644 --- a/apps/renderer/src/modules/entry-content/index.shared.tsx +++ b/apps/renderer/src/modules/entry-content/index.shared.tsx @@ -1,12 +1,20 @@ +import { getViewport } from "@follow/components/hooks/useViewport.js" +import { + CircleProgress, + MaterialSymbolsProgressActivity, +} from "@follow/components/icons/Progress.js" import { AutoResizeHeight } from "@follow/components/ui/auto-resize-height/index.js" +import { Button, MotionButtonBase } from "@follow/components/ui/button/index.js" import { LoadingWithIcon } from "@follow/components/ui/loading/index.jsx" import { RootPortal } from "@follow/components/ui/portal/index.jsx" +import { useScrollViewElement } from "@follow/components/ui/scroll-area/hooks.js" import { IN_ELECTRON } from "@follow/shared/constants" import { EventBus } from "@follow/utils/event-bus" +import { springScrollTo } from "@follow/utils/scroller" import { cn } from "@follow/utils/utils" import type { FallbackRender } from "@sentry/react" import type { FC } from "react" -import { memo, useEffect, useLayoutEffect, useRef } from "react" +import { memo, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react" import { useTranslation } from "react-i18next" import { useShowAISummary } from "~/atoms/ai-summary" @@ -18,12 +26,17 @@ import { } from "~/atoms/readability" import { enableShowSourceContent } from "~/atoms/source-content" import { Toc } from "~/components/ui/markdown/components/Toc" +import { IconOpacityTransition } from "~/components/ux/transition/icon" import { isWebBuild } from "~/constants" import { useEntryReadabilityToggle } from "~/hooks/biz/useEntryActions" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useAuthQuery } from "~/hooks/common/useBizQuery" import { getNewIssueUrl } from "~/lib/issues" -import { useIsSoFWrappedElement, useWrappedElement } from "~/providers/wrapped-element-provider" +import { + useIsSoFWrappedElement, + useWrappedElement, + useWrappedElementSize, +} from "~/providers/wrapped-element-provider" import { Queries } from "~/queries" import { useEntry } from "~/store/entry" import { useFeedById } from "~/store/feed" @@ -183,31 +196,64 @@ export const RenderError: FallbackRender = ({ error }) => { {t("entry_content.render_error")} {nextError.message} - { + window.open( + getNewIssueUrl({ + body: [ + "### Error", + "", + nextError.message, + "", + "### Stack", + "", + "```", + nextError.stack, + "```", + ].join("\n"), + label: "bug", + title: "Render error", + }), + ) + }} > {t("entry_content.report_issue")} - + ) } +const useReadPercent = () => { + const y = 55 + const { h } = useWrappedElementSize() + + const scrollElement = useScrollViewElement() + const [scrollTop, setScrollTop] = useState(0) + + useEffect(() => { + const handler = () => { + if (scrollElement) { + setScrollTop(scrollElement.scrollTop) + } + } + handler() + scrollElement?.addEventListener("scroll", handler) + return () => { + scrollElement?.removeEventListener("scroll", handler) + } + }, [scrollElement]) + + const readPercent = useMemo(() => { + const winHeight = getViewport().h + const deltaHeight = Math.min(scrollTop, winHeight) + + return Math.floor(Math.min(Math.max(0, ((scrollTop - y + deltaHeight) / h) * 100), 100)) || 0 + }, [y, h, scrollTop]) + + return [readPercent, scrollTop] +} + export const ContainerToc: FC = memo(() => { const wrappedElement = useWrappedElement() @@ -226,12 +272,47 @@ export const ContainerToc: FC = memo(() => { "@[700px]:-translate-x-12 @[800px]:-translate-x-16 @[900px]:translate-x-0 @[900px]:items-start", )} /> + + ) }) +const BackTopIndicator = memo(() => { + const [readPercent] = useReadPercent() + const scrollElement = useScrollViewElement() + return ( + +
+ } + icon2={} + status={readPercent === 0 ? "init" : "done"} + /> + {readPercent}%
+
+ { + springScrollTo(0, scrollElement!) + }} + className={cn( + "mt-1 flex flex-nowrap items-center gap-2 opacity-50 transition-all duration-500 hover:opacity-100", + readPercent > 10 ? "" : "pointer-events-none opacity-0", + )} + > + + Back Top + +
+ ) +}) + export function AISummary({ entryId }: { entryId: string }) { const { t } = useTranslation() const entry = useEntry(entryId) diff --git a/packages/components/src/icons/Progress.tsx b/packages/components/src/icons/Progress.tsx new file mode 100644 index 000000000..effc8ab9f --- /dev/null +++ b/packages/components/src/icons/Progress.tsx @@ -0,0 +1,63 @@ +import type { SVGProps } from "react" + +export function MaterialSymbolsProgressActivity(props: SVGProps) { + return ( + + + + ) +} + +interface CircleProgressProps { + percent: number + size?: number + strokeWidth?: number + strokeColor?: string + backgroundColor?: string + className?: string +} + +export const CircleProgress: React.FC = ({ + percent, + size = 100, + strokeWidth = 8, + strokeColor = "currentColor", + backgroundColor = "hsl(var(--background))", + className, +}) => { + const normalizedPercent = Math.min(100, Math.max(0, percent)) + const radius = (size - strokeWidth) / 2 + const circumference = radius * 2 * Math.PI + const offset = circumference - (normalizedPercent / 100) * circumference + + return ( + + + + + ) +} diff --git a/packages/utils/src/scroller.ts b/packages/utils/src/scroller.ts index d2a501e3d..56735a946 100644 --- a/packages/utils/src/scroller.ts +++ b/packages/utils/src/scroller.ts @@ -24,7 +24,7 @@ export const springScrollTo = ( autoplay: true, ...spring, onPlay() { - el.addEventListener("wheel", stopSpringScrollHandler) + el.addEventListener("wheel", stopSpringScrollHandler, { capture: true }) el.addEventListener("touchmove", stopSpringScrollHandler) }, @@ -38,7 +38,7 @@ export const springScrollTo = ( }) animation.then(() => { - el.removeEventListener("wheel", stopSpringScrollHandler) + el.removeEventListener("wheel", stopSpringScrollHandler, { capture: true }) el.removeEventListener("touchmove", stopSpringScrollHandler) }) diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json index 70eb01c53..8ebcfd1ae 100644 --- a/packages/utils/tsconfig.json +++ b/packages/utils/tsconfig.json @@ -2,7 +2,7 @@ "extends": "../tsconfig.extend.json", "compilerOptions": { "baseUrl": ".", - "declaration": true, + "declaration": false, "types": ["@follow/types/global"], "paths": { "@follow/utils/*": ["./src/*"]