From 8ed0a6897fd6e0f4db2e6e413971a605431c948b Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 23 Jun 2025 18:51:27 +0800 Subject: [PATCH 1/4] refactor(desktop): rename and restructure navigation functions for clarity - Renamed `navigateEntry` to `getNavigateEntryPath` for better clarity on its purpose. - Introduced a memoized `navigationPath` in `EntryItemWrapper` to optimize path generation. - Updated the navigation logic to utilize the new function, improving code organization and readability. Signed-off-by: Innei --- .../src/hooks/biz/useNavigateEntry.ts | 75 ++++++++++++++----- .../entry-column/layouts/EntryItemWrapper.tsx | 22 ++++-- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/apps/desktop/layer/renderer/src/hooks/biz/useNavigateEntry.ts b/apps/desktop/layer/renderer/src/hooks/biz/useNavigateEntry.ts index ea9c0dbdc..643b02cce 100644 --- a/apps/desktop/layer/renderer/src/hooks/biz/useNavigateEntry.ts +++ b/apps/desktop/layer/renderer/src/hooks/biz/useNavigateEntry.ts @@ -4,6 +4,7 @@ import { useSheetContext } from "@follow/components/ui/sheet/context.js" import type { FeedViewType } from "@follow/constants" import { tracker } from "@follow/tracker" import { useCallback } from "react" +import { toast } from "sonner" import { disableShowAISummaryOnce } from "~/atoms/ai-summary" import { disableShowAITranslationOnce } from "~/atoms/ai-translation" @@ -46,14 +47,14 @@ export const useNavigateEntry = () => { ) } -/* - * /timeline/:timelineId/:feedId/:entryId - * timelineId: view-1 - * feedId: xxx, folder-xxx, list-xxx, inbox-xxx - * entryId: xxx - */ -export const navigateEntry = (options: NavigateEntryOptions) => { - const { entryId, feedId, view, folderName, inboxId, listId, timelineId, backPath } = options || {} +type ParsedNavigateEntryOptions = { + feedId: string + timelineId: string + entryId: string +} + +const parseNavigateEntryOptions = (options: NavigateEntryOptions): ParsedNavigateEntryOptions => { + const { entryId, feedId, view, folderName, inboxId, listId, timelineId } = options || {} const route = getReadonlyRoute() const { params } = route let finalFeedId = feedId || params.feedId || ROUTE_FEED_PENDING @@ -62,10 +63,6 @@ export const navigateEntry = (options: NavigateEntryOptions) => { const subscription = getSubscriptionByFeedId(finalFeedId) const finalView = subscription?.view || view - if (backPath) { - setPreviewBackPath(backPath) - } - if ("feedId" in options && feedId === null) { finalFeedId = ROUTE_FEED_PENDING } @@ -88,17 +85,61 @@ export const navigateEntry = (options: NavigateEntryOptions) => { finalTimelineId = `${ROUTE_TIMELINE_OF_VIEW}${finalView}` } + return { + feedId: finalFeedId, + timelineId: finalTimelineId, + entryId: finalEntryId, + } +} + +export function getNavigateEntryPath(options: NavigateEntryOptions | ParsedNavigateEntryOptions) { + if ("feedId" in options) { + return `/timeline/${options.timelineId}/${options.feedId}/${options.entryId}` + } + + const { feedId, timelineId, entryId } = parseNavigateEntryOptions(options) + + return `/timeline/${timelineId}/${feedId}/${entryId}` +} + +/* + * /timeline/:timelineId/:feedId/:entryId + * timelineId: view-1 + * feedId: xxx, folder-xxx, list-xxx, inbox-xxx + * entryId: xxx + */ +export const navigateEntry = (options: NavigateEntryOptions) => { + const parsedOptions = parseNavigateEntryOptions(options) + const path = getNavigateEntryPath(parsedOptions) + const { backPath } = options || {} + const route = getReadonlyRoute() + const currentPath = route.location.pathname + route.location.search + if (path === currentPath) return + + if (backPath) { + setPreviewBackPath(backPath) + } + + tracker.navigateEntry({ + feedId: parsedOptions.feedId, + entryId: parsedOptions.entryId, + timelineId: parsedOptions.timelineId, + }) + resetShowSourceContent() disableShowAISummaryOnce() disableShowAITranslationOnce() - tracker.navigateEntry({ feedId: finalFeedId, entryId: finalEntryId, timelineId: finalTimelineId }) + const navigate = getStableRouterNavigate() - const path = `/timeline/${finalTimelineId}/${finalFeedId}/${finalEntryId}` + if (!navigate) { + const message = + "Navigation is not available, maybe a mistake in the code, please report an issue. thx." + toast.error(message) + throw new Error(message, { cause: "Navigation is not available" }) + } - const currentPath = route.location.pathname + route.location.search - if (path === currentPath) return - return getStableRouterNavigate()?.(path) + return navigate?.(path) } export const useBackHome = (timelineId?: string) => { diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx index 75027436e..21208e608 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx @@ -5,8 +5,9 @@ import { views } from "@follow/constants" import { EventBus } from "@follow/utils/event-bus" import { cn } from "@follow/utils/utils" import type { FC, PropsWithChildren } from "react" -import { useCallback, useState } from "react" +import { useCallback, useMemo, useState } from "react" import { useTranslation } from "react-i18next" +import { NavLink } from "react-router" import { useDebounceCallback } from "usehooks-ts" import { @@ -21,7 +22,7 @@ import { useEntryIsRead } from "~/hooks/biz/useAsRead" import { useContextMenuActionShortCutTrigger } from "~/hooks/biz/useContextMenuActionShortCutTrigger" import { useEntryActions } from "~/hooks/biz/useEntryActions" import { useFeedActions } from "~/hooks/biz/useFeedActions" -import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" +import { getNavigateEntryPath, useNavigateEntry } from "~/hooks/biz/useNavigateEntry" import { getRouteParams, useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useContextMenu } from "~/hooks/common/useContextMenu" import { COMMAND_ID } from "~/modules/command/commands/id" @@ -74,8 +75,16 @@ export const EntryItemWrapper: FC< ) const navigate = useNavigateEntry() + + const navigationPath = useMemo(() => { + if (!entry?.id) return "#" + return getNavigateEntryPath({ + entryId: entry?.id, + }) + }, [entry?.id]) const handleClick = useCallback( (e) => { + e.preventDefault() e.stopPropagation() const shouldNavigate = getRouteParams().entryId !== entry?.id @@ -96,7 +105,7 @@ export const EntryItemWrapper: FC< }, [asRead, entry?.id, entry?.feedId, navigate], ) - const handleDoubleClick: React.MouseEventHandler = useCallback( + const handleDoubleClick: React.MouseEventHandler = useCallback( () => entry?.url && window.open(entry.url, "_blank"), [entry?.url], ) @@ -168,9 +177,10 @@ export const EntryItemWrapper: FC< return (
-
{children} -
+
) } From 8102400404d200e3e9d5c9db9a17de7ceb211894 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 23 Jun 2025 19:15:37 +0800 Subject: [PATCH 2/4] feat(desktop): add debug logging in EntryItemWrapper for click handling - Introduced console logging in the handleClick function to track navigation decisions and entry details. - This addition aids in debugging navigation logic by providing insights into the entryId and feedId during click events. Signed-off-by: Innei --- .../src/modules/entry-column/layouts/EntryItemWrapper.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx index 21208e608..cfdb079a7 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx @@ -88,6 +88,14 @@ export const EntryItemWrapper: FC< e.stopPropagation() const shouldNavigate = getRouteParams().entryId !== entry?.id + console.info( + "handleClick", + e, + getRouteParams().entryId, + entry?.id, + shouldNavigate, + entry?.feedId, + ) if (!shouldNavigate) return if (!entry?.feedId) return if (!asRead) { From f8dc0aa5e8a1d5ab1a7bc2709ce20b15262ddc07 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 23 Jun 2025 19:30:10 +0800 Subject: [PATCH 3/4] refactor(desktop): remove debug logging from handleClick in EntryItemWrapper - Commented out console logging in the handleClick function to clean up the code and reduce unnecessary output during click events. - This change maintains the functionality while improving the overall readability of the code. Signed-off-by: Innei --- .../entry-column/layouts/EntryItemWrapper.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx index cfdb079a7..7cdb0a034 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx @@ -88,14 +88,14 @@ export const EntryItemWrapper: FC< e.stopPropagation() const shouldNavigate = getRouteParams().entryId !== entry?.id - console.info( - "handleClick", - e, - getRouteParams().entryId, - entry?.id, - shouldNavigate, - entry?.feedId, - ) + // console.info( + // "handleClick", + // e, + // getRouteParams().entryId, + // entry?.id, + // shouldNavigate, + // entry?.feedId, + // ) if (!shouldNavigate) return if (!entry?.feedId) return if (!asRead) { From c7b7b71e581f828bd81673c32cec538acc72162b Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 23 Jun 2025 19:34:06 +0800 Subject: [PATCH 4/4] cleanup Signed-off-by: Innei --- .../modules/entry-column/layouts/EntryItemWrapper.tsx | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx index 7cdb0a034..372024b2d 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx @@ -88,14 +88,7 @@ export const EntryItemWrapper: FC< e.stopPropagation() const shouldNavigate = getRouteParams().entryId !== entry?.id - // console.info( - // "handleClick", - // e, - // getRouteParams().entryId, - // entry?.id, - // shouldNavigate, - // entry?.feedId, - // ) + if (!shouldNavigate) return if (!entry?.feedId) return if (!asRead) {