import { LOCAL_ID } from "coral-framework/lib/relay" ; import { GQLCOMMENT_SORT } from "coral-framework/schema" ; import { MAX_REPLY_INDENT_DEPTH } from "coral-stream/constants" ; import { ConnectionHandler , RecordProxy , RecordSourceSelectorProxy , } from "relay-runtime" ; function isNodeIDInConnection ( id , connection ) { return connection . getLinkedRecords ( "edges" ) ?. some ( ( edge ) => edge . getLinkedRecord ( "node" ) ?. getDataID ( ) === id ) ; } function isNodeIDInRepliesConnection ( id , connection ) { return connection . getLinkedRecords ( "edges" ) ?. concat ( connection . getLinkedRecords ( "viewNewEdges" ) || [ ] ) . some ( ( edge ) => edge . getLinkedRecord ( "node" ) ?. getDataID ( ) === id ) ; } function isCommentInsideParentRepliesConnection ( comment ) { const parent = comment . getLinkedRecord ( "parent" ) ; if ( ! parent ) { return false ; } const repliesConnection = ConnectionHandler . getConnection ( parent , "ReplyList_replies" , { orderBy : GQLCOMMENT_SORT . CREATED_AT_ASC } ) ! ; if ( ! repliesConnection ) { return false ; } return isNodeIDInRepliesConnection ( comment . getDataID ( ) , repliesConnection ) ; } function findFlattenedParent ( comment , ancestorID ? ) { const maxDepth = MAX_REPLY_INDENT_DEPTH + 1 ; const ancestors = [ ] ; let cur = comment ; while ( cur ) { ancestors . unshift ( cur ) ; if ( cur . getDataID ( ) === ancestorID ) { break ; } cur = cur . getLinkedRecord ( "parent" ) ; } if ( ancestors . length > maxDepth ) { return { flattendParent : ancestors [ maxDepth - 1 ] , exceededDepth : ancestors . length - maxDepth , } ; } return null ; } /**
 * Returns depth until story or null if could not trace back to
 * comments that is loaded inside the stream!
 */ export function determineDepthTillStory ( store , comment , storyID , orderBy , storyConnectionKey , tag ? ) { const story = store . get ( storyID ) ! ; const storyConnection = ConnectionHandler . getConnection ( story , storyConnectionKey , { orderBy , tag , } ) ! ; if ( isNodeIDInConnection ( comment . getDataID ( ) , storyConnection ) ) { return 0 ; } const firstParent = comment . getLinkedRecord ( "parent" ) ; if ( ! firstParent ) { return null ; } const flattenReplies = store . get ( LOCAL_ID ) ?. getValue ( "flattenReplies" ) ; const result = flattenReplies ? findFlattenedParent ( comment ) : null ; let depth = result !== null ? result . exceededDepth : 1 ; let cur = result !== null ? result . flattendParent : firstParent ; while ( cur ) { const parent = cur . getLinkedRecord ( "parent" ) ; if ( parent === null ) { if ( ! isNodeIDInConnection ( cur . getDataID ( ) , storyConnection ) ) { return null ; } return depth ; } else if ( isCommentInsideParentRepliesConnection ( cur ) ) { depth ++ ; } else { return null ; } cur = parent ; } return depth ; } /**
 * Returns depth until ancestor or null if could not trace back to
 * comments that is loaded inside the stream!
 */ export function determineDepthTillAncestor ( store , comment , ancestorID ? ) { if ( comment . getDataID ( ) === ancestorID ) { return 0 ; } const firstParent = comment . getLinkedRecord ( "parent" ) ; if ( ! firstParent ) { return null ; } if ( firstParent . getDataID ( ) === ancestorID ) { return 1 ; } const flattenReplies = store . get ( LOCAL_ID ) ?. getValue ( "flattenReplies" ) ; const result = flattenReplies ? findFlattenedParent ( comment , ancestorID ) : null ; let depth = result !== null ? result . exceededDepth : 1 ; let cur = result !== null ? result . flattendParent : firstParent ; while ( cur ) { const parent = cur . getLinkedRecord ( "parent" ) ; if ( parent !== null ) { if ( ! isCommentInsideParentRepliesConnection ( cur ) ) { return null ; } depth ++ ; if ( parent . getDataID ( ) === ancestorID ) { return depth ; } } cur = parent ; } return null ; }