Skip to content

Commit 1d3c387

Browse files
authored
chore: Fix onFileUploaded UI update in thread. (#813)
1 parent 0f8b6de commit 1d3c387

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

src/modules/Channel/context/hooks/useSendMultipleFilesMessage.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { MultipleFilesMessage } from '@sendbird/chat/message';
66
import type { Logger } from '../../../../lib/SendbirdState';
77
import type { Nullable } from '../../../../types';
88
import PUBSUB_TOPICS from '../../../../lib/pubSub/topics';
9-
import { scrollIntoLast } from '../utils';
9+
import { scrollIntoLast as scrollIntoLastForChannel } from '../utils';
1010
import { SendableMessageType } from '../../../../utils';
1111
import { PublishingModuleType } from '../../../internalInterfaces';
12+
import { scrollIntoLast as scrollIntoLastForThread } from '../../../Thread/context/utils';
1213

1314
export type OnBeforeSendMFMType = (
1415
files: Array<File>,
@@ -107,10 +108,15 @@ export const useSendMultipleFilesMessage = ({
107108
channel: currentChannel,
108109
publishingModules,
109110
});
110-
if (scrollRef) {
111-
// We need this delay because rendering MFM takes time due to large image files.
112-
setTimeout(() => scrollIntoLast(0, scrollRef), 100);
113-
}
111+
// We need this delay because rendering MFM takes time due to large image files.
112+
setTimeout(() => {
113+
if (scrollRef && publishingModules.includes(PublishingModuleType.CHANNEL)) {
114+
scrollIntoLastForChannel(0, scrollRef);
115+
}
116+
if (publishingModules.includes(PublishingModuleType.THREAD)) {
117+
scrollIntoLastForThread(0);
118+
}
119+
}, 100);
114120
})
115121
.onFailed((error, failedMessage: MultipleFilesMessage) => {
116122
logger.error('Channel: Sending MFM failed.', { error, failedMessage });
@@ -128,10 +134,15 @@ export const useSendMultipleFilesMessage = ({
128134
message: succeededMessage,
129135
publishingModules,
130136
});
131-
if (scrollRef) {
132-
// We need this delay because rendering MFM takes time due to large image files.
133-
setTimeout(() => scrollIntoLast(0, scrollRef), 100);
134-
}
137+
// We need this delay because rendering MFM takes time due to large image files.
138+
setTimeout(() => {
139+
if (scrollRef && publishingModules.includes(PublishingModuleType.CHANNEL)) {
140+
scrollIntoLastForChannel(0, scrollRef);
141+
}
142+
if (publishingModules.includes(PublishingModuleType.THREAD)) {
143+
scrollIntoLastForThread(0);
144+
}
145+
}, 100);
135146
resolve(succeededMessage);
136147
});
137148
} catch (error) {

src/modules/Thread/context/dux/actionTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export enum ThreadContextActionTypes {
3232
ON_MESSAGE_UPDATED = 'ON_MESSAGE_UPDATED',
3333
ON_MESSAGE_DELETED = 'ON_MESSAGE_DELETED',
3434
ON_REACTION_UPDATED = 'ON_REACTION_UPDATED',
35+
ON_FILE_INFO_UPLOADED = 'ON_FILE_INFO_UPLOADED',
3536
// event handlers - user status change
3637
ON_USER_MUTED = 'ON_USER_MUTED',
3738
ON_USER_UNMUTED = 'ON_USER_UNMUTED',

src/modules/Thread/context/dux/reducer.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GroupChannel } from '@sendbird/chat/groupChannel';
2-
import { ReactionEvent, UserMessage } from '@sendbird/chat/message';
2+
import { MultipleFilesMessage, ReactionEvent, UserMessage } from '@sendbird/chat/message';
33
import { NEXT_THREADS_FETCH_SIZE, PREV_THREADS_FETCH_SIZE } from '../../consts';
44
import { ChannelStateTypes, ParentMessageStateTypes, ThreadListStateTypes } from '../../types';
55
import { compareIds } from '../utils';
@@ -351,8 +351,37 @@ export default function reducer(
351351
...state,
352352
};
353353
}
354+
case actionTypes.ON_FILE_INFO_UPLOADED: {
355+
const { channelUrl, requestId, index, uploadableFileInfo, error } = action.payload;
356+
if (!compareIds(channelUrl, state.currentChannel?.url)) {
357+
return state;
358+
}
359+
/**
360+
* We don't have to do anything here because
361+
* onFailed() will be called so handle error there instead.
362+
*/
363+
if (error) return state;
364+
const { localThreadMessages } = state;
365+
const messageToUpdate = localThreadMessages.find((message) => compareIds(hasReqId(message) && message.reqId, requestId),
366+
);
367+
const fileInfoList = (messageToUpdate as MultipleFilesMessage)
368+
.messageParams?.fileInfoList;
369+
if (Array.isArray(fileInfoList)) {
370+
fileInfoList[index] = uploadableFileInfo;
371+
}
372+
return {
373+
...state,
374+
localThreadMessages,
375+
};
376+
}
354377
default: {
355378
return state;
356379
}
357380
}
358381
}
382+
383+
function hasReqId<T extends object>(
384+
message: T,
385+
): message is T & { reqId: string } {
386+
return 'reqId' in message;
387+
}

