Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mobile navigation to comment for hidden comments #983

Merged
merged 1 commit into from
May 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions frontend/app/components/root/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,13 @@ const messages = defineMessages({
},
});

const getCollapsedParents = (
hash: string,
childToParentComments: Record<string, string>,
collapsedThreads: Record<string, boolean>
) => {
const collapsedParents = [];
const getCollapsedParent = (hash: string, childToParentComments: Record<string, string>) => {
let id = hash.replace(`#${COMMENT_NODE_CLASSNAME_PREFIX}`, '');

while (childToParentComments[id]) {
id = childToParentComments[id];
if (collapsedThreads[id]) {
collapsedParents.push(id);
}
}

return collapsedParents;
return id;
};

/** main component fr main comments widget */
Expand Down Expand Up @@ -160,20 +151,29 @@ export class Root extends Component<Props, State> {
if (e) e.preventDefault();

if (!document.querySelector(hash)) {
const ids = getCollapsedParents(hash, this.props.childToParentComments, this.props.collapsedThreads);
ids.forEach((id) => this.props.setCollapse(id, false));
const id = getCollapsedParent(hash, this.props.childToParentComments);
const indexHash = this.props.topComments.findIndex((item) => item === id);
const multiplierCollapsed = Math.ceil(indexHash / MAX_SHOWN_ROOT_COMMENTS);
this.setState(
{
commentsShown: this.state.commentsShown + MAX_SHOWN_ROOT_COMMENTS * multiplierCollapsed,
},
() => setTimeout(() => this.toMessage(hash), 500)
);
} else {
this.toMessage(hash);
}
}
};

toMessage = (hash: string) => {
const comment = document.querySelector(hash);
if (comment) {
postMessage({ scrollTo: comment.getBoundingClientRect().top });
comment.classList.add('comment_highlighting');
setTimeout(() => {
const comment = document.querySelector(hash);
if (comment) {
postMessage({ scrollTo: comment.getBoundingClientRect().top });
comment.classList.add('comment_highlighting');
setTimeout(() => {
comment.classList.remove('comment_highlighting');
}, 5e3);
}
}, 500);
comment.classList.remove('comment_highlighting');
}, 5e3);
}
};

Expand Down