Skip to content
This repository was archived by the owner on Sep 8, 2023. It is now read-only.

Commit 032e039

Browse files
committed
[react-threads] Support Threads without some data
1 parent a788c1f commit 032e039

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

react-threads/src/components/Thread.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type ThreadImageProps = {
5353
export const ThreadImage: React.FC<ThreadImageProps> = ({ post }) => {
5454
const bestCandidate = useMemo(() => {
5555
type CandiateItem = Candidate | ThreadsHdProfilePicVersion;
56-
const candidates: CandiateItem[] = post.image_versions2?.candidates;
56+
const candidates: CandiateItem[] = post.image_versions2?.candidates || [];
5757

5858
if (!candidates.length) {
5959
return null;
@@ -98,16 +98,16 @@ export type ThreadProps = {
9898
thread?: ThreadPost;
9999
};
100100
export const Thread: React.FC<ThreadProps> = ({ thread }) => {
101-
const item = thread?.thread_items?.[0];
101+
const item = useMemo(() => thread?.thread_items?.find((v) => !!v?.post?.user), []);
102102

103-
const reposted_post = item?.post.text_post_app_info.share_info.reposted_post;
103+
const reposted_post = item?.post?.text_post_app_info?.share_info?.reposted_post;
104104
// const quoted_post = item?.post.text_post_app_info.share_info.quoted_post;
105105
const post = reposted_post || item?.post || null;
106106
const user = post?.user;
107107

108108
const nestedPost = useMemo(() => {
109-
const reposted_post = post?.text_post_app_info.share_info.reposted_post;
110-
const quoted_post = post?.text_post_app_info.share_info.quoted_post;
109+
const reposted_post = post?.text_post_app_info?.share_info?.reposted_post;
110+
const quoted_post = post?.text_post_app_info?.share_info?.quoted_post;
111111
return reposted_post || quoted_post || null;
112112
}, [post]);
113113

@@ -237,7 +237,7 @@ export const Thread: React.FC<ThreadProps> = ({ thread }) => {
237237
<span>&nbsp;·&nbsp;</span>
238238
)}
239239
{(nestedPost?.like_count || 0) >= 1 && (
240-
<span>{nestedPost?.like_count.toLocaleString()} likes</span>
240+
<span>{nestedPost?.like_count?.toLocaleString()} likes</span>
241241
)}
242242
</div>
243243
</div>
@@ -263,9 +263,11 @@ export const Thread: React.FC<ThreadProps> = ({ thread }) => {
263263

264264
<div className="flex items-end row-[4] col-[1/span_2]">
265265
<div className="flex items-center min-h-[22px] leading-[21px] text-[15px] text-[rgb(97,97,97)]">
266-
<span>{item.view_replies_cta_string}</span>
267-
<span>&nbsp;·&nbsp;</span>
268-
<span>{post?.like_count.toLocaleString()} likes</span>
266+
{!!item.view_replies_cta_string && <span>{item.view_replies_cta_string}</span>}
267+
{!!item.view_replies_cta_string && (post?.like_count || 0) >= 1 && (
268+
<span>&nbsp;·&nbsp;</span>
269+
)}
270+
{(post?.like_count || 0) >= 1 && <span>{post?.like_count?.toLocaleString()} likes</span>}
269271
</div>
270272
</div>
271273
</div>

0 commit comments

Comments
 (0)