Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 2,480 Bytes
c1f12bf 89b09eb c1f12bf 89b09eb 5bd8810 b594e33 5bd8810 89b09eb c1f12bf 5bd8810 89b09eb 5bd8810 c1f12bf 89b09eb c1f12bf 89b09eb c1f12bf 89b09eb 5bd8810 89b09eb c1f12bf 89b09eb c1f12bf 89b09eb c1f12bf 89b09eb c1f12bf 89b09eb 5bd8810 89b09eb 5bd8810 89b09eb c1f12bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import { useEffect, useState } from 'react'
import { useFilePicker } from 'use-file-picker'
import { parseFileName } from '@/services/io/parseFileName'
import { useIO } from '@/services/io/useIO'
const defaultSupportedExtensions = ['clap', 'txt', 'mp4', 'mp3']
export function useOpenFilePicker(
{
supportedExtensions = defaultSupportedExtensions,
}: {
supportedExtensions: string[]
} = {
supportedExtensions: defaultSupportedExtensions,
}
) {
const [isLoading, setIsLoading] = useState(false)
const openClapBlob = useIO((s) => s.openClapBlob)
const openScreenplay = useIO((s) => s.openScreenplay)
const openVideo = useIO((s) => s.openVideo)
const { openFilePicker, filesContent, loading } = useFilePicker({
accept: supportedExtensions.map((ext) => `.${ext}`),
readAs: 'ArrayBuffer',
})
const fileData = filesContent[0]
useEffect(() => {
const fn = async () => {
const input = `${fileData?.name || ''}`
if (!input) {
return
}
const { fileName, projectName, extension } = parseFileName(input)
if (!defaultSupportedExtensions.includes(extension)) {
console.error(`unsupported extension "${extension}"`)
return
}
const blob = new Blob([fileData.content])
if (extension === 'clap') {
try {
setIsLoading(true)
await openClapBlob(projectName, fileName, blob)
} catch (err) {
console.error('failed to load the Clap file:', err)
} finally {
setIsLoading(false)
}
} else if (extension === 'txt') {
try {
setIsLoading(true)
await openScreenplay(projectName, fileName, blob)
} catch (err) {
console.error('failed to load the Clap file:', err)
} finally {
setIsLoading(false)
}
} else if (extension === 'mp4') {
try {
setIsLoading(true)
await openVideo(projectName, fileName, blob)
} catch (err) {
console.error('failed to load the Clap file:', err)
} finally {
setIsLoading(false)
}
} else if (extension === 'mp3') {
alert('Initializing a project from a mp3 is not supported yet')
}
}
fn()
}, [
fileData?.name,
fileData?.content,
openClapBlob,
openScreenplay,
openVideo,
])
return {
openFilePicker,
filesContent,
fileData,
isLoading: loading || isLoading,
}
}
|