mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix thumbnail performance
This commit is contained in:
@@ -214,25 +214,28 @@ const PostInfo = ({ openReplyModal, post, postReplyCount = 0, roles, isHidden }:
|
|||||||
|
|
||||||
const PostMedia = ({ post }: PostProps) => {
|
const PostMedia = ({ post }: PostProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { link, spoiler } = post || {};
|
const { link, spoiler, cid } = post || {};
|
||||||
|
|
||||||
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
|
// Reset state by remounting component when post changes
|
||||||
const [commentMediaInfo, setCommentMediaInfo] = useState<CommentMediaInfo | undefined>();
|
return <PostMediaContent key={cid} post={post} link={link} spoiler={spoiler} t={t} />;
|
||||||
useEffect(() => {
|
};
|
||||||
const loadThumbnail = async () => {
|
|
||||||
|
const PostMediaContent = ({ post, link, spoiler, t }: { post: any; link: string; spoiler: boolean; t: any }) => {
|
||||||
const initialInfo = getCommentMediaInfo(post);
|
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 () => {
|
||||||
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
|
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
|
||||||
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
|
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
|
||||||
setCommentMediaInfo(newMediaInfo);
|
setWebpageThumbnail(newMediaInfo);
|
||||||
} else {
|
|
||||||
setCommentMediaInfo(initialInfo);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadThumbnail();
|
loadThumbnail();
|
||||||
return () => {
|
}, [initialInfo]);
|
||||||
setCommentMediaInfo(undefined);
|
|
||||||
};
|
const commentMediaInfo = webpageThumbnail || initialInfo;
|
||||||
}, [post]);
|
|
||||||
|
|
||||||
const { url } = commentMediaInfo || {};
|
const { url } = commentMediaInfo || {};
|
||||||
let type = commentMediaInfo?.type;
|
let type = commentMediaInfo?.type;
|
||||||
|
|||||||
@@ -40,26 +40,24 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P
|
|||||||
const isInPostPageView = isPostPageView(location.pathname, params);
|
const isInPostPageView = isPostPageView(location.pathname, params);
|
||||||
const isInSubscriptionsView = isSubscriptionsView(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>();
|
|
||||||
useEffect(() => {
|
|
||||||
const loadThumbnail = async () => {
|
|
||||||
const initialInfo = getCommentMediaInfo(post);
|
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 () => {
|
||||||
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
|
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
|
||||||
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
|
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
|
||||||
setCommentMediaInfo(newMediaInfo);
|
setWebpageThumbnail(newMediaInfo);
|
||||||
} else {
|
|
||||||
setCommentMediaInfo(initialInfo);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadThumbnail();
|
loadThumbnail();
|
||||||
return () => {
|
return () => {
|
||||||
setCommentMediaInfo(undefined);
|
setWebpageThumbnail(undefined);
|
||||||
};
|
};
|
||||||
}, [post]);
|
}, [initialInfo]);
|
||||||
|
const commentMediaInfo = webpageThumbnail || initialInfo;
|
||||||
|
|
||||||
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
|
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
|
||||||
const [showThumbnail, setShowThumbnail] = useState(true);
|
|
||||||
|
|
||||||
const isReply = parentCid;
|
const isReply = parentCid;
|
||||||
|
|
||||||
@@ -184,13 +182,33 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P
|
|||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{(hasThumbnail || link) && !(deleted || removed) && (
|
{(hasThumbnail || link) && !(deleted || removed) && <PostMediaContent key={cid} post={post} link={link} t={t} />}
|
||||||
<CommentMedia commentMediaInfo={commentMediaInfo} post={post} showThumbnail={showThumbnail} setShowThumbnail={setShowThumbnail} />
|
|
||||||
)}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 ReplyBacklinks = ({ post }: PostProps) => {
|
||||||
const { cid, parentCid } = post || {};
|
const { cid, parentCid } = post || {};
|
||||||
const replies = useReplies(post);
|
const replies = useReplies(post);
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ const PostPage = () => {
|
|||||||
)}
|
)}
|
||||||
{/* TODO: remove this replyCount error once api supports scrolling replies pages */}
|
{/* 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>}
|
{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 ? (
|
{isInDescriptionView ? (
|
||||||
<SubplebbitDescription
|
<SubplebbitDescription
|
||||||
avatarUrl={suggested?.avatarUrl}
|
avatarUrl={suggested?.avatarUrl}
|
||||||
|
|||||||
Reference in New Issue
Block a user