(props: PromptFieldProps)
| 134 | } |
| 135 | |
| 136 | export function PromptField(props: PromptFieldProps) { |
| 137 | let { |
| 138 | children, |
| 139 | acceptedAttachmentTypes, |
| 140 | isGenerating, |
| 141 | onStop, |
| 142 | styles, |
| 143 | onAddAttachments, |
| 144 | onRemoveAttachments |
| 145 | } = props; |
| 146 | let [prompt, setPrompt] = useState<TokenSegmentList>(new AutoLinkingSegmentList([])); |
| 147 | let [attachments, setAttachments] = useState<PromptFieldAttachment[]>([]); |
| 148 | |
| 149 | // Not using RAC DropZone because it adds its own focusable button, |
| 150 | // and we want to avoid an extra tab. We support pasting files directly into the input. |
| 151 | let inputRef = useRef<HTMLDivElement>(null); |
| 152 | let {dropProps, isDropTarget} = useDrop({ |
| 153 | ref: inputRef, |
| 154 | hasDropButton: true, |
| 155 | isDisabled: !acceptedAttachmentTypes, |
| 156 | getDropOperation(types) { |
| 157 | return acceptedAttachmentTypes && types.has(acceptedAttachmentTypes) ? 'copy' : 'cancel'; |
| 158 | }, |
| 159 | async onDrop(e) { |
| 160 | let files = await Promise.all( |
| 161 | e.items |
| 162 | .filter(isFileDropItem) |
| 163 | .filter(item => matchMimeType(item.type, acceptedAttachmentTypes!)) |
| 164 | .map(async item => ({ |
| 165 | id: crypto.randomUUID(), |
| 166 | file: await item.getFile(), |
| 167 | image: item.type.startsWith('image/') ? URL.createObjectURL(await item.getFile()) : '' |
| 168 | })) |
| 169 | ); |
| 170 | onAddAttachments?.(files); |
| 171 | setAttachments(attachments => [...attachments, ...files]); |
| 172 | } |
| 173 | }); |
| 174 | |
| 175 | let {onFocusChange} = useContext(PromptFocusContext); |
| 176 | let {focusWithinProps} = useFocusWithin({onFocusWithinChange: onFocusChange}); |
| 177 | |
| 178 | let onSubmit = () => { |
| 179 | if (prompt.segments.length === 0) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | props.onSubmit?.(prompt, attachments); |
| 184 | setPrompt(new AutoLinkingSegmentList([])); |
| 185 | setAttachments([]); |
| 186 | inputRef.current?.focus(); |
| 187 | }; |
| 188 | |
| 189 | return ( |
| 190 | <PromptFieldContext.Provider |
| 191 | value={{ |
| 192 | attachments, |
| 193 | setAttachments, |
nothing calls this directly
no test coverage detected