FileOpenerProps

This commit is contained in:
Djeeberjr 2021-08-01 12:59:54 +02:00
parent c4cd110fd2
commit 5a31062b21
5 changed files with 61 additions and 20 deletions

View File

@ -0,0 +1,41 @@
import React from "react"
import { useEffect } from "react"
import genDownloadLink from "../functions/genDownloadLink"
import { FileOpenerProps } from "../types/FileOpenerProps"
const AudioOpener: React.FC<FileOpenerProps> = (props) => {
let audio: HTMLAudioElement
useEffect(()=>{
if (audio){
audio.pause()
audio.remove()
}
audio = new Audio(genDownloadLink(props.file.id))
audio.volume = 0.1
},[props.file])
useEffect(()=>{
if(!props.active){
audio.pause
console.debug("Pause")
}
console.debug("active",props.active)
},[props.active])
return (
<div>
<button onClick={()=>{
if (audio){
(audio.paused && audio.play()) || audio.pause()
}
}}>
Play/Pause
</button>
</div>
)
}
export default AudioOpener

View File

@ -4,6 +4,7 @@ import { useGetFileQuery } from "../generated/graphql"
import ImageOpener from "./ImageOpener" import ImageOpener from "./ImageOpener"
import TextOpener from "./TextOpener" import TextOpener from "./TextOpener"
import Modal from "./Modal" import Modal from "./Modal"
import AudioOpener from "./AudioOpener"
interface Props { interface Props {
id: string id: string
@ -28,9 +29,9 @@ const FileOpen: React.FC<Props> = (props) => {
if(data?.file != null){ if(data?.file != null){
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
if (data.file.contentType?.startsWith("image/")){ if (data.file.contentType?.startsWith("image/")){
opener = <ImageOpener file={data?.file} /> opener = <ImageOpener file={data?.file} active={props.show} />
}else if (data.file.contentType?.startsWith("text/")){ }else if (data.file.contentType?.startsWith("text/")){
opener = <TextOpener file={data.file} /> opener = <TextOpener file={data.file} active={props.show} />
}else{ }else{
// Get Opener bases on file extension // Get Opener bases on file extension
const ext = data.file.name?.split(".").pop() const ext = data.file.name?.split(".").pop()
@ -40,14 +41,17 @@ const FileOpen: React.FC<Props> = (props) => {
case "jpeg": case "jpeg":
case "gif": case "gif":
// TODO: more ext // TODO: more ext
opener = <ImageOpener file={data.file} /> opener = <ImageOpener file={data.file} active={props.show} />
break break
case "txt": case "txt":
case "md": case "md":
opener = <TextOpener file={data.file} /> opener = <TextOpener file={data.file} active={props.show} />
break
case "mp3":
opener = <AudioOpener file={data.file} active={props.show} />
break break
default: default:
opener = <ImageOpener file={data?.file} /> opener = <ImageOpener file={data?.file} active={props.show} />
break break
} }
} }

View File

@ -1,18 +1,12 @@
import React from "react" import React from "react"
import PropTypes from "prop-types" import PropTypes from "prop-types"
import { File } from "../generated/graphql"
import genDownloadLink from "../functions/genDownloadLink" import genDownloadLink from "../functions/genDownloadLink"
import style from "./../App.module.scss" import style from "./../App.module.scss"
import { FileOpenerProps } from "../types/FileOpenerProps"
interface Props { const ImageOpener: React.FC<FileOpenerProps> = (props) => {
file: File
}
const ImageOpener: React.FC<Props> = (props) => {
return ( return (
<div className={style.imageOpener}> <img className={`img-responsive img-fit-cover ${style.imageOpenerImage}`} src={genDownloadLink(props.file.id)}/>
<img className={`img-responsive img-fit-contain ${style.imageOpenerImage}`} src={genDownloadLink(props.file.id)}/>
</div>
) )
} }

View File

@ -1,12 +1,8 @@
import React from "react" import React from "react"
import PropTypes from "prop-types" import PropTypes from "prop-types"
import { File } from "../generated/graphql" import { FileOpenerProps } from "../types/FileOpenerProps"
interface Props { const TextOpener: React.FC<FileOpenerProps> = (props) => {
file: File
}
const TextOpener: React.FC<Props> = (props) => {
return ( return (
<div> <div>
<textarea> <textarea>

View File

@ -0,0 +1,6 @@
import { File } from "../generated/graphql"
export interface FileOpenerProps{
file: File
active: boolean
}