diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayAnalyticsPart.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayAnalyticsPart.tsx index 5d2cd962c..cdf6df72f 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayAnalyticsPart.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayAnalyticsPart.tsx @@ -14,11 +14,11 @@ import { TableRow, } from "@follow/components/ui/table/index.js" import dayjs from "dayjs" +import { memo } from "react" import type { AIDisplayAnalyticsTool } from "../../store/types" -import { ErrorState, LoadingState } from "../shared/common-states" -import { toolMemo } from "./share" -import { ChartPlaceholder, StatCard } from "./shared" +import { withDisplayStateHandler } from "./share" +import { StatCard } from "./shared" type AnalyticsData = AIDisplayAnalyticsTool["output"]["analyticsData"] @@ -132,8 +132,6 @@ const ReadingAnalytics = ({ data }: { data: AnalyticsData["readingStats"] }) => - - Recent Reading Activity @@ -230,72 +228,56 @@ const OverviewAnalytics = ({ data }: { data: AnalyticsData["overviewStats"] }) = ) } -export const AIDisplayAnalyticsPart = toolMemo(({ part }: { part: AIDisplayAnalyticsTool }) => { - // Handle error state - if (part.state === "output-error") { - return ( - }) => { + const { analyticsData, analyticsType, timeRange, displayType, title } = output + + const renderAnalytics = () => { + switch (analyticsType) { + case "feed": { + return + } + case "subscription": { + return + } + case "reading": { + return + } + case "trending": { + return + } + case "overview": { + return + } + default: { + return
No analytics data available
} - maxWidth="max-w-4xl" - /> - ) - } - - // Handle no output or invalid state - if (part.state !== "output-available" || !part.output) { - return ( - - ) - } - - const { analyticsData, analyticsType, timeRange, displayType, title } = part.output - - const renderAnalytics = () => { - switch (analyticsType) { - case "feed": { - return - } - case "subscription": { - return - } - case "reading": { - return - } - case "trending": { - return - } - case "overview": { - return - } - default: { - return
No analytics data available
} } - } - return ( - - - - 📊 - - {title || `${analyticsType.charAt(0).toUpperCase() + analyticsType.slice(1)} Analytics`} - - - - {formatTimeRange(timeRange)} • Display type: {displayType} - - - {renderAnalytics()} - - ) -}) + return ( + + + + 📊 + + {title || + `${analyticsType.charAt(0).toUpperCase() + analyticsType.slice(1)} Analytics`} + + + + {formatTimeRange(timeRange)} • Display type: {displayType} + + + {renderAnalytics()} + + ) + }, +) + +export const AIDisplayAnalyticsPart = withDisplayStateHandler({ + title: "Analytics", + loadingDescription: "Fetching analytics data...", + errorTitle: "Analytics Error", + maxWidth: "max-w-4xl", +})(AIDisplayAnalyticsPartBase) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayEntriesPart.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayEntriesPart.tsx index 1b60ff92b..4b258967f 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayEntriesPart.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayEntriesPart.tsx @@ -13,7 +13,7 @@ import { usePeekModal } from "~/hooks/biz/usePeekModal" import { FeedIcon } from "~/modules/feed/feed-icon" import type { AIDisplayEntriesTool } from "../../store/types" -import { ErrorState, LoadingState } from "../shared/common-states" +import { DisplayCardWrapper, withDisplayStateHandler } from "./share" import { CategoryTag, EmptyState, StatCard } from "./shared" type EntryData = AIDisplayEntriesTool["output"]["entries"] @@ -235,18 +235,10 @@ const GroupedEntries = ({ ) const renderGroup = (groupData: EntryData) => { - switch (displayType) { - case "timeline": { - return ( - - ) - } - default: { - return ( - - ) - } + if (displayType === "timeline") { + return } + return } return ( @@ -270,83 +262,48 @@ const GroupedEntries = ({ ) } -export const AIDisplayEntriesPart = memo(({ part }: { part: AIDisplayEntriesTool }) => { - // Handle error state - if (part.state === "output-error") { - return ( - - ) - } +const AIDisplayEntriesPartBase = memo( + ({ output }: { output: NonNullable }) => { + const { + entries, + displayType = "grid", + showSummary = true, + showMetadata = true, + title, + groupBy = "none", + } = output - // Handle no output or invalid state - if (part.state !== "output-available" || !part.output) { - return ( - - ) - } + // Calculate statistics + const totalEntries = entries.length + const feedsCount = new Set(entries.map((e) => e.entry.feedId)).size + const authorsCount = new Set(entries.map((e) => e.entry.author).filter(Boolean)).size + const categoriesCount = new Set(entries.flatMap((e) => e.entry.categories || [])).size - // Extract output with proper typing - const output = part.output as NonNullable + const renderEntries = () => { + if (groupBy !== "none") { + return ( + + ) + } - const { - entries, - displayType = "grid", - showSummary = true, - showMetadata = true, - title, - groupBy = "none", - } = output - - // Calculate statistics - const totalEntries = entries.length - const feedsCount = new Set(entries.map((e) => e.entry.feedId)).size - const authorsCount = new Set(entries.map((e) => e.entry.author).filter(Boolean)).size - const categoriesCount = new Set(entries.flatMap((e) => e.entry.categories || [])).size - - const renderEntries = () => { - if (groupBy !== "none") { - return ( - - ) - } - - switch (displayType) { - case "timeline": { + if (displayType === "timeline") { return } - default: { - return - } + return } - } - return ( - -
- - - 📰 - {title || "Entries"} - - - {formatDisplayType(displayType)} • {formatGroupBy(groupBy)} • {totalEntries} entries - - - + return ( + {/* Statistics Overview */}
@@ -357,7 +314,13 @@ export const AIDisplayEntriesPart = memo(({ part }: { part: AIDisplayEntriesTool {/* Entries Display */} {renderEntries()} - - - ) -}) + + ) + }, +) + +export const AIDisplayEntriesPart = withDisplayStateHandler({ + title: "Entries", + loadingDescription: "Fetching entry data...", + errorTitle: "Entries Error", +})(AIDisplayEntriesPartBase) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayFeedsPart.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayFeedsPart.tsx index 4b5cfef33..e73453f89 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayFeedsPart.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplayFeedsPart.tsx @@ -11,7 +11,7 @@ import { memo } from "react" import { FeedIcon } from "~/modules/feed/feed-icon" import type { AIDisplayFeedsTool } from "../../store/types" -import { ErrorState, LoadingState } from "../shared/common-states" +import { DisplayCardWrapper, withDisplayStateHandler } from "./share" import { AnalyticsMetrics, EmptyState, StatCard } from "./shared" type FeedData = AIDisplayFeedsTool["output"]["feeds"] @@ -77,63 +77,26 @@ const FeedsGrid = ({ data, showAnalytics }: { data: FeedData; showAnalytics: boo ) } -export const AIDisplayFeedsPart = memo(({ part }: { part: AIDisplayFeedsTool }) => { - // Handle error state - if (part.state === "output-error") { - return ( - +const AIDisplayFeedsPartBase = memo( + ({ output }: { output: NonNullable }) => { + const { feeds, showAnalytics = true, title } = output + + // Calculate statistics + const totalFeeds = feeds.length + const activeFeeds = feeds.filter((f) => !f.feed.errorMessage).length + const errorFeeds = feeds.filter((f) => f.feed.errorMessage).length + const totalSubscriptions = feeds.reduce( + (acc, f) => acc + (f.analytics?.subscriptionCount || 0), + 0, ) - } + const totalViews = feeds.reduce((acc, f) => acc + (f.analytics?.view || 0), 0) - // Handle no output or invalid state - if (part.state !== "output-available" || !part.output) { return ( - - ) - } - - // Extract output with proper typing - const output = part.output as NonNullable - - const { feeds, displayType = "list", showAnalytics = true, title } = output - - // Calculate statistics - const totalFeeds = feeds.length - const activeFeeds = feeds.filter((f) => !f.feed.errorMessage).length - const errorFeeds = feeds.filter((f) => f.feed.errorMessage).length - const totalSubscriptions = feeds.reduce( - (acc, f) => acc + (f.analytics?.subscriptionCount || 0), - 0, - ) - const totalViews = feeds.reduce((acc, f) => acc + (f.analytics?.view || 0), 0) - - const renderFeeds = () => { - switch (displayType) { - default: { - return - } - } - } - - return ( - -
- - - 📡 - {title || "RSS Feeds"} - - {totalFeeds} feeds - - + {/* Statistics Overview */}
@@ -155,9 +118,14 @@ export const AIDisplayFeedsPart = memo(({ part }: { part: AIDisplayFeedsTool }) )}
- {/* Feeds Display */} - {renderFeeds()} -
- - ) -}) + + + ) + }, +) + +export const AIDisplayFeedsPart = withDisplayStateHandler({ + title: "Feeds", + loadingDescription: "Fetching feed data...", + errorTitle: "Feeds Error", +})(AIDisplayFeedsPartBase) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplaySubscriptionsPart.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplaySubscriptionsPart.tsx index 5f5101cad..12de893ae 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplaySubscriptionsPart.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/AIDisplaySubscriptionsPart.tsx @@ -6,12 +6,12 @@ import { CardTitle, } from "@follow/components/ui/card/index.js" import dayjs from "dayjs" +import { memo } from "react" import { FeedIcon } from "~/modules/feed/feed-icon" import type { AIDisplaySubscriptionsTool } from "../../store/types" -import { ErrorState, LoadingState } from "../shared/common-states" -import { toolMemo } from "./share" +import { DisplayCardWrapper, withDisplayStateHandler } from "./share" import { AnalyticsMetrics, CategoryTag, EmptyState, StatCard } from "./shared" type SubscriptionData = AIDisplaySubscriptionsTool["output"]["subscriptions"] @@ -93,10 +93,14 @@ const SubscriptionsGrid = ({
)}
- Subscribed:{" "} - {sub.subscription?.createdAt - ? dayjs(sub.subscription.createdAt).format("MMM DD, YYYY") - : "Unknown"} + + Subscribed:{" "} + + {sub.subscription?.createdAt + ? dayjs(sub.subscription.createdAt).format("MMM DD, YYYY") + : "Unknown"} + +
{showAnalytics && ( { @@ -149,59 +151,24 @@ const GroupedSubscriptions = ({ {} as Record, ) - const renderGroup = (groupData: SubscriptionData) => { - switch (displayType) { - default: { - return ( - - ) - } - } - } - return (
{Object.entries(groups).map(([groupName, groupData]) => (

{groupName}

- {renderGroup(groupData)} +
))}
) } -export const AIDisplaySubscriptionsPart = toolMemo( - ({ part }: { part: AIDisplaySubscriptionsTool }) => { - // Handle error state - if (part.state === "output-error") { - return ( - - ) - } - - // Handle loading state - if (part.state !== "output-available" || !part.output) { - return ( - - ) - } - - // Extract output with proper typing - const output = part.output as NonNullable - +const AIDisplaySubscriptionsPartBase = memo( + ({ output }: { output: NonNullable }) => { const { subscriptions, displayType = "list", @@ -226,56 +193,51 @@ export const AIDisplaySubscriptionsPart = toolMemo( ) } - switch (displayType) { - default: { - return ( - - ) - } - } + return ( + + ) } return ( - -
- - - 📋 - {title || "My Subscriptions"} - - - {formatDisplayType(displayType)} • {formatFilterBy(filterBy)} • {formatGroupBy(groupBy)} - - - - {/* Statistics Overview */} -
- - - {showCategories && } - -
+ + {/* Statistics Overview */} +
+ + + {showCategories && } + +
- {/* Subscriptions Display */} - {renderSubscriptions()} -
- + {/* Subscriptions Display */} + {renderSubscriptions()} + ) }, ) + +export const AIDisplaySubscriptionsPart = withDisplayStateHandler< + AIDisplaySubscriptionsTool["output"] +>({ + title: "Subscriptions", + loadingDescription: "Fetching subscription data...", + errorTitle: "Subscriptions Error", +})(AIDisplaySubscriptionsPartBase) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/share.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/share.ts deleted file mode 100644 index 956a7deed..000000000 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/share.ts +++ /dev/null @@ -1,15 +0,0 @@ -import type { ComponentType } from "react" -import { memo } from "react" -import isEqual from "react-fast-compare" - -interface PartWithState { - part: { - state: string - } -} - -export const toolMemo =

(FC: ComponentType

): ComponentType

=> - memo(FC, (prev, next) => { - if (prev.part.state === "output-available") return true - return isEqual(prev, next) - }) as ComponentType

diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/share.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/share.tsx new file mode 100644 index 000000000..79c9fb0b2 --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/share.tsx @@ -0,0 +1,96 @@ +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@follow/components/ui/card/index.js" +import type { ComponentType, ReactNode } from "react" +import { memo } from "react" +import isEqual from "react-fast-compare" + +import { ErrorState, LoadingState } from "../shared/common-states" + +interface PartWithState { + part: { + state: string + } +} + +export const toolMemo =

(FC: ComponentType

): ComponentType

=> + memo(FC, (prev, next) => { + if (prev.part.state === "output-available") return true + return isEqual(prev, next) + }) as ComponentType

+ +// Higher-Order Component for display state handling +export function withDisplayStateHandler(config: { + title: string + loadingDescription: string + errorTitle: string + maxWidth?: string +}) { + return function

( + WrappedComponent: ComponentType

}>, + ): ComponentType

{ + const WithDisplayStateHandler = (props: P) => { + const { part } = props + + // Handle error state + if (part.state === "output-error") { + return ( + + ) + } + + // Handle loading/invalid state + if (part.state !== "output-available" || !part.output) { + return ( + + ) + } + + // Render the wrapped component with the validated output + return } /> + } + + WithDisplayStateHandler.displayName = `withDisplayStateHandler(${WrappedComponent.displayName || WrappedComponent.name})` + + return WithDisplayStateHandler + } +} + +// Common card wrapper for display components +export const DisplayCardWrapper = ({ + title, + emoji, + description, + children, + maxWidth = "max-w-6xl", +}: { + title: string + emoji: string + description?: string + children: ReactNode + maxWidth?: string +}) => ( + +

+ + + {emoji} + {title} + + {description && {description}} + + {children} + +) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/ChartPlaceholder.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/ChartPlaceholder.tsx deleted file mode 100644 index 6c51e5b58..000000000 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/ChartPlaceholder.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import { Card, CardContent, CardHeader, CardTitle } from "@follow/components/ui/card/index.js" -import { cn } from "@follow/utils/utils" - -export interface ChartPlaceholderProps { - title: string - data: any[] - height?: string - className?: string -} - -export const ChartPlaceholder = ({ - title, - data, - height = "h-32", - className, -}: ChartPlaceholderProps) => ( - - - {title} - - -
- Chart visualization ({data.length} data points) -
-
-
-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/index.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/index.ts index 6279e0af9..4b9f64cdf 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/index.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/displays/shared/index.ts @@ -1,6 +1,5 @@ export * from "./AnalyticsMetrics" export * from "./CategoryTag" -export * from "./ChartPlaceholder" export * from "./DisplayHeader" export * from "./EmptyState" export * from "./FeedItemCard" 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 c91d70158..0f15a9e66 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 @@ -24,6 +24,7 @@ import { import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useAIChatStore } from "~/modules/ai-chat/store/AIChatContext" import type { AIChatContextBlock } from "~/modules/ai-chat/store/types" +import { useSettingModal } from "~/modules/settings/modal/use-setting-modal-hack" import { useChatBlockActions } from "../../store/hooks" @@ -39,6 +40,7 @@ export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) => [shortcuts], ) + const showSettingModal = useSettingModal() const contextMenuContent = ( @@ -100,21 +102,21 @@ export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) =>
No shortcuts configured
) : ( enabledShortcuts.map((shortcut) => ( - onSendShortcut?.(shortcut.prompt)} - className="text-xs" - shortcut={shortcut.hotkey} - > -
-
- - {shortcut.name} -
-
+ onSendShortcut?.(shortcut.prompt)}> + + {shortcut.name} )) )} + + { + showSettingModal("ai") + }} + > + + Manage Shortcut +
) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx index 84461c135..cd782d48a 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx @@ -11,9 +11,8 @@ import { memo, useCallback, useRef, useState } from "react" import { AIChatContextBar } from "~/modules/ai-chat/components/layouts/AIChatContextBar" import { MentionPlugin } from "../../editor" -import { useChatActions, useChatError, useChatStatus } from "../../store/hooks" +import { useChatActions, useChatStatus } from "../../store/hooks" import { AIChatSendButton } from "./AIChatSendButton" -import { CollapsibleError } from "./CollapsibleError" const chatInputVariants = cva( [ @@ -40,7 +39,7 @@ interface ChatInputProps extends VariantProps { export const ChatInput = memo(({ onSend, variant }: ChatInputProps) => { const status = useChatStatus() const chatActions = useChatActions() - const error = useChatError() + const stop = useCallback(() => { chatActions.stop() }, [chatActions]) @@ -85,44 +84,38 @@ export const ChatInput = memo(({ onSend, variant }: ChatInputProps) => { }, []) return ( -
- {/* Error Display */} - {error && } - - {/* Integrated Input Container with Context Bar */} -
- {/* Input Area */} -
- - - -
- -
-
- - {/* Context Bar - Always shown, positioned below the input area */} -
- onSend(prompt, null)} +
+ {/* Input Area */} +
+ + + +
+
+ + {/* Context Bar - Always shown, positioned below the input area */} +
+ onSend(prompt, null)} + /> +
) }) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx index 485c8baa6..6dff7875e 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx @@ -26,6 +26,7 @@ import { import { convertLexicalToMarkdown } from "../../utils/lexical-markdown" import { ChatInput } from "./ChatInput" +import { CollapsibleError } from "./CollapsibleError" import { WelcomeScreen } from "./WelcomeScreen" const SCROLL_BOTTOM_THRESHOLD = 50 @@ -187,6 +188,7 @@ export const ChatInterface = () => { {hasMessages && (
+ {error && }
)} diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMessageParts.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMessageParts.tsx index 64e89ea95..3de34fb19 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMessageParts.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMessageParts.tsx @@ -55,7 +55,7 @@ export const AIMessageParts: React.FC = React.memo(({ message return ( <> {message.parts.map((part, index) => { - const partKey = `${message.id}-part-${index}` + const partKey = `${message.id}-${index}` switch (part.type) { case "text": { @@ -100,6 +100,9 @@ export const AIMessageParts: React.FC = React.memo(({ message default: { if (part.type.startsWith("tool-")) { + if (part.type.startsWith("tool-chunkBreak")) { + return null + } return } return null