@@ -15,9 +15,13 @@ limitations under the License.
1515*/
1616
1717import React from 'react' ;
18- import { IEventRelation , MatrixEvent , Room } from 'matrix-js-sdk/src' ;
1918import { Thread , ThreadEvent } from 'matrix-js-sdk/src/models/thread' ;
2019import { RelationType } from 'matrix-js-sdk/src/@types/event' ;
20+ import { Room } from 'matrix-js-sdk/src/models/room' ;
21+ import { IEventRelation , MatrixEvent } from 'matrix-js-sdk/src/models/event' ;
22+ import { TimelineWindow } from 'matrix-js-sdk/src/timeline-window' ;
23+ import { Direction } from 'matrix-js-sdk/src/models/event-timeline' ;
24+ import { IRelationsRequestOpts } from 'matrix-js-sdk/src/@types/requests' ;
2125import classNames from "classnames" ;
2226
2327import BaseCard from "../views/right_panel/BaseCard" ;
@@ -141,7 +145,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
141145 private setupThread = ( mxEv : MatrixEvent ) => {
142146 let thread = this . props . room . threads ?. get ( mxEv . getId ( ) ) ;
143147 if ( ! thread ) {
144- thread = this . props . room . createThread ( [ mxEv ] ) ;
148+ thread = this . props . room . createThread ( mxEv ) ;
145149 }
146150 thread . on ( ThreadEvent . Update , this . updateLastThreadReply ) ;
147151 thread . once ( ThreadEvent . Ready , this . updateThread ) ;
@@ -167,10 +171,13 @@ export default class ThreadView extends React.Component<IProps, IState> {
167171 this . setState ( {
168172 thread,
169173 lastThreadReply : thread . lastReply ( ( ev : MatrixEvent ) => {
170- return ! ev . status ;
174+ return ev . isThreadRelation && ! ev . status ;
171175 } ) ,
172- } , ( ) => {
176+ } , async ( ) => {
173177 thread . emit ( ThreadEvent . ViewThread ) ;
178+ if ( ! thread . initialEventsFetched ) {
179+ await thread . fetchInitialEvents ( ) ;
180+ }
174181 this . timelinePanelRef . current ?. refreshTimeline ( ) ;
175182 } ) ;
176183 }
@@ -180,7 +187,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
180187 if ( this . state . thread ) {
181188 this . setState ( {
182189 lastThreadReply : this . state . thread . lastReply ( ( ev : MatrixEvent ) => {
183- return ! ev . status ;
190+ return ev . isThreadRelation && ! ev . status ;
184191 } ) ,
185192 } ) ;
186193 }
@@ -207,6 +214,31 @@ export default class ThreadView extends React.Component<IProps, IState> {
207214 </ div > ;
208215 } ;
209216
217+ private onPaginationRequest = async (
218+ timelineWindow : TimelineWindow | null ,
219+ direction = Direction . Backward ,
220+ limit = 20 ,
221+ ) : Promise < boolean > => {
222+ if ( ! this . state . thread . hasServerSideSupport ) {
223+ return false ;
224+ }
225+
226+ const timelineIndex = timelineWindow . getTimelineIndex ( direction ) ;
227+
228+ const paginationKey = direction === Direction . Backward ? "from" : "to" ;
229+ const paginationToken = timelineIndex . timeline . getPaginationToken ( direction ) ;
230+
231+ const opts : IRelationsRequestOpts = {
232+ limit,
233+ [ paginationKey ] : paginationToken ,
234+ direction,
235+ } ;
236+
237+ await this . state . thread . fetchEvents ( opts ) ;
238+
239+ return timelineWindow . paginate ( direction , limit ) ;
240+ } ;
241+
210242 public render ( ) : JSX . Element {
211243 const highlightedEventId = this . props . isInitialEventHighlighted
212244 ? this . props . initialEvent ?. getId ( )
@@ -262,6 +294,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
262294 eventId = { this . props . initialEvent ?. getId ( ) }
263295 highlightedEventId = { highlightedEventId }
264296 onUserScroll = { this . onScroll }
297+ onPaginationRequest = { this . onPaginationRequest }
265298 />
266299 ) }
267300
0 commit comments