"use client"; import { PropsWithChildren, useEffect, useState, type FC } from "react"; import { XIcon, PlusIcon, FileText } from "lucide-react"; import { AttachmentPrimitive, ComposerPrimitive, MessagePrimitive, useAuiState, useAui, } from "@assistant-ui/react"; import { useShallow } from "zustand/shallow"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { Dialog, DialogTitle, DialogContent, DialogTrigger, } from "@/components/ui/dialog"; import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"; import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button"; import { cn } from "@/lib/utils"; const useFileSrc = (file: File | undefined) => { const [src, setSrc] = useState(undefined); useEffect(() => { if (!file) { setSrc(undefined); return; } const objectUrl = URL.createObjectURL(file); setSrc(objectUrl); return () => { URL.revokeObjectURL(objectUrl); }; }, [file]); return src; }; const useAttachmentSrc = () => { const { file, src } = useAuiState( useShallow((s): { file?: File; src?: string } => { if (s.attachment.type !== "image") return {}; if (s.attachment.file) return { file: s.attachment.file }; const src = s.attachment.content?.filter((c) => c.type === "image")[0] ?.image; if (!src) return {}; return { src }; }), ); return useFileSrc(file) ?? src; }; type AttachmentPreviewProps = { src: string; }; const AttachmentPreview: FC = ({ src }) => { const [isLoaded, setIsLoaded] = useState(false); return ( Image Preview setIsLoaded(true)} /> ); }; const AttachmentPreviewDialog: FC = ({ children }) => { const src = useAttachmentSrc(); if (!src) return children; return ( {children} Image Attachment Preview
); }; const AttachmentThumb: FC = () => { const src = useAttachmentSrc(); return ( ); }; const AttachmentUI: FC = () => { const aui = useAui(); const isComposer = aui.attachment.source !== "message"; const isImage = useAuiState((s) => s.attachment.type === "image"); const typeLabel = useAuiState((s) => { const type = s.attachment.type; switch (type) { case "image": return "Image"; case "document": return "Document"; case "file": return "File"; default: return type; } }); return (
{isComposer && }
); }; const AttachmentRemove: FC = () => { return ( ); }; export const UserMessageAttachments: FC = () => { return (
{() => }
); }; export const ComposerAttachments: FC = () => { return (
{() => }
); }; export const ComposerAddAttachment: FC = () => { return ( ); };