import { Formik, Field, Form } from "formik"; import type { CameraConfig, CameraSettingErrorValues, CameraSettingValues, ZoomInOptions } from "../../types/types"; import { useEffect, useMemo, useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faEye, faEyeSlash } from "@fortawesome/free-regular-svg-icons"; import CardHeader from "../UI/CardHeader"; import { useCameraMode, useCameraZoom } from "../../hooks/useCameraZoom"; import { parseRTSPUrl, reverseZoomMapping, zoomMapping } from "../../utils/utils"; type CameraSettingsProps = { initialData: CameraConfig; updateCameraConfig: (values: CameraSettingValues) => Promise | void; zoomLevel?: number; onZoomLevelChange?: (level: number | undefined) => void; updateCameraConfigError: null | Error; }; const CameraSettingFields = ({ initialData, updateCameraConfig, zoomLevel, onZoomLevelChange, }: CameraSettingsProps) => { const [showPwd, setShowPwd] = useState(false); const cameraControllerSide = initialData?.id === "CameraA" ? "CameraControllerA" : "CameraControllerB"; const { mutation, query } = useCameraZoom({ camera: cameraControllerSide }); const { cameraModeQuery, cameraModeMutation } = useCameraMode({ camera: cameraControllerSide }); const zoomOptions = [1, 2, 4]; const magnification = query?.data?.propMagnification?.value; const apiZoom = reverseZoomMapping(magnification); const parsed = parseRTSPUrl(initialData?.propURI?.value); const cameraMode = cameraModeQuery?.data?.propDayNightMode?.value; useEffect(() => { if (!query?.data) return; onZoomLevelChange?.(apiZoom); }, [query?.data, onZoomLevelChange, apiZoom]); const initialValues = useMemo( () => ({ friendlyName: initialData?.id ?? "", cameraAddress: initialData?.propURI?.value ?? "", userName: parsed?.username ?? "", password: parsed?.password ?? "", id: initialData?.id, mode: cameraMode ?? "day", zoom: apiZoom, }), [initialData?.id, initialData?.propURI?.value, parsed?.username, parsed?.password, cameraMode, apiZoom] ); const validateValues = (values: CameraSettingValues) => { const errors: CameraSettingErrorValues = {}; if (!values.friendlyName) errors.friendlyName = "Required"; if (!values.cameraAddress) errors.cameraAddress = "Required"; return errors; }; const handleSubmit = (values: CameraSettingValues) => { updateCameraConfig(values); }; const handleRadioButtonChange = async (levelNumber: number) => { if (!onZoomLevelChange || !zoomLevel) return; const text = zoomMapping(levelNumber); onZoomLevelChange(levelNumber); const zoomInOptions: ZoomInOptions = { camera: cameraControllerSide, multiplier: levelNumber, multiplierText: text, }; mutation.mutate(zoomInOptions); }; const selectedZoom = zoomLevel ?? 1; return ( {({ errors, touched, values, setFieldValue, isSubmitting }) => (
{touched.friendlyName && errors.friendlyName && ( {errors.friendlyName} )}
{touched.cameraAddress && errors.cameraAddress && ( {errors.cameraAddress} )}
{touched.userName && errors.userName && ( {errors.userName} )}
{touched.password && errors.password && ( {errors.password} )}
setShowPwd((s) => !s)} icon={showPwd ? faEyeSlash : faEye} />
{zoomOptions.map((zoom) => (
handleRadioButtonChange(zoom)} />
))}
{["day", "night"].map((el) => (
{ setFieldValue("mode", el); await cameraModeMutation.mutateAsync({ camera: cameraControllerSide, mode: el }); }} />
))}
{ }
)}
); }; export default CameraSettingFields;