83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import { Button, Spin } from 'antd';
|
|
import type { DouyuTaskItem } from '../api/types';
|
|
import { getExchangeImage, savedExchangeImage } from '../utils/exchangeImage';
|
|
|
|
interface Props {
|
|
task: DouyuTaskItem;
|
|
onClick?: (task: DouyuTaskItem, url: string) => void;
|
|
variant?: 'compact' | 'detail';
|
|
}
|
|
|
|
export default function ExchangeResultImage({ task, onClick, variant = 'compact' }: Props) {
|
|
const [url, setUrl] = useState('');
|
|
const [failed, setFailed] = useState(false);
|
|
const [gen, setGen] = useState(0);
|
|
const savedObjectKey = savedExchangeImage(task)?.objectKey || '';
|
|
const taskRef = useRef(task);
|
|
|
|
useEffect(() => {
|
|
taskRef.current = task;
|
|
}, [task]);
|
|
|
|
useEffect(() => {
|
|
let alive = true;
|
|
setUrl('');
|
|
setFailed(false);
|
|
getExchangeImage(taskRef.current)
|
|
.then(({ url }) => { if (alive) setUrl(url); })
|
|
.catch(() => { if (alive) setFailed(true); });
|
|
return () => { alive = false; };
|
|
}, [task.id, task.finished_at, savedObjectKey, gen]);
|
|
|
|
if (failed) {
|
|
return (
|
|
<Button
|
|
size="small"
|
|
type="text"
|
|
onClick={() => setGen((g) => g + 1)}
|
|
>
|
|
重试
|
|
</Button>
|
|
);
|
|
}
|
|
if (!url) return <Spin size="small" />;
|
|
const detail = variant === 'detail';
|
|
return (
|
|
<div
|
|
onClick={() => onClick?.(task, url)}
|
|
style={{
|
|
cursor: onClick ? 'pointer' : 'default',
|
|
display: 'inline-block',
|
|
lineHeight: 0,
|
|
width: detail ? '100%' : undefined,
|
|
textAlign: detail ? 'center' : undefined,
|
|
}}
|
|
title={onClick ? '点击查看大图并复制' : undefined}
|
|
>
|
|
<img
|
|
src={url}
|
|
alt="兑换结果"
|
|
style={
|
|
detail
|
|
? {
|
|
width: '100%',
|
|
maxWidth: 480,
|
|
height: 'auto',
|
|
objectFit: 'contain',
|
|
borderRadius: 8,
|
|
border: '1px solid #f0f0f0',
|
|
}
|
|
: {
|
|
width: 56,
|
|
height: 82,
|
|
objectFit: 'cover',
|
|
borderRadius: 6,
|
|
border: '1px solid #f0f0f0',
|
|
}
|
|
}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|