@@ -208,12 +208,8 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
208208 return allProjects [ 0 ] || '' ;
209209 } , [ dashboard , editableProjects , allProjects ] ) ;
210210
211- const { schema : validationSchema } = useDashboardValidationSchema ( defaultProject , t ) ;
212-
213211 const form = useForm < CreateDashboardValidationType > ( {
214- resolver : validationSchema
215- ? zodResolver ( validationSchema )
216- : zodResolver ( createDashboardDialogValidationSchema ( t ) ) ,
212+ resolver : zodResolver ( createDashboardDialogValidationSchema ( t ) ) ,
217213 mode : 'onBlur' ,
218214 defaultValues : {
219215 projectName : defaultProject ,
@@ -222,6 +218,12 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
222218 } ) ;
223219
224220 const selectedProjectName = form . watch ( 'projectName' ) ;
221+ const dashboardName = form . watch ( 'dashboardName' ) ;
222+
223+ const { schema : dynamicValidationSchema , isSchemaLoading } = useDashboardValidationSchema (
224+ selectedProjectName ,
225+ t ,
226+ ) ;
225227
226228 const projectOptions = useMemo < TypeaheadSelectOption [ ] > ( ( ) => {
227229 if ( ! editableProjects ) {
@@ -236,6 +238,48 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
236238
237239 const createDashboardMutation = useCreateDashboardMutation ( ) ;
238240
241+ React . useEffect ( ( ) => {
242+ const isPerseProject = persesProjects ?. some (
243+ ( project ) => project . metadata ?. name === selectedProjectName ,
244+ ) ;
245+
246+ if ( dynamicValidationSchema && selectedProjectName && ! isSchemaLoading && isPerseProject ) {
247+ const currentValues = form . getValues ( ) ;
248+ const result = dynamicValidationSchema . safeParse ( currentValues ) ;
249+
250+ if ( ! result . success ) {
251+ const hasDashboardIssue = result . error . issues . some (
252+ ( issue ) => issue . path [ 0 ] === 'dashboardName' ,
253+ ) ;
254+
255+ if ( hasDashboardIssue ) {
256+ result . error . issues . forEach ( ( issue ) => {
257+ if ( issue . path [ 0 ] === 'dashboardName' ) {
258+ form . setError ( 'dashboardName' , {
259+ type : 'validate' ,
260+ message : issue . message ,
261+ } ) ;
262+ }
263+ } ) ;
264+ } else {
265+ form . clearErrors ( 'dashboardName' ) ;
266+ }
267+ } else {
268+ form . clearErrors ( 'dashboardName' ) ;
269+ }
270+ } else if ( ! isPerseProject && selectedProjectName ) {
271+ // Clear any existing validation errors for non-Perses projects
272+ form . clearErrors ( 'dashboardName' ) ;
273+ }
274+ } , [
275+ selectedProjectName ,
276+ dynamicValidationSchema ,
277+ form ,
278+ dashboardName ,
279+ isSchemaLoading ,
280+ persesProjects ,
281+ ] ) ;
282+
239283 React . useEffect ( ( ) => {
240284 if ( isOpen && dashboard && editableProjects ?. length > 0 && defaultProject ) {
241285 form . reset ( {
@@ -435,9 +479,10 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
435479 ! ( form . watch ( 'dashboardName' ) || '' ) ?. trim ( ) ||
436480 ! ( form . watch ( 'projectName' ) || '' ) ?. trim ( ) ||
437481 ! hasEditableProject ||
482+ isSchemaLoading ||
438483 createDashboardMutation . isPending
439484 }
440- isLoading = { createDashboardMutation . isPending }
485+ isLoading = { createDashboardMutation . isPending || isSchemaLoading }
441486 >
442487 { t ( 'Duplicate' ) }
443488 </ Button >
0 commit comments