- got to a good place on individual hotlist sounds

This commit is contained in:
2026-01-16 15:42:52 +00:00
parent cbfce837a3
commit 80bcae6fd5
12 changed files with 182 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
{
"name": "in-car-system-fe",
"private": true,
"version": "1.0.28",
"version": "1.0.29",
"type": "module",
"scripts": {
"dev": "vite",

View File

@@ -1,4 +1,4 @@
import { Field, Form, Formik } from "formik";
import { Field, FieldArray, Form, Formik } from "formik";
import FormGroup from "../components/FormGroup";
import type { FormValues, Hotlist } from "../../../types/types";
import { useSoundContext } from "../../../context/SoundContext";
@@ -10,6 +10,15 @@ const SoundSettingsFields = () => {
const { state, dispatch } = useSoundContext();
const { mutation } = useCameraBlackboard();
const hotlistsoundMapKeys = Array.from(state.hotlistSoundMap.entries());
const hotlistsoundMapValues = hotlistsoundMapKeys.map(([key, value]) => {
return { key, value };
});
console.log(hotlistsoundMapValues);
console.log(hotlistsoundMapKeys);
const hotlists: Hotlist[] = state.hotlists;
const soundOptions = state?.soundOptions?.map((soundOption) => ({
@@ -25,12 +34,15 @@ const SoundSettingsFields = () => {
};
const handleSubmit = async (values: FormValues) => {
console.log(values);
const updatedValues = {
...values,
sightingVolume: state.sightingVolume,
NPEDsoundVolume: state.NPEDsoundVolume,
hotlistSoundVolume: state.hotlistSoundVolume,
soundOptions: [...(state.soundOptions ?? [])],
hotlists: [...(values.hotlists ?? [])],
hotlistSoundMap: { ...state.hotlistSoundMap },
};
dispatch({ type: "UPDATE", payload: updatedValues });
@@ -50,9 +62,11 @@ const SoundSettingsFields = () => {
toast.success("Sound Settings successfully updated");
}
};
console.log(state);
return (
<Formik initialValues={initialValues} onSubmit={handleSubmit}>
{() => (
{({ values }) => (
<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">
@@ -109,7 +123,7 @@ const SoundSettingsFields = () => {
<SliderComponent soundCategory="HOTLISTVOLUME" />
</div>
</FormGroup>
{/* <FormGroup>
<FormGroup>
<FieldArray
name="hotlists"
render={() => (
@@ -140,7 +154,7 @@ const SoundSettingsFields = () => {
</div>
)}
/>
</FormGroup> */}
</FormGroup>
</div>
<button
type="submit"

View File

@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { HitKind, QueuedHit, ReducedSightingType, SightingType } from "../../types/types";
import { BLANK_IMG } from "../../utils/utils";
import { BLANK_IMG, checkWhichHotlistHit } from "../../utils/utils";
import NumberPlate from "../PlateStack/NumberPlate";
import Card from "../UI/Card";
import CardHeader from "../UI/CardHeader";
@@ -16,6 +16,8 @@ import NPED_CAT_D from "/NPED_Cat_D.svg";
import { useIntegrationsContext } from "../../context/IntegrationsContext";
import Loading from "../UI/Loading";
import { checkIsHotListHit, getNPEDCategory } from "../../utils/utils";
import HotlistSoundPlayer from "../UI/HotlistSoundPlayer";
import { useSoundContext } from "../../context/SoundContext";
function useNow(tickMs = 1000) {
const [, setNow] = useState(() => Date.now());
@@ -52,10 +54,12 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
const { dispatch, state: alertState } = useAlertHitContext();
const { state: integrationState, dispatch: integrationDispatch } = useIntegrationsContext();
const { state: soundContextState } = useSoundContext();
const hotlists = soundContextState.hotlists;
const sessionStarted = integrationState.sessionStarted;
const sessionPaused = integrationState.sessionPaused;
const processedRefs = useRef<Set<number | string>>(new Set());
const hotlistPlayersRef = useRef(new Map<string, () => void>());
const hasAutoOpenedRef = useRef(false);
const npedRef = useRef(false);
@@ -64,6 +68,15 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
const isCatCEnabled = integrationState?.iscatEnabled?.catC;
const isCatDEnabled = integrationState?.iscatEnabled?.catD;
const handleHotlistReady = useCallback((name: string, play: () => void) => {
hotlistPlayersRef.current.set(name, play);
}, []);
const playHotlistByName = (name: string) => {
const fn = hotlistPlayersRef.current.get(name);
if (fn) fn();
};
const enqueue = useCallback(
(sighting: SightingType, kind: HitKind) => {
if (!sighting) return;
@@ -111,6 +124,22 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
const rows = useMemo(() => sightings?.filter(Boolean) as SightingType[], [sightings]);
// Separate effect to play hotlist sounds on every hit
useEffect(() => {
if (!rows?.length) return;
const latestSighting = rows[0];
if (!latestSighting) return;
const isHotlistHit = checkIsHotListHit(latestSighting);
if (isHotlistHit) {
const matchedHotlists = checkWhichHotlistHit(latestSighting);
if (matchedHotlists?.[0]) {
playHotlistByName(matchedHotlists[0]);
}
}
}, [rows]);
useEffect(() => {
if (!rows?.length) return;
@@ -127,7 +156,7 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
(npedcategory === "D" && isCatDEnabled);
if (isNPED || isHotlistHit) {
enqueue(sighting, isNPED ? "NPED" : "HOTLIST"); // enqueue ONLY
enqueue(sighting, isNPED ? "NPED" : "HOTLIST");
}
}
}, [rows, enqueue, isCatAEnabled, isCatBEnabled, isCatCEnabled, isCatDEnabled]);
@@ -201,6 +230,17 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
return (
<>
{hotlists.map((hotlist) => {
return (
<HotlistSoundPlayer
key={hotlist.name}
hotlist={hotlist}
soundOptions={soundContextState.soundOptions}
volume={soundContextState.hotlistSoundVolume}
onReady={handleHotlistReady}
/>
);
})}
<Card className={clsx("overflow-y-auto min-h-[40vh] md:min-h-[60vh] max-h-[80vh] lg:w-[40%] p-4", className)}>
<CardHeader title={title} />
<div className="flex flex-col gap-3 ">

View File

@@ -0,0 +1,32 @@
import { useSound } from "react-sounds";
import notification from "../../assets/sounds/ui/notification.mp3";
import { useCachedSoundSrc } from "../../hooks/usecachedSoundSrc";
import type { Hotlist, SoundUploadValue } from "../../types/types";
import { useEffect } from "react";
import { useDebouncedCallback } from "use-debounce";
type HotlistSoundPlayerProps = {
hotlist: Hotlist;
volume?: number;
soundOptions: SoundUploadValue[] | undefined;
onReady?: (name: string, play: () => void) => void;
};
const HotlistSoundPlayer = ({ hotlist, soundOptions, volume, onReady }: HotlistSoundPlayerProps) => {
const { src } = useCachedSoundSrc(hotlist.sound, soundOptions, notification);
const { play } = useSound(src, { volume: volume ?? 10 });
const playHotlistSound = useDebouncedCallback(() => {
play();
}, 500);
useEffect(() => {
if (!onReady) return;
onReady(hotlist.name, () => {
playHotlistSound();
});
}, [hotlist.name, onReady, playHotlistSound]);
return null;
};
export default HotlistSoundPlayer;

View File

@@ -2,6 +2,8 @@ import { useEffect, useMemo, useReducer, useRef, useState, type ReactNode } from
import { SoundContext } from "../SoundContext";
import { initialState, reducer } from "../reducers/SoundContextReducer";
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
import { useHotlistData } from "../../hooks/useHotListData";
import type { HotlistFile } from "../../types/types";
type SoundContextProviderProps = {
children: ReactNode;
@@ -13,6 +15,7 @@ const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
const audioCtxRef = useRef<AudioContext | null>(null);
const [state, dispatch] = useReducer(reducer, initialState);
const { mutation } = useCameraBlackboard();
const { query: hotlistQuery } = useHotlistData();
useEffect(() => {
const fetchSound = async () => {
@@ -36,6 +39,25 @@ const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (!hotlistQuery?.data) return;
const fetchedHotlists = hotlistQuery.data.hotlists || [];
const hotlistSoundSettings = fetchedHotlists.map((hotlist: HotlistFile) => {
return {
name: hotlist.filename,
sound: state.hotlistSound,
};
});
const hotlistSoundMap = new Map<string, string>();
fetchedHotlists.forEach((hotlist: HotlistFile) => {
hotlistSoundMap.set(hotlist.filename, state.hotlistSound);
});
dispatch({ type: "SETHOTLISTSOUNDMAP", payload: hotlistSoundMap });
dispatch({ type: "SETHOTLISTS", payload: hotlistSoundSettings });
}, [hotlistQuery.data, state.hotlistSound]);
useEffect(() => {
const unlock = async () => {
if (!audioCtxRef.current) audioCtxRef.current = new AudioContext();

View File

@@ -5,6 +5,7 @@ export const initialState: SoundState = {
NPEDsound: "popup",
hotlistSound: "warning",
hotlists: [{ name: "hotlistName", sound: "notification" }],
hotlistSoundMap: new Map<string, string>(),
soundOptions: [
{ name: "Switch (Default)", soundFileName: "switch" },
{ name: "Popup", soundFileName: "popup" },
@@ -24,19 +25,23 @@ export const initialState: SoundState = {
export function reducer(state: SoundState, action: SoundAction): SoundState {
switch (action.type) {
case "UPDATE": {
const nextHotlistSoundMap = new Map<string, string>();
if (action.payload.hotlistSoundMap) {
for (const [key, value] of Object.entries(action.payload.hotlistSoundMap)) {
nextHotlistSoundMap.set(key, value);
}
}
return {
...state,
sightingSound: action.payload.sightingSound,
NPEDsound: action.payload.NPEDsound,
hotlistSound: action.payload.hotlistSound,
hotlists: action.payload.hotlists?.map((hotlist) => ({
name: hotlist.name,
sound: hotlist.sound,
})),
NPEDsoundVolume: action.payload.NPEDsoundVolume,
sightingVolume: action.payload.sightingVolume,
hotlistSoundVolume: action.payload.hotlistSoundVolume,
soundOptions: action.payload.soundOptions,
hotlists: action.payload.hotlists,
hotlistSoundMap: nextHotlistSoundMap,
};
}
@@ -69,6 +74,16 @@ export function reducer(state: SoundState, action: SoundAction): SoundState {
...state,
uploadedSound: action.payload,
};
case "SETHOTLISTS":
return {
...state,
hotlists: action.payload,
};
case "SETHOTLISTSOUNDMAP":
return {
...state,
hotlistSoundMap: action.payload,
};
case "RESET":
return initialState;
default:

View File

@@ -0,0 +1,7 @@
import type { Hotlist } from "../types/types";
export function useHotlistSounds(hotlists: Hotlist[], hotlist: Hotlist) {
console.log(hotlists);
const foundHotlist = hotlists.find((item) => item.name === hotlist.name);
console.log(foundHotlist);
}

View File

@@ -84,7 +84,6 @@ export function useSightingFeed(url: string | undefined) {
if (!data || data.ref === -1) return;
const isHotListHit = checkIsHotListHit(data);
const cat = getNPEDCategory(data);
const isNPEDHitA = cat === "A";
const isNPEDHitB = cat === "B";
const isNPEDHitC = cat === "C";

View File

@@ -324,6 +324,7 @@ export type SoundUploadValue = {
export type SoundState = {
sightingSound: SoundValue;
hotlistSoundMap: Map<string, string>;
NPEDsound: SoundValue;
hotlists: Hotlist[];
hotlistSound: SoundValue;
@@ -345,6 +346,7 @@ type UpdateAction = {
hotlistSoundVolume: number;
hotlistSound: SoundValue;
soundOptions?: SoundUploadValue[];
hotlistSoundMap: Map<string, string>;
};
};
@@ -367,7 +369,24 @@ type ResetAction = {
type: "RESET";
};
export type SoundAction = UpdateAction | AddAction | VolumeAction | UploadedState | ResetAction;
type SetHotlistsAction = {
type: "SETHOTLISTS";
payload: Hotlist[];
};
type SetHotlistSoundMapAction = {
type: "SETHOTLISTSOUNDMAP";
payload: Map<string, string>;
};
export type SoundAction =
| UpdateAction
| AddAction
| VolumeAction
| UploadedState
| ResetAction
| SetHotlistsAction
| SetHotlistSoundMapAction;
export type WifiSettingValues = {
ssid: string;
password: string;

View File

@@ -1,4 +1,4 @@
const rawCamBase = import.meta.env.VITE_CHRIS_BOX_URL;
const rawCamBase = import.meta.env.VITE_AGX_BOX_URL;
const environment = import.meta.env.MODE;
export const CAM_BASE = environment === "development" ? rawCamBase : window.location.origin;

View File

@@ -6,7 +6,7 @@ import warning from "../assets/sounds/ui/Warning.wav";
import ding from "../assets/sounds/ui/Ding.wav";
import shutter from "../assets/sounds/ui/shutter.mp3";
import attention from "../assets/sounds/ui/Attention.wav";
import type { HotlistMatches, SightingType } from "../types/types";
import type { Hotlist, HotlistMatches, SightingType } from "../types/types";
export function getSoundFileURL(name: string) {
const sounds: Record<string, string> = {
@@ -215,3 +215,20 @@ export function isUtcOutOfSync(
const driftMs = Math.abs(utc.getTime() - local.getTime() + utc.getTimezoneOffset() * 60_000);
return { driftMs, outOfSync: driftMs > maxDriftMs };
}
export function getHotlistSoundNames(hotlists: Hotlist[], hotlistName: string) {
if (!hotlists || hotlists.length === 0) return;
const hotlistSoundName = hotlists?.find((hotlist) => hotlist.name === hotlistName)?.sound;
return hotlistSoundName;
}
export const checkWhichHotlistHit = (sigthing: SightingType | null) => {
if (!sigthing) return;
if (sigthing?.metadata?.hotlistMatches) {
const hotlistHits = Object.entries(sigthing?.metadata?.hotlistMatches)
.filter(([, value]) => value === true)
.map(([key]) => key);
return hotlistHits;
}
};

View File

@@ -27,7 +27,7 @@ export default defineConfig({
host: true,
proxy: {
"/api": {
target: "http://100.72.72.70:8080",
target: "http://100.118.196.113:8080",
changeOrigin: true,
},
},