1 Commits

Author SHA1 Message Date
80bcae6fd5 - got to a good place on individual hotlist sounds 2026-01-16 15:42:52 +00:00
12 changed files with 182 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
{ {
"name": "in-car-system-fe", "name": "in-car-system-fe",
"private": true, "private": true,
"version": "1.0.28", "version": "1.0.29",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "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 FormGroup from "../components/FormGroup";
import type { FormValues, Hotlist } from "../../../types/types"; import type { FormValues, Hotlist } from "../../../types/types";
import { useSoundContext } from "../../../context/SoundContext"; import { useSoundContext } from "../../../context/SoundContext";
@@ -10,6 +10,15 @@ const SoundSettingsFields = () => {
const { state, dispatch } = useSoundContext(); const { state, dispatch } = useSoundContext();
const { mutation } = useCameraBlackboard(); 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 hotlists: Hotlist[] = state.hotlists;
const soundOptions = state?.soundOptions?.map((soundOption) => ({ const soundOptions = state?.soundOptions?.map((soundOption) => ({
@@ -25,12 +34,15 @@ const SoundSettingsFields = () => {
}; };
const handleSubmit = async (values: FormValues) => { const handleSubmit = async (values: FormValues) => {
console.log(values);
const updatedValues = { const updatedValues = {
...values, ...values,
sightingVolume: state.sightingVolume, sightingVolume: state.sightingVolume,
NPEDsoundVolume: state.NPEDsoundVolume, NPEDsoundVolume: state.NPEDsoundVolume,
hotlistSoundVolume: state.hotlistSoundVolume, hotlistSoundVolume: state.hotlistSoundVolume,
soundOptions: [...(state.soundOptions ?? [])], soundOptions: [...(state.soundOptions ?? [])],
hotlists: [...(values.hotlists ?? [])],
hotlistSoundMap: { ...state.hotlistSoundMap },
}; };
dispatch({ type: "UPDATE", payload: updatedValues }); dispatch({ type: "UPDATE", payload: updatedValues });
@@ -50,9 +62,11 @@ const SoundSettingsFields = () => {
toast.success("Sound Settings successfully updated"); toast.success("Sound Settings successfully updated");
} }
}; };
console.log(state);
return ( return (
<Formik initialValues={initialValues} onSubmit={handleSubmit}> <Formik initialValues={initialValues} onSubmit={handleSubmit}>
{() => ( {({ values }) => (
<Form className="flex flex-col space-y-3"> <Form className="flex flex-col space-y-3">
<FormGroup> <FormGroup>
<div className="flex flex-col md:flex-row space-y-2 w-full justify-between gap-3"> <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" /> <SliderComponent soundCategory="HOTLISTVOLUME" />
</div> </div>
</FormGroup> </FormGroup>
{/* <FormGroup> <FormGroup>
<FieldArray <FieldArray
name="hotlists" name="hotlists"
render={() => ( render={() => (
@@ -140,7 +154,7 @@ const SoundSettingsFields = () => {
</div> </div>
)} )}
/> />
</FormGroup> */} </FormGroup>
</div> </div>
<button <button
type="submit" type="submit"

View File

@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { HitKind, QueuedHit, ReducedSightingType, SightingType } from "../../types/types"; 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 NumberPlate from "../PlateStack/NumberPlate";
import Card from "../UI/Card"; import Card from "../UI/Card";
import CardHeader from "../UI/CardHeader"; import CardHeader from "../UI/CardHeader";
@@ -16,6 +16,8 @@ import NPED_CAT_D from "/NPED_Cat_D.svg";
import { useIntegrationsContext } from "../../context/IntegrationsContext"; import { useIntegrationsContext } from "../../context/IntegrationsContext";
import Loading from "../UI/Loading"; import Loading from "../UI/Loading";
import { checkIsHotListHit, getNPEDCategory } from "../../utils/utils"; import { checkIsHotListHit, getNPEDCategory } from "../../utils/utils";
import HotlistSoundPlayer from "../UI/HotlistSoundPlayer";
import { useSoundContext } from "../../context/SoundContext";
function useNow(tickMs = 1000) { function useNow(tickMs = 1000) {
const [, setNow] = useState(() => Date.now()); const [, setNow] = useState(() => Date.now());
@@ -52,10 +54,12 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
const { dispatch, state: alertState } = useAlertHitContext(); const { dispatch, state: alertState } = useAlertHitContext();
const { state: integrationState, dispatch: integrationDispatch } = useIntegrationsContext(); const { state: integrationState, dispatch: integrationDispatch } = useIntegrationsContext();
const { state: soundContextState } = useSoundContext();
const hotlists = soundContextState.hotlists;
const sessionStarted = integrationState.sessionStarted; const sessionStarted = integrationState.sessionStarted;
const sessionPaused = integrationState.sessionPaused; const sessionPaused = integrationState.sessionPaused;
const processedRefs = useRef<Set<number | string>>(new Set()); const processedRefs = useRef<Set<number | string>>(new Set());
const hotlistPlayersRef = useRef(new Map<string, () => void>());
const hasAutoOpenedRef = useRef(false); const hasAutoOpenedRef = useRef(false);
const npedRef = useRef(false); const npedRef = useRef(false);
@@ -64,6 +68,15 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
const isCatCEnabled = integrationState?.iscatEnabled?.catC; const isCatCEnabled = integrationState?.iscatEnabled?.catC;
const isCatDEnabled = integrationState?.iscatEnabled?.catD; 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( const enqueue = useCallback(
(sighting: SightingType, kind: HitKind) => { (sighting: SightingType, kind: HitKind) => {
if (!sighting) return; if (!sighting) return;
@@ -111,6 +124,22 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
const rows = useMemo(() => sightings?.filter(Boolean) as SightingType[], [sightings]); 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(() => { useEffect(() => {
if (!rows?.length) return; if (!rows?.length) return;
@@ -127,7 +156,7 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
(npedcategory === "D" && isCatDEnabled); (npedcategory === "D" && isCatDEnabled);
if (isNPED || isHotlistHit) { if (isNPED || isHotlistHit) {
enqueue(sighting, isNPED ? "NPED" : "HOTLIST"); // enqueue ONLY enqueue(sighting, isNPED ? "NPED" : "HOTLIST");
} }
} }
}, [rows, enqueue, isCatAEnabled, isCatBEnabled, isCatCEnabled, isCatDEnabled]); }, [rows, enqueue, isCatAEnabled, isCatBEnabled, isCatCEnabled, isCatDEnabled]);
@@ -201,6 +230,17 @@ export default function SightingHistoryWidget({ className, title }: SightingHist
return ( 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)}> <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} /> <CardHeader title={title} />
<div className="flex flex-col gap-3 "> <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 { SoundContext } from "../SoundContext";
import { initialState, reducer } from "../reducers/SoundContextReducer"; import { initialState, reducer } from "../reducers/SoundContextReducer";
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard"; import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
import { useHotlistData } from "../../hooks/useHotListData";
import type { HotlistFile } from "../../types/types";
type SoundContextProviderProps = { type SoundContextProviderProps = {
children: ReactNode; children: ReactNode;
@@ -13,6 +15,7 @@ const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
const audioCtxRef = useRef<AudioContext | null>(null); const audioCtxRef = useRef<AudioContext | null>(null);
const [state, dispatch] = useReducer(reducer, initialState); const [state, dispatch] = useReducer(reducer, initialState);
const { mutation } = useCameraBlackboard(); const { mutation } = useCameraBlackboard();
const { query: hotlistQuery } = useHotlistData();
useEffect(() => { useEffect(() => {
const fetchSound = async () => { const fetchSound = async () => {
@@ -36,6 +39,25 @@ const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps // 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(() => { useEffect(() => {
const unlock = async () => { const unlock = async () => {
if (!audioCtxRef.current) audioCtxRef.current = new AudioContext(); if (!audioCtxRef.current) audioCtxRef.current = new AudioContext();

View File

@@ -5,6 +5,7 @@ export const initialState: SoundState = {
NPEDsound: "popup", NPEDsound: "popup",
hotlistSound: "warning", hotlistSound: "warning",
hotlists: [{ name: "hotlistName", sound: "notification" }], hotlists: [{ name: "hotlistName", sound: "notification" }],
hotlistSoundMap: new Map<string, string>(),
soundOptions: [ soundOptions: [
{ name: "Switch (Default)", soundFileName: "switch" }, { name: "Switch (Default)", soundFileName: "switch" },
{ name: "Popup", soundFileName: "popup" }, { name: "Popup", soundFileName: "popup" },
@@ -24,19 +25,23 @@ export const initialState: SoundState = {
export function reducer(state: SoundState, action: SoundAction): SoundState { export function reducer(state: SoundState, action: SoundAction): SoundState {
switch (action.type) { switch (action.type) {
case "UPDATE": { 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 { return {
...state, ...state,
sightingSound: action.payload.sightingSound, sightingSound: action.payload.sightingSound,
NPEDsound: action.payload.NPEDsound, NPEDsound: action.payload.NPEDsound,
hotlistSound: action.payload.hotlistSound, hotlistSound: action.payload.hotlistSound,
hotlists: action.payload.hotlists?.map((hotlist) => ({
name: hotlist.name,
sound: hotlist.sound,
})),
NPEDsoundVolume: action.payload.NPEDsoundVolume, NPEDsoundVolume: action.payload.NPEDsoundVolume,
sightingVolume: action.payload.sightingVolume, sightingVolume: action.payload.sightingVolume,
hotlistSoundVolume: action.payload.hotlistSoundVolume, hotlistSoundVolume: action.payload.hotlistSoundVolume,
soundOptions: action.payload.soundOptions, soundOptions: action.payload.soundOptions,
hotlists: action.payload.hotlists,
hotlistSoundMap: nextHotlistSoundMap,
}; };
} }
@@ -69,6 +74,16 @@ export function reducer(state: SoundState, action: SoundAction): SoundState {
...state, ...state,
uploadedSound: action.payload, uploadedSound: action.payload,
}; };
case "SETHOTLISTS":
return {
...state,
hotlists: action.payload,
};
case "SETHOTLISTSOUNDMAP":
return {
...state,
hotlistSoundMap: action.payload,
};
case "RESET": case "RESET":
return initialState; return initialState;
default: 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; if (!data || data.ref === -1) return;
const isHotListHit = checkIsHotListHit(data); const isHotListHit = checkIsHotListHit(data);
const cat = getNPEDCategory(data); const cat = getNPEDCategory(data);
const isNPEDHitA = cat === "A"; const isNPEDHitA = cat === "A";
const isNPEDHitB = cat === "B"; const isNPEDHitB = cat === "B";
const isNPEDHitC = cat === "C"; const isNPEDHitC = cat === "C";

View File

@@ -324,6 +324,7 @@ export type SoundUploadValue = {
export type SoundState = { export type SoundState = {
sightingSound: SoundValue; sightingSound: SoundValue;
hotlistSoundMap: Map<string, string>;
NPEDsound: SoundValue; NPEDsound: SoundValue;
hotlists: Hotlist[]; hotlists: Hotlist[];
hotlistSound: SoundValue; hotlistSound: SoundValue;
@@ -345,6 +346,7 @@ type UpdateAction = {
hotlistSoundVolume: number; hotlistSoundVolume: number;
hotlistSound: SoundValue; hotlistSound: SoundValue;
soundOptions?: SoundUploadValue[]; soundOptions?: SoundUploadValue[];
hotlistSoundMap: Map<string, string>;
}; };
}; };
@@ -367,7 +369,24 @@ type ResetAction = {
type: "RESET"; 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 = { export type WifiSettingValues = {
ssid: string; ssid: string;
password: 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; const environment = import.meta.env.MODE;
export const CAM_BASE = environment === "development" ? rawCamBase : window.location.origin; 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 ding from "../assets/sounds/ui/Ding.wav";
import shutter from "../assets/sounds/ui/shutter.mp3"; import shutter from "../assets/sounds/ui/shutter.mp3";
import attention from "../assets/sounds/ui/Attention.wav"; 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) { export function getSoundFileURL(name: string) {
const sounds: Record<string, 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); const driftMs = Math.abs(utc.getTime() - local.getTime() + utc.getTimezoneOffset() * 60_000);
return { driftMs, outOfSync: driftMs > maxDriftMs }; 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, host: true,
proxy: { proxy: {
"/api": { "/api": {
target: "http://100.72.72.70:8080", target: "http://100.118.196.113:8080",
changeOrigin: true, changeOrigin: true,
}, },
}, },