From 2dcdf0d6f20df1bf812939cf4108e433dcadef33 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 14 Aug 2025 00:43:15 +0800 Subject: [PATCH] fix(entry-content): `escape` key to back to entry list in zen mode - Refactored featureConfigMap to utilize TypeScript's `satisfies` operator for improved type safety. - Added a focusable reference in EntryContent to manage focus behavior during animations. - Integrated hotkey handling in EntryScrollingAndNavigationHandler to improve navigation experience based on AI feature status and Zen mode. These changes collectively enhance the code structure and user interaction within the entry content components, providing a more responsive and intuitive experience. Signed-off-by: Innei --- .../layer/renderer/src/lib/features.tsx | 7 +++---- .../entry-content/EntryContent.ai.tsx | 3 +++ .../EntryScrollingAndNavigationHandler.tsx | 21 ++++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/apps/desktop/layer/renderer/src/lib/features.tsx b/apps/desktop/layer/renderer/src/lib/features.tsx index 7de933ade..f218f0a41 100644 --- a/apps/desktop/layer/renderer/src/lib/features.tsx +++ b/apps/desktop/layer/renderer/src/lib/features.tsx @@ -3,10 +3,9 @@ import type { FC } from "react" import { useFeature } from "~/hooks/biz/useFeature" -export const featureConfigMap: Record> = - { - ai: "AI_CHAT_ENABLED", - } +export const featureConfigMap = { + ai: "AI_CHAT_ENABLED", +} satisfies Record> export const withFeature = (feature: keyof typeof featureConfigMap) => diff --git a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryContent.ai.tsx b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryContent.ai.tsx index 3ef0075f1..24501c7f6 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryContent.ai.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryContent.ai.tsx @@ -97,9 +97,11 @@ const EntryContentImpl: Component = ({ }, [addOrUpdateBlock, entryId, removeBlock]) const animationController = useAnimationControls() + const focusableRef = useRef(null) useEffect(() => { animationController.set(contentVariants.exit) animationController.start(contentVariants.animate) + focusableRef.current?.focus() return () => { animationController.stop() } @@ -116,6 +118,7 @@ const EntryContentImpl: Component = ({
diff --git a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx index 2cbfd8794..5ee76327b 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx @@ -11,9 +11,13 @@ import type { JSAnimation } from "motion/react" import { AnimatePresence, m } from "motion/react" import * as React from "react" import { useEffect, useRef, useState } from "react" +import { useHotkeys } from "react-hotkeys-hook" import { useEventCallback } from "usehooks-ts" +import { useIsZenMode } from "~/atoms/settings/ui" import { FocusablePresets } from "~/components/common/Focusable" +import { useFeature } from "~/hooks/biz/useFeature" +import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" import { COMMAND_ID } from "~/modules/command/commands/id" import { useCommandBinding } from "~/modules/command/hooks/use-command-binding" import { useCommandHotkey } from "~/modules/command/hooks/use-register-hotkey" @@ -50,12 +54,27 @@ export const EntryScrollingAndNavigationHandler = ({ when, }) + const isZenMode = useIsZenMode() + // TODO: Here, do not rely on the AI switch, but should be deps on the new layout. + const isAiEnabled = useFeature("ai") + + const useBackHandler = isZenMode || isAiEnabled + useCommandHotkey({ commandId: COMMAND_ID.layout.focusToTimeline, - when, + when: when && !useBackHandler, shortcut: "Backspace, Escape", }) + const navigateToTimeline = useNavigateEntry() + useHotkeys( + "Escape", + () => { + navigateToTimeline({ entryId: null }) + }, + { enabled: when && useBackHandler }, + ) + const { highlightBoundary } = useFocusActions() const smoothScrollTo = useSmoothScroll() const navigateToNext = useEventCallback(() => {