add file manager

This commit is contained in:
Qing
2022-12-31 21:07:08 +08:00
parent b847ded828
commit 2dd95be90d
17 changed files with 5402 additions and 5872 deletions

View File

@@ -0,0 +1,90 @@
import React, { ReactNode, useEffect, useMemo, useState } from 'react'
import PhotoAlbum, { RenderPhoto } from 'react-photo-album'
import Modal from '../shared/Modal'
interface Photo {
src: string
height: number
width: number
}
interface Filename {
name: string
height: number
width: number
}
const renderPhoto: RenderPhoto = ({
layout,
layoutOptions,
imageProps: { alt, style, ...restImageProps },
}) => (
<div
style={{
boxSizing: 'content-box',
alignItems: 'center',
}}
>
<img
alt={alt}
style={{ ...style, width: '100%', padding: 0 }}
{...restImageProps}
/>
</div>
)
interface Props {
show: boolean
onClose: () => void
onPhotoClick: (filename: string) => void
}
export default function FileManager(props: Props) {
const { show, onClose, onPhotoClick } = props
const [filenames, setFileNames] = useState<Filename[]>([])
const onClick = ({ index }: { index: number }) => {
onPhotoClick(filenames[index].name)
}
useEffect(() => {
const fetchData = async () => {
const res = await fetch('/medias')
if (res.ok) {
const newFilenames = await res.json()
setFileNames(newFilenames)
}
}
fetchData()
}, [])
const photos = useMemo(() => {
return filenames.map((filename: Filename) => {
const width = 256
const height = filename.height * (width / filename.width)
const src = `/media_thumbnail/${filename.name}?width=${width}&height=${height}`
return { src, height, width }
})
}, [filenames])
return (
<Modal
onClose={onClose}
title="Files"
className="file-manager-modal"
show={show}
>
<div className="file-manager">
<PhotoAlbum
layout="columns"
photos={photos}
renderPhoto={renderPhoto}
spacing={6}
padding={4}
onClick={onClick}
/>
</div>
</Modal>
)
}