@@ -238,75 +238,79 @@ export const useDiscussionMessagesById = ({
238238 setIsBatchLoading ( true ) ;
239239 }
240240
241- DiscussionMessageService . subscribeToDiscussionMessagesByDiscussionId (
242- discussionId ,
243- lastVisible && lastVisible [ discussionId ] ,
244- async (
245- addedDiscussionMessages ,
246- modifiedDiscussionMessages ,
247- removedDiscussionMessages ,
248- lastVisibleDocument ,
249- ) => {
250- const updatedDiscussionMessages = [
251- ...addedDiscussionMessages ,
252- ...modifiedDiscussionMessages ,
253- ] ;
254- setLastVisible ( ( prevVisible ) => ( {
255- ...prevVisible ,
256- [ discussionId ] : lastVisibleDocument ,
257- } ) ) ;
258-
259- const hasLastVisibleDocument = ! ! lastVisibleDocument ?. data ( ) ;
260-
261- const discussionsWithText = await Promise . all (
262- updatedDiscussionMessages . map ( async ( discussionMessage ) => {
263- const isUserDiscussionMessage =
264- checkIsUserDiscussionMessage ( discussionMessage ) ;
265- const isSystemMessage =
266- checkIsSystemDiscussionMessage ( discussionMessage ) ;
267-
268- const parsedText = await getTextFromTextEditorString ( {
269- userId,
270- ownerId : isUserDiscussionMessage
271- ? discussionMessage . ownerId
272- : null ,
273- textEditorString : discussionMessage . text ,
274- users,
275- commonId : discussionMessage . commonId ,
276- systemMessage : isSystemMessage ? discussionMessage : undefined ,
277- getCommonPagePath,
278- getCommonPageAboutTabPath,
279- directParent,
280- onUserClick,
281- onFeedItemClick,
282- onInternalLinkClick,
283- } ) ;
284-
285- return {
286- ...discussionMessage ,
287- parsedText,
288- } ;
289- } ) ,
290- ) ;
291- if (
292- discussionsWithText . length < MESSAGES_NUMBER_IN_BATCH &&
293- ! hasLastVisibleDocument
294- ) {
295- setIsEndOfList ( ( prevIsEndOfList ) => ( {
296- ...prevIsEndOfList ,
297- [ discussionId ] : true ,
241+ try {
242+ DiscussionMessageService . subscribeToDiscussionMessagesByDiscussionId (
243+ discussionId ,
244+ lastVisible && lastVisible [ discussionId ] ,
245+ async (
246+ addedDiscussionMessages ,
247+ modifiedDiscussionMessages ,
248+ removedDiscussionMessages ,
249+ lastVisibleDocument ,
250+ ) => {
251+ const updatedDiscussionMessages = [
252+ ...addedDiscussionMessages ,
253+ ...modifiedDiscussionMessages ,
254+ ] ;
255+ setLastVisible ( ( prevVisible ) => ( {
256+ ...prevVisible ,
257+ [ discussionId ] : lastVisibleDocument ,
298258 } ) ) ;
299- }
300- dispatch (
301- cacheActions . updateDiscussionMessagesStateByDiscussionId ( {
302- discussionId,
303- removedDiscussionMessages,
304- updatedDiscussionMessages : discussionsWithText ,
305- } ) ,
306- ) ;
307- setIsBatchLoading ( false ) ;
308- } ,
309- ) ;
259+
260+ const hasLastVisibleDocument = ! ! lastVisibleDocument ?. data ( ) ;
261+
262+ const discussionsWithText = await Promise . all (
263+ updatedDiscussionMessages . map ( async ( discussionMessage ) => {
264+ const isUserDiscussionMessage =
265+ checkIsUserDiscussionMessage ( discussionMessage ) ;
266+ const isSystemMessage =
267+ checkIsSystemDiscussionMessage ( discussionMessage ) ;
268+
269+ const parsedText = await getTextFromTextEditorString ( {
270+ userId,
271+ ownerId : isUserDiscussionMessage
272+ ? discussionMessage . ownerId
273+ : null ,
274+ textEditorString : discussionMessage . text ,
275+ users,
276+ commonId : discussionMessage . commonId ,
277+ systemMessage : isSystemMessage ? discussionMessage : undefined ,
278+ getCommonPagePath,
279+ getCommonPageAboutTabPath,
280+ directParent,
281+ onUserClick,
282+ onFeedItemClick,
283+ onInternalLinkClick,
284+ } ) ;
285+
286+ return {
287+ ...discussionMessage ,
288+ parsedText,
289+ } ;
290+ } ) ,
291+ ) ;
292+ if (
293+ discussionsWithText . length < MESSAGES_NUMBER_IN_BATCH &&
294+ ! hasLastVisibleDocument
295+ ) {
296+ setIsEndOfList ( ( prevIsEndOfList ) => ( {
297+ ...prevIsEndOfList ,
298+ [ discussionId ] : true ,
299+ } ) ) ;
300+ }
301+ dispatch (
302+ cacheActions . updateDiscussionMessagesStateByDiscussionId ( {
303+ discussionId,
304+ removedDiscussionMessages,
305+ updatedDiscussionMessages : discussionsWithText ,
306+ } ) ,
307+ ) ;
308+ setIsBatchLoading ( false ) ;
309+ } ,
310+ ) ;
311+ } catch ( err ) {
312+ setIsBatchLoading ( false ) ;
313+ }
310314 } ;
311315
312316 useDeepCompareEffect ( ( ) => {
@@ -316,70 +320,74 @@ export const useDiscussionMessagesById = ({
316320 return ;
317321 }
318322
319- const discussionMessages = [ ...state . data ] ;
320-
321- // if (discussionMessages.length > 0 && users.length === 0) {
322- // return;
323- // }
323+ try {
324+ const discussionMessages = [ ...state . data ] ;
324325
325- const filteredMessages = discussionMessages . filter (
326- ( { moderation } ) =>
327- moderation ?. flag !== ModerationFlags . Hidden || hasPermissionToHide ,
328- ) ;
329- const loadedDiscussionMessages = await Promise . all (
330- filteredMessages . map ( async ( d ) => {
331- const newDiscussionMessage = { ...d } ;
332- const parentMessage = filteredMessages . find (
333- ( { id } ) => id === d . parentId ,
334- ) ;
335- if (
336- checkIsUserDiscussionMessage ( d ) &&
337- checkIsUserDiscussionMessage ( newDiscussionMessage )
338- ) {
339- const commonMemberMessageOwner = [
340- AI_USER ,
341- AI_PRO_USER ,
342- ...users ,
343- ...externalCommonUsers ,
344- ] . find ( ( o ) => o . uid === d . ownerId ) ;
345- const messageOwner =
346- commonMemberMessageOwner ||
347- ( await UserService . getUserById ( d . ownerId ) ) ;
348- newDiscussionMessage . owner = messageOwner ;
349- if ( ! commonMemberMessageOwner && messageOwner ) {
350- dispatch (
351- cacheActions . addUserToExternalCommonUsers ( {
352- user : messageOwner ,
353- } ) ,
354- ) ;
355- }
356- }
357- newDiscussionMessage . parentMessage = parentMessage
358- ? {
359- id : parentMessage . id ,
360- text : parentMessage . text ,
361- ownerName : parentMessage ?. ownerName ,
362- ...( checkIsUserDiscussionMessage ( parentMessage ) && {
363- ownerId : parentMessage . ownerId ,
364- } ) ,
365- moderation : parentMessage ?. moderation ,
366- images : parentMessage ?. images ,
367- files : parentMessage ?. files ,
368- createdAt : parentMessage . createdAt ,
326+ const filteredMessages = discussionMessages . filter (
327+ ( { moderation } ) =>
328+ moderation ?. flag !== ModerationFlags . Hidden || hasPermissionToHide ,
329+ ) ;
330+ const loadedDiscussionMessages = await Promise . all (
331+ filteredMessages . map ( async ( d ) => {
332+ const newDiscussionMessage = { ...d } ;
333+ const parentMessage = filteredMessages . find (
334+ ( { id } ) => id === d . parentId ,
335+ ) ;
336+ if (
337+ checkIsUserDiscussionMessage ( d ) &&
338+ checkIsUserDiscussionMessage ( newDiscussionMessage )
339+ ) {
340+ const commonMemberMessageOwner = [
341+ AI_USER ,
342+ AI_PRO_USER ,
343+ ...users ,
344+ ...externalCommonUsers ,
345+ ] . find ( ( o ) => o . uid === d . ownerId ) ;
346+ const messageOwner =
347+ commonMemberMessageOwner ||
348+ ( await UserService . getUserById ( d . ownerId ) ) ;
349+ newDiscussionMessage . owner = messageOwner ;
350+ if ( ! commonMemberMessageOwner && messageOwner ) {
351+ dispatch (
352+ cacheActions . addUserToExternalCommonUsers ( {
353+ user : messageOwner ,
354+ } ) ,
355+ ) ;
369356 }
370- : null ;
371-
372- return newDiscussionMessage ;
373- } ) ,
374- ) ;
375-
376- setDiscussionMessagesWithOwners ( loadedDiscussionMessages ) ;
377- setIsFirstBatchLoaded ( ( prev ) => ( {
378- ...prev ,
379- [ discussionId ] : true ,
380- } ) ) ;
357+ }
358+ newDiscussionMessage . parentMessage = parentMessage
359+ ? {
360+ id : parentMessage . id ,
361+ text : parentMessage . text ,
362+ ownerName : parentMessage ?. ownerName ,
363+ ...( checkIsUserDiscussionMessage ( parentMessage ) && {
364+ ownerId : parentMessage . ownerId ,
365+ } ) ,
366+ moderation : parentMessage ?. moderation ,
367+ images : parentMessage ?. images ,
368+ files : parentMessage ?. files ,
369+ createdAt : parentMessage . createdAt ,
370+ }
371+ : null ;
372+
373+ return newDiscussionMessage ;
374+ } ) ,
375+ ) ;
376+
377+ setDiscussionMessagesWithOwners ( loadedDiscussionMessages ) ;
378+ setIsFirstBatchLoaded ( ( prev ) => ( {
379+ ...prev ,
380+ [ discussionId ] : true ,
381+ } ) ) ;
382+ } catch ( err ) {
383+ setDiscussionMessagesWithOwners ( [ ] ) ;
384+ setIsFirstBatchLoaded ( ( prev ) => ( {
385+ ...prev ,
386+ [ discussionId ] : true ,
387+ } ) ) ;
388+ }
381389 } ) ( ) ;
382- } , [ state . data , hasPermissionToHide , users , externalCommonUsers ] ) ;
390+ } , [ state . data , hasPermissionToHide , users , externalCommonUsers , discussionId ] ) ;
383391
384392 return {
385393 ...state ,
0 commit comments