|
| 1 | +"""Tweet card component for Little X.""" |
| 2 | + |
| 3 | +import from "lucide-react" { Heart, MessageCircle, Trash2, Repeat2 } |
| 4 | + |
| 5 | +def:pub TweetCard(props: Any) -> Any { |
| 6 | + tweet = props.tweet; |
| 7 | + myUsername = props.myUsername; |
| 8 | + onLike = props.onLike; |
| 9 | + onComment = props.onComment; |
| 10 | + onDelete = props.onDelete; |
| 11 | + onRepost = props.onRepost; |
| 12 | + |
| 13 | + has showComments: bool = False, |
| 14 | + commentText: str = ""; |
| 15 | + |
| 16 | + likeFn = onLike; |
| 17 | + commentFn = onComment; |
| 18 | + deleteFn = onDelete; |
| 19 | + repostFn = onRepost; |
| 20 | + |
| 21 | + is_liked = tweet["likes"].includes(myUsername); |
| 22 | + like_count = String(len(tweet["likes"])); |
| 23 | + comment_count = String(len(tweet["comments"])); |
| 24 | + author_avatar = "https://i.pravatar.cc/150?u=fake@" + tweet["author_username"]; |
| 25 | + |
| 26 | + raw_name = tweet["author_username"] if tweet["author_username"] else "unknown"; |
| 27 | + display_name = raw_name.split("@")[0] if raw_name.includes("@") else raw_name; |
| 28 | + |
| 29 | + diff_ms = Date.now() - Reflect.construct(Date, [tweet["created_at"]]).getTime() if tweet["created_at"] else 0; |
| 30 | + diff_min = Math.floor(diff_ms / 60000); |
| 31 | + diff_hr = Math.floor(diff_min / 60); |
| 32 | + diff_day = Math.floor(diff_hr / 24); |
| 33 | + date_str = ( |
| 34 | + "now" if diff_min < 1 else ( |
| 35 | + String(diff_min) + "m" if diff_hr < 1 else ( |
| 36 | + String(diff_hr) + "h" if diff_day < 1 else ( |
| 37 | + String(diff_day) + "d" if diff_day < 30 else tweet["created_at"].substring(0, 10) |
| 38 | + ) |
| 39 | + ) |
| 40 | + ) |
| 41 | + ) if tweet["created_at"] else ""; |
| 42 | + |
| 43 | + return <div className="lx-tweet"> |
| 44 | + <img className="lx-avatar" src={author_avatar} alt={tweet["author_username"]} /> |
| 45 | + <div className="lx-tweet-body"> |
| 46 | + <div className="lx-tweet-header"> |
| 47 | + <span className="lx-tweet-name">{display_name}</span> |
| 48 | + <span className="lx-tweet-handle">{"@" + display_name}</span> |
| 49 | + <span className="lx-tweet-dot">·</span> |
| 50 | + <span className="lx-tweet-time">{date_str}</span> |
| 51 | + </div> |
| 52 | + <div className="lx-tweet-text">{tweet["content"]}</div> |
| 53 | + <div className="lx-tweet-actions"> |
| 54 | + <button |
| 55 | + className="lx-act-btn lx-act-comment" |
| 56 | + onClick={lambda -> None { showComments = not showComments; }} |
| 57 | + > |
| 58 | + <MessageCircle size={16} /> |
| 59 | + <span>{comment_count}</span> |
| 60 | + </button> |
| 61 | + <button |
| 62 | + className="lx-act-btn lx-act-repost" |
| 63 | + onClick={lambda -> None { repostFn.call(None, tweet["id"], tweet["content"], tweet["author_username"]); }} |
| 64 | + title="Repost" |
| 65 | + > |
| 66 | + <Repeat2 size={16} /> |
| 67 | + </button> |
| 68 | + <button |
| 69 | + className={"lx-act-btn lx-act-liked" if is_liked else "lx-act-btn lx-act-like"} |
| 70 | + onClick={lambda -> None { likeFn.call(None, tweet["id"]); }} |
| 71 | + > |
| 72 | + <Heart size={16} /> |
| 73 | + <span>{like_count}</span> |
| 74 | + </button> |
| 75 | + {( |
| 76 | + <button |
| 77 | + className="lx-act-btn lx-act-delete" |
| 78 | + onClick={lambda -> None { deleteFn.call(None, tweet["id"]); }} |
| 79 | + > |
| 80 | + <Trash2 size={16} /> |
| 81 | + </button> |
| 82 | + ) if tweet["is_mine"] else None} |
| 83 | + </div> |
| 84 | + {( |
| 85 | + <div className="lx-comments-section"> |
| 86 | + {[ |
| 87 | + <div className="lx-comment-item" key={c["created_at"]}> |
| 88 | + <img className="lx-avatar-sm" src={"https://i.pravatar.cc/150?u=fake@" + c["username"]} alt={c["username"]} /> |
| 89 | + <div> |
| 90 | + <div className="lx-comment-meta"> |
| 91 | + <strong>{c["username"]}</strong> |
| 92 | + <span>{" · "}</span> |
| 93 | + </div> |
| 94 | + <div className="lx-comment-text">{c["content"]}</div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + for c in tweet["comments"] |
| 98 | + ]} |
| 99 | + <div className="lx-comment-form"> |
| 100 | + <input |
| 101 | + className="lx-comment-input" |
| 102 | + placeholder="Reply..." |
| 103 | + value={commentText} |
| 104 | + onChange={lambda e: Any -> None { commentText = e.target.value; }} |
| 105 | + onKeyDown={lambda e: Any -> None { |
| 106 | + if e.key == "Enter" and commentText.trim() { |
| 107 | + commentFn.call(None, tweet["id"], commentText); |
| 108 | + commentText = ""; |
| 109 | + } |
| 110 | + }} |
| 111 | + /> |
| 112 | + <button |
| 113 | + className="lx-comment-submit" |
| 114 | + onClick={lambda -> None { |
| 115 | + if commentText.trim() { |
| 116 | + commentFn.call(None, tweet["id"], commentText); |
| 117 | + commentText = ""; |
| 118 | + } |
| 119 | + }} |
| 120 | + > |
| 121 | + Reply |
| 122 | + </button> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ) if showComments else None} |
| 126 | + </div> |
| 127 | + </div>; |
| 128 | +} |
0 commit comments