Files
NapCatQQ/napcat.webui/src/components/file_manage/image_name_button.tsx
T
时瑾 06f6a542f5 refactor: 优化eslint配置,提升代码质量 (#1341)
* feat: 统一并标准化eslint

* lint: napcat.webui

* lint: napcat.webui

* lint: napcat.core

* build: fix

* lint: napcat.webui

* refactor: 重构eslint

* Update README.md
2025-11-03 16:30:45 +08:00

93 lines
2.0 KiB
TypeScript

import { Button } from '@heroui/button';
import { Image } from '@heroui/image';
import { Spinner } from '@heroui/spinner';
import { useRequest } from 'ahooks';
import path from 'path-browserify';
import { useEffect } from 'react';
import FileManager from '@/controllers/file_manager';
import FileIcon from '../file_icon';
export interface PreviewImage {
key: string
src: string
alt: string
}
export const imageExts = ['.png', '.jpg', '.jpeg', '.gif', '.bmp'];
export interface ImageNameButtonProps {
name: string
filePath: string
onPreview: () => void
onAddPreview: (image: PreviewImage) => void
}
export default function ImageNameButton ({
name,
filePath,
onPreview,
onAddPreview,
}: ImageNameButtonProps) {
const { data, loading, error, run } = useRequest(
async () => FileManager.downloadToURL(filePath),
{
refreshDeps: [filePath],
manual: true,
refreshDepsAction: () => {
const ext = path.extname(filePath).toLowerCase();
if (!filePath || !imageExts.includes(ext)) {
return;
}
run();
},
}
);
useEffect(() => {
if (data) {
onAddPreview({
key: name,
src: data,
alt: name,
});
}
}, [data, name, onAddPreview]);
useEffect(() => {
if (filePath) {
run();
}
}, []);
return (
<Button
variant='light'
className='text-left justify-start'
onPress={onPreview}
startContent={
error
? (
<FileIcon name={name} isDirectory={false} />
)
: loading || !data
? (
<Spinner size='sm' />
)
: (
<Image
src={data}
alt={name}
className='w-8 h-8 flex-shrink-0'
classNames={{
wrapper: 'w-8 h-8 flex-shrink-0',
}}
radius='sm'
/>
)
}
>
{name}
</Button>
);
}