s3browser-frontend/src/components/FileOpen.tsx

68 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-07-28 10:49:35 +00:00
import React from "react"
import { useGetFileQuery } from "../generated/graphql"
import ImageOpener from "./ImageOpener"
import TextOpener from "./TextOpener"
2021-07-30 20:54:36 +00:00
import Modal from "./Modal"
2021-08-01 10:59:54 +00:00
import AudioOpener from "./AudioOpener"
2021-07-28 10:49:35 +00:00
interface Props {
id: string
show: boolean
onCloseClick?: ()=>void
}
const FileOpen: React.FC<Props> = (props) => {
2021-07-31 18:52:26 +00:00
if (!props.id) {
return <></>
}
2021-07-28 10:49:35 +00:00
const { data } = useGetFileQuery({
variables:{
id: props.id
}
})
let opener = <div>TODO</div>
if(data?.file != null){
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
if (data.file.contentType?.startsWith("image/")){
2021-08-01 10:59:54 +00:00
opener = <ImageOpener file={data?.file} active={props.show} />
2021-07-28 10:49:35 +00:00
}else if (data.file.contentType?.startsWith("text/")){
2021-08-01 10:59:54 +00:00
opener = <TextOpener file={data.file} active={props.show} />
2021-07-28 10:49:35 +00:00
}else{
// Get Opener bases on file extension
const ext = data.file.name?.split(".").pop()
switch (ext) {
case "png":
case "jpg":
case "jpeg":
case "gif":
// TODO: more ext
2021-08-01 10:59:54 +00:00
opener = <ImageOpener file={data.file} active={props.show} />
2021-07-28 10:49:35 +00:00
break
case "txt":
case "md":
2021-08-01 10:59:54 +00:00
opener = <TextOpener file={data.file} active={props.show} />
break
case "mp3":
opener = <AudioOpener file={data.file} active={props.show} />
2021-07-28 10:49:35 +00:00
break
default:
2021-08-01 10:59:54 +00:00
opener = <ImageOpener file={data?.file} active={props.show} />
2021-07-28 10:49:35 +00:00
break
}
}
}
return (
2021-07-30 20:54:36 +00:00
<Modal onCloseClick={()=>{
props.onCloseClick?.()
}} show={props.show} >
{opener}
</Modal>
2021-07-28 10:49:35 +00:00
)
}
export default FileOpen