import { Field, Form, Formik } from "formik"; import FormGroup from "../components/FormGroup"; import type { FormValues, Hotlist } from "../../../types/types"; import { useSoundContext } from "../../../context/SoundContext"; import { useCameraBlackboard } from "../../../hooks/useCameraBlackboard"; import { toast } from "sonner"; import SliderComponent from "../../UI/Slider"; import { useFileUpload } from "../../../hooks/useFileUpload"; import { useSound } from "react-sounds"; import { useMemo } from "react"; import { getSoundFileURL } from "../../../utils/utils"; const SoundSettingsFields = () => { const { state, dispatch } = useSoundContext(); const { mutation } = useCameraBlackboard(); const { query } = useFileUpload({ queryKey: state.sightingSound ? [state.sightingSound] : undefined, }); const hotlists: Hotlist[] = state.hotlists; const soundOptions = state?.soundOptions?.map((soundOption) => ({ value: soundOption?.soundFileName, label: soundOption?.name, })); const initialValues: FormValues = { sightingSound: state.sightingSound ?? "switch", NPEDsound: state.NPEDsound ?? "popup", hotlistSound: state.hotlistSound ?? "notification", hotlists, }; const soundSrc = useMemo(() => { if (state?.sightingSound?.includes(".mp3") || state.sightingSound?.includes(".wav")) { const file = state.soundOptions?.find((item) => item.name === state.sightingSound); query?.refetch(); console.log(query?.data); // set state dispatch({ type: "UPLOADEDSOUND", payload: query?.data }); if (!query?.data) { query?.refetch(); return; } //get from state? if (!state.uploadedSound) return "switchSound"; const url = URL.createObjectURL(state.uploadedSound); return url ?? "switchSound"; } return getSoundFileURL(state?.sightingSound) ?? "switchSound"; }, [state.sightingSound, state.soundOptions]); const { play } = useSound(soundSrc); const handletest = () => { console.log(state.uploadedSound); play(); }; const handleSubmit = async (values: FormValues) => { const updatedValues = { ...values, sightingVolume: state.sightingVolume, NPEDsoundVolume: state.NPEDsoundVolume, hotlistSoundVolume: state.hotlistSoundVolume, soundOptions: [...(state.soundOptions ?? [])], }; dispatch({ type: "UPDATE", payload: updatedValues }); const result = await mutation.mutateAsync({ operation: "INSERT", path: "soundSettings", value: updatedValues, }); if (result.reason !== "OK") { toast.error("Cannot update sound settings"); } else { toast.success("Sound Settings successfully updated"); } }; return ( {() => (
{soundOptions?.map(({ value, label }) => { return ( ); })}
{soundOptions?.map(({ value, label }) => ( ))}

Hotlist Sounds

{soundOptions?.map(({ value, label }) => ( ))}
{/* (
{values?.hotlists?.length > 0 ? ( values?.hotlists?.map((hotlist, index) => (
{soundOptions?.map(({ value, label }) => ( ))}
)) ) : (

No hotlists yet, Add one

)}
)} />
*/}
)}
); }; export default SoundSettingsFields;