-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
565 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
napcat.webui/src/components/file_manage/file_preview_modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Button } from '@heroui/button' | ||
import { | ||
Modal, | ||
ModalBody, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader | ||
} from '@heroui/modal' | ||
import { Spinner } from '@heroui/spinner' | ||
import { useRequest } from 'ahooks' | ||
import path from 'path-browserify' | ||
|
||
import FileManager from '@/controllers/file_manager' | ||
|
||
interface FilePreviewModalProps { | ||
isOpen: boolean | ||
filePath: string | ||
onClose: () => void | ||
} | ||
|
||
const imageExts = ['.png', '.jpg', '.jpeg', '.gif', '.bmp'] | ||
const videoExts = ['.mp4', '.webm'] | ||
const audioExts = ['.mp3', '.wav'] | ||
|
||
const supportedPreviewExts = [...imageExts, ...videoExts, ...audioExts] | ||
|
||
export default function FilePreviewModal({ | ||
isOpen, | ||
filePath, | ||
onClose | ||
}: FilePreviewModalProps) { | ||
const ext = path.extname(filePath).toLowerCase() | ||
const { data, loading, error, run } = useRequest( | ||
async (path: string) => FileManager.downloadToURL(path), | ||
{ | ||
refreshDeps: [filePath], | ||
refreshDepsAction: () => { | ||
const ext = path.extname(filePath).toLowerCase() | ||
if (!filePath || !supportedPreviewExts.includes(ext)) { | ||
return | ||
} | ||
run(filePath) | ||
} | ||
} | ||
) | ||
|
||
let contentElement = null | ||
if (!supportedPreviewExts.includes(ext)) { | ||
contentElement = <div>暂不支持预览此文件类型</div> | ||
} else if (error) { | ||
contentElement = <div>读取文件失败</div> | ||
} else if (loading || !data) { | ||
contentElement = ( | ||
<div className="flex justify-center items-center h-full"> | ||
<Spinner /> | ||
</div> | ||
) | ||
} else if (imageExts.includes(ext)) { | ||
contentElement = ( | ||
<img src={data} alt="预览" className="max-w-full max-h-96" /> | ||
) | ||
} else if (videoExts.includes(ext)) { | ||
contentElement = ( | ||
<video src={data} controls className="max-w-full max-h-96" /> | ||
) | ||
} else if (audioExts.includes(ext)) { | ||
contentElement = <audio src={data} controls className="w-full" /> | ||
} | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onClose={onClose} scrollBehavior="inside"> | ||
<ModalContent> | ||
<ModalHeader>文件预览</ModalHeader> | ||
<ModalBody className="flex justify-center items-center"> | ||
{contentElement} | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="danger" variant="flat" onPress={onClose}> | ||
关闭 | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.