jam-tracks / frontend /src /hooks /useSession.js
Mina Emadi
Move player runtime to browser-only Node app
aad4714
Raw History Blame Contribute Delete
1.42 kB
import { useState, useCallback } from 'react'
import {
getPresetManifestEntry,
presetToChordsData,
presetToUploadData
} from '../utils/presetManifest'
export function useSession() {
const [sessionId, setSessionId] = useState(null)
const [stems, setStems] = useState([])
const [detection, setDetection] = useState(null)
const [chordsData, setChordsData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const clearError = useCallback(() => {
setError(null)
}, [])
const loadPreset = useCallback(async (presetName) => {
setLoading(true)
setError(null)
try {
const preset = await getPresetManifestEntry(presetName)
if (!preset) {
throw new Error(`Preset '${presetName}' is missing from the manifest.`)
}
const localSessionId = `preset:${presetName}`
const uploadData = presetToUploadData(preset, localSessionId)
setSessionId(localSessionId)
setStems(uploadData.stems)
setDetection(preset.detection)
setChordsData(presetToChordsData(preset))
return { upload: uploadData, detection: preset.detection }
} catch (err) {
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [])
return {
sessionId,
stems,
detection,
chordsData,
loading,
error,
loadPreset,
clearError
}
}