Skip to content

Commit 065ef21

Browse files
fix: some miscellaneous cleanup
1 parent eb46ef5 commit 065ef21

File tree

6 files changed

+38
-60
lines changed

6 files changed

+38
-60
lines changed

package/src/components/Attachment/GalleryImage.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type GalleryImageWithContextProps<
1313
export const GalleryImageWithContext: React.FC<GalleryImageWithContextProps> = (props) => {
1414
const { ImageComponent = Image, uri, ...rest } = props;
1515

16+
// Caching image components such as FastImage will not work with local images.
17+
// This for the case of local uris, we use the default Image component.
1618
if (!isLocalUrl(uri)) {
1719
return (
1820
<ImageComponent

package/src/components/ChannelList/hooks/usePaginatedChannels.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useEffect, useMemo, useRef, useState } from 'react';
22

33
import type { Channel, ChannelFilters, ChannelOptions, ChannelSort } from 'stream-chat';
44

5+
import { useActiveChannelsRefContext } from '../../../contexts/activeChannelsRefContext/ActiveChannelsRefContext';
6+
57
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
68
import { useIsMountedRef } from '../../../hooks/useIsMountedRef';
79

@@ -48,7 +50,7 @@ export const usePaginatedChannels = <
4850
const { client } = useChatContext<StreamChatGenerics>();
4951
const [channels, setChannels] = useState<Channel<StreamChatGenerics>[] | null>(null);
5052
const [staticChannelsActive, setStaticChannelsActive] = useState<boolean>(false);
51-
53+
const activeChannels = useActiveChannelsRefContext();
5254
const [error, setError] = useState<Error>();
5355
const [hasNextPage, setHasNextPage] = useState(true);
5456
const lastRefresh = useRef(Date.now());
@@ -99,17 +101,17 @@ export const usePaginatedChannels = <
99101
};
100102

101103
try {
102-
const activeChannelIds = [];
104+
const activeChannelIds: string[] = [];
103105
for (const cid in client.activeChannels) {
104106
if (client.activeChannels[cid].id) {
107+
// @ts-ignore
105108
activeChannelIds.push(client.activeChannels[cid].id);
106109
}
107110
}
108111

109112
// TODO: Think about the implications of this.
110113
const channelQueryResponse = await client.queryChannels(filters, sort, newOptions, {
111-
// @ts-ignore
112-
skipInitialization: activeChannelIds,
114+
skipInitialization: enableOfflineSupport ? activeChannelIds : activeChannels.current,
113115
});
114116
if (isQueryStale() || !isMountedRef.current) {
115117
return;

package/src/components/MessageInput/FileUploadPreview.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ type FileUploadPreviewPropsWithContext<
128128
'fileUploads' | 'removeFile' | 'uploadFile' | 'setFileUploads'
129129
> &
130130
Pick<MessagesContextValue<StreamChatGenerics>, 'AudioAttachment' | 'FileAttachmentIcon'> &
131-
Pick<ChatContextValue<StreamChatGenerics>, 'isOnline' | 'enableOfflineSupport'>;
131+
Pick<ChatContextValue<StreamChatGenerics>, 'enableOfflineSupport'>;
132132

133133
const FileUploadPreviewWithContext = <
134134
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -140,7 +140,6 @@ const FileUploadPreviewWithContext = <
140140
enableOfflineSupport,
141141
FileAttachmentIcon,
142142
fileUploads,
143-
isOnline,
144143
removeFile,
145144
setFileUploads,
146145
uploadFile,
@@ -215,7 +214,7 @@ const FileUploadPreviewWithContext = <
215214
} = useTheme();
216215

217216
const renderItem = ({ index, item }: { index: number; item: FileUpload }) => {
218-
const indicatorType = getIndicatorTypeForFileState(item.state, isOnline, enableOfflineSupport);
217+
const indicatorType = getIndicatorTypeForFileState(item.state, enableOfflineSupport);
219218

220219
const lastIndexOfDot = item.file.name.lastIndexOf('.');
221220

@@ -393,7 +392,7 @@ export const FileUploadPreview = <
393392
>(
394393
props: FileUploadPreviewProps<StreamChatGenerics>,
395394
) => {
396-
const { enableOfflineSupport, isOnline } = useChatContext<StreamChatGenerics>();
395+
const { enableOfflineSupport } = useChatContext<StreamChatGenerics>();
397396
const { fileUploads, removeFile, setFileUploads, uploadFile } =
398397
useMessageInputContext<StreamChatGenerics>();
399398
const { AudioAttachment, FileAttachmentIcon } = useMessagesContext<StreamChatGenerics>();
@@ -408,7 +407,7 @@ export const FileUploadPreview = <
408407
setFileUploads,
409408
uploadFile,
410409
}}
411-
{...{ enableOfflineSupport, isOnline }}
410+
{...{ enableOfflineSupport }}
412411
{...props}
413412
/>
414413
);

package/src/components/MessageInput/ImageUploadPreview.tsx

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type ImageUploadPreviewPropsWithContext<
8080
MessageInputContextValue<StreamChatGenerics>,
8181
'imageUploads' | 'removeImage' | 'uploadImage'
8282
> &
83-
Pick<ChatContextValue<StreamChatGenerics>, 'isOnline' | 'enableOfflineSupport'>;
83+
Pick<ChatContextValue<StreamChatGenerics>, 'enableOfflineSupport'>;
8484

8585
export type ImageUploadPreviewProps<
8686
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -93,7 +93,7 @@ const ImageUploadPreviewWithContext = <
9393
>(
9494
props: ImageUploadPreviewPropsWithContext<StreamChatGenerics>,
9595
) => {
96-
const { enableOfflineSupport, imageUploads, isOnline, removeImage, uploadImage } = props;
96+
const { enableOfflineSupport, imageUploads, removeImage, uploadImage } = props;
9797

9898
const {
9999
theme: {
@@ -131,7 +131,7 @@ const ImageUploadPreviewWithContext = <
131131
};
132132

133133
const renderItem = ({ index, item }: ImageUploadPreviewItem) => {
134-
const indicatorType = getIndicatorTypeForFileState(item.state, isOnline, enableOfflineSupport);
134+
const indicatorType = getIndicatorTypeForFileState(item.state, enableOfflineSupport);
135135
const itemMarginForIndex = index === imageUploads.length - 1 ? { marginRight: 8 } : {};
136136

137137
return (
@@ -202,40 +202,15 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
202202
prevProps: ImageUploadPreviewPropsWithContext<StreamChatGenerics>,
203203
nextProps: ImageUploadPreviewPropsWithContext<StreamChatGenerics>,
204204
) => {
205-
const {
206-
enableOfflineSupport: prevEnableOfflineSupport,
207-
imageUploads: prevImageUploads,
208-
isOnline: prevIsOnline,
209-
} = prevProps;
210-
const {
211-
enableOfflineSupport: nextEnableOfflineSupport,
212-
imageUploads: nextImageUploads,
213-
isOnline: nextIsOnline,
214-
} = nextProps;
215-
216-
const isOnlineEqual = prevIsOnline === nextIsOnline;
217-
218-
if (!isOnlineEqual) {
219-
return false;
220-
}
221-
222-
const enableOfflineSupportEqual = prevEnableOfflineSupport === nextEnableOfflineSupport;
223-
224-
if (!enableOfflineSupportEqual) {
225-
return false;
226-
}
205+
const { imageUploads: prevImageUploads } = prevProps;
206+
const { imageUploads: nextImageUploads } = nextProps;
227207

228-
const imageUploadsEqual =
208+
return (
229209
prevImageUploads.length === nextImageUploads.length &&
230210
prevImageUploads.every(
231211
(prevImageUpload, index) => prevImageUpload.state === nextImageUploads[index].state,
232-
);
233-
234-
if (!imageUploadsEqual) {
235-
return false;
236-
}
237-
238-
return true;
212+
)
213+
);
239214
};
240215

241216
const MemoizedImageUploadPreviewWithContext = React.memo(
@@ -251,13 +226,13 @@ export const ImageUploadPreview = <
251226
>(
252227
props: ImageUploadPreviewProps<StreamChatGenerics>,
253228
) => {
254-
const { enableOfflineSupport, isOnline } = useChatContext<StreamChatGenerics>();
229+
const { enableOfflineSupport } = useChatContext<StreamChatGenerics>();
255230
const { imageUploads, removeImage, uploadImage } = useMessageInputContext<StreamChatGenerics>();
256231

257232
return (
258233
<MemoizedImageUploadPreviewWithContext
259234
{...{ imageUploads, removeImage, uploadImage }}
260-
{...{ enableOfflineSupport, isOnline }}
235+
{...{ enableOfflineSupport }}
261236
{...props}
262237
/>
263238
);

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -491,21 +491,20 @@ export const MessageInputProvider = <
491491
return true;
492492
}
493493

494-
for (const image of imageUploads) {
495-
if ((!image || image.state === FileState.UPLOAD_FAILED) && !enableOfflineSupport) {
496-
continue;
497-
}
498-
499-
if (image.state === FileState.UPLOADING) {
500-
// TODO: show error to user that they should wait until image is uploaded
501-
return false;
494+
const imagesAndFiles = [...imageUploads, ...fileUploads];
495+
if (enableOfflineSupport) {
496+
// Allow only if none of the attachments have unsupported status
497+
for (const file of imagesAndFiles) {
498+
if (file.state === FileState.NOT_SUPPORTED) {
499+
return false;
500+
}
502501
}
503502

504503
return true;
505504
}
506505

507-
for (const file of fileUploads) {
508-
if ((!file || file.state === FileState.UPLOAD_FAILED) && !enableOfflineSupport) {
506+
for (const file of imagesAndFiles) {
507+
if (!file || file.state === FileState.UPLOAD_FAILED) {
509508
continue;
510509
}
511510
if (file.state === FileState.UPLOADING) {

package/src/utils/utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ type IndicatorStatesMap = Record<ValueOf<typeof FileState>, Progress | null>;
6464

6565
export const getIndicatorTypeForFileState = (
6666
fileState: FileStateValue,
67-
isOnline: boolean,
6867
enableOfflineSupport: boolean,
6968
): Progress | null => {
7069
const indicatorMap: IndicatorStatesMap = {
71-
[FileState.UPLOADING]: ProgressIndicatorTypes.IN_PROGRESS,
72-
[FileState.UPLOAD_FAILED]:
73-
!isOnline && enableOfflineSupport
74-
? ProgressIndicatorTypes.INACTIVE
75-
: ProgressIndicatorTypes.RETRY,
70+
[FileState.UPLOADING]: enableOfflineSupport
71+
? ProgressIndicatorTypes.INACTIVE
72+
: ProgressIndicatorTypes.IN_PROGRESS,
73+
// If offline support is disabled, then there is no need
74+
[FileState.UPLOAD_FAILED]: enableOfflineSupport
75+
? ProgressIndicatorTypes.INACTIVE
76+
: ProgressIndicatorTypes.RETRY,
7677
[FileState.NOT_SUPPORTED]: ProgressIndicatorTypes.NOT_SUPPORTED,
7778
[FileState.UPLOADED]: ProgressIndicatorTypes.INACTIVE,
7879
[FileState.FINISHED]: ProgressIndicatorTypes.INACTIVE,

0 commit comments

Comments
 (0)