ui / src / components / agent-chat / use-composer-attachments.tsmain at d68d7e1
// The composer's file-attachment state machine: size limits, the oversize warning, stable chip
// identity, and the hidden picker input (render `AttachmentFileInput` from
// composer-attachments.tsx once next to the composer).
import { useRef, useState } from "react";
import { formatFileSize } from "./agent-events.ts";
const MAX_MESSAGE_FILE_SIZE_BYTES = 25 * 1024 * 1024;
function partitionFilesBySize(files: readonly File[]): {
accepted: File[];
rejected: File[];
} {
const accepted: File[] = [];
const rejected: File[] = [];
for (const file of files) {
if (file.size <= MAX_MESSAGE_FILE_SIZE_BYTES) accepted.push(file);
else rejected.push(file);
}
return { accepted, rejected };
}
function fileSizeErrorMessage(rejected: readonly File[]): string | undefined {
if (rejected.length === 0) return undefined;
const label = rejected.length === 1 ? rejected[0]!.name : `${String(rejected.length)} files`;
return `${label} must be ${formatFileSize(MAX_MESSAGE_FILE_SIZE_BYTES)} or smaller.`;
}
export type AttachmentEntry = { id: string; file: File };
export type ComposerAttachments = ReturnType<typeof useComposerAttachments>;
export function useComposerAttachments() {
const inputRef = useRef<HTMLInputElement>(null);
const [entries, setEntries] = useState<AttachmentEntry[]>([]);
const [fileError, setFileError] = useState<string>();
function addFiles(fileList: FileList | readonly File[] | null) {
const files = Array.from(fileList || []);
if (files.length === 0) return;
const { accepted, rejected } = partitionFilesBySize(files);
setFileError(fileSizeErrorMessage(rejected));
if (accepted.length > 0) {
setEntries((previous) => [
...previous,
...accepted.map((file) => ({ id: crypto.randomUUID(), file })),
]);
}
}
function removeFile(id: string) {
setEntries((previous) => previous.filter((entry) => entry.id !== id));
// The oversize warning is transient; any deliberate chip interaction dismisses it.
setFileError(undefined);
}
function clearFiles() {
setEntries([]);
setFileError(undefined);
}
return {
entries,
files: entries.map((entry) => entry.file),
fileError,
addFiles,
removeFile,
clearFiles,
openFilePicker: () => inputRef.current?.click(),
inputRef,
};
}
/** What a person has written and not sent. It outlives the composer, which goes while the Events
* tab is open. */
export function useComposerDraft(initialMessage = "") {
const [message, setMessage] = useState(initialMessage);
const attachments = useComposerAttachments();
return { message, setMessage, attachments };
}
export type ComposerDraft = ReturnType<typeof useComposerDraft>;