@@ -287,15 +287,97 @@ class GitalkComponent extends Component {
287287 return res . data
288288 } )
289289 }
290+ // Although github v3 doesn't support sort, we still could do the math!
291+ getCommentsV3OrderDesc = issue => {
292+ const { clientID, clientSecret, perPage } = this . options
293+ const { page } = this . state
294+
295+ const headers = {
296+ Accept : 'application/vnd.github.v3.full+json'
297+ }
298+ const auth = {
299+ username : clientID ,
300+ password : clientSecret
301+ }
302+ const params = {
303+ per_page : perPage ,
304+ page
305+ }
306+ const totalCount = issue . comments
307+ const totalPage = Math . ceil ( totalCount / perPage )
308+ const lastPageCount = totalCount % perPage
309+ const ascPage = totalPage - page + 1
310+ if (
311+ totalPage === 1 || // of course...
312+ lastPageCount === 0 || // only need to get 1 page
313+ page >= totalPage // last page
314+ ) {
315+ params . page = ascPage
316+ if ( page >= totalPage ) {
317+ params . per_page = lastPageCount === 0 ? perPage : lastPageCount
318+ }
319+ return axiosGithub . get ( issue . comments_url , {
320+ headers,
321+ auth,
322+ params,
323+ } ) . then ( res => {
324+ const { comments, issue } = this . state
325+ let isLoadOver = false
326+ const cs = comments . concat ( res . data . reverse ( ) )
327+ if ( cs . length >= issue . comments || res . data . length < perPage ) {
328+ isLoadOver = true
329+ }
330+ this . setState ( {
331+ comments : cs ,
332+ isLoadOver,
333+ page : page + 1
334+ } )
335+ return cs
336+ } )
337+ } else {
338+ return Promise . all ( [
339+ axiosGithub . get ( issue . comments_url , {
340+ headers,
341+ auth,
342+ params : { ...params , page : ascPage } ,
343+ } ) ,
344+ axiosGithub . get ( issue . comments_url , {
345+ headers,
346+ auth,
347+ params : { ...params , page : ascPage - 1 } ,
348+ } )
349+ ] ) . then ( ( [ commentsRes1 , commentsRes2 ] ) => {
350+ const data = commentsRes1 . data . splice ( 0 , lastPageCount ) . reverse ( )
351+ data . push (
352+ ...commentsRes2 . data . reverse ( ) . splice ( 0 , perPage - lastPageCount )
353+ )
354+ const { comments, issue } = this . state
355+ let isLoadOver = false
356+ const cs = comments . concat ( data )
357+ if ( cs . length >= issue . comments || data . length < perPage ) {
358+ isLoadOver = true
359+ }
360+ this . setState ( {
361+ comments : cs ,
362+ isLoadOver,
363+ page : page + 1
364+ } )
365+ return cs
366+ } )
367+ }
368+ }
290369 // Get comments via v3 api, don't require login, but sorting feature is disable
291370 getCommentsV3 = issue => {
292- const { clientID, clientSecret, perPage } = this . options
371+ const { clientID, clientSecret, perPage, pagerDirection } = this . options
293372 const { page } = this . state
294373
295374 return this . getIssue ( )
296375 . then ( issue => {
297376 if ( ! issue ) return
298377
378+ if ( pagerDirection === 'last' ) {
379+ return this . getCommentsV3OrderDesc ( issue )
380+ }
299381 return axiosGithub . get ( issue . comments_url , {
300382 headers : {
301383 Accept : 'application/vnd.github.v3.full+json'
0 commit comments