forked from housseindjirdeh/angular2-hn
-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Migrate CommentComponent from Angular to React #175
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
Open
devin-ai-integration
wants to merge
1
commit into
master
Choose a base branch
from
devin/1771204517-migrate-comment-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| $mobile-only: "only screen and (max-width : 768px)"; | ||
| $tablet-only: "only screen and (max-width : 1024px)"; | ||
|
|
||
| a { | ||
| font-weight: bold; | ||
| text-decoration: none; | ||
| &:hover { | ||
| text-decoration: underline; | ||
| } | ||
| } | ||
|
|
||
| .meta { | ||
| font-size: 13px; | ||
| color: #696969; | ||
| font-weight: bold; | ||
| letter-spacing: 0.5px; | ||
| margin-bottom: 8px; | ||
| a { | ||
| text-decoration: none; | ||
|
|
||
| &:hover { | ||
| text-decoration: underline; | ||
| } | ||
| } | ||
| .time { | ||
| padding-left: 5px; | ||
| } | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .meta { | ||
| font-size: 14px; | ||
| margin-bottom: 10px; | ||
| .time { | ||
| padding: 0; | ||
| float: right; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .meta-collapse { | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| .deleted-meta { | ||
| font-size: 12px; | ||
| font-weight: bold; | ||
| letter-spacing: 0.5px; | ||
| margin: 30px 0; | ||
| a { | ||
| text-decoration: none; | ||
| } | ||
| } | ||
|
|
||
| .collapse { | ||
| font-size: 13px; | ||
| letter-spacing: 2px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .comment-tree { | ||
| margin-left: 24px; | ||
| } | ||
|
|
||
| @media #{$tablet-only} { | ||
| .comment-tree { | ||
| margin-left: 8px; | ||
| } | ||
| } | ||
|
|
||
| .comment-text { | ||
| font-size: 15px; | ||
| margin-top: 0; | ||
| margin-bottom: 20px; | ||
| word-wrap: break-word; | ||
| line-height: 1.5em; | ||
| } | ||
|
|
||
| .subtree { | ||
| margin-left: 0; | ||
| padding: 0; | ||
| list-style-type: none; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import React, { useState } from 'react'; | ||
| import { Link } from 'react-router-dom'; | ||
| import { CommentProps } from './Comment.types'; | ||
| import styles from './Comment.module.scss'; | ||
|
|
||
| const Comment: React.FC<CommentProps> = ({ comment }) => { | ||
| const [collapse, setCollapse] = useState(false); | ||
|
|
||
| if (comment.deleted) { | ||
| return ( | ||
| <div> | ||
| <div className={styles['deleted-meta']}> | ||
| <span className={styles.collapse}>[deleted]</span> | Comment Deleted | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <div className={collapse ? `${styles.meta} ${styles['meta-collapse']}` : styles.meta}> | ||
| <span className={styles.collapse} onClick={() => setCollapse(!collapse)}> | ||
| [{collapse ? '+' : '-'}] | ||
| </span>{' '} | ||
| <Link to={`/user/${comment.user}`}>{comment.user}</Link> | ||
| <span className={styles.time}>{comment.time_ago}</span> | ||
| </div> | ||
| <div className={styles['comment-tree']}> | ||
| {!collapse && ( | ||
| <div> | ||
| <p | ||
| className={styles['comment-text']} | ||
| dangerouslySetInnerHTML={{ __html: comment.content }} | ||
| /> | ||
| <ul className={styles.subtree}> | ||
| {comment.comments.map((subComment) => ( | ||
| <li key={subComment.id}> | ||
| <Comment comment={subComment} /> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Comment; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export interface Comment { | ||
| id: number; | ||
| level: number; | ||
| user: string; | ||
| time: number; | ||
| time_ago: string; | ||
| content: string; | ||
| deleted: boolean; | ||
| comments: Comment[]; | ||
| } | ||
|
|
||
| export interface CommentProps { | ||
| comment: Comment; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴
comment.comments.map()crashes ifcommentsis undefined/null from APICalling
.map()directly oncomment.commentsat line 36 will throw aTypeErrorat runtime if the API returns a comment object wherecommentsisundefinedornull.Root Cause
The data comes from an external API (
node-hnapi.herokuapp.com), and the TypeScript interface atsrc/components/Comment/Comment.types.ts:9declarescomments: Comment[]as always present, but this provides no runtime guarantee. If the API ever returns a comment without acommentsfield (or withnull), the call tocomment.comments.map(...)will throw:Notably, the original Angular implementation at
src/app/item-details/comment/comment.component.html:11uses*ngFor="let subComment of comment.comments", which silently handlesnull/undefinedby simply not iterating. The React port loses this safety.Impact: A single comment with a missing
commentsfield will crash the entire comment tree (and potentially the whole page if there's no error boundary), since React will propagate the error up.Was this helpful? React with 👍 or 👎 to provide feedback.