// * Packages import { useEffect, useRef } from "react"; // * Other import { ModelSelect } from "@/back/Components/Inputs/ModelSelect"; import { extractExcludedIdFromExists, extractRequiredValidation, extractTableFromExists, } from "@/hooks/use-laravel-validation"; import { ConfigField, DynamicInternalField } from "@/back/types/dynamic-pages"; /* eslint-disable no-unused-vars */ type DynamicWrapperModelSelectProps = { field: ConfigField; fieldName: string; fieldValue: string | number | boolean | File | Date; content: DynamicInternalField | null; setData: ( name: string, value: string | number | boolean | File | object | null, ) => void; getTrans: (fieldLabel: string) => string; }; /* eslint-enable no-unused-vars */ export default function DynamicWrapperModelSelect({ field, fieldName, fieldValue, content, setData, getTrans, }: DynamicWrapperModelSelectProps) { // Extract only the model name from the validation. const targetModel = extractTableFromExists(field.validation) || "dynamic_pages"; // Add a custom prefix in the last part of the name. const newName = fieldName.replace(/([^.\s]+)$/, "reserved_$1"); // Set a state at false. const initializedRef = useRef(false); // Set the model name in the custom key only once. useEffect(() => { if (initializedRef.current) return; initializedRef.current = true; // If the value isn't already present in content. if ( content && !content[`reserved_${field.key}` as keyof typeof content] ) { setData(newName, targetModel); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [newName, targetModel]); return ( ); }