@@ -210,7 +210,7 @@ type WorkerMessageData =
210210 | WorkerMessageComplete
211211 | WorkerMessageError ;
212212
213- // --- Debounce Utility ---
213+ // --- Debounce Utility --- // ADDED
214214function debounce < F extends ( ...args : any [ ] ) => any > (
215215 func : F ,
216216 waitFor : number ,
@@ -880,54 +880,7 @@ export default function CsvValidator() {
880880 [ memoizedSetValidationResults , toast ] ,
881881 ) ;
882882
883- // --- Debounced validation trigger ---
884- const debouncedValidate = useMemo ( ( ) => {
885- let timeout : NodeJS . Timeout | null = null ;
886- return ( csv : string , schema : Record < string , unknown > | string ) => {
887- if ( timeout ) clearTimeout ( timeout ) ;
888- timeout = setTimeout ( ( ) => {
889- runWorkerValidation ( csv , schema ) ;
890- } , 350 ) ;
891- } ;
892- } , [ runWorkerValidation ] ) ;
893-
894- // --- CSV edit effect: debounce and use worker ---
895- useEffect ( ( ) => {
896- const effectiveSchema = useUploadedSchema ? uploadedSchemaContent : selectedSchemaContent ;
897-
898- if ( ! csvRawText . trim ( ) || ! effectiveSchema ) { // Combined checks
899- setValidationResults ( [ ] ) ;
900- setOverallCsvStatus ( "pending" ) ;
901- setTotalErrorCount ( 0 ) ;
902- setTotalWarningCount ( 0 ) ;
903- setVisibleResultCount ( 20 ) ;
904- // Optionally cancel any pending worker task if schema becomes null
905- if ( workerRef . current && workerBusy ) {
906- workerRef . current . terminate ( ) ;
907- workerRef . current = null ; // Clear ref
908- setWorkerBusy ( false ) ;
909- }
910- if ( validationTimeout . current ) clearTimeout ( validationTimeout . current ) ;
911- return ;
912- }
913- // Debounced and use worker on every csvRawText change
914- // Provide default empty object to satisfy type checker, although the if check prevents null case
915- debouncedValidate (
916- csvRawText ,
917- effectiveSchema ?? { } ,
918- ) ;
919-
920- } , [
921- csvRawText ,
922- selectedSchemaContent ,
923- uploadedSchemaContent ,
924- useUploadedSchema ,
925- debouncedValidate , // Keep debouncedValidate here
926- workerBusy // ADD workerBusy to dependency array
927- // No need to include effectiveSchema directly, its parts are dependencies
928- ] ) ;
929-
930- // --- Manual Validation Trigger --- //
883+ // --- Debounced validation trigger --- // RENAMED & SIMPLIFIED
931884 const triggerValidation = useCallback ( async ( ) => {
932885 // Determine the schema to use
933886 const schemaToUse = useUploadedSchema ? uploadedSchemaContent : selectedSchemaContent ;
@@ -936,18 +889,21 @@ export default function CsvValidator() {
936889 console . warn ( "No schema selected for validation" ) ;
937890 return ;
938891 }
892+
893+ // Mark editor as clean now that validation is explicitly triggered
894+ setIsEditorDirty ( false ) ;
939895
940896 // Call the worker validation function
941897 runWorkerValidation ( csvRawText , schemaToUse ) ;
942898 } , [ csvRawText , selectedSchemaContent , uploadedSchemaContent , useUploadedSchema , runWorkerValidation ] ) ;
943899
944- // --- Debounced Validation --- //
900+ // --- Debounced Validation --- // ADDED FOR AUTO-VALIDATION ON EDIT
945901 // Use useRef to keep the debounced function stable across renders
946902 const debouncedValidationRef = useRef (
947903 debounce ( triggerValidation , 750 ) // Debounce validation by 750ms
948904 ) ;
949905
950- // --- CSV Content Change Handler --- //
906+ // --- CSV Content Change Handler --- // UPDATED FOR DEBOUNCING
951907 const handleCsvContentChange = useCallback (
952908 ( value : string | undefined ) => {
953909 const newCsvText = value || "" ;
@@ -1121,30 +1077,13 @@ export default function CsvValidator() {
11211077 < div className = "flex-grow" > </ div >
11221078
11231079 < Button
1124- onClick = { ( ) => {
1125- const effectiveSchema = useUploadedSchema
1126- ? uploadedSchemaContent
1127- : selectedSchemaContent ;
1128- // Only call validate if effectiveSchema is not null
1129- if ( effectiveSchema ) {
1130- debouncedValidate (
1131- csvRawText ,
1132- effectiveSchema , // Pass the non-null schema
1133- ) ;
1134- } else {
1135- // Optional: Show a toast or log an error if schema is missing
1136- toast ( {
1137- title : "Schema Missing" ,
1138- description : "Please select or upload a schema before validating." ,
1139- variant : "destructive" ,
1140- } ) ;
1141- }
1142- } }
1080+ onClick = { triggerValidation } // Use direct trigger on button click
11431081 disabled = {
11441082 workerBusy ||
11451083 ! csvRawText . trim ( ) ||
11461084 ( ! selectedSchemaName && ! useUploadedSchema ) ||
1147- ! ( useUploadedSchema ? uploadedSchemaContent : selectedSchemaContent )
1085+ ! ( useUploadedSchema ? uploadedSchemaContent : selectedSchemaContent ) ||
1086+ ! isEditorDirty // Disable if editor hasn't changed since last validation
11481087 }
11491088 size = "sm"
11501089 className = "bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white"
0 commit comments