Files
Mav-Mobile-UI/src/components/SettingForms/Sound/SoundSettingsFields.tsx

189 lines
6.9 KiB
TypeScript
Raw Normal View History

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";
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";
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 = () => {
const { state, dispatch } = useSoundContext();
const { mutation } = useCameraBlackboard();
const { query } = useFileUpload({
queryKey: state.sightingSound ? [state.sightingSound] : undefined,
});
const hotlists: Hotlist[] = state.hotlists;
2025-09-30 13:25:11 +01:00
const soundOptions = state?.soundOptions?.map((soundOption) => ({
2025-10-20 11:32:45 +01:00
value: soundOption?.soundFileName,
label: soundOption?.name,
}));
2025-09-30 13:25:11 +01:00
const initialValues: FormValues = {
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);
const handletest = () => {
2025-10-28 08:50:55 +00:00
console.log(state.uploadedSound);
play();
};
const handleSubmit = async (values: FormValues) => {
const updatedValues = {
...values,
sightingVolume: state.sightingVolume,
NPEDsoundVolume: state.NPEDsoundVolume,
2025-10-20 11:32:45 +01:00
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");
}
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>
<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>
<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={() => (
<div className="w-full m-2">
{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"
>
{soundOptions?.map(({ value, label }) => (
2025-09-30 13:25:11 +01:00
<option key={value} value={value}>
{label}
</option>
))}
</Field>
</div>
))
) : (
<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>
<button onClick={handletest} type="button">
click
</button>
2025-09-30 13:25:11 +01:00
</Form>
)}
</Formik>
);
};
export default SoundSettingsFields;