2025-10-20 11:32:45 +01:00
|
|
|
import { Field, Form, Formik } from "formik";
|
2025-09-30 13:25:11 +01:00
|
|
|
import FormGroup from "../components/FormGroup";
|
|
|
|
|
import type { FormValues, Hotlist } from "../../../types/types";
|
2025-09-30 14:51:37 +01:00
|
|
|
import { useSoundContext } from "../../../context/SoundContext";
|
2025-10-08 11:08:41 +01:00
|
|
|
import { useCameraBlackboard } from "../../../hooks/useCameraBlackboard";
|
2025-09-30 15:32:00 +01:00
|
|
|
import { toast } from "sonner";
|
2025-10-17 16:12:02 +01:00
|
|
|
import SliderComponent from "../../UI/Slider";
|
2025-10-22 16:12:49 +01:00
|
|
|
import { useFileUpload } from "../../../hooks/useFileUpload";
|
|
|
|
|
import { useSound } from "react-sounds";
|
2025-10-28 08:50:55 +00:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { getSoundFileURL } from "../../../utils/utils";
|
2025-09-30 13:25:11 +01:00
|
|
|
|
|
|
|
|
const SoundSettingsFields = () => {
|
2025-09-30 14:51:37 +01:00
|
|
|
const { state, dispatch } = useSoundContext();
|
2025-10-08 11:08:41 +01:00
|
|
|
const { mutation } = useCameraBlackboard();
|
2025-10-22 16:12:49 +01:00
|
|
|
|
|
|
|
|
const { query } = useFileUpload({
|
|
|
|
|
queryKey: state.sightingSound ? [state.sightingSound] : undefined,
|
|
|
|
|
});
|
2025-10-08 11:08:41 +01:00
|
|
|
|
|
|
|
|
const hotlists: Hotlist[] = state.hotlists;
|
2025-09-30 13:25:11 +01:00
|
|
|
|
2025-10-01 15:21:07 +01:00
|
|
|
const soundOptions = state?.soundOptions?.map((soundOption) => ({
|
2025-10-20 11:32:45 +01:00
|
|
|
value: soundOption?.soundFileName,
|
2025-10-01 15:21:07 +01:00
|
|
|
label: soundOption?.name,
|
|
|
|
|
}));
|
2025-09-30 13:25:11 +01:00
|
|
|
|
|
|
|
|
const initialValues: FormValues = {
|
2025-09-30 14:51:37 +01:00
|
|
|
sightingSound: state.sightingSound ?? "switch",
|
|
|
|
|
NPEDsound: state.NPEDsound ?? "popup",
|
2025-10-20 11:32:45 +01:00
|
|
|
hotlistSound: state.hotlistSound ?? "notification",
|
2025-09-30 13:25:11 +01:00
|
|
|
hotlists,
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-28 08:50:55 +00:00
|
|
|
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);
|
2025-10-22 16:12:49 +01:00
|
|
|
const handletest = () => {
|
2025-10-28 08:50:55 +00:00
|
|
|
console.log(state.uploadedSound);
|
|
|
|
|
play();
|
2025-10-22 16:12:49 +01:00
|
|
|
};
|
2025-10-08 11:08:41 +01:00
|
|
|
const handleSubmit = async (values: FormValues) => {
|
2025-10-20 09:11:05 +01:00
|
|
|
const updatedValues = {
|
|
|
|
|
...values,
|
|
|
|
|
sightingVolume: state.sightingVolume,
|
|
|
|
|
NPEDsoundVolume: state.NPEDsoundVolume,
|
2025-10-20 11:32:45 +01:00
|
|
|
hotlistSoundVolume: state.hotlistSoundVolume,
|
2025-10-21 12:52:14 +01:00
|
|
|
soundOptions: [...(state.soundOptions ?? [])],
|
2025-10-20 09:11:05 +01:00
|
|
|
};
|
|
|
|
|
dispatch({ type: "UPDATE", payload: updatedValues });
|
2025-10-21 12:52:14 +01:00
|
|
|
|
2025-10-08 11:08:41 +01:00
|
|
|
const result = await mutation.mutateAsync({
|
|
|
|
|
operation: "INSERT",
|
|
|
|
|
path: "soundSettings",
|
2025-10-20 09:11:05 +01:00
|
|
|
value: updatedValues,
|
2025-10-08 11:08:41 +01:00
|
|
|
});
|
|
|
|
|
if (result.reason !== "OK") {
|
|
|
|
|
toast.error("Cannot update sound settings");
|
|
|
|
|
} else {
|
|
|
|
|
toast.success("Sound Settings successfully updated");
|
|
|
|
|
}
|
2025-09-30 13:25:11 +01:00
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<Formik initialValues={initialValues} onSubmit={handleSubmit}>
|
2025-10-20 11:32:45 +01:00
|
|
|
{() => (
|
2025-09-30 13:25:11 +01:00
|
|
|
<Form className="flex flex-col space-y-3">
|
|
|
|
|
<FormGroup>
|
2025-10-17 16:12:02 +01:00
|
|
|
<div className="flex flex-col md:flex-row space-y-2 w-full justify-between gap-3">
|
|
|
|
|
<label htmlFor="sightingSound">Sighting Sound</label>
|
|
|
|
|
<Field
|
|
|
|
|
as="select"
|
|
|
|
|
name="sightingSound"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg text-white bg-[#253445] w-full md:w-60"
|
|
|
|
|
>
|
|
|
|
|
{soundOptions?.map(({ value, label }) => {
|
|
|
|
|
return (
|
|
|
|
|
<option key={label} value={value}>
|
|
|
|
|
{label}
|
|
|
|
|
</option>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Field>
|
|
|
|
|
<SliderComponent soundCategory="SIGHTINGVOLUME" />
|
|
|
|
|
</div>
|
2025-09-30 13:25:11 +01:00
|
|
|
</FormGroup>
|
|
|
|
|
<FormGroup>
|
2025-10-17 16:12:02 +01:00
|
|
|
<div className="flex flex-col md:flex-row space-y-2 w-full justify-between gap-3">
|
|
|
|
|
<label htmlFor="NPEDsound">NPED notification Sound</label>
|
|
|
|
|
<Field
|
|
|
|
|
as="select"
|
|
|
|
|
name="NPEDsound"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg text-white bg-[#253445] w-full md:w-60"
|
|
|
|
|
>
|
|
|
|
|
{soundOptions?.map(({ value, label }) => (
|
|
|
|
|
<option key={value} value={value}>
|
|
|
|
|
{label}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</Field>
|
|
|
|
|
<SliderComponent soundCategory="NPEDVOLUME" />
|
|
|
|
|
</div>
|
2025-09-30 13:25:11 +01:00
|
|
|
</FormGroup>
|
|
|
|
|
<div>
|
|
|
|
|
<h3 className="text-lg font-semibold mb-2">Hotlist Sounds</h3>
|
|
|
|
|
<FormGroup>
|
2025-10-20 11:32:45 +01:00
|
|
|
<div className="flex flex-col md:flex-row space-y-2 w-full justify-between gap-3">
|
|
|
|
|
<label htmlFor="hotlistSound">All hotlist Sounds</label>
|
|
|
|
|
<Field
|
|
|
|
|
as="select"
|
|
|
|
|
name="hotlistSound"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg text-white bg-[#253445] w-full md:w-60"
|
|
|
|
|
>
|
|
|
|
|
{soundOptions?.map(({ value, label }) => (
|
|
|
|
|
<option key={value} value={value}>
|
|
|
|
|
{label}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</Field>
|
|
|
|
|
<SliderComponent soundCategory="HOTLISTVOLUME" />
|
|
|
|
|
</div>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
{/* <FormGroup>
|
2025-09-30 13:25:11 +01:00
|
|
|
<FieldArray
|
|
|
|
|
name="hotlists"
|
|
|
|
|
render={() => (
|
2025-09-30 15:32:00 +01:00
|
|
|
<div className="w-full m-2">
|
2025-10-08 11:08:41 +01:00
|
|
|
{values?.hotlists?.length > 0 ? (
|
|
|
|
|
values?.hotlists?.map((hotlist, index) => (
|
2025-10-17 10:17:01 +01:00
|
|
|
<div key={hotlist.name} className="flex items-center m-2 w-full justify-between">
|
|
|
|
|
<label htmlFor={`hotlists.${index}.sound`} className="w-32 shrink-0">
|
2025-09-30 13:25:11 +01:00
|
|
|
{hotlist.name}
|
|
|
|
|
</label>
|
|
|
|
|
<Field
|
|
|
|
|
as="select"
|
|
|
|
|
name={`hotlists.${index}.sound`}
|
|
|
|
|
id={`hotlists.${index}.sound`}
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg text-white bg-[#253445] w-full md:w-60"
|
|
|
|
|
>
|
2025-10-01 15:21:07 +01:00
|
|
|
{soundOptions?.map(({ value, label }) => (
|
2025-09-30 13:25:11 +01:00
|
|
|
<option key={value} value={value}>
|
|
|
|
|
{label}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</Field>
|
|
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
) : (
|
2025-09-30 15:32:00 +01:00
|
|
|
<p>No hotlists yet, Add one</p>
|
2025-09-30 13:25:11 +01:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-10-20 11:32:45 +01:00
|
|
|
</FormGroup> */}
|
2025-09-30 13:25:11 +01:00
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="w-1/4 text-white bg-green-700 hover:bg-green-800 font-small rounded-lg text-sm px-2 py-2.5"
|
|
|
|
|
>
|
|
|
|
|
Save Settings
|
|
|
|
|
</button>
|
2025-10-22 16:12:49 +01:00
|
|
|
<button onClick={handletest} type="button">
|
|
|
|
|
click
|
|
|
|
|
</button>
|
2025-09-30 13:25:11 +01:00
|
|
|
</Form>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SoundSettingsFields;
|