From 4cc5a1cf5eae0191289fb10c80fcec26b62cd56c Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Tue, 30 Sep 2025 15:58:49 +0800 Subject: [PATCH] feat: add unread only context --- .../context-bar/blocks/ContextBlock.tsx | 48 ++++++++++++++++--- .../components/layouts/AIChatContextBar.tsx | 24 +++++++++- .../components/message/AIDataBlockItem.tsx | 43 +++++++++++++---- .../components/message/AIDataBlockPart.tsx | 5 +- .../components/message/ai-block-constants.ts | 7 +++ .../modules/ai-chat/hooks/useDisplayBlocks.ts | 27 ++++++++--- .../ai-chat/store/slices/block.slice.ts | 1 + .../src/modules/ai-chat/store/types.ts | 2 +- 8 files changed, 129 insertions(+), 28 deletions(-) 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 5bfd5401a..b0819f423 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 @@ -75,33 +75,58 @@ type ValueBlockOf = Omit type MainFeedBlock = ValueBlockOf<"mainFeed"> +type UnreadOnlyBlock = ValueBlockOf<"unreadOnly"> -export const MainViewFeedContextBlock: FC<{ +export const CombinedContextBlock: FC<{ viewBlock: MainViewBlock - feedBlock: MainFeedBlock -}> = memo(({ viewBlock, feedBlock }) => { + feedBlock?: MainFeedBlock + unreadOnlyBlock?: UnreadOnlyBlock +}> = memo(({ viewBlock, feedBlock, unreadOnlyBlock }) => { + const { t } = useTranslation("common") const blockActions = useChatBlockActions() const viewIcon = views.find((v) => v.view === Number(viewBlock.value))?.icon.props.className const canRemove = - !blockTypeCanNotBeRemoved.has(viewBlock.type) && !blockTypeCanNotBeRemoved.has(feedBlock.type) + !blockTypeCanNotBeRemoved.has(viewBlock.type) && + (!feedBlock || !blockTypeCanNotBeRemoved.has(feedBlock.type)) const handleRemove = () => { blockActions.removeBlock(viewBlock.id) - blockActions.removeBlock(feedBlock.id) + if (feedBlock) { + blockActions.removeBlock(feedBlock.id) + } + if (unreadOnlyBlock) { + blockActions.removeBlock(unreadOnlyBlock.id) + } } + // Determine what to display + const displayContent = feedBlock ? ( + + + {unreadOnlyBlock && } + + ) : ( + + {(() => { + const viewName = views.find((v) => v.view === Number(viewBlock.value))?.name + return viewName ? t(viewName) : viewBlock.value + })()} + {unreadOnlyBlock && } + + ) + return ( } + content={displayContent} /> ) }) -MainViewFeedContextBlock.displayName = "MainViewFeedContextBlock" +CombinedContextBlock.displayName = "CombinedContextBlock" export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) => { const { t } = useTranslation("common") @@ -125,6 +150,9 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) 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) @@ -159,6 +187,9 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) case "selectedText": { return `"${block.value}"` } + case "unreadOnly": { + return "Unread Only" + } case "fileAttachment": { const { type, name, dataUrl, previewUrl, uploadStatus, errorMessage, uploadProgress } = block.attachment @@ -255,6 +286,9 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) case "selectedText": { return "Text" } + case "unreadOnly": { + return "Filter" + } case "fileAttachment": { return "File" } 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 0a39e39e2..460cfc6cb 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 @@ -2,6 +2,7 @@ import { cn } from "@follow/utils/utils" import { memo, useCallback, useEffect, useMemo, useRef } from "react" import { useAISettingValue } from "~/atoms/settings/ai" +import { useGeneralSettingKey } from "~/atoms/settings/general" import { DropdownMenu, DropdownMenuTrigger } from "~/components/ui/dropdown-menu/dropdown-menu" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useDisplayBlocks } from "~/modules/ai-chat/hooks/useDisplayBlocks" @@ -11,7 +12,7 @@ import { SUPPORTED_MIME_ACCEPT } from "~/modules/ai-chat/utils/file-validation" import { useBlockActions } from "../../store/hooks" import { BlockSliceAction } from "../../store/slices/block.slice" -import { ContextBlock, MainViewFeedContextBlock } from "../context-bar/blocks" +import { CombinedContextBlock, ContextBlock } from "../context-bar/blocks" import { ContextMenuContent, ShortcutsMenuContent } from "../context-bar/menus" export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) => void }> = memo( @@ -71,6 +72,24 @@ export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) => } }, [addOrUpdateBlock, feedId, removeBlock]) + // Add unreadOnly context block only when unreadOnly is enabled + const unreadOnly = useGeneralSettingKey("unreadOnly") + useEffect(() => { + if (unreadOnly) { + addOrUpdateBlock({ + id: BlockSliceAction.SPECIAL_TYPES.unreadOnly, + type: "unreadOnly", + value: "true", + }) + } else { + removeBlock(BlockSliceAction.SPECIAL_TYPES.unreadOnly) + } + + return () => { + removeBlock(BlockSliceAction.SPECIAL_TYPES.unreadOnly) + } + }, [addOrUpdateBlock, unreadOnly, removeBlock]) + const displayBlocks = useDisplayBlocks(blocks) return ( @@ -128,10 +147,11 @@ export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) => {displayBlocks.map((item) => { if (item.kind === "combined") { 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 6ed7f9476..9283c6cb3 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 @@ -24,9 +24,10 @@ type ValueBlockOf = Omit - feedBlock: ValueBlockOf<"mainFeed"> + feedBlock?: ValueBlockOf<"mainFeed"> + unreadOnlyBlock?: ValueBlockOf<"unreadOnly"> } /** @@ -48,6 +49,9 @@ const getDisplayContent = (block: AIChatContextBlock): React.ReactNode => { case "selectedText": { return `"${block.value}"` } + case "unreadOnly": { + return "Unread Only" + } case "fileAttachment": { if (!block.attachment) { return "[File: Unknown]" @@ -153,21 +157,40 @@ export const AIDataBlockItem: React.FC = React.memo(({ blo AIDataBlockItem.displayName = "AIDataBlockItem" /** - * Combined block item component for main view and main feed - * Displays view icon with feed title content + * Combined block item component for main view with optional feed and unread filter + * Displays view icon with appropriate content based on available blocks */ -export const MainViewFeedDataBlockItem: React.FC = React.memo( - ({ viewBlock, feedBlock }) => { +export const CombinedDataBlockItem: React.FC = React.memo( + ({ viewBlock, feedBlock, unreadOnlyBlock }) => { 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 + const unreadOnlyText = unreadOnlyBlock ? " (Unread Only)" : "" + + // Determine content based on whether feedBlock exists + const content = feedBlock ? ( + + + {unreadOnlyBlock && } + + ) : ( + + {viewName} + {unreadOnlyBlock && } + + ) + + const titleText = feedBlock + ? `${viewName} - ${feedBlock.value}${unreadOnlyText}` + : `${viewName}${unreadOnlyText}` + return { styles: getBlockStyles("mainView"), - displayContent: , - title: `${viewName} - ${feedBlock.value}`, + displayContent: content, + title: titleText, } - }, [viewBlock.value, feedBlock.value]) + }, [viewBlock.value, feedBlock, unreadOnlyBlock]) return ( }, ) -MainViewFeedDataBlockItem.displayName = "MainViewFeedDataBlockItem" +CombinedDataBlockItem.displayName = "CombinedDataBlockItem" 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 1e1db7522..79541fbc7 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 @@ -4,7 +4,7 @@ import * as React from "react" import { useDisplayBlocks } from "~/modules/ai-chat/hooks/useDisplayBlocks" import type { AIChatContextBlock } from "~/modules/ai-chat/store/types" -import { AIDataBlockItem, MainViewFeedDataBlockItem } from "./AIDataBlockItem" +import { AIDataBlockItem, CombinedDataBlockItem } from "./AIDataBlockItem" interface AIDataBlockPartProps { blocks: AIChatContextBlock[] @@ -40,10 +40,11 @@ export const AIDataBlockPart: React.FC = React.memo(({ blo {displayBlocks.map((item, index) => { if (item.kind === "combined") { return ( - ) } diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/ai-block-constants.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/ai-block-constants.ts index 061c542e5..04a7adecc 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/ai-block-constants.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/ai-block-constants.ts @@ -35,6 +35,11 @@ export const BLOCK_STYLES = { icon: "bg-pink/10 text-pink", label: "text-pink", }, + unreadOnly: { + container: "from-green/5 to-green/10 border-green/20 hover:border-green/30", + icon: "bg-green/10 text-green", + label: "text-green", + }, } as const /** @@ -55,6 +60,7 @@ export const BLOCK_ICONS = { referEntry: "i-mgc-paper-cute-fi", selectedText: "i-mgc-quill-pen-cute-re", fileAttachment: "i-mgc-file-upload-cute-re", + unreadOnly: "i-mgc-round-cute-fi", } as const /** @@ -67,6 +73,7 @@ export const BLOCK_LABELS = { selectedText: "Text", fileAttachment: "File", mainView: "View", + unreadOnly: "Filter", } as const /** 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 index fe8fb20a8..e8a72da98 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useDisplayBlocks.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useDisplayBlocks.ts @@ -10,7 +10,8 @@ export type DisplayBlockItem = | { kind: "combined" viewBlock: ValueBlockOf<"mainView"> - feedBlock: ValueBlockOf<"mainFeed"> + feedBlock?: ValueBlockOf<"mainFeed"> + unreadOnlyBlock?: ValueBlockOf<"unreadOnly"> } | { kind: "single"; block: AIChatContextBlock } @@ -31,14 +32,27 @@ export const useDisplayBlocks = (blocks: AIChatContextBlock[]): DisplayBlockItem const mainFeedBlock = blocks.find( (block): block is ValueBlockOf<"mainFeed"> => block.type === "mainFeed", ) + const unreadOnlyBlock = blocks.find( + (block): block is ValueBlockOf<"unreadOnly"> => block.type === "unreadOnly", + ) - if (mainViewBlock && mainFeedBlock) { + // Always filter out unreadOnly from single blocks since it should only appear in combined blocks + const filteredBlocks = blocks.filter((block) => block.type !== "unreadOnly") + + if (mainViewBlock) { const items: DisplayBlockItem[] = [] - items.push({ kind: "combined", viewBlock: mainViewBlock, feedBlock: mainFeedBlock }) + // Create combined block with optional feedBlock + items.push({ + kind: "combined", + viewBlock: mainViewBlock, + ...(mainFeedBlock && { feedBlock: mainFeedBlock }), + ...(unreadOnlyBlock && { unreadOnlyBlock }), + }) - const otherBlocks = blocks.filter( - (block) => block.id !== mainViewBlock.id && block.id !== mainFeedBlock.id, + // Add other blocks (excluding mainView, mainFeed, and unreadOnly) + const otherBlocks = filteredBlocks.filter( + (block) => block.id !== mainViewBlock.id && block.id !== mainFeedBlock?.id, ) otherBlocks.forEach((block) => { items.push({ kind: "single", block }) @@ -47,6 +61,7 @@ export const useDisplayBlocks = (blocks: AIChatContextBlock[]): DisplayBlockItem return items } - return blocks.map((block) => ({ kind: "single" as const, block })) + // If no mainView, show all blocks except unreadOnly + return filteredBlocks.map((block) => ({ kind: "single" as const, block })) }, [blocks]) } diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts index 1d2220853..a5d38bf52 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts @@ -34,6 +34,7 @@ export class BlockSliceAction { mainEntry: "mainEntry", mainFeed: "mainFeed", selectedText: "selectedText", + unreadOnly: "unreadOnly", } get set() { return this.params[0] 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 b381d5f1f..8e31b57ee 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,7 +19,7 @@ interface BaseContextBlock { } export interface ValueContextBlock extends BaseContextBlock { - type: "mainView" | "mainEntry" | "mainFeed" | "referEntry" | "selectedText" + type: "mainView" | "mainEntry" | "mainFeed" | "referEntry" | "selectedText" | "unreadOnly" value: string }