Update chat.tsx

This commit is contained in:
Yanyutin753
2024-04-13 11:22:05 +08:00
parent 24c8ca610c
commit 2da12d397e

View File

@@ -476,22 +476,38 @@ export function ChatActions(props: {
} }
const onImageSelected = async (e: any) => { const onImageSelected = async (e: any) => {
const file = e.target.files[0]; const files = e.target.files;
if (!file) return; if (!files.length) return;
const api = new ClientApi(); const api = new ClientApi();
// Here we create a Promise for each file upload,
// then use Promise.all to wait for all of them to complete.
const uploadPromises = Array.from(files).map(async (file) => {
setUploadLoading(true); setUploadLoading(true);
const uploadFile = await api.file const uploadFile = await api.file
.upload(file) .upload(file)
.catch((e) => { .catch((e) => {
console.error("[Upload]", e); console.error("[Upload]", e);
showToast(prettyObject(e)); showToast(prettyObject(e));
// return null if upload fails
return null;
}) })
.finally(() => setUploadLoading(false)); .finally(() => setUploadLoading(false));
if (uploadFile) {
// use a callback or event to inform about the new file
props.imageSelected({ props.imageSelected({
fileName: uploadFile.fileName, fileName: uploadFile.fileName,
fileUrl: uploadFile.filePath, fileUrl: uploadFile.filePath,
}); });
}
// Clear file input on each iteration.
e.target.value = null; e.target.value = null;
});
// Wait for all uploads to finish.
await Promise.all(uploadPromises);
}; };
// switch model // switch model
@@ -518,7 +534,7 @@ export function ChatActions(props: {
const items = event.clipboardData?.items || []; const items = event.clipboardData?.items || [];
const api = new ClientApi(); const api = new ClientApi();
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") === -1) continue; // if (items[i].type.indexOf("image") === -1) continue;
const file = items[i].getAsFile(); const file = items[i].getAsFile();
if (file !== null) { if (file !== null) {
setUploadLoading(true); setUploadLoading(true);
@@ -622,7 +638,7 @@ export function ChatActions(props: {
)} )}
{(currentModel.includes("vision") || currentModel.includes("gizmo")) && ( {(currentModel.includes("vision") || currentModel.includes("gizmo")) && (
<ChatAction <ChatAction
onClick={selectImages} onClick={selectImage}
text="选择文件" text="选择文件"
loading={uploadLoading} loading={uploadLoading}
icon={<UploadIcon />} icon={<UploadIcon />}
@@ -633,7 +649,7 @@ export function ChatActions(props: {
accept="*/*" accept="*/*"
id="chat-image-file-select-upload" id="chat-image-file-select-upload"
style={{ display: "none" }} style={{ display: "none" }}
onChange={onImagesSelected} onChange={onImageSelected}
/> />
} }
/> />