From 1a91d57cf019b2297d3eebbe6d3b0463debdcf47 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Fri, 26 Sep 2025 16:05:07 +0800 Subject: [PATCH] refactor: merge main view and feed in data block --- .../components/layouts/AIChatContextBar.tsx | 39 +-------- .../components/message/AIDataBlockItem.tsx | 80 ++++++++++++++++--- .../components/message/AIDataBlockPart.tsx | 23 ++++-- .../modules/ai-chat/hooks/useDisplayBlocks.ts | 52 ++++++++++++ 4 files changed, 142 insertions(+), 52 deletions(-) create mode 100644 apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useDisplayBlocks.ts diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatContextBar.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatContextBar.tsx index 2b73110dc..0a39e39e2 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatContextBar.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatContextBar.tsx @@ -4,9 +4,9 @@ import { memo, useCallback, useEffect, useMemo, useRef } from "react" import { useAISettingValue } from "~/atoms/settings/ai" import { DropdownMenu, DropdownMenuTrigger } from "~/components/ui/dropdown-menu/dropdown-menu" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" +import { useDisplayBlocks } from "~/modules/ai-chat/hooks/useDisplayBlocks" import { useFileUploadWithDefaults } from "~/modules/ai-chat/hooks/useFileUpload" import { useAIChatStore } from "~/modules/ai-chat/store/AIChatContext" -import type { AIChatContextBlock, ValueContextBlock } from "~/modules/ai-chat/store/types" import { SUPPORTED_MIME_ACCEPT } from "~/modules/ai-chat/utils/file-validation" import { useBlockActions } from "../../store/hooks" @@ -14,10 +14,6 @@ import { BlockSliceAction } from "../../store/slices/block.slice" import { ContextBlock, MainViewFeedContextBlock } from "../context-bar/blocks" import { ContextMenuContent, ShortcutsMenuContent } from "../context-bar/menus" -type ValueBlockOf = Omit & { - type: Type -} - export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) => void }> = memo( ({ className, onSendShortcut }) => { const blocks = useAIChatStore()((s) => s.blocks) @@ -75,38 +71,7 @@ export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) => } }, [addOrUpdateBlock, feedId, removeBlock]) - const displayBlocks = useMemo(() => { - const mainViewBlock = blocks.find( - (block): block is ValueBlockOf<"mainView"> => block.type === "mainView", - ) - const mainFeedBlock = blocks.find( - (block): block is ValueBlockOf<"mainFeed"> => block.type === "mainFeed", - ) - - if (mainViewBlock && mainFeedBlock) { - const items: ( - | { - kind: "combined" - viewBlock: ValueBlockOf<"mainView"> - feedBlock: ValueBlockOf<"mainFeed"> - } - | { kind: "single"; block: AIChatContextBlock } - )[] = [] - - items.push({ kind: "combined", viewBlock: mainViewBlock, feedBlock: mainFeedBlock }) - - const otherBlocks = blocks.filter( - (block) => block.id !== mainViewBlock.id && block.id !== mainFeedBlock.id, - ) - otherBlocks.forEach((block) => { - items.push({ kind: "single", block }) - }) - - return items - } - - return blocks.map((block) => ({ kind: "single" as const, block })) - }, [blocks]) + const displayBlocks = useDisplayBlocks(blocks) return (
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockItem.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockItem.tsx index 1317c47fd..6ed7f9476 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockItem.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockItem.tsx @@ -3,7 +3,7 @@ import { cn } from "@follow/utils/utils" import { t } from "i18next" import * as React from "react" -import type { AIChatContextBlock } from "~/modules/ai-chat/store/types" +import type { AIChatContextBlock, ValueContextBlock } from "~/modules/ai-chat/store/types" import { getBlockIcon, @@ -20,6 +20,15 @@ interface AIDataBlockItemProps { index: number } +type ValueBlockOf = Omit & { + type: Type +} + +interface MainViewFeedDataBlockItemProps { + viewBlock: ValueBlockOf<"mainView"> + feedBlock: ValueBlockOf<"mainFeed"> +} + /** * Gets the display content for a context block */ @@ -87,13 +96,15 @@ const BlockIcon: React.FC<{ BlockIcon.displayName = "BlockIcon" /** - * Individual block item component with optimized rendering and animations + * Shared block container component to ensure consistency */ -export const AIDataBlockItem: React.FC = React.memo(({ block }) => { - const styles = React.useMemo(() => getBlockStyles(block.type), [block.type]) - const label = React.useMemo(() => getBlockLabel(block.type), [block.type]) - const displayContent = React.useMemo(() => getDisplayContent(block), [block]) - +const BlockContainer: React.FC<{ + styles: ReturnType + block: AIChatContextBlock + label?: string + displayContent: React.ReactNode + title?: string +}> = React.memo(({ styles, block, label, displayContent, title }) => { return (
= React.memo(({ blo {/* Label and content */}
- {label} - · + {label && ( + <> + {label} + · + + )} {displayContent} @@ -120,4 +135,49 @@ export const AIDataBlockItem: React.FC = React.memo(({ blo ) }) +BlockContainer.displayName = "BlockContainer" + +/** + * Individual block item component with optimized rendering and animations + */ +export const AIDataBlockItem: React.FC = React.memo(({ block }) => { + const styles = React.useMemo(() => getBlockStyles(block.type), [block.type]) + const label = React.useMemo(() => getBlockLabel(block.type), [block.type]) + const displayContent = React.useMemo(() => getDisplayContent(block), [block]) + + return ( + + ) +}) + AIDataBlockItem.displayName = "AIDataBlockItem" + +/** + * Combined block item component for main view and main feed + * Displays view icon with feed title content + */ +export const MainViewFeedDataBlockItem: React.FC = React.memo( + ({ viewBlock, feedBlock }) => { + const { styles, displayContent, title } = React.useMemo(() => { + const view = views.find((v) => v.view === Number(viewBlock.value)) + const viewName = view?.name ? t(view.name, { ns: "common" }) : viewBlock.value + + return { + styles: getBlockStyles("mainView"), + displayContent: , + title: `${viewName} - ${feedBlock.value}`, + } + }, [viewBlock.value, feedBlock.value]) + + return ( + + ) + }, +) + +MainViewFeedDataBlockItem.displayName = "MainViewFeedDataBlockItem" diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockPart.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockPart.tsx index a9710040f..1e1db7522 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockPart.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIDataBlockPart.tsx @@ -1,9 +1,10 @@ import { cn } from "@follow/utils/utils" import * as React from "react" +import { useDisplayBlocks } from "~/modules/ai-chat/hooks/useDisplayBlocks" import type { AIChatContextBlock } from "~/modules/ai-chat/store/types" -import { AIDataBlockItem } from "./AIDataBlockItem" +import { AIDataBlockItem, MainViewFeedDataBlockItem } from "./AIDataBlockItem" interface AIDataBlockPartProps { blocks: AIChatContextBlock[] @@ -14,8 +15,10 @@ interface AIDataBlockPartProps { * Displays various types of context (entries, feeds, text, files) with compact styling */ export const AIDataBlockPart: React.FC = React.memo(({ blocks }) => { + const displayBlocks = useDisplayBlocks(blocks) + // Early return for empty blocks - if (!blocks?.length) { + if (displayBlocks.length === 0) { return null } @@ -34,9 +37,19 @@ export const AIDataBlockPart: React.FC = React.memo(({ blo
{/* Render individual block items */} - {blocks.map((block, index) => ( - - ))} + {displayBlocks.map((item, index) => { + if (item.kind === "combined") { + return ( + + ) + } + + return + })}
) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useDisplayBlocks.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useDisplayBlocks.ts new file mode 100644 index 000000000..fe8fb20a8 --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useDisplayBlocks.ts @@ -0,0 +1,52 @@ +import { useMemo } from "react" + +import type { AIChatContextBlock, ValueContextBlock } from "~/modules/ai-chat/store/types" + +type ValueBlockOf = Omit & { + type: Type +} + +export type DisplayBlockItem = + | { + kind: "combined" + viewBlock: ValueBlockOf<"mainView"> + feedBlock: ValueBlockOf<"mainFeed"> + } + | { kind: "single"; block: AIChatContextBlock } + +/** + * Custom hook to process blocks and merge mainView + mainFeed when both exist + * Returns an array of display items that can be either combined or single blocks + */ +export const useDisplayBlocks = (blocks: AIChatContextBlock[]): DisplayBlockItem[] => { + return useMemo(() => { + // Early return for empty blocks + if (!blocks?.length) { + return [] + } + + const mainViewBlock = blocks.find( + (block): block is ValueBlockOf<"mainView"> => block.type === "mainView", + ) + const mainFeedBlock = blocks.find( + (block): block is ValueBlockOf<"mainFeed"> => block.type === "mainFeed", + ) + + if (mainViewBlock && mainFeedBlock) { + const items: DisplayBlockItem[] = [] + + items.push({ kind: "combined", viewBlock: mainViewBlock, feedBlock: mainFeedBlock }) + + const otherBlocks = blocks.filter( + (block) => block.id !== mainViewBlock.id && block.id !== mainFeedBlock.id, + ) + otherBlocks.forEach((block) => { + items.push({ kind: "single", block }) + }) + + return items + } + + return blocks.map((block) => ({ kind: "single" as const, block })) + }, [blocks]) +}