增加动漫数据源配置
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
'use client';
|
||||
|
||||
import React from 'react';
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
|
||||
import { processImageUrl, tryApplyDoubanImageFallback } from '@/lib/utils';
|
||||
import {
|
||||
processImageUrl,
|
||||
tryApplyBangumiImageFallback,
|
||||
tryApplyDoubanImageFallback,
|
||||
} from '@/lib/utils';
|
||||
|
||||
interface ProxyImageProps extends React.ImgHTMLAttributes<HTMLImageElement> {
|
||||
originalSrc: string;
|
||||
@@ -22,17 +26,49 @@ const ProxyImage: React.FC<ProxyImageProps> = ({
|
||||
src: _src,
|
||||
...props
|
||||
}) => {
|
||||
const initialSrc = useMemo(
|
||||
() => displaySrc || processImageUrl(originalSrc),
|
||||
[displaySrc, originalSrc]
|
||||
);
|
||||
const [currentSrc, setCurrentSrc] = useState(initialSrc);
|
||||
const imgRef = useRef<HTMLImageElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setCurrentSrc(initialSrc);
|
||||
}, [initialSrc]);
|
||||
|
||||
useEffect(() => {
|
||||
if (displaySrc) return;
|
||||
|
||||
const timer = window.setTimeout(() => {
|
||||
const img = imgRef.current;
|
||||
if (!img || img.complete || img.dataset.bangumiBackupTried === 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tryApplyBangumiImageFallback(img, originalSrc)) {
|
||||
setCurrentSrc(img.src);
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [currentSrc, displaySrc, originalSrc]);
|
||||
|
||||
const handleError = (e: React.SyntheticEvent<HTMLImageElement, Event>) => {
|
||||
const img = e.currentTarget;
|
||||
|
||||
if (tryApplyDoubanImageFallback(img, originalSrc)) {
|
||||
if (
|
||||
tryApplyDoubanImageFallback(img, originalSrc) ||
|
||||
tryApplyBangumiImageFallback(img, originalSrc)
|
||||
) {
|
||||
setCurrentSrc(img.src);
|
||||
return;
|
||||
}
|
||||
|
||||
if (retryOnError && !img.dataset.retried) {
|
||||
img.dataset.retried = 'true';
|
||||
window.setTimeout(() => {
|
||||
img.src = displaySrc || processImageUrl(originalSrc);
|
||||
setCurrentSrc(initialSrc);
|
||||
}, retryDelay);
|
||||
}
|
||||
|
||||
@@ -42,7 +78,8 @@ const ProxyImage: React.FC<ProxyImageProps> = ({
|
||||
return (
|
||||
<img
|
||||
{...props}
|
||||
src={displaySrc || processImageUrl(originalSrc)}
|
||||
ref={imgRef}
|
||||
src={currentSrc}
|
||||
loading={loading}
|
||||
decoding={decoding}
|
||||
onError={handleError}
|
||||
|
||||
Reference in New Issue
Block a user