Skip to content

Commit

Permalink
fix: support flatten replies
Browse files Browse the repository at this point in the history
  • Loading branch information
cvle committed Nov 11, 2021
1 parent 541a7ad commit 4f5f317
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function addCommentReplyToStory(
const singleCommentID = lookup(environment, LOCAL_ID).commentID;
const comment = commentEdge.getLinkedRecord("node")!;
const depth = singleCommentID
? determineDepthTillAncestor(comment, singleCommentID)
? determineDepthTillAncestor(store, comment, singleCommentID)
: determineDepthTillStory(
store,
comment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function insertReply(
}

const depth = ancestorID
? determineDepthTillAncestor(comment, ancestorID)
? determineDepthTillAncestor(store, comment, ancestorID)
: determineDepthTillStory(
store,
comment,
Expand Down
51 changes: 44 additions & 7 deletions src/core/client/stream/tabs/Comments/helpers/determineDepthTill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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,
Expand Down Expand Up @@ -36,10 +38,32 @@ function isCommentInsideParentRepliesConnection(comment: RecordProxy) {
"ReplyList_replies",
{ orderBy: GQLCOMMENT_SORT.CREATED_AT_ASC }
)!;

if (!repliesConnection) {
return false;
}
return isNodeIDInRepliesConnection(comment.getDataID(), repliesConnection);
}

function findFlattenedParent(comment: RecordProxy, ancestorID?: string | null) {
const maxDepth = MAX_REPLY_INDENT_DEPTH + 1; // +1 to include root level.
const ancestors: RecordProxy[] = [];
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!
Expand Down Expand Up @@ -75,8 +99,12 @@ export function determineDepthTillStory(
return null;
}

let depth = 1;
let cur = firstParent;
// If flattenReplies start iterating from flattened Parent.
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: RecordProxy | null = cur.getLinkedRecord("parent");
if (parent === null) {
Expand All @@ -103,25 +131,34 @@ export function determineDepthTillStory(
* comments that is loaded inside the stream!
*/
export function determineDepthTillAncestor(
store: RecordSourceSelectorProxy<unknown>,
comment: RecordProxy,
ancestorID?: string | null
) {
// Already ancestor, return 0;
if (comment.getDataID() === ancestorID) {
return 0;
}
let cur: RecordProxy | null | undefined = comment.getLinkedRecord("parent");
if (!cur) {
const firstParent: RecordProxy | null | undefined = comment.getLinkedRecord(
"parent"
);
if (!firstParent) {
// It's a top level comment, so can't determine depth till ancestor.
return null;
}

// Parent is ancestor, return 1.
if (cur.getDataID() === ancestorID) {
if (firstParent.getDataID() === ancestorID) {
return 1;
}

let depth = 1;
// If flattenReplies start iterating from flattened Parent.
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: RecordProxy | null = cur.getLinkedRecord("parent");
if (parent !== null) {
Expand Down

0 comments on commit 4f5f317

Please sign in to comment.