mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix thumbnails could get stuck
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Trans, useTranslation } from 'react-i18next';
|
import { Trans, useTranslation } from 'react-i18next';
|
||||||
import { Link, useLocation, useParams } from 'react-router-dom';
|
import { Link, useLocation, useParams } from 'react-router-dom';
|
||||||
import { Comment, useAuthorAvatar, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
import { Comment, useAuthorAvatar, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
||||||
import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js';
|
import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js';
|
||||||
import styles from '../../views/post/post.module.css';
|
import styles from '../../views/post/post.module.css';
|
||||||
import { fetchWebpageThumbnailIfNeeded, getCommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail, getMediaDimensions } from '../../lib/utils/media-utils';
|
import {
|
||||||
|
CommentMediaInfo,
|
||||||
|
fetchWebpageThumbnailIfNeeded,
|
||||||
|
getCommentMediaInfo,
|
||||||
|
getDisplayMediaInfoType,
|
||||||
|
getHasThumbnail,
|
||||||
|
getMediaDimensions,
|
||||||
|
} from '../../lib/utils/media-utils';
|
||||||
import { hashStringToColor, getTextColorForBackground } from '../../lib/utils/post-utils';
|
import { hashStringToColor, getTextColorForBackground } from '../../lib/utils/post-utils';
|
||||||
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
|
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
|
||||||
import { isValidURL } from '../../lib/utils/url-utils';
|
import { isValidURL } from '../../lib/utils/url-utils';
|
||||||
@@ -210,19 +217,22 @@ const PostMedia = ({ post }: PostProps) => {
|
|||||||
const { link, spoiler } = post || {};
|
const { link, spoiler } = post || {};
|
||||||
|
|
||||||
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
|
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
|
||||||
const initialCommentMediaInfo = useMemo(() => getCommentMediaInfo(post), [post]);
|
const [commentMediaInfo, setCommentMediaInfo] = useState<CommentMediaInfo | undefined>();
|
||||||
const [commentMediaInfo, setCommentMediaInfo] = useState(initialCommentMediaInfo);
|
|
||||||
|
|
||||||
const fetchThumbnail = useCallback(async () => {
|
|
||||||
if (initialCommentMediaInfo?.type === 'webpage' && !initialCommentMediaInfo.thumbnail) {
|
|
||||||
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialCommentMediaInfo);
|
|
||||||
setCommentMediaInfo(newMediaInfo);
|
|
||||||
}
|
|
||||||
}, [initialCommentMediaInfo]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchThumbnail();
|
const loadThumbnail = async () => {
|
||||||
}, [fetchThumbnail]);
|
const initialInfo = getCommentMediaInfo(post);
|
||||||
|
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
|
||||||
|
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
|
||||||
|
setCommentMediaInfo(newMediaInfo);
|
||||||
|
} else {
|
||||||
|
setCommentMediaInfo(initialInfo);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
loadThumbnail();
|
||||||
|
return () => {
|
||||||
|
setCommentMediaInfo(undefined);
|
||||||
|
};
|
||||||
|
}, [post]);
|
||||||
|
|
||||||
const { url } = commentMediaInfo || {};
|
const { url } = commentMediaInfo || {};
|
||||||
let type = commentMediaInfo?.type;
|
let type = commentMediaInfo?.type;
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Trans, useTranslation } from 'react-i18next';
|
import { Trans, useTranslation } from 'react-i18next';
|
||||||
import { Link, useLocation, useParams } from 'react-router-dom';
|
import { Link, useLocation, useParams } from 'react-router-dom';
|
||||||
import { Comment, useAuthorAvatar, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
import { Comment, useAuthorAvatar, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
||||||
import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js';
|
import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js';
|
||||||
import styles from '../../views/post/post.module.css';
|
import styles from '../../views/post/post.module.css';
|
||||||
import { fetchWebpageThumbnailIfNeeded, getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
|
import { CommentMediaInfo, fetchWebpageThumbnailIfNeeded, getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
|
||||||
import { getTextColorForBackground, hashStringToColor } from '../../lib/utils/post-utils';
|
import { getTextColorForBackground, hashStringToColor } from '../../lib/utils/post-utils';
|
||||||
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
|
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
|
||||||
import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
||||||
@@ -41,19 +41,22 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P
|
|||||||
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
|
// some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false
|
||||||
const initialCommentMediaInfo = useMemo(() => getCommentMediaInfo(post), [post]);
|
const [commentMediaInfo, setCommentMediaInfo] = useState<CommentMediaInfo | undefined>();
|
||||||
const [commentMediaInfo, setCommentMediaInfo] = useState(initialCommentMediaInfo);
|
|
||||||
|
|
||||||
const fetchThumbnail = useCallback(async () => {
|
|
||||||
if (initialCommentMediaInfo?.type === 'webpage' && !initialCommentMediaInfo.thumbnail) {
|
|
||||||
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialCommentMediaInfo);
|
|
||||||
setCommentMediaInfo(newMediaInfo);
|
|
||||||
}
|
|
||||||
}, [initialCommentMediaInfo]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchThumbnail();
|
const loadThumbnail = async () => {
|
||||||
}, [fetchThumbnail]);
|
const initialInfo = getCommentMediaInfo(post);
|
||||||
|
if (initialInfo?.type === 'webpage' && !initialInfo.thumbnail) {
|
||||||
|
const newMediaInfo = await fetchWebpageThumbnailIfNeeded(initialInfo);
|
||||||
|
setCommentMediaInfo(newMediaInfo);
|
||||||
|
} else {
|
||||||
|
setCommentMediaInfo(initialInfo);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
loadThumbnail();
|
||||||
|
return () => {
|
||||||
|
setCommentMediaInfo(undefined);
|
||||||
|
};
|
||||||
|
}, [post]);
|
||||||
|
|
||||||
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
|
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
|
||||||
const [showThumbnail, setShowThumbnail] = useState(true);
|
const [showThumbnail, setShowThumbnail] = useState(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user