-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVE] Message Collection Hooks (#20121)
- Loading branch information
Showing
33 changed files
with
1,889 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { AsyncStatePhase } from '../../lib/asyncState'; | ||
import { RecordList } from '../../lib/lists/RecordList'; | ||
import { IRocketChatRecord } from '../../../definition/IRocketChatRecord'; | ||
|
||
type RecordListValue<T> = { | ||
phase: AsyncStatePhase; | ||
items: T[]; | ||
itemCount: number; | ||
error: Error | undefined; | ||
} | ||
|
||
export const useRecordList = <T extends IRocketChatRecord>( | ||
recordList: RecordList<T>, | ||
): RecordListValue<T> => { | ||
const [state, setState] = useState<RecordListValue<T>>(() => ({ | ||
phase: recordList.phase, | ||
items: recordList.items, | ||
itemCount: recordList.itemCount, | ||
error: undefined, | ||
})); | ||
|
||
useEffect(() => { | ||
const disconnectMutatingEvent = recordList.on('mutating', () => { | ||
setState(() => ({ | ||
phase: recordList.phase, | ||
items: recordList.items, | ||
itemCount: recordList.itemCount, | ||
error: undefined, | ||
})); | ||
}); | ||
|
||
const disconnectMutatedEvent = recordList.on('mutated', () => { | ||
setState((prevState) => ({ | ||
phase: recordList.phase, | ||
items: recordList.items, | ||
itemCount: recordList.itemCount, | ||
error: prevState.error, | ||
})); | ||
}); | ||
|
||
const disconnectClearedEvent = recordList.on('cleared', () => { | ||
setState(() => ({ | ||
phase: recordList.phase, | ||
items: recordList.items, | ||
itemCount: recordList.itemCount, | ||
error: undefined, | ||
})); | ||
}); | ||
|
||
const disconnectErroredEvent = recordList.on('errored', (error) => { | ||
setState((state) => ({ ...state, error })); | ||
}); | ||
|
||
return (): void => { | ||
disconnectMutatingEvent(); | ||
disconnectMutatedEvent(); | ||
disconnectClearedEvent(); | ||
disconnectErroredEvent(); | ||
}; | ||
}, [recordList]); | ||
|
||
return state; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { IMessage } from '../../../definition/IMessage'; | ||
import { MessageList } from '../../lib/lists/MessageList'; | ||
import { ObjectFromApi } from '../../../definition/ObjectFromApi'; | ||
import { useScrollableRecordList } from './useScrollableRecordList'; | ||
import { RecordListBatchChanges } from '../../lib/lists/RecordList'; | ||
|
||
const convertMessageFromApi = (apiMessage: ObjectFromApi<IMessage>): IMessage => ({ | ||
...apiMessage, | ||
_updatedAt: new Date(apiMessage._updatedAt), | ||
ts: new Date(apiMessage.ts), | ||
...apiMessage.tlm && { tlm: new Date(apiMessage.tlm) }, | ||
}); | ||
|
||
export const useScrollableMessageList = ( | ||
messageList: MessageList, | ||
fetchMessages: (start: number, end: number) => Promise<RecordListBatchChanges<ObjectFromApi<IMessage>>>, | ||
initialItemCount?: number, | ||
): ReturnType<typeof useScrollableRecordList> => { | ||
const fetchItems = useCallback(async (start: number, end: number): Promise<RecordListBatchChanges<IMessage>> => { | ||
const batchChanges = await fetchMessages(start, end); | ||
return { | ||
...batchChanges.items && { items: batchChanges.items.map(convertMessageFromApi) }, | ||
...batchChanges.itemCount && { itemCount: batchChanges.itemCount }, | ||
}; | ||
}, [fetchMessages]); | ||
|
||
return useScrollableRecordList(messageList, fetchItems, initialItemCount); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
import { RecordList, RecordListBatchChanges } from '../../lib/lists/RecordList'; | ||
import { IRocketChatRecord } from '../../../definition/IRocketChatRecord'; | ||
|
||
const INITIAL_ITEM_COUNT = 25; | ||
|
||
export const useScrollableRecordList = <T extends IRocketChatRecord>( | ||
recordList: RecordList<T>, | ||
fetchBatchChanges: (start: number, end: number) => Promise<RecordListBatchChanges<T>>, | ||
initialItemCount: number = INITIAL_ITEM_COUNT, | ||
): { | ||
loadMoreItems: (start: number, end: number) => void; | ||
initialItemCount: number; | ||
} => { | ||
const loadMoreItems = useCallback( | ||
(start: number, end: number) => { | ||
recordList.batchHandle(() => fetchBatchChanges(start, end)); | ||
}, | ||
[recordList, fetchBatchChanges], | ||
); | ||
|
||
useEffect(() => { | ||
loadMoreItems(0, initialItemCount ?? INITIAL_ITEM_COUNT); | ||
}, [loadMoreItems, initialItemCount]); | ||
|
||
return { loadMoreItems, initialItemCount }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { useStream } from '../../contexts/ServerContext'; | ||
import { IMessage } from '../../../definition/IMessage'; | ||
import { | ||
createFilterFromQuery, | ||
FieldExpression, | ||
Query, | ||
} from '../../lib/minimongo'; | ||
import { MessageList } from '../../lib/lists/MessageList'; | ||
import { IRoom } from '../../../definition/IRoom'; | ||
import { IUser } from '../../../definition/IUser'; | ||
|
||
type RoomMessagesRidEvent = IMessage; | ||
|
||
type NotifyRoomRidDeleteMessageEvent = { _id: IMessage['_id'] }; | ||
|
||
type NotifyRoomRidDeleteMessageBulkEvent = { | ||
rid: IMessage['rid']; | ||
excludePinned: boolean; | ||
ignoreDiscussion: boolean; | ||
ts: FieldExpression<Date>; | ||
users: string[]; | ||
}; | ||
|
||
const createDeleteCriteria = ( | ||
params: NotifyRoomRidDeleteMessageBulkEvent, | ||
): ((message: IMessage) => boolean) => { | ||
const query: Query<IMessage> = { ts: params.ts }; | ||
|
||
if (params.excludePinned) { | ||
query.pinned = { $ne: true }; | ||
} | ||
|
||
if (params.ignoreDiscussion) { | ||
query.drid = { $exists: false }; | ||
} | ||
if (params.users && params.users.length) { | ||
query['u.username'] = { $in: params.users }; | ||
} | ||
|
||
return createFilterFromQuery<IMessage>(query); | ||
}; | ||
|
||
export const useStreamUpdatesForMessageList = (messageList: MessageList, uid: IUser['_id'] | null, rid: IRoom['_id'] | null): void => { | ||
const subscribeToRoomMessages = useStream('room-messages'); | ||
const subscribeToNotifyRoom = useStream('notify-room'); | ||
|
||
useEffect(() => { | ||
if (!uid || !rid) { | ||
messageList.clear(); | ||
return; | ||
} | ||
|
||
const unsubscribeFromRoomMessages = subscribeToRoomMessages<RoomMessagesRidEvent>(rid, (message) => { | ||
messageList.handle(message); | ||
}); | ||
|
||
const unsubscribeFromDeleteMessage = subscribeToNotifyRoom<NotifyRoomRidDeleteMessageEvent>( | ||
`${ rid }/deleteMessage`, | ||
({ _id: mid }) => { | ||
messageList.remove(mid); | ||
}, | ||
); | ||
|
||
const unsubscribeFromDeleteMessageBulk = subscribeToNotifyRoom<NotifyRoomRidDeleteMessageBulkEvent>( | ||
`${ rid }/deleteMessageBulk`, | ||
(params) => { | ||
const matchDeleteCriteria = createDeleteCriteria(params); | ||
messageList.prune(matchDeleteCriteria); | ||
}, | ||
); | ||
|
||
return (): void => { | ||
unsubscribeFromRoomMessages(); | ||
unsubscribeFromDeleteMessage(); | ||
unsubscribeFromDeleteMessageBulk(); | ||
}; | ||
}, [subscribeToRoomMessages, subscribeToNotifyRoom, uid, rid, messageList]); | ||
}; |
Oops, something went wrong.