feat: implement context block visibility management with popover for hidden blocks in AIChatContextBar

- Added logic to limit visible context blocks to a maximum of 4, with additional blocks accessible via a popover.
- Updated ContextBlock component to accept an optional className prop for better styling flexibility.
- Improved Tailwind CSS class ordering for consistency.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-10-21 19:53:14 +08:00
parent 8ec65625c9
commit d3bf8a89db
2 changed files with 79 additions and 24 deletions
@@ -22,15 +22,17 @@ const BlockContainer: FC<{
onDisableClick?: () => void
content: ReactNode
readOnly?: boolean
}> = memo(({ icon, label, onRemove, content, disabled, onDisableClick, readOnly }) => {
className?: string
}> = memo(({ icon, label, onRemove, content, disabled, onDisableClick, readOnly, className }) => {
const isStringContent = typeof content === "string"
return (
<div
className={clsx(
"group relative flex h-7 min-w-0 items-center gap-2 overflow-hidden rounded-lg px-2.5",
"border border-border bg-fill-tertiary",
disabled && "cursor-pointer border-dashed opacity-50",
"border-border bg-fill-tertiary border",
disabled && "cursor-pointer border-dashed italic opacity-50",
className,
)}
onClick={() => {
if (disabled) {
@@ -49,13 +51,13 @@ const BlockContainer: FC<{
<div className="flex min-w-0 flex-1 items-center gap-1.5">
<div className="flex items-center gap-1">
{icon && <i className={cn("size-3.5 flex-shrink-0", icon)} />}
{label && <span className="text-xs font-medium text-text-tertiary">{label}</span>}
{label && <span className="text-text-tertiary text-xs font-medium">{label}</span>}
</div>
{isStringContent ? (
<span className="min-w-0 flex-1 truncate text-xs text-text">{content}</span>
<span className="text-text min-w-0 flex-1 truncate text-xs">{content}</span>
) : (
<div className="min-w-0 flex-1 truncate text-xs text-text">{content}</div>
<div className="text-text min-w-0 flex-1 truncate text-xs">{content}</div>
)}
</div>
</div>
@@ -64,7 +66,7 @@ const BlockContainer: FC<{
<button
type="button"
onClick={onRemove}
className="absolute inset-y-0 right-2 flex-shrink-0 cursor-button text-text/90 opacity-0 transition-all ease-in hover:text-text group-hover:opacity-100"
className="cursor-button text-text/90 hover:text-text absolute inset-y-0 right-2 flex-shrink-0 opacity-0 transition-all ease-in group-hover:opacity-100"
>
<i className="i-mgc-close-cute-re size-3" />
</button>
@@ -1,5 +1,6 @@
import { Popover, PopoverContent, PopoverTrigger } from "@follow/components/ui/popover/index.jsx"
import { cn } from "@follow/utils/utils"
import { memo, useCallback, useEffect, useRef } from "react"
import { memo, useCallback, useEffect, useMemo, useRef } from "react"
import { useGeneralSettingKey } from "~/atoms/settings/general"
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
@@ -13,6 +14,9 @@ import { BlockSliceAction } from "../../store/slices/block.slice"
import { CombinedContextBlock, ContextBlock } from "../context-bar/blocks"
import { MentionButton } from "../context-bar/MentionButton"
// Maximum number of context blocks to show before collapsing into "more" popover
const MAX_VISIBLE_BLOCKS = 4
export const AIChatContextBar: Component = memo(({ className }) => {
const blocks = useAIChatStore()((s) => s.blocks)
const fileInputRef = useRef<HTMLInputElement>(null)
@@ -83,15 +87,41 @@ export const AIChatContextBar: Component = memo(({ className }) => {
const displayBlocks = useDisplayBlocks(blocks)
// Split blocks into visible and hidden based on MAX_VISIBLE_BLOCKS
const { visibleBlocks, hiddenBlocks } = useMemo(() => {
if (displayBlocks.length <= MAX_VISIBLE_BLOCKS) {
return { visibleBlocks: displayBlocks, hiddenBlocks: [] }
}
return {
visibleBlocks: displayBlocks.slice(0, MAX_VISIBLE_BLOCKS),
hiddenBlocks: displayBlocks.slice(MAX_VISIBLE_BLOCKS),
}
}, [displayBlocks])
const renderBlock = useCallback((item: (typeof displayBlocks)[number]) => {
if (item.kind === "combined") {
return (
<CombinedContextBlock
key={`combined-${item.viewBlock?.id}-${item.feedBlock?.id}-${item.unreadOnlyBlock?.id}`}
viewBlock={item.viewBlock}
feedBlock={item.feedBlock}
unreadOnlyBlock={item.unreadOnlyBlock}
/>
)
}
return <ContextBlock key={item.block.id} block={item.block} />
}, [])
return (
<div className={cn("flex flex-wrap items-center gap-2 px-4 py-3", className)}>
<div className={cn("flex items-center gap-2 px-4 py-3", className)}>
<MentionButton />
{/* File Upload Button */}
<button
type="button"
onClick={handleAttachFile}
className="flex size-7 items-center justify-center rounded-md border border-border bg-material-medium text-text-secondary transition-colors hover:bg-material-thin hover:text-text-secondary"
className="border-border bg-material-medium text-text-secondary hover:bg-material-thin hover:text-text-secondary flex size-7 shrink-0 items-center justify-center rounded-md border transition-colors"
title="Upload Files"
>
<i className="i-mgc-attachment-cute-re size-3.5" />
@@ -107,21 +137,44 @@ export const AIChatContextBar: Component = memo(({ className }) => {
className="hidden"
/>
{/* Context Blocks */}
{displayBlocks.map((item) => {
if (item.kind === "combined") {
return (
<CombinedContextBlock
key={`combined-${item.viewBlock?.id}-${item.feedBlock?.id}-${item.unreadOnlyBlock?.id}`}
viewBlock={item.viewBlock}
feedBlock={item.feedBlock}
unreadOnlyBlock={item.unreadOnlyBlock}
/>
)
}
{/* Visible Context Blocks */}
<div className="flex min-w-0 flex-1 items-center gap-2">
{visibleBlocks.map((item) => (
<div
key={
item.kind === "combined"
? `combined-${item.viewBlock?.id}-${item.feedBlock?.id}-${item.unreadOnlyBlock?.id}`
: item.block.id
}
className="max-w-[240px] shrink-0"
>
{renderBlock(item)}
</div>
))}
return <ContextBlock key={item.block.id} block={item.block} />
})}
{/* More Button with Popover */}
{hiddenBlocks.length > 0 && (
<Popover>
<PopoverTrigger asChild>
<button
type="button"
className="border-border bg-fill-tertiary text-text-secondary hover:bg-fill-secondary hover:text-text flex h-7 shrink-0 items-center gap-1.5 rounded-lg border px-2.5 text-xs transition-colors"
>
<i className="i-mgc-more-1-cute-re size-3.5" />
<span>+{hiddenBlocks.length}</span>
</button>
</PopoverTrigger>
<PopoverContent className="w-80 p-3" align="start">
<div className="flex flex-col gap-2">
<div className="text-text-secondary mb-1 text-xs font-medium">
Additional Context
</div>
{hiddenBlocks.map((item) => renderBlock(item))}
</div>
</PopoverContent>
</Popover>
)}
</div>
</div>
)
})