Skip to content

Commit

Permalink
fix thumbnail performance
Browse files Browse the repository at this point in the history
  • Loading branch information
plebeius-eth committed Nov 21, 2024
1 parent fd8b3ea commit ab85514
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
25 changes: 14 additions & 11 deletions src/components/post-desktop/post-desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,25 +214,28 @@ const PostInfo = ({ openReplyModal, post, postReplyCount = 0, roles, isHidden }:

const PostMedia = ({ post }: PostProps) => {
const { t } = useTranslation();
const { link, spoiler } = post || {};
const { link, spoiler, cid } = post || {};

// Reset state by remounting component when post changes
return <PostMediaContent key={cid} post={post} link={link} spoiler={spoiler} t={t} />;
};

const PostMediaContent = ({ post, link, spoiler, t }: { post: any; link: string; spoiler: boolean; t: any }) => {
const initialInfo = getCommentMediaInfo(post);
const [webpageThumbnail, setWebpageThumbnail] = useState<CommentMediaInfo | undefined>();

// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
const [commentMediaInfo, setCommentMediaInfo] = useState<CommentMediaInfo | undefined>();
useEffect(() => {
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
const loadThumbnail = async () => {
const initialInfo = getCommentMediaInfo(post);
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
setCommentMediaInfo(newMediaInfo);
} else {
setCommentMediaInfo(initialInfo);
setWebpageThumbnail(newMediaInfo);
}
};
loadThumbnail();
return () => {
setCommentMediaInfo(undefined);
};
}, [post]);
}, [initialInfo]);

const commentMediaInfo = webpageThumbnail || initialInfo;

const { url } = commentMediaInfo || {};
let type = commentMediaInfo?.type;
Expand Down
42 changes: 30 additions & 12 deletions src/components/post-mobile/post-mobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,24 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P
const isInPostPageView = isPostPageView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);

// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
const [commentMediaInfo, setCommentMediaInfo] = useState<CommentMediaInfo | undefined>();
const initialInfo = getCommentMediaInfo(post);
const [webpageThumbnail, setWebpageThumbnail] = useState<CommentMediaInfo | undefined>();
useEffect(() => {
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
const loadThumbnail = async () => {
const initialInfo = getCommentMediaInfo(post);
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
setCommentMediaInfo(newMediaInfo);
} else {
setCommentMediaInfo(initialInfo);
setWebpageThumbnail(newMediaInfo);
}
};
loadThumbnail();
return () => {
setCommentMediaInfo(undefined);
setWebpageThumbnail(undefined);
};
}, [post]);
}, [initialInfo]);
const commentMediaInfo = webpageThumbnail || initialInfo;

const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
const [showThumbnail, setShowThumbnail] = useState(true);

const isReply = parentCid;

Expand Down Expand Up @@ -184,13 +182,33 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P
</span>
</span>
</div>
{(hasThumbnail || link) && !(deleted || removed) && (
<CommentMedia commentMediaInfo={commentMediaInfo} post={post} showThumbnail={showThumbnail} setShowThumbnail={setShowThumbnail} />
)}
{(hasThumbnail || link) && !(deleted || removed) && <PostMediaContent key={cid} post={post} link={link} t={t} />}
</>
);
};

const PostMediaContent = ({ post, link, t }: { post: any; link: string; t: any }) => {
const initialInfo = getCommentMediaInfo(post);
const [webpageThumbnail, setWebpageThumbnail] = useState<CommentMediaInfo | undefined>();
const [showThumbnail, setShowThumbnail] = useState(true);

useEffect(() => {
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
const loadThumbnail = async () => {
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
setWebpageThumbnail(newMediaInfo);
}
};
loadThumbnail();
}, [initialInfo]);

const commentMediaInfo = webpageThumbnail || initialInfo;
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);

return hasThumbnail && <CommentMedia commentMediaInfo={commentMediaInfo} post={post} showThumbnail={showThumbnail} setShowThumbnail={setShowThumbnail} />;
};

const ReplyBacklinks = ({ post }: PostProps) => {
const { cid, parentCid } = post || {};
const replies = useReplies(post);
Expand Down
2 changes: 1 addition & 1 deletion src/views/post/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const PostPage = () => {
)}
{/* TODO: remove this replyCount error once api supports scrolling replies pages */}
{replyCount > 60 && <span className={styles.error}>Error: this thread has too many replies, some of them cannot be displayed right now.</span>}
{error && <span className={styles.error}>Error: {error?.message || error}</span>}
{error && <span className={styles.error}>Error: {error?.message || error?.toString?.()}</span>}
{isInDescriptionView ? (
<SubplebbitDescription
avatarUrl={suggested?.avatarUrl}
Expand Down

0 comments on commit ab85514

Please sign in to comment.