src/modules/Thread/context/hooks/useHandleThreadPubsubEvents.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useEffect } from 'react';
22
import { GroupChannel } from '@sendbird/chat/groupChannel';
33
import { CustomUseReducerDispatcher, Logger } from '../../../../lib/SendbirdState';
4-
import topics from '../../../../lib/pubSub/topics';
4+
import topics, { PUBSUB_TOPICS } from '../../../../lib/pubSub/topics';
55
import { scrollIntoLast } from '../utils';
66
import { ThreadContextActionTypes } from '../dux/actionTypes';
77
import { SendableMessageType } from '../../../../utils';
8-
import { PublishingModuleType } from './useSendMultipleFilesMessage';
8+
import { FileUploadedPayload, PublishingModuleType } from './useSendMultipleFilesMessage';
9+
import * as channelActions from '../../../Channel/context/dux/actionTypes';
910

1011
interface DynamicProps {
1112
sdkInit: boolean;
@@ -32,7 +33,6 @@ export default function useHandleThreadPubsubEvents({
3233
if (!pubSub || !pubSub.subscribe) {
3334
return subscriber;
3435
}
35-
// TODO: subscribe ON_FILE_INFO_UPLOADED
3636
subscriber.set(topics.SEND_MESSAGE_START, pubSub.subscribe(topics.SEND_MESSAGE_START, (props) => {
3737
const {
3838
channel,
@@ -60,6 +60,21 @@ export default function useHandleThreadPubsubEvents({
6060
}
6161
scrollIntoLast?.();
6262
}));
63+
subscriber.set(PUBSUB_TOPICS.ON_FILE_INFO_UPLOADED, pubSub.subscribe(PUBSUB_TOPICS.ON_FILE_INFO_UPLOADED, (props) => {
64+
const {
65+
response,
66+
publishingModules,
67+
} = props as { response: FileUploadedPayload, publishingModules: PublishingModuleType[] };
68+
if (
69+
currentChannel?.url === response.channelUrl
70+
&& publishingModules.includes(PublishingModuleType.THREAD)
71+
) {
72+
threadDispatcher({
73+
type: channelActions.ON_FILE_INFO_UPLOADED,
74+
payload: response,
75+
});
76+
}
77+
}));
6378
subscriber.set(topics.SEND_USER_MESSAGE, pubSub.subscribe(topics.SEND_USER_MESSAGE, (props) => {
6479
const {
6580
channel,

0 commit comments

Comments
 (0)