stream-chat-react-native-core 9.0.2-beta.2 → 9.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/components/Attachment/Attachment.js +38 -10
- package/lib/commonjs/components/Attachment/Attachment.js.map +1 -1
- package/lib/commonjs/components/Attachment/AttachmentFileUploadProgressIndicator.js +97 -0
- package/lib/commonjs/components/Attachment/AttachmentFileUploadProgressIndicator.js.map +1 -0
- package/lib/commonjs/components/Attachment/AttachmentUploadIndicator.js +97 -0
- package/lib/commonjs/components/Attachment/AttachmentUploadIndicator.js.map +1 -0
- package/lib/commonjs/components/Attachment/CircularProgressIndicator.js +141 -0
- package/lib/commonjs/components/Attachment/CircularProgressIndicator.js.map +1 -0
- package/lib/commonjs/components/Attachment/FileAttachment.js +18 -4
- package/lib/commonjs/components/Attachment/FileAttachment.js.map +1 -1
- package/lib/commonjs/components/Attachment/Gallery.js +7 -1
- package/lib/commonjs/components/Attachment/Gallery.js.map +1 -1
- package/lib/commonjs/components/Attachment/MediaUploadProgressOverlay.js +59 -0
- package/lib/commonjs/components/Attachment/MediaUploadProgressOverlay.js.map +1 -0
- package/lib/commonjs/components/Attachment/VideoThumbnail.js +18 -8
- package/lib/commonjs/components/Attachment/VideoThumbnail.js.map +1 -1
- package/lib/commonjs/components/Attachment/utils/buildGallery/buildThumbnail.js +2 -0
- package/lib/commonjs/components/Attachment/utils/buildGallery/buildThumbnail.js.map +1 -1
- package/lib/commonjs/components/Channel/Channel.js +94 -92
- package/lib/commonjs/components/Channel/Channel.js.map +1 -1
- package/lib/commonjs/components/Chat/Chat.js +10 -1
- package/lib/commonjs/components/Chat/Chat.js.map +1 -1
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.js +42 -54
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.js.map +1 -1
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.js +10 -6
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.js.map +1 -1
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/FileAttachmentUploadPreview.js +12 -6
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/FileAttachmentUploadPreview.js.map +1 -1
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/ImageAttachmentUploadPreview.js +13 -7
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/ImageAttachmentUploadPreview.js.map +1 -1
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/VideoAttachmentUploadPreview.js +6 -2
- package/lib/commonjs/components/MessageInput/components/AttachmentPreview/VideoAttachmentUploadPreview.js.map +1 -1
- package/lib/commonjs/components/index.js +33 -0
- package/lib/commonjs/components/index.js.map +1 -1
- package/lib/commonjs/contexts/componentsContext/defaultComponents.js +6 -0
- package/lib/commonjs/contexts/componentsContext/defaultComponents.js.map +1 -1
- package/lib/commonjs/contexts/messageInputContext/hooks/useCreateMessageInputContext.js +3 -1
- package/lib/commonjs/contexts/messageInputContext/hooks/useCreateMessageInputContext.js.map +1 -1
- package/lib/commonjs/contexts/messageInputContext/hooks/useMessageComposerHasSendableData.js +4 -1
- package/lib/commonjs/contexts/messageInputContext/hooks/useMessageComposerHasSendableData.js.map +1 -1
- package/lib/commonjs/contexts/themeContext/utils/theme.js +5 -0
- package/lib/commonjs/contexts/themeContext/utils/theme.js.map +1 -1
- package/lib/commonjs/hooks/index.js +11 -0
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/usePendingAttachmentUpload.js +106 -0
- package/lib/commonjs/hooks/usePendingAttachmentUpload.js.map +1 -0
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/middlewares/attachments.js +2 -0
- package/lib/commonjs/middlewares/attachments.js.map +1 -1
- package/lib/commonjs/native.js +8 -1
- package/lib/commonjs/native.js.map +1 -1
- package/lib/commonjs/nativeMultipartUpload.js +237 -0
- package/lib/commonjs/nativeMultipartUpload.js.map +1 -0
- package/lib/commonjs/types/types.js.map +1 -1
- package/lib/commonjs/utils/installNativeMultipartAdapter.js +223 -0
- package/lib/commonjs/utils/installNativeMultipartAdapter.js.map +1 -0
- package/lib/commonjs/utils/utils.js +2 -2
- package/lib/commonjs/utils/utils.js.map +1 -1
- package/lib/commonjs/version.json +1 -1
- package/lib/module/components/Attachment/Attachment.js +38 -10
- package/lib/module/components/Attachment/Attachment.js.map +1 -1
- package/lib/module/components/Attachment/AttachmentFileUploadProgressIndicator.js +97 -0
- package/lib/module/components/Attachment/AttachmentFileUploadProgressIndicator.js.map +1 -0
- package/lib/module/components/Attachment/AttachmentUploadIndicator.js +97 -0
- package/lib/module/components/Attachment/AttachmentUploadIndicator.js.map +1 -0
- package/lib/module/components/Attachment/CircularProgressIndicator.js +141 -0
- package/lib/module/components/Attachment/CircularProgressIndicator.js.map +1 -0
- package/lib/module/components/Attachment/FileAttachment.js +18 -4
- package/lib/module/components/Attachment/FileAttachment.js.map +1 -1
- package/lib/module/components/Attachment/Gallery.js +7 -1
- package/lib/module/components/Attachment/Gallery.js.map +1 -1
- package/lib/module/components/Attachment/MediaUploadProgressOverlay.js +59 -0
- package/lib/module/components/Attachment/MediaUploadProgressOverlay.js.map +1 -0
- package/lib/module/components/Attachment/VideoThumbnail.js +18 -8
- package/lib/module/components/Attachment/VideoThumbnail.js.map +1 -1
- package/lib/module/components/Attachment/utils/buildGallery/buildThumbnail.js +2 -0
- package/lib/module/components/Attachment/utils/buildGallery/buildThumbnail.js.map +1 -1
- package/lib/module/components/Channel/Channel.js +94 -92
- package/lib/module/components/Channel/Channel.js.map +1 -1
- package/lib/module/components/Chat/Chat.js +10 -1
- package/lib/module/components/Chat/Chat.js.map +1 -1
- package/lib/module/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.js +42 -54
- package/lib/module/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.js.map +1 -1
- package/lib/module/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.js +10 -6
- package/lib/module/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.js.map +1 -1
- package/lib/module/components/MessageInput/components/AttachmentPreview/FileAttachmentUploadPreview.js +12 -6
- package/lib/module/components/MessageInput/components/AttachmentPreview/FileAttachmentUploadPreview.js.map +1 -1
- package/lib/module/components/MessageInput/components/AttachmentPreview/ImageAttachmentUploadPreview.js +13 -7
- package/lib/module/components/MessageInput/components/AttachmentPreview/ImageAttachmentUploadPreview.js.map +1 -1
- package/lib/module/components/MessageInput/components/AttachmentPreview/VideoAttachmentUploadPreview.js +6 -2
- package/lib/module/components/MessageInput/components/AttachmentPreview/VideoAttachmentUploadPreview.js.map +1 -1
- package/lib/module/components/index.js +33 -0
- package/lib/module/components/index.js.map +1 -1
- package/lib/module/contexts/componentsContext/defaultComponents.js +6 -0
- package/lib/module/contexts/componentsContext/defaultComponents.js.map +1 -1
- package/lib/module/contexts/messageInputContext/hooks/useCreateMessageInputContext.js +3 -1
- package/lib/module/contexts/messageInputContext/hooks/useCreateMessageInputContext.js.map +1 -1
- package/lib/module/contexts/messageInputContext/hooks/useMessageComposerHasSendableData.js +4 -1
- package/lib/module/contexts/messageInputContext/hooks/useMessageComposerHasSendableData.js.map +1 -1
- package/lib/module/contexts/themeContext/utils/theme.js +5 -0
- package/lib/module/contexts/themeContext/utils/theme.js.map +1 -1
- package/lib/module/hooks/index.js +11 -0
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/usePendingAttachmentUpload.js +106 -0
- package/lib/module/hooks/usePendingAttachmentUpload.js.map +1 -0
- package/lib/module/index.js +12 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/middlewares/attachments.js +2 -0
- package/lib/module/middlewares/attachments.js.map +1 -1
- package/lib/module/native.js +8 -1
- package/lib/module/native.js.map +1 -1
- package/lib/module/nativeMultipartUpload.js +237 -0
- package/lib/module/nativeMultipartUpload.js.map +1 -0
- package/lib/module/types/types.js.map +1 -1
- package/lib/module/utils/installNativeMultipartAdapter.js +223 -0
- package/lib/module/utils/installNativeMultipartAdapter.js.map +1 -0
- package/lib/module/utils/utils.js +2 -2
- package/lib/module/utils/utils.js.map +1 -1
- package/lib/module/version.json +1 -1
- package/lib/typescript/components/Attachment/Attachment.d.ts.map +1 -1
- package/lib/typescript/components/Attachment/AttachmentFileUploadProgressIndicator.d.ts +14 -0
- package/lib/typescript/components/Attachment/AttachmentFileUploadProgressIndicator.d.ts.map +1 -0
- package/lib/typescript/components/Attachment/AttachmentUploadIndicator.d.ts +18 -0
- package/lib/typescript/components/Attachment/AttachmentUploadIndicator.d.ts.map +1 -0
- package/lib/typescript/components/Attachment/CircularProgressIndicator.d.ts +18 -0
- package/lib/typescript/components/Attachment/CircularProgressIndicator.d.ts.map +1 -0
- package/lib/typescript/components/Attachment/FileAttachment.d.ts.map +1 -1
- package/lib/typescript/components/Attachment/Gallery.d.ts.map +1 -1
- package/lib/typescript/components/Attachment/MediaUploadProgressOverlay.d.ts +12 -0
- package/lib/typescript/components/Attachment/MediaUploadProgressOverlay.d.ts.map +1 -0
- package/lib/typescript/components/Attachment/VideoThumbnail.d.ts +4 -0
- package/lib/typescript/components/Attachment/VideoThumbnail.d.ts.map +1 -1
- package/lib/typescript/components/Attachment/utils/buildGallery/buildThumbnail.d.ts.map +1 -1
- package/lib/typescript/components/Attachment/utils/buildGallery/types.d.ts +2 -0
- package/lib/typescript/components/Attachment/utils/buildGallery/types.d.ts.map +1 -1
- package/lib/typescript/components/Channel/Channel.d.ts.map +1 -1
- package/lib/typescript/components/Chat/Chat.d.ts +10 -0
- package/lib/typescript/components/Chat/Chat.d.ts.map +1 -1
- package/lib/typescript/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.d.ts +7 -2
- package/lib/typescript/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.d.ts.map +1 -1
- package/lib/typescript/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.d.ts.map +1 -1
- package/lib/typescript/components/MessageInput/components/AttachmentPreview/FileAttachmentUploadPreview.d.ts.map +1 -1
- package/lib/typescript/components/MessageInput/components/AttachmentPreview/ImageAttachmentUploadPreview.d.ts.map +1 -1
- package/lib/typescript/components/MessageInput/components/AttachmentPreview/VideoAttachmentUploadPreview.d.ts.map +1 -1
- package/lib/typescript/components/index.d.ts +3 -0
- package/lib/typescript/components/index.d.ts.map +1 -1
- package/lib/typescript/contexts/componentsContext/defaultComponents.d.ts +5 -2
- package/lib/typescript/contexts/componentsContext/defaultComponents.d.ts.map +1 -1
- package/lib/typescript/contexts/messageInputContext/hooks/useCreateMessageInputContext.d.ts +1 -1
- package/lib/typescript/contexts/messageInputContext/hooks/useCreateMessageInputContext.d.ts.map +1 -1
- package/lib/typescript/contexts/messageInputContext/hooks/useMessageComposerHasSendableData.d.ts.map +1 -1
- package/lib/typescript/contexts/themeContext/ThemeContext.d.ts +5 -0
- package/lib/typescript/contexts/themeContext/ThemeContext.d.ts.map +1 -1
- package/lib/typescript/contexts/themeContext/utils/theme.d.ts +5 -0
- package/lib/typescript/contexts/themeContext/utils/theme.d.ts.map +1 -1
- package/lib/typescript/hooks/index.d.ts +1 -0
- package/lib/typescript/hooks/index.d.ts.map +1 -1
- package/lib/typescript/hooks/usePendingAttachmentUpload.d.ts +14 -0
- package/lib/typescript/hooks/usePendingAttachmentUpload.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/middlewares/attachments.d.ts.map +1 -1
- package/lib/typescript/native.d.ts +5 -2
- package/lib/typescript/native.d.ts.map +1 -1
- package/lib/typescript/nativeMultipartUpload.d.ts +98 -0
- package/lib/typescript/nativeMultipartUpload.d.ts.map +1 -0
- package/lib/typescript/types/types.d.ts +2 -0
- package/lib/typescript/types/types.d.ts.map +1 -1
- package/lib/typescript/utils/installNativeMultipartAdapter.d.ts +8 -0
- package/lib/typescript/utils/installNativeMultipartAdapter.d.ts.map +1 -0
- package/lib/typescript/utils/utils.d.ts +1 -1
- package/lib/typescript/utils/utils.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/nativeMultipartUpload.test.ts +267 -0
- package/src/components/Attachment/Attachment.tsx +54 -5
- package/src/components/Attachment/AttachmentFileUploadProgressIndicator.tsx +108 -0
- package/src/components/Attachment/AttachmentUploadIndicator.tsx +112 -0
- package/src/components/Attachment/CircularProgressIndicator.tsx +161 -0
- package/src/components/Attachment/FileAttachment.tsx +22 -6
- package/src/components/Attachment/Gallery.tsx +9 -1
- package/src/components/Attachment/MediaUploadProgressOverlay.tsx +77 -0
- package/src/components/Attachment/VideoThumbnail.tsx +17 -9
- package/src/components/Attachment/__tests__/Attachment.test.tsx +63 -16
- package/src/components/Attachment/utils/buildGallery/buildThumbnail.ts +3 -0
- package/src/components/Attachment/utils/buildGallery/types.ts +2 -0
- package/src/components/Channel/Channel.tsx +65 -61
- package/src/components/Chat/Chat.tsx +20 -0
- package/src/components/MessageInput/__tests__/AttachmentUploadPreviewList.test.tsx +51 -6
- package/src/components/MessageInput/__tests__/AudioAttachmentUploadPreview.test.tsx +31 -1
- package/src/components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator.tsx +30 -44
- package/src/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.tsx +11 -5
- package/src/components/MessageInput/components/AttachmentPreview/FileAttachmentUploadPreview.tsx +20 -5
- package/src/components/MessageInput/components/AttachmentPreview/ImageAttachmentUploadPreview.tsx +20 -9
- package/src/components/MessageInput/components/AttachmentPreview/VideoAttachmentUploadPreview.tsx +7 -1
- package/src/components/index.ts +3 -0
- package/src/contexts/componentsContext/defaultComponents.ts +6 -0
- package/src/contexts/messageInputContext/hooks/useCreateMessageInputContext.ts +3 -1
- package/src/contexts/messageInputContext/hooks/useMessageComposerHasSendableData.ts +5 -1
- package/src/contexts/themeContext/utils/theme.ts +10 -0
- package/src/hooks/__tests__/usePendingAttachmentUpload.test.tsx +106 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/usePendingAttachmentUpload.ts +131 -0
- package/src/index.ts +1 -0
- package/src/middlewares/attachments.ts +2 -0
- package/src/native.ts +28 -0
- package/src/nativeMultipartUpload.ts +384 -0
- package/src/types/types.ts +2 -0
- package/src/utils/__tests__/installNativeMultipartAdapter.test.ts +437 -0
- package/src/utils/installNativeMultipartAdapter.ts +302 -0
- package/src/utils/utils.ts +3 -3
- package/src/version.json +1 -1
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import type { AxiosAdapter, AxiosProgressEvent, InternalAxiosRequestConfig } from 'axios';
|
|
3
|
+
import type { StreamChat } from 'stream-chat';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
isNativeMultipartUploadAvailable,
|
|
7
|
+
NativeHandlers,
|
|
8
|
+
NativeMultipartUploadProgressConfig,
|
|
9
|
+
NativeMultipartUploadRequest,
|
|
10
|
+
} from '../native';
|
|
11
|
+
|
|
12
|
+
type FormDataPartValue =
|
|
13
|
+
| string
|
|
14
|
+
| {
|
|
15
|
+
contentType?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
type?: string;
|
|
18
|
+
uri: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type NativeMultipartAxiosRequestConfig = InternalAxiosRequestConfig & {
|
|
22
|
+
onUploadProgress?: (event: AxiosProgressEvent) => void;
|
|
23
|
+
uploadProgressOptions?: NativeMultipartUploadProgressConfig;
|
|
24
|
+
uploadProgress?: (event: AxiosProgressEvent) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type ResolvableAxiosAdapter = Parameters<typeof axios.getAdapter>[0];
|
|
28
|
+
|
|
29
|
+
const DEFAULT_COMPLETION_PROGRESS_CAP = 90;
|
|
30
|
+
|
|
31
|
+
const installedAdapters = new WeakSet<StreamChat>();
|
|
32
|
+
|
|
33
|
+
const getFormDataEntries = (data: unknown): [string, FormDataPartValue][] | null => {
|
|
34
|
+
if (!data || typeof data !== 'object') {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if ('entries' in data && typeof data.entries === 'function') {
|
|
39
|
+
return Array.from(data.entries()) as [string, FormDataPartValue][];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const parts = Reflect.get(data, '_parts');
|
|
43
|
+
|
|
44
|
+
if (Array.isArray(parts)) {
|
|
45
|
+
return parts as [string, FormDataPartValue][];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const normalizeHeaders = (
|
|
52
|
+
headers: NativeMultipartAxiosRequestConfig['headers'],
|
|
53
|
+
): Record<string, string> => {
|
|
54
|
+
const rawHeaders = headers?.toJSON() ?? {};
|
|
55
|
+
const normalizedHeaders: Record<string, string> = {};
|
|
56
|
+
|
|
57
|
+
Object.entries(rawHeaders ?? {}).forEach(([key, value]) => {
|
|
58
|
+
if (value == null) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
normalizedHeaders[key] = Array.isArray(value) ? value.join(', ') : String(value);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return normalizedHeaders;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const getFileNameFromUri = (uri: string) => uri.split('/').filter(Boolean).pop() || 'file';
|
|
69
|
+
|
|
70
|
+
const getNativeProgressOptions = (
|
|
71
|
+
progress?: NativeMultipartUploadProgressConfig,
|
|
72
|
+
): NativeMultipartUploadProgressConfig | undefined => {
|
|
73
|
+
if (!progress) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const nativeProgressOptions = { ...progress };
|
|
78
|
+
delete nativeProgressOptions.completionProgressCap;
|
|
79
|
+
|
|
80
|
+
return Object.keys(nativeProgressOptions).length ? nativeProgressOptions : undefined;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const createNativeMultipartRequest = (
|
|
84
|
+
client: StreamChat,
|
|
85
|
+
config: NativeMultipartAxiosRequestConfig,
|
|
86
|
+
): NativeMultipartUploadRequest | null => {
|
|
87
|
+
const entries = getFormDataEntries(config.data);
|
|
88
|
+
|
|
89
|
+
if (!entries) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const parts: NativeMultipartUploadRequest['parts'] = [];
|
|
94
|
+
|
|
95
|
+
for (const [fieldName, value] of entries) {
|
|
96
|
+
if (typeof value === 'string') {
|
|
97
|
+
parts.push({
|
|
98
|
+
fieldName,
|
|
99
|
+
kind: 'text',
|
|
100
|
+
value,
|
|
101
|
+
});
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (value && typeof value === 'object' && 'uri' in value && typeof value.uri === 'string') {
|
|
106
|
+
parts.push({
|
|
107
|
+
fieldName,
|
|
108
|
+
fileName: value.name || getFileNameFromUri(value.uri),
|
|
109
|
+
kind: 'file',
|
|
110
|
+
mimeType: value.type || value.contentType,
|
|
111
|
+
uri: value.uri,
|
|
112
|
+
});
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!parts.some((part) => part.kind === 'file')) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
headers: normalizeHeaders(config.headers),
|
|
125
|
+
method: (config.method || 'POST').toUpperCase(),
|
|
126
|
+
parts,
|
|
127
|
+
progress: getNativeProgressOptions(config.uploadProgressOptions),
|
|
128
|
+
signal: config.signal,
|
|
129
|
+
timeoutMs: config.timeout,
|
|
130
|
+
url: client.axiosInstance.getUri(config),
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const toFiniteNumber = (value: unknown) => {
|
|
135
|
+
if (typeof value === 'number') {
|
|
136
|
+
return Number.isFinite(value) ? value : undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (typeof value === 'string' && value.trim()) {
|
|
140
|
+
const parsed = Number(value);
|
|
141
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return undefined;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const getCompletionProgressCap = (config: NativeMultipartAxiosRequestConfig) => {
|
|
148
|
+
const cap = toFiniteNumber(config.uploadProgressOptions?.completionProgressCap);
|
|
149
|
+
if (cap === undefined) {
|
|
150
|
+
return DEFAULT_COMPLETION_PROGRESS_CAP;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return Math.min(100, Math.max(0, cap));
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const getDisplayLoaded = ({
|
|
157
|
+
completionProgressCap,
|
|
158
|
+
loaded,
|
|
159
|
+
total,
|
|
160
|
+
}: {
|
|
161
|
+
completionProgressCap: number;
|
|
162
|
+
loaded: number;
|
|
163
|
+
total?: number;
|
|
164
|
+
}) => {
|
|
165
|
+
if (typeof total !== 'number' || total <= 0) {
|
|
166
|
+
return loaded;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return Math.min(loaded, total * (completionProgressCap / 100));
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const getUploadProgressCallbacks = (config: NativeMultipartAxiosRequestConfig) => {
|
|
173
|
+
const callbacks = [config.onUploadProgress, config.uploadProgress].filter(
|
|
174
|
+
(callback): callback is NonNullable<typeof config.onUploadProgress> =>
|
|
175
|
+
typeof callback === 'function',
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
return Array.from(new Set(callbacks));
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const createUploadProgressEvent = ({
|
|
182
|
+
bytes,
|
|
183
|
+
loaded,
|
|
184
|
+
total,
|
|
185
|
+
}: {
|
|
186
|
+
bytes: unknown;
|
|
187
|
+
loaded: unknown;
|
|
188
|
+
total?: unknown;
|
|
189
|
+
}) => {
|
|
190
|
+
const normalizedBytes = toFiniteNumber(bytes) ?? 0;
|
|
191
|
+
const normalizedLoaded = toFiniteNumber(loaded) ?? 0;
|
|
192
|
+
const normalizedTotal = toFiniteNumber(total);
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
bytes: normalizedBytes,
|
|
196
|
+
download: false,
|
|
197
|
+
event: undefined,
|
|
198
|
+
lengthComputable: typeof normalizedTotal === 'number' && normalizedTotal > 0,
|
|
199
|
+
loaded: normalizedLoaded,
|
|
200
|
+
progress:
|
|
201
|
+
typeof normalizedTotal === 'number' && normalizedTotal > 0
|
|
202
|
+
? normalizedLoaded / normalizedTotal
|
|
203
|
+
: undefined,
|
|
204
|
+
total: normalizedTotal,
|
|
205
|
+
upload: true,
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const nativeMultipartAxiosAdapter = async (
|
|
210
|
+
request: NativeMultipartUploadRequest,
|
|
211
|
+
config: NativeMultipartAxiosRequestConfig,
|
|
212
|
+
) => {
|
|
213
|
+
const uploadProgressCallbacks = getUploadProgressCallbacks(config);
|
|
214
|
+
const completionProgressCap = getCompletionProgressCap(config);
|
|
215
|
+
let lastLoaded = 0;
|
|
216
|
+
|
|
217
|
+
const response = await NativeHandlers.multipartUpload({
|
|
218
|
+
...request,
|
|
219
|
+
onProgress: uploadProgressCallbacks.length
|
|
220
|
+
? ({ loaded, total }) => {
|
|
221
|
+
const normalizedLoaded = toFiniteNumber(loaded) ?? 0;
|
|
222
|
+
const normalizedTotal = toFiniteNumber(total);
|
|
223
|
+
const displayLoaded = getDisplayLoaded({
|
|
224
|
+
completionProgressCap,
|
|
225
|
+
loaded: normalizedLoaded,
|
|
226
|
+
total: normalizedTotal,
|
|
227
|
+
});
|
|
228
|
+
const event = createUploadProgressEvent({
|
|
229
|
+
bytes: Math.max(0, displayLoaded - lastLoaded),
|
|
230
|
+
loaded: displayLoaded,
|
|
231
|
+
total: normalizedTotal,
|
|
232
|
+
});
|
|
233
|
+
lastLoaded = displayLoaded;
|
|
234
|
+
uploadProgressCallbacks.forEach((callback) => callback(event));
|
|
235
|
+
}
|
|
236
|
+
: undefined,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
if (!response) {
|
|
240
|
+
throw new Error('Native multipart upload did not return a response');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
config,
|
|
245
|
+
data: response.body,
|
|
246
|
+
headers: response.headers ?? {},
|
|
247
|
+
request: null,
|
|
248
|
+
status: response.status,
|
|
249
|
+
statusText: response.statusText ?? '',
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const resolveAxiosAdapter = (adapter: ResolvableAxiosAdapter): AxiosAdapter =>
|
|
254
|
+
axios.getAdapter(adapter);
|
|
255
|
+
|
|
256
|
+
const createNativeMultipartAwareAdapter = (
|
|
257
|
+
client: StreamChat,
|
|
258
|
+
fallbackAdapter: ResolvableAxiosAdapter,
|
|
259
|
+
): AxiosAdapter => {
|
|
260
|
+
const resolvedFallbackAdapter = resolveAxiosAdapter(fallbackAdapter);
|
|
261
|
+
|
|
262
|
+
return (config) => {
|
|
263
|
+
const nativeMultipartRequest = createNativeMultipartRequest(
|
|
264
|
+
client,
|
|
265
|
+
config as NativeMultipartAxiosRequestConfig,
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
if (!nativeMultipartRequest) {
|
|
269
|
+
return resolvedFallbackAdapter(config);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return nativeMultipartAxiosAdapter(nativeMultipartRequest, config);
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export const wrapAxiosAdapterWithNativeMultipart = (
|
|
277
|
+
client: StreamChat,
|
|
278
|
+
fallbackAdapter: ResolvableAxiosAdapter,
|
|
279
|
+
): AxiosAdapter => {
|
|
280
|
+
if (!isNativeMultipartUploadAvailable()) {
|
|
281
|
+
return resolveAxiosAdapter(fallbackAdapter);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return createNativeMultipartAwareAdapter(client, fallbackAdapter);
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
export const installNativeMultipartAdapter = (client: StreamChat) => {
|
|
288
|
+
if (!isNativeMultipartUploadAvailable()) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (installedAdapters.has(client)) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const previousAdapter = client.axiosInstance.defaults.adapter;
|
|
297
|
+
client.axiosInstance.defaults.adapter = wrapAxiosAdapterWithNativeMultipart(
|
|
298
|
+
client,
|
|
299
|
+
previousAdapter,
|
|
300
|
+
);
|
|
301
|
+
installedAdapters.add(client);
|
|
302
|
+
};
|
package/src/utils/utils.ts
CHANGED
|
@@ -57,14 +57,14 @@ type IndicatorStatesMap = Record<AttachmentLoadingState, Progress | undefined>;
|
|
|
57
57
|
|
|
58
58
|
export const getIndicatorTypeForFileState = (
|
|
59
59
|
fileState: AttachmentLoadingState,
|
|
60
|
-
|
|
60
|
+
allowSendBeforeAttachmentsUpload: boolean,
|
|
61
61
|
): Progress | undefined => {
|
|
62
62
|
const indicatorMap: IndicatorStatesMap = {
|
|
63
|
-
[FileState.UPLOADING]:
|
|
63
|
+
[FileState.UPLOADING]: allowSendBeforeAttachmentsUpload
|
|
64
64
|
? ProgressIndicatorTypes.INACTIVE
|
|
65
65
|
: ProgressIndicatorTypes.IN_PROGRESS,
|
|
66
66
|
[FileState.BLOCKED]: ProgressIndicatorTypes.NOT_SUPPORTED,
|
|
67
|
-
[FileState.FAILED]:
|
|
67
|
+
[FileState.FAILED]: allowSendBeforeAttachmentsUpload
|
|
68
68
|
? ProgressIndicatorTypes.INACTIVE
|
|
69
69
|
: ProgressIndicatorTypes.RETRY,
|
|
70
70
|
[FileState.PENDING]: ProgressIndicatorTypes.PENDING,
|
package/src/version.json
CHANGED