refactor(ai-chat): streamline display components and enhance state handling
- Refactored AIDisplay components to utilize a new higher-order component for display state management, improving code clarity and reducing redundancy. - Introduced DisplayCardWrapper for consistent card layout across display components. - Removed unused ChartPlaceholder component and consolidated state handling logic. - Updated AIDisplay components to enhance user experience by ensuring proper loading and error states. These changes improve maintainability and user experience in the AI chat interface. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
+52
-70
@@ -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"] }) =>
|
||||
<StatCard title="Active Days" value={data.length} emoji="🗓️" />
|
||||
</div>
|
||||
|
||||
<ChartPlaceholder title="Reading Activity Over Time" data={data} />
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Recent Reading Activity</CardTitle>
|
||||
@@ -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 (
|
||||
<ErrorState
|
||||
title="Analytics Error"
|
||||
error={
|
||||
typeof part.output === "string"
|
||||
? part.output
|
||||
: "An error occurred while loading analytics"
|
||||
const AIDisplayAnalyticsPartBase = memo(
|
||||
({ output }: { output: NonNullable<AIDisplayAnalyticsTool["output"]> }) => {
|
||||
const { analyticsData, analyticsType, timeRange, displayType, title } = output
|
||||
|
||||
const renderAnalytics = () => {
|
||||
switch (analyticsType) {
|
||||
case "feed": {
|
||||
return <FeedAnalytics data={analyticsData.feedData} />
|
||||
}
|
||||
case "subscription": {
|
||||
return <SubscriptionAnalytics data={analyticsData.subscriptionStats} />
|
||||
}
|
||||
case "reading": {
|
||||
return <ReadingAnalytics data={analyticsData.readingStats} />
|
||||
}
|
||||
case "trending": {
|
||||
return <TrendingAnalytics data={analyticsData.trendingFeeds} />
|
||||
}
|
||||
case "overview": {
|
||||
return <OverviewAnalytics data={analyticsData.overviewStats} />
|
||||
}
|
||||
default: {
|
||||
return <div className="text-text-secondary">No analytics data available</div>
|
||||
}
|
||||
maxWidth="max-w-4xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Handle no output or invalid state
|
||||
if (part.state !== "output-available" || !part.output) {
|
||||
return (
|
||||
<LoadingState
|
||||
title="Loading Analytics..."
|
||||
description="Fetching analytics data..."
|
||||
maxWidth="max-w-4xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const { analyticsData, analyticsType, timeRange, displayType, title } = part.output
|
||||
|
||||
const renderAnalytics = () => {
|
||||
switch (analyticsType) {
|
||||
case "feed": {
|
||||
return <FeedAnalytics data={analyticsData.feedData} />
|
||||
}
|
||||
case "subscription": {
|
||||
return <SubscriptionAnalytics data={analyticsData.subscriptionStats} />
|
||||
}
|
||||
case "reading": {
|
||||
return <ReadingAnalytics data={analyticsData.readingStats} />
|
||||
}
|
||||
case "trending": {
|
||||
return <TrendingAnalytics data={analyticsData.trendingFeeds} />
|
||||
}
|
||||
case "overview": {
|
||||
return <OverviewAnalytics data={analyticsData.overviewStats} />
|
||||
}
|
||||
default: {
|
||||
return <div className="text-text-secondary">No analytics data available</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mx-auto mb-2 w-full max-w-4xl">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-text flex items-center gap-2 text-xl font-semibold">
|
||||
<span className="text-lg">📊</span>
|
||||
<span>
|
||||
{title || `${analyticsType.charAt(0).toUpperCase() + analyticsType.slice(1)} Analytics`}
|
||||
</span>
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{formatTimeRange(timeRange)} • Display type: {displayType}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="@container">{renderAnalytics()}</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})
|
||||
return (
|
||||
<Card className="mx-auto mb-2 w-full max-w-4xl">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-text flex items-center gap-2 text-xl font-semibold">
|
||||
<span className="text-lg">📊</span>
|
||||
<span>
|
||||
{title ||
|
||||
`${analyticsType.charAt(0).toUpperCase() + analyticsType.slice(1)} Analytics`}
|
||||
</span>
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{formatTimeRange(timeRange)} • Display type: {displayType}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="@container">{renderAnalytics()}</CardContent>
|
||||
</Card>
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
export const AIDisplayAnalyticsPart = withDisplayStateHandler<AIDisplayAnalyticsTool["output"]>({
|
||||
title: "Analytics",
|
||||
loadingDescription: "Fetching analytics data...",
|
||||
errorTitle: "Analytics Error",
|
||||
maxWidth: "max-w-4xl",
|
||||
})(AIDisplayAnalyticsPartBase)
|
||||
|
||||
+49
-86
@@ -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 (
|
||||
<TimelineView data={groupData} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
)
|
||||
}
|
||||
default: {
|
||||
return (
|
||||
<EntriesGrid data={groupData} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
)
|
||||
}
|
||||
if (displayType === "timeline") {
|
||||
return <TimelineView data={groupData} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
}
|
||||
return <EntriesGrid data={groupData} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -270,83 +262,48 @@ const GroupedEntries = ({
|
||||
)
|
||||
}
|
||||
|
||||
export const AIDisplayEntriesPart = memo(({ part }: { part: AIDisplayEntriesTool }) => {
|
||||
// Handle error state
|
||||
if (part.state === "output-error") {
|
||||
return (
|
||||
<ErrorState
|
||||
title="Entries Error"
|
||||
error="An error occurred while loading entries"
|
||||
maxWidth="max-w-6xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
const AIDisplayEntriesPartBase = memo(
|
||||
({ output }: { output: NonNullable<AIDisplayEntriesTool["output"]> }) => {
|
||||
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 (
|
||||
<LoadingState
|
||||
title="Loading Entries..."
|
||||
description="Fetching entry data..."
|
||||
maxWidth="max-w-6xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
// 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<AIDisplayEntriesTool["output"]>
|
||||
const renderEntries = () => {
|
||||
if (groupBy !== "none") {
|
||||
return (
|
||||
<GroupedEntries
|
||||
data={entries}
|
||||
groupBy={groupBy}
|
||||
displayType={displayType}
|
||||
showSummary={showSummary}
|
||||
showMetadata={showMetadata}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
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 (
|
||||
<GroupedEntries
|
||||
data={entries}
|
||||
groupBy={groupBy}
|
||||
displayType={displayType}
|
||||
showSummary={showSummary}
|
||||
showMetadata={showMetadata}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
switch (displayType) {
|
||||
case "timeline": {
|
||||
if (displayType === "timeline") {
|
||||
return <TimelineView data={entries} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
}
|
||||
default: {
|
||||
return <EntriesGrid data={entries} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
}
|
||||
return <EntriesGrid data={entries} showSummary={showSummary} showMetadata={showMetadata} />
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mb-2 w-full min-w-0">
|
||||
<div className="w-[9999px] max-w-[calc(var(--ai-chat-layout-width,65ch)_-120px)]" />
|
||||
<CardHeader>
|
||||
<CardTitle className="text-text flex items-center gap-2 text-xl font-semibold">
|
||||
<span className="text-lg">📰</span>
|
||||
<span>{title || "Entries"}</span>
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{formatDisplayType(displayType)} • {formatGroupBy(groupBy)} • {totalEntries} entries
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="@container space-y-6">
|
||||
return (
|
||||
<DisplayCardWrapper
|
||||
title={title || "Entries"}
|
||||
emoji="📰"
|
||||
description={`${formatDisplayType(displayType)} • ${formatGroupBy(groupBy)} • ${totalEntries} entries`}
|
||||
>
|
||||
{/* Statistics Overview */}
|
||||
<div className="@[600px]:grid-cols-4 @[400px]:grid-cols-2 grid grid-cols-1 gap-4">
|
||||
<StatCard title="Total Entries" value={totalEntries} emoji="📄" />
|
||||
@@ -357,7 +314,13 @@ export const AIDisplayEntriesPart = memo(({ part }: { part: AIDisplayEntriesTool
|
||||
|
||||
{/* Entries Display */}
|
||||
{renderEntries()}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})
|
||||
</DisplayCardWrapper>
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
export const AIDisplayEntriesPart = withDisplayStateHandler<AIDisplayEntriesTool["output"]>({
|
||||
title: "Entries",
|
||||
loadingDescription: "Fetching entry data...",
|
||||
errorTitle: "Entries Error",
|
||||
})(AIDisplayEntriesPartBase)
|
||||
|
||||
+29
-61
@@ -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 (
|
||||
<ErrorState
|
||||
title="Feeds Error"
|
||||
error="An error occurred while loading feeds"
|
||||
maxWidth="max-w-6xl"
|
||||
/>
|
||||
const AIDisplayFeedsPartBase = memo(
|
||||
({ output }: { output: NonNullable<AIDisplayFeedsTool["output"]> }) => {
|
||||
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 (
|
||||
<LoadingState
|
||||
title="Loading Feeds..."
|
||||
description="Fetching feed data..."
|
||||
maxWidth="max-w-6xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Extract output with proper typing
|
||||
const output = part.output as NonNullable<AIDisplayFeedsTool["output"]>
|
||||
|
||||
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 <FeedsGrid data={feeds} showAnalytics={showAnalytics} />
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mb-2 w-full min-w-0">
|
||||
<div className="w-[9999px] max-w-[calc(var(--ai-chat-layout-width,65ch)_-120px)]" />
|
||||
<CardHeader>
|
||||
<CardTitle className="text-text flex items-center gap-2 text-xl font-semibold">
|
||||
<span className="text-lg">📡</span>
|
||||
<span>{title || "RSS Feeds"}</span>
|
||||
</CardTitle>
|
||||
<CardDescription>{totalFeeds} feeds</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="@container space-y-6">
|
||||
<DisplayCardWrapper
|
||||
title={title || "RSS Feeds"}
|
||||
emoji="📡"
|
||||
description={`${totalFeeds} feeds`}
|
||||
>
|
||||
{/* Statistics Overview */}
|
||||
<div className="@[600px]:grid-cols-4 grid grid-cols-2 gap-4 md:grid-cols-4">
|
||||
<StatCard title="Total Feeds" value={totalFeeds} emoji="📊" />
|
||||
@@ -155,9 +118,14 @@ export const AIDisplayFeedsPart = memo(({ part }: { part: AIDisplayFeedsTool })
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Feeds Display */}
|
||||
{renderFeeds()}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})
|
||||
<FeedsGrid data={feeds} showAnalytics={showAnalytics} />
|
||||
</DisplayCardWrapper>
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
export const AIDisplayFeedsPart = withDisplayStateHandler<AIDisplayFeedsTool["output"]>({
|
||||
title: "Feeds",
|
||||
loadingDescription: "Fetching feed data...",
|
||||
errorTitle: "Feeds Error",
|
||||
})(AIDisplayFeedsPartBase)
|
||||
|
||||
+52
-90
@@ -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 = ({
|
||||
</div>
|
||||
)}
|
||||
<div className="text-text-secondary text-xs">
|
||||
Subscribed:{" "}
|
||||
{sub.subscription?.createdAt
|
||||
? dayjs(sub.subscription.createdAt).format("MMM DD, YYYY")
|
||||
: "Unknown"}
|
||||
<span>
|
||||
Subscribed:{" "}
|
||||
<span>
|
||||
{sub.subscription?.createdAt
|
||||
? dayjs(sub.subscription.createdAt).format("MMM DD, YYYY")
|
||||
: "Unknown"}
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
{showAnalytics && (
|
||||
<AnalyticsMetrics
|
||||
@@ -116,13 +120,11 @@ const SubscriptionsGrid = ({
|
||||
const GroupedSubscriptions = ({
|
||||
data,
|
||||
groupBy,
|
||||
displayType,
|
||||
showAnalytics,
|
||||
showCategories,
|
||||
}: {
|
||||
data: SubscriptionData
|
||||
groupBy: string
|
||||
displayType: string
|
||||
showAnalytics: boolean
|
||||
showCategories: boolean
|
||||
}) => {
|
||||
@@ -149,59 +151,24 @@ const GroupedSubscriptions = ({
|
||||
{} as Record<string, SubscriptionData>,
|
||||
)
|
||||
|
||||
const renderGroup = (groupData: SubscriptionData) => {
|
||||
switch (displayType) {
|
||||
default: {
|
||||
return (
|
||||
<SubscriptionsGrid
|
||||
data={groupData}
|
||||
showAnalytics={showAnalytics}
|
||||
showCategories={showCategories}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{Object.entries(groups).map(([groupName, groupData]) => (
|
||||
<div key={groupName}>
|
||||
<h3 className="text-text mb-4 text-lg font-semibold">{groupName}</h3>
|
||||
{renderGroup(groupData)}
|
||||
<SubscriptionsGrid
|
||||
data={groupData}
|
||||
showAnalytics={showAnalytics}
|
||||
showCategories={showCategories}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const AIDisplaySubscriptionsPart = toolMemo(
|
||||
({ part }: { part: AIDisplaySubscriptionsTool }) => {
|
||||
// Handle error state
|
||||
if (part.state === "output-error") {
|
||||
return (
|
||||
<ErrorState
|
||||
title="Subscriptions Error"
|
||||
error="An error occurred while loading subscriptions"
|
||||
maxWidth="max-w-6xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Handle loading state
|
||||
if (part.state !== "output-available" || !part.output) {
|
||||
return (
|
||||
<LoadingState
|
||||
title="Loading Subscriptions..."
|
||||
description="Fetching subscription data..."
|
||||
maxWidth="max-w-6xl"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Extract output with proper typing
|
||||
const output = part.output as NonNullable<AIDisplaySubscriptionsTool["output"]>
|
||||
|
||||
const AIDisplaySubscriptionsPartBase = memo(
|
||||
({ output }: { output: NonNullable<AIDisplaySubscriptionsTool["output"]> }) => {
|
||||
const {
|
||||
subscriptions,
|
||||
displayType = "list",
|
||||
@@ -226,56 +193,51 @@ export const AIDisplaySubscriptionsPart = toolMemo(
|
||||
<GroupedSubscriptions
|
||||
data={subscriptions}
|
||||
groupBy={groupBy}
|
||||
displayType={displayType}
|
||||
showAnalytics={showAnalytics}
|
||||
showCategories={showCategories}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
switch (displayType) {
|
||||
default: {
|
||||
return (
|
||||
<SubscriptionsGrid
|
||||
data={subscriptions}
|
||||
showAnalytics={showAnalytics}
|
||||
showCategories={showCategories}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
return (
|
||||
<SubscriptionsGrid
|
||||
data={subscriptions}
|
||||
showAnalytics={showAnalytics}
|
||||
showCategories={showCategories}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mb-2 w-full min-w-0">
|
||||
<div className="w-[9999px] max-w-[calc(var(--ai-chat-layout-width,65ch)_-120px)]" />
|
||||
<CardHeader>
|
||||
<CardTitle className="text-text flex items-center gap-2 text-xl font-semibold">
|
||||
<span className="text-lg">📋</span>
|
||||
<span>{title || "My Subscriptions"}</span>
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{formatDisplayType(displayType)} • {formatFilterBy(filterBy)} • {formatGroupBy(groupBy)}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="@container space-y-6">
|
||||
{/* Statistics Overview */}
|
||||
<div className="@[700px]:grid-cols-4 grid grid-cols-2 gap-4">
|
||||
<StatCard title="Total Subscriptions" value={totalSubscriptions} emoji="📊" />
|
||||
<StatCard
|
||||
title="Active Feeds"
|
||||
value={activeSubscriptions}
|
||||
description={`${totalSubscriptions - activeSubscriptions} inactive`}
|
||||
emoji="🟢"
|
||||
/>
|
||||
{showCategories && <StatCard title="Categories" value={categoriesCount} emoji="🏷️" />}
|
||||
<StatCard title="Total Views" value={totalViews.toLocaleString()} emoji="👀" />
|
||||
</div>
|
||||
<DisplayCardWrapper
|
||||
title={title || "My Subscriptions"}
|
||||
emoji="📋"
|
||||
description={`${formatDisplayType(displayType)} • ${formatFilterBy(filterBy)} • ${formatGroupBy(groupBy)}`}
|
||||
>
|
||||
{/* Statistics Overview */}
|
||||
<div className="@[700px]:grid-cols-4 grid grid-cols-2 gap-4">
|
||||
<StatCard title="Total Subscriptions" value={totalSubscriptions} emoji="📊" />
|
||||
<StatCard
|
||||
title="Active Feeds"
|
||||
value={activeSubscriptions}
|
||||
description={`${totalSubscriptions - activeSubscriptions} inactive`}
|
||||
emoji="🟢"
|
||||
/>
|
||||
{showCategories && <StatCard title="Categories" value={categoriesCount} emoji="🏷️" />}
|
||||
<StatCard title="Total Views" value={totalViews.toLocaleString()} emoji="👀" />
|
||||
</div>
|
||||
|
||||
{/* Subscriptions Display */}
|
||||
{renderSubscriptions()}
|
||||
</CardContent>
|
||||
</Card>
|
||||
{/* Subscriptions Display */}
|
||||
{renderSubscriptions()}
|
||||
</DisplayCardWrapper>
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
export const AIDisplaySubscriptionsPart = withDisplayStateHandler<
|
||||
AIDisplaySubscriptionsTool["output"]
|
||||
>({
|
||||
title: "Subscriptions",
|
||||
loadingDescription: "Fetching subscription data...",
|
||||
errorTitle: "Subscriptions Error",
|
||||
})(AIDisplaySubscriptionsPartBase)
|
||||
|
||||
@@ -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 = <P extends PartWithState>(FC: ComponentType<P>): ComponentType<P> =>
|
||||
memo(FC, (prev, next) => {
|
||||
if (prev.part.state === "output-available") return true
|
||||
return isEqual(prev, next)
|
||||
}) as ComponentType<P>
|
||||
@@ -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 = <P extends PartWithState>(FC: ComponentType<P>): ComponentType<P> =>
|
||||
memo(FC, (prev, next) => {
|
||||
if (prev.part.state === "output-available") return true
|
||||
return isEqual(prev, next)
|
||||
}) as ComponentType<P>
|
||||
|
||||
// Higher-Order Component for display state handling
|
||||
export function withDisplayStateHandler<T>(config: {
|
||||
title: string
|
||||
loadingDescription: string
|
||||
errorTitle: string
|
||||
maxWidth?: string
|
||||
}) {
|
||||
return function <P extends { part: { state: string; output?: T } }>(
|
||||
WrappedComponent: ComponentType<P & { output: NonNullable<T> }>,
|
||||
): ComponentType<P> {
|
||||
const WithDisplayStateHandler = (props: P) => {
|
||||
const { part } = props
|
||||
|
||||
// Handle error state
|
||||
if (part.state === "output-error") {
|
||||
return (
|
||||
<ErrorState
|
||||
title={config.errorTitle}
|
||||
error={`An error occurred while loading ${config.title.toLowerCase()}`}
|
||||
maxWidth={config.maxWidth || "max-w-6xl"}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Handle loading/invalid state
|
||||
if (part.state !== "output-available" || !part.output) {
|
||||
return (
|
||||
<LoadingState
|
||||
title={`Loading ${config.title}...`}
|
||||
description={config.loadingDescription}
|
||||
maxWidth={config.maxWidth || "max-w-6xl"}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// Render the wrapped component with the validated output
|
||||
return <WrappedComponent {...props} output={part.output as NonNullable<T>} />
|
||||
}
|
||||
|
||||
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
|
||||
}) => (
|
||||
<Card className={`mb-2 w-full min-w-0 ${maxWidth}`}>
|
||||
<div className="w-[9999px] max-w-[calc(var(--ai-chat-layout-width,65ch)_-120px)]" />
|
||||
<CardHeader>
|
||||
<CardTitle className="text-text flex items-center gap-2 text-xl font-semibold">
|
||||
<span className="text-lg">{emoji}</span>
|
||||
<span>{title}</span>
|
||||
</CardTitle>
|
||||
{description && <CardDescription>{description}</CardDescription>}
|
||||
</CardHeader>
|
||||
<CardContent className="@container space-y-6">{children}</CardContent>
|
||||
</Card>
|
||||
)
|
||||
-32
@@ -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) => (
|
||||
<Card className={cn("p-4", className)}>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-text-secondary text-sm font-medium">{title}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="pt-0">
|
||||
<div
|
||||
className={cn(
|
||||
"bg-material-medium text-text-tertiary flex items-center justify-center rounded-lg text-sm",
|
||||
height,
|
||||
)}
|
||||
>
|
||||
Chart visualization ({data.length} data points)
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from "./AnalyticsMetrics"
|
||||
export * from "./CategoryTag"
|
||||
export * from "./ChartPlaceholder"
|
||||
export * from "./DisplayHeader"
|
||||
export * from "./EmptyState"
|
||||
export * from "./FeedItemCard"
|
||||
|
||||
+14
-12
@@ -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 = (
|
||||
<DropdownMenuContent align="start">
|
||||
<DropdownMenuSub>
|
||||
@@ -100,21 +102,21 @@ export const AIChatContextBar: Component<{ onSendShortcut?: (prompt: string) =>
|
||||
<div className="text-text-tertiary p-3 text-center text-xs">No shortcuts configured</div>
|
||||
) : (
|
||||
enabledShortcuts.map((shortcut) => (
|
||||
<DropdownMenuItem
|
||||
key={shortcut.id}
|
||||
onClick={() => onSendShortcut?.(shortcut.prompt)}
|
||||
className="text-xs"
|
||||
shortcut={shortcut.hotkey}
|
||||
>
|
||||
<div className="flex w-full items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<i className="i-mgc-magic-2-cute-re size-3.5" />
|
||||
<span className="truncate">{shortcut.name}</span>
|
||||
</div>
|
||||
</div>
|
||||
<DropdownMenuItem key={shortcut.id} onClick={() => onSendShortcut?.(shortcut.prompt)}>
|
||||
<i className="i-mgc-magic-2-cute-re mr-1.5 size-3.5" />
|
||||
<span className="truncate">{shortcut.name}</span>
|
||||
</DropdownMenuItem>
|
||||
))
|
||||
)}
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
showSettingModal("ai")
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-settings-7-cute-re mr-1.5 size-3.5" />
|
||||
<span>Manage Shortcut</span>
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
)
|
||||
|
||||
|
||||
@@ -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<typeof chatInputVariants> {
|
||||
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 (
|
||||
<div className="w-full">
|
||||
{/* Error Display */}
|
||||
{error && <CollapsibleError error={error} />}
|
||||
|
||||
{/* Integrated Input Container with Context Bar */}
|
||||
<div className={cn(chatInputVariants({ variant }))}>
|
||||
{/* Input Area */}
|
||||
<div className="relative z-10 flex items-end" onContextMenu={stopPropagation}>
|
||||
<ScrollArea rootClassName="mx-5 my-3.5 mr-14 flex-1 overflow-auto">
|
||||
<LexicalRichEditor
|
||||
ref={editorRef}
|
||||
placeholder="Message AI assistant..."
|
||||
className="w-full"
|
||||
onChange={handleEditorChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
autoFocus
|
||||
plugins={[MentionPlugin]}
|
||||
namespace="AIChatRichEditor"
|
||||
/>
|
||||
</ScrollArea>
|
||||
<div className="absolute right-3 top-3">
|
||||
<AIChatSendButton
|
||||
onClick={isProcessing ? stop : handleSend}
|
||||
disabled={!isProcessing && isEmpty}
|
||||
isProcessing={isProcessing}
|
||||
size="sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Context Bar - Always shown, positioned below the input area */}
|
||||
<div className="border-border/20 relative z-10 border-t bg-transparent">
|
||||
<AIChatContextBar
|
||||
className="border-0 bg-transparent px-4 py-2.5"
|
||||
onSendShortcut={(prompt) => onSend(prompt, null)}
|
||||
<div className={cn(chatInputVariants({ variant }))}>
|
||||
{/* Input Area */}
|
||||
<div className="relative z-10 flex items-end" onContextMenu={stopPropagation}>
|
||||
<ScrollArea rootClassName="mx-5 my-3.5 mr-14 flex-1 overflow-auto">
|
||||
<LexicalRichEditor
|
||||
ref={editorRef}
|
||||
placeholder="Message AI assistant..."
|
||||
className="w-full"
|
||||
onChange={handleEditorChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
autoFocus
|
||||
plugins={[MentionPlugin]}
|
||||
namespace="AIChatRichEditor"
|
||||
/>
|
||||
</ScrollArea>
|
||||
<div className="absolute right-3 top-3">
|
||||
<AIChatSendButton
|
||||
onClick={isProcessing ? stop : handleSend}
|
||||
disabled={!isProcessing && isEmpty}
|
||||
isProcessing={isProcessing}
|
||||
size="sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Context Bar - Always shown, positioned below the input area */}
|
||||
<div className="border-border/20 relative z-10 border-t bg-transparent">
|
||||
<AIChatContextBar
|
||||
className="border-0 bg-transparent px-4 py-2.5"
|
||||
onSendShortcut={(prompt) => onSend(prompt, null)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
@@ -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 && (
|
||||
<div className="absolute inset-x-0 bottom-0 mx-auto max-w-4xl px-6 pb-6">
|
||||
{error && <CollapsibleError error={error} />}
|
||||
<ChatInput onSend={handleSendMessage} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
+4
-1
@@ -55,7 +55,7 @@ export const AIMessageParts: React.FC<MessagePartsProps> = 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<MessagePartsProps> = React.memo(({ message
|
||||
|
||||
default: {
|
||||
if (part.type.startsWith("tool-")) {
|
||||
if (part.type.startsWith("tool-chunkBreak")) {
|
||||
return null
|
||||
}
|
||||
return <ToolInvocationComponent key={partKey} part={part as ToolUIPart} />
|
||||
}
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user