Skip to content

Commit 3323320

Browse files
fix: allow sending files irrespective of status if offline mode is enabled
1 parent 065ef21 commit 3323320

File tree

1 file changed

+78
-64
lines changed

1 file changed

+78
-64
lines changed

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,62 @@ export const MessageInputProvider = <
623623
setText('');
624624
};
625625

626+
const mapImageUploadToAttachment = (image: ImageUpload) => {
627+
const mime_type: string | boolean = lookup(image.file.filename as string);
628+
return {
629+
fallback: image.file.name,
630+
image_url: image.url,
631+
mime_type: mime_type ? mime_type : undefined,
632+
original_height: image.height,
633+
original_width: image.width,
634+
originalFile: image.file,
635+
type: 'image',
636+
} as Attachment;
637+
};
638+
639+
const mapFileUploadToAttachment = (file: FileUpload) => {
640+
const mime_type: string | boolean = lookup(file.file.name as string);
641+
if (file.file.type?.startsWith('image/')) {
642+
return {
643+
fallback: file.file.name,
644+
image_url: file.url,
645+
mime_type: mime_type ? mime_type : undefined,
646+
originalFile: file.file,
647+
type: 'image',
648+
};
649+
} else if (file.file.type?.startsWith('audio/')) {
650+
return {
651+
asset_url: file.url || file.file.uri,
652+
duration: file.file.duration,
653+
file_size: file.file.size,
654+
mime_type: file.file.type,
655+
originalFile: file.file,
656+
title: file.file.name,
657+
type: 'audio',
658+
};
659+
} else if (file.file.type?.startsWith('video/')) {
660+
return {
661+
asset_url: file.url || file.file.uri,
662+
duration: file.file.duration,
663+
file_size: file.file.size,
664+
mime_type: file.file.type,
665+
originalFile: file.file,
666+
thumb_url: file.thumb_url,
667+
title: file.file.name,
668+
type: 'video',
669+
};
670+
} else {
671+
return {
672+
asset_url: file.url || file.file.uri,
673+
file_size: file.file.size,
674+
mime_type: file.file.type,
675+
originalFile: file.file,
676+
title: file.file.name,
677+
type: 'file',
678+
};
679+
}
680+
};
681+
626682
// TODO: Figure out why this is async, as it doesn't await any promise.
627683
// eslint-disable-next-line require-await
628684
const sendMessage = async () => {
@@ -648,6 +704,14 @@ export const MessageInputProvider = <
648704

649705
const attachments = [] as Attachment<StreamChatGenerics>[];
650706
for (const image of imageUploads) {
707+
if (enableOfflineSupport) {
708+
if (image.state === FileState.NOT_SUPPORTED) {
709+
return;
710+
}
711+
attachments.push(mapImageUploadToAttachment(image));
712+
continue;
713+
}
714+
651715
if ((!image || image.state === FileState.UPLOAD_FAILED) && !enableOfflineSupport) {
652716
continue;
653717
}
@@ -667,28 +731,21 @@ export const MessageInputProvider = <
667731
}
668732

669733
// To get the mime type of the image from the file name and send it as an response for an image
670-
const mime_type: string | boolean = lookup(image.file.filename as string);
671-
672-
if (
673-
image.state === FileState.UPLOADED ||
674-
image.state === FileState.FINISHED ||
675-
(enableOfflineSupport && image.state === FileState.UPLOAD_FAILED)
676-
) {
677-
// @ts-ignore
678-
attachments.push({
679-
fallback: image.file.name,
680-
image_url: image.url,
681-
mime_type: mime_type ? mime_type : undefined,
682-
original_height: image.height,
683-
original_width: image.width,
684-
originalFile: image.file,
685-
type: 'image',
686-
});
734+
if (image.state === FileState.UPLOADED || image.state === FileState.FINISHED) {
735+
attachments.push(mapImageUploadToAttachment(image));
687736
}
688737
}
689738

690739
for (const file of fileUploads) {
691-
if ((!file || file.state === FileState.UPLOAD_FAILED) && !enableOfflineSupport) {
740+
if (enableOfflineSupport) {
741+
if (file.state === FileState.NOT_SUPPORTED) {
742+
return;
743+
}
744+
attachments.push(mapFileUploadToAttachment(file));
745+
continue;
746+
}
747+
748+
if (!file || file.state === FileState.UPLOAD_FAILED) {
692749
continue;
693750
}
694751

@@ -697,52 +754,9 @@ export const MessageInputProvider = <
697754
sending.current = false;
698755
return;
699756
}
700-
const mime_type: string | boolean = lookup(file.file.name as string);
701-
702-
if (
703-
file.state === FileState.UPLOADED ||
704-
file.state === FileState.FINISHED ||
705-
(enableOfflineSupport && file.state === FileState.UPLOAD_FAILED)
706-
) {
707-
if (file.file.type?.startsWith('image/')) {
708-
attachments.push({
709-
fallback: file.file.name,
710-
image_url: file.url,
711-
mime_type: mime_type ? mime_type : undefined,
712-
originalFile: file.file,
713-
type: 'image',
714-
});
715-
} else if (file.file.type?.startsWith('audio/')) {
716-
attachments.push({
717-
asset_url: file.url || file.file.uri,
718-
duration: file.file.duration,
719-
file_size: file.file.size,
720-
mime_type: file.file.type,
721-
originalFile: file.file,
722-
title: file.file.name,
723-
type: 'audio',
724-
});
725-
} else if (file.file.type?.startsWith('video/')) {
726-
attachments.push({
727-
asset_url: file.url || file.file.uri,
728-
duration: file.file.duration,
729-
file_size: file.file.size,
730-
mime_type: file.file.type,
731-
originalFile: file.file,
732-
thumb_url: file.thumb_url,
733-
title: file.file.name,
734-
type: 'video',
735-
});
736-
} else {
737-
attachments.push({
738-
asset_url: file.url || file.file.uri,
739-
file_size: file.file.size,
740-
mime_type: file.file.type,
741-
originalFile: file.file,
742-
title: file.file.name,
743-
type: 'file',
744-
});
745-
}
757+
758+
if (file.state === FileState.UPLOADED || file.state === FileState.FINISHED) {
759+
attachments.push(mapFileUploadToAttachment(file));
746760
}
747761
}
748762

0 commit comments

Comments
 (0)