import { type ComponentPropsWithoutRef, type MouseEvent } from 'react'; import { useAttachments } from './ai-message-composer'; import clsx from 'clsx'; const byteValueNumberFormatter = Intl.NumberFormat('en', { notation: 'compact', style: 'unit', unit: 'byte', unitDisplay: 'narrow', }); export const Item = ({ file, onDelete, onRetry, state, title, imagePreviewSource, }: { file: File; state?: 'uploading' | 'finished' | 'failed' | 'pending' | (string & {}); title?: string; imagePreviewSource?: string; onDelete?: (_: MouseEvent) => void; onRetry?: (_: MouseEvent) => void; }) => { const fileName = title || file.name || 'Unknown file name'; const readableFileSize = byteValueNumberFormatter.format(file.size); const isImage = file.type.startsWith('image/'); return (
{state === 'failed' && (
)} {!isImage && (
description
{fileName}
{readableFileSize}
)} {isImage && ( {fileName} )}
); }; export const AttachmentPreview = ({ children, ...restProps }: ComponentPropsWithoutRef<'div'> & { children?: | React.ReactNode | ((_: ReturnType) => React.ReactNode); }) => { const _ = useAttachments(); if (!children) { return null; } return (
{typeof children === 'function' ? children(_) : children}
); }; AttachmentPreview.Item = Item; export const chunk = (array: T, size: number) => { const chunkCount = Math.ceil(array.length / size); return Array.from( { length: chunkCount }, (_, index) => array.slice(size * index, size * index + size) as T, ); };