import { commentHrefToIds } from '../../commentHrefToIds' ; import { makeTr } from './makeTr' ; const getTrs = ( ) => [ ... document . querySelectorAll < HTMLTableRowElement > ( '.history-table > tbody tr[data-postid]' ) ] ; const trToIds = ( tr ) => commentHrefToIds ( tr . querySelector < HTMLAnchorElement > ( 'a[href^="/questions"]' ) ! . href ) ; /**
 * Finds and inserts comments that, given the date range, are in the database and should exist in the table, but don't
 */ export const insertMissingCommentTrs = ( savedComments ) => { const savedCommentsArrLatestFirst = Object . values ( savedComments ) . reverse ( ) ; const commentTrs = getTrs ( ) ; const [ startTRIndexInSavedCommentsArr , endTRIndexInSavedCommentsArr ] = [ commentTrs [ 0 ] , commentTrs [ commentTrs . length - 1 ] ] . map ( ( tr ) => { const commentIdToFind = trToIds ( tr ) . commentId ; return savedCommentsArrLatestFirst . findIndex ( ( { commentHref } ) => ( commentHrefToIds ( commentHref ) . commentId <= commentIdToFind ) ) ; } ) ; const pageNumbersElm = document . querySelector ( '#user-tab-activity .user-tab-paging .is-selected' ) ; const thisPageNumber = pageNumbersElm ? Number ( pageNumbersElm . textContent ) : 1 ; const sliceStartIndex = ( startTRIndexInSavedCommentsArr === - 1 || thisPageNumber === 1 ) ? 0 : startTRIndexInSavedCommentsArr ; const sliceEndIndex = endTRIndexInSavedCommentsArr === - 1 ? savedCommentsArrLatestFirst . length - 1 : Math . min ( endTRIndexInSavedCommentsArr + 7 , savedCommentsArrLatestFirst . length - 1 ) ; const allCommentsToBeShownOnThisPage = savedCommentsArrLatestFirst . slice ( sliceStartIndex , sliceEndIndex ) ; const commentIdsAlreadyOnPage = new Set ( commentTrs . map ( tr => trToIds ( tr ) . commentId ) ) ; const tBody = commentTrs [ 0 ] . parentElement ! ; const lastOriginalTrCommentId = trToIds ( commentTrs [ commentTrs . length - 1 ] ) . commentId ; const insertTr = ( savedComment ) => { const thisCommentId = commentHrefToIds ( savedComment . commentHref ) . commentId ; if ( commentIdsAlreadyOnPage . has ( thisCommentId ) ) { return ; } const trThisNewRowShouldBeInsertedBefore = getTrs ( ) . find ( tr => trToIds ( tr ) . commentId < thisCommentId ) ; const isTrailing = thisCommentId < lastOriginalTrCommentId ; if ( Number . isNaN ( savedComment . timestamp ) ) { return ; } const trToInsert = makeTr ( savedComment , isTrailing ) ; tBody . insertBefore ( trToInsert , trThisNewRowShouldBeInsertedBefore || null ) ; } ; allCommentsToBeShownOnThisPage . forEach ( insertTr ) ; } ;