/* eslint-disable @typescript-eslint/no-explicit-any */ import { useEffect, useState } from "react"; import { BaseProps } from "../../Factory/BaseType"; import InformationAlertView from "./InformationAlertView"; import { useEffectiveStyle } from "../../utils/useEffectiveStyle"; export type InformationAlertProps = BaseProps & { value: any; title: string; messageHeader?: string; messageBody?: string; iconUrl?: string; iconName?: string; confirmText?: string; onConfirm?: () => void; onModelUpdate?: ( callBack: ((args: any) => void) | null, fieldName: string, value: any, ) => void; handleHeader?: ( callback: (header: string) => void, header: string, viewModel?: Record, ) => void; handleBody?: ( callback: (body: string) => void, body: string, value?: any, ) => void; }; const InformationAlert = (props: InformationAlertProps) => { useEffectiveStyle(props.widgetStyle); const [header, setHeader] = useState(props.messageHeader); const [body, setBody] = useState(props.messageBody); useEffect(() => { if (props.handleHeader) { props.handleHeader(setHeader, props.messageHeader ?? "", props.viewModel); } else { setHeader(props.messageHeader); } }, [props.messageHeader, props.viewModel, props.handleHeader]); useEffect(() => { if (props.handleBody) { props.handleBody(setBody, props.messageBody ?? "", props.value); } else { setBody(props.messageBody); } }, [props.messageBody, props.value, props.handleBody]); // Clicking the button - or the dialog's own close affordance, which is wired // to this same handler - closes immediately. Previously this awaited the // action first, so a slow one left the popup on screen for the whole round // trip. // // Resolve the handler BEFORE closing: the fallback lives at value.onConfirm, // inside the screenData field the close nulls out. const handleConfirm = () => { const onConfirm = props.onConfirm ?? props?.value?.onConfirm; props.onModelUpdate?.(null, props.screenDataField ?? "", null); // Not awaited - the dialog is already gone. The catch only stops a rejected // host-supplied closure from becoming an unhandled rejection. if (!onConfirm) return; try { Promise.resolve(onConfirm()).catch((error) => console.error("[vs-template] InformationAlert action failed", error), ); } catch (error) { console.error("[vs-template] InformationAlert action failed", error); } }; return ( ); }; export default InformationAlert;