Skip to content

Commit fda27f7

Browse files
committed
fix: OU-1236 coderabbit suggestions
1 parent 003fea3 commit fda27f7

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

web/src/components/dashboards/perses/dashboard-action-modals.tsx

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,12 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
218218
});
219219

220220
const selectedProjectName = form.watch('projectName');
221+
const dashboardName = form.watch('dashboardName');
221222

222-
const { schema: dynamicValidationSchema } = useDashboardValidationSchema(selectedProjectName, t);
223+
const { schema: dynamicValidationSchema, isSchemaLoading } = useDashboardValidationSchema(
224+
selectedProjectName,
225+
t,
226+
);
223227

224228
const projectOptions = useMemo<TypeaheadSelectOption[]>(() => {
225229
if (!editableProjects) {
@@ -235,24 +239,46 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
235239
const createDashboardMutation = useCreateDashboardMutation();
236240

237241
React.useEffect(() => {
238-
if (dynamicValidationSchema && selectedProjectName) {
242+
const isPerseProject = persesProjects?.some(
243+
(project) => project.metadata?.name === selectedProjectName,
244+
);
245+
246+
if (dynamicValidationSchema && selectedProjectName && !isSchemaLoading && isPerseProject) {
239247
const currentValues = form.getValues();
240248
const result = dynamicValidationSchema.safeParse(currentValues);
241249

242250
if (!result.success) {
243-
result.error.issues.forEach((issue) => {
244-
if (issue.path[0] === 'dashboardName') {
245-
form.setError('dashboardName', {
246-
type: 'validate',
247-
message: issue.message,
248-
});
249-
}
250-
});
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+
}
251267
} else {
252268
form.clearErrors('dashboardName');
253269
}
270+
} else if (!isPerseProject && selectedProjectName) {
271+
// Clear any existing validation errors for non-Perses projects
272+
form.clearErrors('dashboardName');
254273
}
255-
}, [selectedProjectName, dynamicValidationSchema, form]);
274+
}, [
275+
selectedProjectName,
276+
dynamicValidationSchema,
277+
form,
278+
dashboardName,
279+
isSchemaLoading,
280+
persesProjects,
281+
]);
256282

257283
React.useEffect(() => {
258284
if (isOpen && dashboard && editableProjects?.length > 0 && defaultProject) {
@@ -453,9 +479,10 @@ export const DuplicateActionModal = ({ dashboard, isOpen, onClose }: ActionModal
453479
!(form.watch('dashboardName') || '')?.trim() ||
454480
!(form.watch('projectName') || '')?.trim() ||
455481
!hasEditableProject ||
482+
isSchemaLoading ||
456483
createDashboardMutation.isPending
457484
}
458-
isLoading={createDashboardMutation.isPending}
485+
isLoading={createDashboardMutation.isPending || isSchemaLoading}
459486
>
460487
{t('Duplicate')}
461488
</Button>

web/src/components/dashboards/perses/dashboard-action-validations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function useDashboardValidationSchema(
6060
if (!dashboards?.length)
6161
return {
6262
schema: createDashboardDialogValidationSchema(t),
63-
isSchemaLoading: true,
63+
isSchemaLoading: false,
6464
hasSchemaError: false,
6565
};
6666

@@ -87,6 +87,6 @@ export function useDashboardValidationSchema(
8787
}),
8888
);
8989

90-
return { schema: refinedSchema, isSchemaLoading: true, hasSchemaError: false };
90+
return { schema: refinedSchema, isSchemaLoading: false, hasSchemaError: false };
9191
}, [dashboards, isDashboardsLoading, isError, t]);
9292
}

0 commit comments

Comments
 (0)