forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachmentQueue.tsx
More file actions
125 lines (115 loc) · 3.61 KB
/
Copy pathAttachmentQueue.tsx
File metadata and controls
125 lines (115 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from "react";
import type { Attachment } from "./props";
import {
formatFileSize,
getSourceUrl,
getDocumentIcon,
} from "@copilotkit/shared";
interface AttachmentQueueProps {
attachments: Attachment[];
onRemoveAttachment: (id: string) => void;
className?: string;
}
export const AttachmentQueue: React.FC<AttachmentQueueProps> = ({
attachments,
onRemoveAttachment,
className = "",
}) => {
if (attachments.length === 0) return null;
return (
<div className={`copilotKitAttachmentQueue ${className}`}>
{attachments.map((attachment) => (
<div
key={attachment.id}
className={`copilotKitAttachmentQueueItem copilotKitAttachmentQueueItem--${attachment.type}`}
>
{attachment.status === "uploading" && (
<div className="copilotKitAttachmentQueueOverlay">
<div className="copilotKitAttachmentQueueSpinner" />
</div>
)}
<AttachmentPreview attachment={attachment} />
<button
onClick={() => onRemoveAttachment(attachment.id)}
className="copilotKitAttachmentQueueRemoveButton"
aria-label="Remove attachment"
>
✕
</button>
</div>
))}
</div>
);
};
function AttachmentPreview({ attachment }: { attachment: Attachment }) {
if (attachment.status === "uploading") {
return <div className="copilotKitAttachmentQueuePreviewPlaceholder" />;
}
const src = getSourceUrl(attachment.source);
switch (attachment.type) {
case "image":
return (
<img
src={src}
alt={attachment.filename || "Image attachment"}
className="copilotKitAttachmentQueuePreviewImage"
/>
);
case "audio":
return (
<div className="copilotKitAttachmentQueuePreviewAudio">
<audio src={src} controls preload="metadata" />
{attachment.filename && (
<span className="copilotKitAttachmentQueueFilename">
{attachment.filename}
</span>
)}
</div>
);
case "video":
return (
<div className="copilotKitAttachmentQueuePreviewVideo">
{attachment.thumbnail ? (
<img
src={attachment.thumbnail}
alt={attachment.filename || "Video thumbnail"}
className="copilotKitAttachmentQueuePreviewImage"
/>
) : (
<video
src={src}
preload="metadata"
muted
className="copilotKitAttachmentQueuePreviewImage"
/>
)}
</div>
);
case "document":
return (
<div className="copilotKitAttachmentQueuePreviewDocument">
<div className="copilotKitAttachmentQueueDocIcon">
{getDocumentIcon(attachment.source.mimeType ?? "")}
</div>
<div className="copilotKitAttachmentQueueDocInfo">
<span className="copilotKitAttachmentQueueFilename">
{attachment.filename || "Document"}
</span>
{attachment.size != null && (
<span className="copilotKitAttachmentQueueFileSize">
{formatFileSize(attachment.size)}
</span>
)}
</div>
</div>
);
}
}
/**
* @deprecated Use `AttachmentQueue` from `@copilotkit/react-ui` instead.
* `ImageUploadQueue` only displayed image previews. `AttachmentQueue` supports
* images, audio, video, and documents.
* See https://docs.copilotkit.ai/migration-guides/migrate-attachments
* @since 1.56.0
*/
export { AttachmentQueue as ImageUploadQueue };