(props: AttachFileMenuItemProps)
| 1185 | * AttachFileMenuItem triggers a system file dialog to attach files within an InsertMenuButton. |
| 1186 | */ |
| 1187 | export function AttachFileMenuItem(props: AttachFileMenuItemProps) { |
| 1188 | let {onAction, ...otherProps} = props; |
| 1189 | let {acceptedAttachmentTypes, setAttachments, onAddAttachments} = useContext(PromptFieldContext); |
| 1190 | return ( |
| 1191 | <MenuItem |
| 1192 | {...otherProps} |
| 1193 | onAction={() => { |
| 1194 | onAction?.(); |
| 1195 | let input = document.createElement('input'); |
| 1196 | input.type = 'file'; |
| 1197 | if (acceptedAttachmentTypes) { |
| 1198 | input.accept = acceptedAttachmentTypes.join(','); |
| 1199 | } |
| 1200 | input.multiple = true; |
| 1201 | input.onchange = e => { |
| 1202 | let files = (e.currentTarget as HTMLInputElement).files; |
| 1203 | if (files && acceptedAttachmentTypes) { |
| 1204 | let attachments = Array.from(files) |
| 1205 | .filter(file => matchMimeType(file.type, acceptedAttachmentTypes)) |
| 1206 | .map(file => ({ |
| 1207 | id: crypto.randomUUID(), |
| 1208 | file, |
| 1209 | image: file.type.startsWith('image/') ? URL.createObjectURL(file) : '' |
| 1210 | })); |
| 1211 | if (attachments.length > 0) { |
| 1212 | onAddAttachments?.(attachments); |
| 1213 | setAttachments(prev => [...prev, ...attachments]); |
| 1214 | } |
| 1215 | } |
| 1216 | }; |
| 1217 | input.click(); |
| 1218 | }}> |
| 1219 | <Attach /> |
| 1220 | <Text>Attach a file</Text> |
| 1221 | </MenuItem> |
| 1222 | ); |
| 1223 | } |
| 1224 | |
| 1225 | // either replace the filter text (aka token replace) or insert value at current caret position (aka plain text inject) |
| 1226 | function useInsertPromptSegment(segments: TokenFieldSegment[]) { |
nothing calls this directly
no test coverage detected