added mrt, dev
This commit is contained in:
66
apps/dispatch/app/pilot/_components/mrt/useSounds.ts
Normal file
66
apps/dispatch/app/pilot/_components/mrt/useSounds.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
"use client";
|
||||
import { usePilotConnectionStore } from "_store/pilot/connectionStore";
|
||||
import { useMrtStore } from "_store/pilot/MrtStore";
|
||||
import { editConnectedAircraftAPI } from "querys/aircrafts";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
const MRTstatusSound = new Audio("/sounds/MRT-status.mp3");
|
||||
const MrtMessageReceivedSound = new Audio("/sounds/MRT-message-received.mp3");
|
||||
|
||||
export const useSounds = () => {
|
||||
const mrtState = useMrtStore((state) => state);
|
||||
const { connectedAircraft, selectedStation } = usePilotConnectionStore(
|
||||
(state) => state,
|
||||
);
|
||||
|
||||
const fmsStatus = connectedAircraft?.fmsStatus || "NaN";
|
||||
|
||||
const previousFmsStatus = useRef(fmsStatus || "6");
|
||||
const timeout = useRef<NodeJS.Timeout>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const handleSoundEnd = () => {
|
||||
mrtState.setPage("home");
|
||||
};
|
||||
|
||||
const playSound = (sound: HTMLAudioElement) => {
|
||||
sound.play();
|
||||
sound.addEventListener("ended", handleSoundEnd);
|
||||
};
|
||||
|
||||
if (!connectedAircraft) return;
|
||||
if (mrtState.page === "new-status") {
|
||||
if (fmsStatus === "J") {
|
||||
playSound(MrtMessageReceivedSound);
|
||||
|
||||
timeout.current = setTimeout(() => {
|
||||
editConnectedAircraftAPI(connectedAircraft.id, {
|
||||
fmsStatus: previousFmsStatus.current,
|
||||
});
|
||||
}, 5000);
|
||||
} else if (previousFmsStatus.current !== fmsStatus) {
|
||||
playSound(MRTstatusSound);
|
||||
} else {
|
||||
handleSoundEnd();
|
||||
}
|
||||
if (!timeout.current) {
|
||||
previousFmsStatus.current = fmsStatus || "6";
|
||||
}
|
||||
}
|
||||
return () => {
|
||||
if (timeout.current) clearTimeout(timeout.current);
|
||||
[MRTstatusSound, MrtMessageReceivedSound].forEach((sound) => {
|
||||
sound.removeEventListener("ended", handleSoundEnd);
|
||||
sound.pause();
|
||||
sound.currentTime = 0;
|
||||
});
|
||||
};
|
||||
}, [
|
||||
mrtState,
|
||||
fmsStatus,
|
||||
connectedAircraft,
|
||||
selectedStation,
|
||||
previousFmsStatus,
|
||||
timeout,
|
||||
]);
|
||||
};
|
||||
Reference in New Issue
Block a user