From 2b902efff222f4510bd417d622e919e63da92d87 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 17 Oct 2025 23:10:52 +0800 Subject: [PATCH] reactor: extract context block hook Signed-off-by: Innei --- .../context-bar/blocks/ContextBlock.tsx | 239 ++---------------- .../components/message/AIDataBlockItem.tsx | 90 +++---- .../message/useContextBlockPresentation.tsx | 179 +++++++++++++ .../src/modules/ai-chat/store/types.ts | 14 +- 4 files changed, 253 insertions(+), 269 deletions(-) create mode 100644 apps/desktop/layer/renderer/src/modules/ai-chat/components/message/useContextBlockPresentation.tsx diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx index ad002f29c..6acea82c7 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx @@ -4,26 +4,18 @@ import type { FC, ReactNode } from "react" import { memo } from "react" import { useTranslation } from "react-i18next" -import { ImageThumbnail } from "~/modules/ai-chat/components/message/ImageThumbnail" -import { CircularProgress } from "~/modules/ai-chat/components/ui/UploadProgress" +import { useContextBlockPresentation } from "~/modules/ai-chat/components/message/useContextBlockPresentation" import { useChatBlockActions } from "~/modules/ai-chat/store/hooks" -import type { AIChatContextBlock, ValueContextBlock } from "~/modules/ai-chat/store/types" -import { - getFileCategoryFromMimeType, - getFileIconName, -} from "~/modules/ai-chat/utils/file-validation" +import type { AbstractValueContextBlock, AIChatContextBlock } from "~/modules/ai-chat/store/types" -import { EntryTitle, FeedTitle } from "./TitleComponents" - -const blockTypeCanNotBeRemoved = new Set([]) +import { FeedTitle } from "./TitleComponents" const BlockContainer: FC<{ icon: string | null | undefined label?: string - canRemove: boolean onRemove?: () => void content: ReactNode -}> = memo(({ icon, label, canRemove, onRemove, content }) => { +}> = memo(({ icon, label, onRemove, content }) => { const isStringContent = typeof content === "string" return ( @@ -36,9 +28,7 @@ const BlockContainer: FC<{
@@ -55,27 +45,21 @@ const BlockContainer: FC<{
- {canRemove && ( - - )} + ) }) BlockContainer.displayName = "ContextBlockContainer" -type ValueBlockOf = Omit & { - type: Type -} - -type MainViewBlock = ValueBlockOf<"mainView"> -type MainFeedBlock = ValueBlockOf<"mainFeed"> -type UnreadOnlyBlock = ValueBlockOf<"unreadOnly"> +type MainViewBlock = AbstractValueContextBlock<"mainView"> +type MainFeedBlock = AbstractValueContextBlock<"mainFeed"> +type UnreadOnlyBlock = AbstractValueContextBlock<"unreadOnly"> export const CombinedContextBlock: FC<{ viewBlock?: MainViewBlock @@ -88,15 +72,9 @@ export const CombinedContextBlock: FC<{ const viewIcon = viewBlock && getView(Number(viewBlock.value))?.icon.props.className const handleRemove = () => { - if (viewBlock) { - blockActions.removeBlock(viewBlock.id) - } - if (feedBlock) { - blockActions.removeBlock(feedBlock.id) - } - if (unreadOnlyBlock) { - blockActions.removeBlock(unreadOnlyBlock.id) - } + viewBlock && blockActions.removeBlock(viewBlock.id) + feedBlock && blockActions.removeBlock(feedBlock.id) + unreadOnlyBlock && blockActions.removeBlock(unreadOnlyBlock.id) } // Determine what to display @@ -116,190 +94,21 @@ export const CombinedContextBlock: FC<{ ) - return ( - - ) + return }) CombinedContextBlock.displayName = "CombinedContextBlock" export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) => { - const { t } = useTranslation("common") const blockActions = useChatBlockActions() - const getBlockIcon = () => { - switch (block.type) { - case "mainView": { - const viewIcon = getView(Number(block.value))?.icon.props.className - return viewIcon - } - case "mainEntry": { - return "i-mgc-star-cute-fi" - } - case "mainFeed": { - return "i-mgc-rss-cute-fi" - } - case "selectedText": { - return "i-mgc-quill-pen-cute-re" - } - case "unreadOnly": { - return "i-mgc-round-cute-fi" - } - case "fileAttachment": { - const { type, dataUrl, previewUrl } = block.attachment - const fileCategory = getFileCategoryFromMimeType(type) - - // Don't show icon for images with thumbnails, as the thumbnail serves as the icon - if (fileCategory === "image" && (dataUrl || previewUrl)) { - return null - } - - return getFileIconName(fileCategory) - } - - default: { - return "i-mgc-paper-cute-fi" - } - } - } - - const getDisplayContent = () => { - switch (block.type) { - case "mainView": { - const viewName = getView(Number(block.value))?.name - return viewName ? t(viewName) : block.value - } - case "mainEntry": { - return - } - case "mainFeed": { - return - } - case "selectedText": { - return `"${block.value}"` - } - case "unreadOnly": { - return "Unread Only" - } - case "fileAttachment": { - const { type, name, dataUrl, previewUrl, uploadStatus, errorMessage, uploadProgress } = - block.attachment - - const fileCategory = getFileCategoryFromMimeType(type) - - if (fileCategory === "image" && (dataUrl || previewUrl)) { - return ( -
-
- - {uploadStatus === "uploading" && uploadProgress !== undefined && ( -
- -
- )} - {uploadStatus === "error" && ( -
- -
- )} -
- -
- - {name}{" "} - {uploadStatus === "uploading" && uploadProgress !== undefined && ( -
- ({Math.round(uploadProgress)}%) -
- )} -
- - {uploadStatus === "error" &&
Upload failed
} -
-
- ) - } - - // For non-image files - return ( -
- {name} - {uploadStatus === "uploading" && uploadProgress !== undefined && ( -
- - {Math.round(uploadProgress)}% -
- )} - {uploadStatus === "error" && ( - - )} -
- ) - } - default: { - // This should never happen with proper discriminated union - return "" - } - } - } - - const getBlockLabel = () => { - switch (block.type) { - case "mainView": { - return "" - } - case "mainEntry": { - return "Current" - } - case "mainFeed": { - return "Current" - } - case "selectedText": { - return "Text" - } - case "unreadOnly": { - return "Filter" - } - case "fileAttachment": { - return "File" - } - - default: { - return "" - } - } - } - - const canRemove = !blockTypeCanNotBeRemoved.has(block.type) + const { icon, label, displayContent } = useContextBlockPresentation(block) return ( blockActions.removeBlock(block.id) : undefined} - content={getDisplayContent()} + icon={icon} + label={label} + onRemove={() => blockActions.removeBlock(block.id)} + content={displayContent} /> ) }) 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 53291f5ff..c70e74172 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 @@ -5,15 +5,10 @@ import * as React from "react" import type { AIChatContextBlock, ValueContextBlock } from "~/modules/ai-chat/store/types" -import { - getBlockIcon, - getBlockLabel, - getBlockStyles, - getFileDisplayContent, - isImageAttachment, -} from "./ai-block-constants" -import { EntryTitle, FeedTitle } from "./BlockTitleComponents" +import { getBlockStyles } from "./ai-block-constants" +import { FeedTitle } from "./BlockTitleComponents" import { ImageThumbnail } from "./ImageThumbnail" +import { useContextBlockPresentation } from "./useContextBlockPresentation" interface AIDataBlockItemProps { block: AIChatContextBlock @@ -33,39 +28,6 @@ interface CombinedDataBlockItemProps { /** * Gets the display content for a context block */ -const getDisplayContent = (block: AIChatContextBlock): React.ReactNode => { - switch (block.type) { - case "mainView": { - const viewName = getView(Number(block.value))?.name - return viewName ? t(viewName, { ns: "common" }) : block.value - } - case "mainEntry": { - return - } - case "mainFeed": { - return - } - case "selectedText": { - return `"${block.value}"` - } - case "unreadOnly": { - return "Unread Only" - } - case "fileAttachment": { - if (!block.attachment) { - return "[File: Unknown]" - } - if (block.attachment.name && !block.attachment.uploadStatus) { - return block.attachment.name - } - return getFileDisplayContent(block.attachment) - } - - default: { - return "" - } - } -} /** * Renders the appropriate icon or image thumbnail for a block @@ -73,19 +35,27 @@ const getDisplayContent = (block: AIChatContextBlock): React.ReactNode => { const BlockIcon: React.FC<{ block: AIChatContextBlock styles: ReturnType -}> = React.memo(({ block, styles }) => { - // Handle image thumbnails for file attachments - if (block.type === "fileAttachment" && block.attachment && isImageAttachment(block)) { + presentation: ReturnType +}> = React.memo(({ block, styles, presentation }) => { + if ( + block.type === "fileAttachment" && + presentation.attachment && + presentation.isImageAttachment + ) { return (
- +
) } - const iconClass = getBlockIcon(block) + const iconClass = presentation.icon + + if (!iconClass) { + return null + } return (
= React.memo(({ styles, block, label, displayContent, title }) => { + presentation: ReturnType +}> = React.memo(({ styles, block, label, displayContent, title, presentation }) => { return (
- + {/* Label and content */}
@@ -145,11 +116,17 @@ BlockContainer.displayName = "BlockContainer" */ 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 presentation = useContextBlockPresentation(block) return ( - + ) }) @@ -232,8 +209,17 @@ export const CombinedDataBlockItem: React.FC = React } }, [viewBlock, feedBlock, unreadOnlyBlock]) + const presentation = useContextBlockPresentation(block) + return ( - + ) }, ) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/useContextBlockPresentation.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/useContextBlockPresentation.tsx new file mode 100644 index 000000000..12bf83032 --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/useContextBlockPresentation.tsx @@ -0,0 +1,179 @@ +import { getView } from "@follow/constants" +import type { ReactNode } from "react" +import { useMemo } from "react" +import { useTranslation } from "react-i18next" + +import { ROUTE_FEED_IN_FOLDER } from "~/constants" +import type { AIChatContextBlock, FileAttachment } from "~/modules/ai-chat/store/types" + +import { CircularProgress } from "../ui/UploadProgress" +import { + getBlockIcon, + getBlockLabel, + getFileDisplayContent, + isImageAttachment, +} from "./ai-block-constants" +import { EntryTitle, FeedTitle } from "./BlockTitleComponents" +import { ImageThumbnail } from "./ImageThumbnail" + +export interface ContextBlockPresentation { + icon?: string | null + label?: string + displayContent: ReactNode + title?: string + attachment?: FileAttachment + isImageAttachment: boolean +} + +export function useContextBlockPresentation(block: AIChatContextBlock): ContextBlockPresentation { + const { t } = useTranslation("common") + + return useMemo(() => { + const label = block.type === "mainView" ? "" : getBlockLabel(block.type) + const isImage = isImageAttachment(block) + const icon = block.type === "fileAttachment" && isImage ? null : getBlockIcon(block) + const attachment = block.type === "fileAttachment" ? block.attachment : undefined + + const buildFileContent = (): ReactNode => { + if (!attachment) { + return [File: Unknown] + } + + const { dataUrl, previewUrl, uploadStatus, uploadProgress, errorMessage, name } = attachment + + if (isImage && (dataUrl || previewUrl)) { + return ( +
+
+ + + {uploadStatus === "uploading" && uploadProgress !== undefined && ( +
+ +
+ )} + + {uploadStatus === "error" && ( +
+ +
+ )} +
+ +
+ + {name} + + + {uploadStatus === "uploading" && uploadProgress !== undefined && ( + {Math.round(uploadProgress)}% + )} + + {uploadStatus === "error" && ( + + Upload failed + + )} +
+
+ ) + } + + return ( +
+ + {name} + + + {uploadStatus === "uploading" && uploadProgress !== undefined && ( +
+ + {Math.round(uploadProgress)}% +
+ )} + + {uploadStatus === "error" && ( + + )} +
+ ) + } + + let displayContent: ReactNode + let title: string | undefined + + switch (block.type) { + case "mainView": { + const viewName = getView(Number(block.value))?.name + const translated = viewName ? t(viewName) : block.value + displayContent = translated + title = typeof translated === "string" ? translated : undefined + break + } + case "mainEntry": { + displayContent = + break + } + case "mainFeed": { + const category = block.value?.startsWith(ROUTE_FEED_IN_FOLDER) + ? block.value.slice(ROUTE_FEED_IN_FOLDER.length) + : undefined + + displayContent = category ? ( + {category} + ) : ( + + ) + break + } + case "selectedText": { + displayContent = `"${block.value}"` + title = block.value + break + } + case "unreadOnly": { + displayContent = "Unread Only" + title = "Unread Only" + break + } + case "fileAttachment": { + displayContent = buildFileContent() + title = attachment + ? attachment.name || getFileDisplayContent(attachment) + : "[File: Unknown]" + break + } + default: { + displayContent = "" + break + } + } + + if (!title && typeof displayContent === "string") { + title = displayContent + } + + return { + icon, + label, + displayContent, + title, + attachment, + isImageAttachment: isImage, + } + }, [block, t]) +} diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts index 3e63ce318..8f1413ce2 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts @@ -19,11 +19,19 @@ interface BaseContextBlock { id: string } -export interface ValueContextBlock extends BaseContextBlock { - type: "mainView" | "mainEntry" | "mainFeed" | "selectedText" | "unreadOnly" +export type ValueContextBlockType = + | "mainView" + | "mainEntry" + | "mainFeed" + | "selectedText" + | "unreadOnly" +export interface AbstractValueContextBlock extends BaseContextBlock { + type: T value: string } +export type ValueContextBlock = AbstractValueContextBlock + export interface FileAttachmentContextBlock extends BaseContextBlock { type: "fileAttachment" attachment: FileAttachment @@ -36,6 +44,8 @@ export type AIChatContextBlockInput = | Omit | Omit +export type AIChatContextBlockType = AIChatContextBlock["type"] + export interface AIChatStoreInitial { blocks: AIChatContextBlock[] chatId?: string