Skip to content

Commit 564d801

Browse files
Merge pull request #92 from GetStream/vishal/channel-truncated-event
CRNS-32: Handling channel.truncated event
2 parents 8a1132f + 12db31a commit 564d801

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/components/ChannelList.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ const ChannelList = withChatContext(
7676
* @param {Event} event [Event object](https://getstream.io/chat/docs/#event_object) corresponding to `channel.updated` event
7777
* */
7878
onChannelUpdated: PropTypes.func,
79+
/**
80+
* Function to customize behaviour when channel gets truncated
81+
*
82+
* @param {Component} thisArg Reference to ChannelList component
83+
* @param {Event} event [Event object](https://getstream.io/chat/docs/#event_object) corresponding to `channel.truncated` event
84+
* */
85+
onChannelTruncated: PropTypes.func,
7986
/**
8087
* Function that overrides default behaviour when channel gets deleted. In absence of this prop, channel will be removed from the list.
8188
*
@@ -108,6 +115,10 @@ const ChannelList = withChatContext(
108115
* @param channel A Channel object
109116
*/
110117
setActiveChannel: PropTypes.func,
118+
/**
119+
* If true, channels won't be dynamically sorted by most recent message.
120+
*/
121+
lockChannelOrder: PropTypes.bool,
111122
};
112123

113124
static defaultProps = {
@@ -121,6 +132,7 @@ const ChannelList = withChatContext(
121132
sort: {},
122133
// https://github.com/facebook/react-native/blob/a7a7970e543959e9db5281914d5f132beb01db8d/Libraries/Lists/VirtualizedList.js#L466
123134
loadMoreThreshold: 2,
135+
lockChannelOrder: false,
124136
logger: () => {},
125137
};
126138

@@ -301,7 +313,7 @@ const ChannelList = withChatContext(
301313
}
302314

303315
if (e.type === 'message.new') {
304-
this.moveChannelUp(e.cid);
316+
!this.props.lockChannelOrder && this.moveChannelUp(e.cid);
305317
}
306318

307319
// make sure to re-render the channel list after connection is recovered
@@ -416,6 +428,18 @@ const ChannelList = withChatContext(
416428
}
417429
}
418430

431+
if (e.type === 'channel.truncated') {
432+
this.setState((prevState) => ({
433+
channels: [...prevState.channels],
434+
}));
435+
436+
if (
437+
this.props.onChannelTruncated &&
438+
typeof this.props.onChannelTruncated === 'function'
439+
)
440+
this.props.onChannelTruncated(this, e);
441+
}
442+
419443
return null;
420444
};
421445

types/index.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,29 @@ interface ChannelListProps extends ChatContextValue {
213213
*/
214214
onMessageNew?(
215215
thisArg: React.Component<ChannelListProps>,
216-
e: Client.NotificationNewMessageEvent,
216+
e: Client.Event<Client.NotificationNewMessageEvent>,
217217
): void;
218218
/** Function that overrides default behaviour when users gets added to a channel */
219219
onAddedToChannel?(
220220
thisArg: React.Component<ChannelListProps>,
221-
e: Client.NotificationAddedToChannelEvent,
221+
e: Client.Event<Client.NotificationAddedToChannelEvent>,
222222
): void;
223223
/** Function that overrides default behaviour when users gets removed from a channel */
224224
onRemovedFromChannel?(
225225
thisArg: React.Component<ChannelListProps>,
226-
e: Client.NotificationRemovedFromChannelEvent,
226+
e: Client.Event<Client.NotificationRemovedFromChannelEvent>,
227227
): void;
228228
onChannelUpdated?(
229229
thisArg: React.Component<ChannelListProps>,
230-
e: Client.ChannelUpdatedEvent,
230+
e: Client.Event<Client.ChannelUpdatedEvent>,
231231
): void;
232232
onChannelDeleted?(
233233
thisArg: React.Component<ChannelListProps>,
234-
e: Client.ChannelDeletedEvent,
234+
e: Client.Event<Client.ChannelDeletedEvent>,
235+
): void;
236+
onChannelTruncated?(
237+
thisArg: React.Component<ChannelListProps>,
238+
e: Client.Event<Client.ChannelTruncatedEvent>,
235239
): void;
236240
// TODO: Create proper interface for followings in chat js client.
237241
/** Object containing query filters */

0 commit comments

Comments
 (0)