refactor: unify rich text attachment component
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
+5
@@ -9,3 +9,8 @@ export const useAIMessageId = () => {
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
export const useAIMessageOptionalId = () => {
|
||||
const ctx = use(AIMessageIdContext)
|
||||
return ctx
|
||||
}
|
||||
|
||||
+65
-35
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable react-refresh/only-export-components */
|
||||
import type {
|
||||
DOMConversionMap,
|
||||
DOMConversionOutput,
|
||||
@@ -10,8 +11,9 @@ import type {
|
||||
import { DecoratorNode } from "lexical"
|
||||
import * as React from "react"
|
||||
|
||||
import { useAIMessageId } from "~/modules/ai-chat/components/message/AIMessageIdContext"
|
||||
import { useMessageByIdSelector } from "~/modules/ai-chat/store/hooks"
|
||||
import { useAIMessageOptionalId } from "~/modules/ai-chat/components/message/AIMessageIdContext"
|
||||
import { useChatBlockSelector, useMessageByIdSelector } from "~/modules/ai-chat/store/hooks"
|
||||
import type { FileAttachment } from "~/modules/ai-chat/store/types"
|
||||
import { findFileAttachmentBlock, isDataBlockPart } from "~/modules/ai-chat/utils/extractor"
|
||||
|
||||
export type SerializedFileAttachmentNode = Spread<
|
||||
@@ -110,31 +112,7 @@ interface FileAttachmentComponentProps {
|
||||
node: FileAttachmentNode
|
||||
}
|
||||
|
||||
function FileAttachmentComponent({ node }: FileAttachmentComponentProps) {
|
||||
const attachmentId = node.getAttachmentId()
|
||||
|
||||
const messageId = useAIMessageId()!
|
||||
|
||||
const fileAttachment = useMessageByIdSelector(messageId, (message) => {
|
||||
for (const part of message.parts) {
|
||||
if (!isDataBlockPart(part)) continue
|
||||
|
||||
const block = findFileAttachmentBlock(part, attachmentId)
|
||||
if (block) {
|
||||
return block.attachment
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (!fileAttachment) {
|
||||
return (
|
||||
<span className="bg-fill border-border text-gray inline-flex items-center gap-1 rounded border px-2 py-1 text-xs">
|
||||
<i className="i-mgc-attachment-cute-re" />
|
||||
<span className="max-w-32 truncate">File not found</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function FileAttachmentPill({ attachment }: { attachment: FileAttachment }) {
|
||||
return (
|
||||
<span
|
||||
className="bg-fill border-border inline-flex items-center gap-1 rounded border px-2 py-1 text-xs"
|
||||
@@ -145,23 +123,75 @@ function FileAttachmentComponent({ node }: FileAttachmentComponentProps) {
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-attachment-cute-re" />
|
||||
<span className="max-w-32 truncate" title={fileAttachment.name}>
|
||||
{fileAttachment.name}
|
||||
<span className="max-w-32 truncate" title={attachment.name}>
|
||||
{attachment.name}
|
||||
</span>
|
||||
{fileAttachment.uploadStatus === "uploading" && (
|
||||
{attachment.uploadStatus === "uploading" && (
|
||||
<i className="i-mgc-loading-3-cute-re text-accent animate-spin" />
|
||||
)}
|
||||
{fileAttachment.uploadStatus === "processing" && (
|
||||
{attachment.uploadStatus === "processing" && (
|
||||
<i className="i-mgc-loading-3-cute-re text-accent animate-spin" />
|
||||
)}
|
||||
{fileAttachment.uploadStatus === "error" && <i className="i-mgc-close-cute-re text-red" />}
|
||||
{fileAttachment.uploadStatus === "completed" && (
|
||||
<i className="i-mgc-check-cute-re text-green" />
|
||||
)}
|
||||
{attachment.uploadStatus === "error" && <i className="i-mgc-close-cute-re text-red" />}
|
||||
{attachment.uploadStatus === "completed" && <i className="i-mgc-check-cute-re text-green" />}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function MissingFilePill() {
|
||||
return (
|
||||
<span className="bg-fill border-border text-gray inline-flex items-center gap-1 rounded border px-2 py-1 text-xs">
|
||||
<i className="i-mgc-attachment-cute-re" />
|
||||
<span className="max-w-32 truncate">File not found</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function BlockBasedAttachment({ attachmentId }: { attachmentId: string }) {
|
||||
const block = useChatBlockSelector((state) =>
|
||||
state.blocks.find(
|
||||
(block) => block.type === "fileAttachment" && block.attachment.id === attachmentId,
|
||||
),
|
||||
)
|
||||
|
||||
if (!block || block.type !== "fileAttachment") {
|
||||
return <MissingFilePill />
|
||||
}
|
||||
|
||||
return <FileAttachmentPill attachment={block.attachment} />
|
||||
}
|
||||
|
||||
function MessageBasedAttachment({
|
||||
attachmentId,
|
||||
messageId,
|
||||
}: {
|
||||
attachmentId: string
|
||||
messageId: string
|
||||
}) {
|
||||
const attachment = useMessageByIdSelector(messageId, (message) => {
|
||||
for (const part of message.parts) {
|
||||
if (!isDataBlockPart(part)) continue
|
||||
const block = findFileAttachmentBlock(part, attachmentId)
|
||||
if (block) return block.attachment
|
||||
}
|
||||
})
|
||||
|
||||
if (attachment) return <FileAttachmentPill attachment={attachment} />
|
||||
// Fallback to block-based when message-based lookup fails
|
||||
return <BlockBasedAttachment attachmentId={attachmentId} />
|
||||
}
|
||||
|
||||
function FileAttachmentComponent({ node }: FileAttachmentComponentProps) {
|
||||
const attachmentId = node.getAttachmentId()
|
||||
|
||||
const messageId = useAIMessageOptionalId()
|
||||
|
||||
if (messageId) {
|
||||
return <MessageBasedAttachment attachmentId={attachmentId} messageId={messageId} />
|
||||
}
|
||||
return <BlockBasedAttachment attachmentId={attachmentId} />
|
||||
}
|
||||
|
||||
export function $createFileAttachmentNode(attachmentId: string): FileAttachmentNode {
|
||||
return new FileAttachmentNode(attachmentId)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { BizUIMessage } from "@folo-services/ai-tools"
|
||||
import { useShallow } from "zustand/shallow"
|
||||
|
||||
import { useAIChatStore } from "./AIChatContext"
|
||||
import type { BlockSlice } from "./slices/block.slice"
|
||||
|
||||
/**
|
||||
* Hook to get the current room ID (chat ID) from the AI chat store
|
||||
@@ -88,3 +89,8 @@ export const useChatScene = () => {
|
||||
const store = useAIChatStore()
|
||||
return store((state) => state.scene)
|
||||
}
|
||||
|
||||
export const useChatBlockSelector = <T>(selector: (state: Pick<BlockSlice, "blocks">) => T) => {
|
||||
const store = useAIChatStore()
|
||||
return store(useShallow((state) => selector(state)))
|
||||
}
|
||||
|
||||
@@ -114,17 +114,16 @@ export function getFileCategoryFromMimeType(mimeType: string): FileCategory {
|
||||
export function getFileIconName(category: FileCategory): string {
|
||||
switch (category) {
|
||||
case "image": {
|
||||
return "i-mgc-pic-cute-re"
|
||||
return tw`i-mgc-pic-cute-re`
|
||||
}
|
||||
case "document": {
|
||||
return "i-mgc-file-cute-re"
|
||||
return tw`i-mingcute-file-line`
|
||||
}
|
||||
case "text": {
|
||||
return "i-mgc-document-cute-re"
|
||||
return tw`i-mingcute-text-line`
|
||||
}
|
||||
|
||||
default: {
|
||||
return "i-mgc-attachment-cute-re"
|
||||
return tw`i-mgc-attachment-cute-re`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user