diff --git a/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx b/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx index 62767e6799..6926db85be 100644 --- a/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx +++ b/frontend/src/container/ListOfDashboard/ImportJSON/index.tsx @@ -82,6 +82,12 @@ function ImportJSON({ const dashboardData = JSON.parse(editorValue) as DashboardData; + // Add validation for uuid + if (dashboardData.uuid !== undefined && dashboardData.uuid.trim() === '') { + // silently remove uuid if it is empty + delete dashboardData.uuid; + } + if (dashboardData?.layout) { dashboardData.layout = getUpdatedLayout(dashboardData.layout); } else { @@ -123,11 +129,14 @@ function ImportJSON({ }); } setDashboardCreating(false); - } catch { + } catch (error) { setDashboardCreating(false); setIsFeatureAlert(false); setIsCreateDashboardError(true); + notifications.error({ + message: error instanceof Error ? error.message : t('error_loading_json'), + }); } }; diff --git a/frontend/src/container/NewDashboard/DashboardDescription/index.tsx b/frontend/src/container/NewDashboard/DashboardDescription/index.tsx index d6f63165d5..9559eed42a 100644 --- a/frontend/src/container/NewDashboard/DashboardDescription/index.tsx +++ b/frontend/src/container/NewDashboard/DashboardDescription/index.tsx @@ -47,7 +47,11 @@ import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import { useCopyToClipboard } from 'react-use'; import { AppState } from 'store/reducers'; -import { Dashboard, DashboardData } from 'types/api/dashboard/getAll'; +import { + Dashboard, + DashboardData, + IDashboardVariable, +} from 'types/api/dashboard/getAll'; import AppReducer from 'types/reducer/app'; import { ROLES, USER_ROLES } from 'types/roles'; import { ComponentTypes } from 'utils/permission'; @@ -63,6 +67,30 @@ interface DashboardDescriptionProps { handle: FullScreenHandle; } +function sanitizeDashboardData( + selectedData: DashboardData, +): Omit { + if (!selectedData?.variables) { + const { uuid, ...rest } = selectedData; + return rest; + } + + const updatedVariables = Object.entries(selectedData.variables).reduce( + (acc, [key, value]) => { + const { selectedValue, ...rest } = value; + acc[key] = rest; + return acc; + }, + {} as Record, + ); + + const { uuid, ...restData } = selectedData; + return { + ...restData, + variables: updatedVariables, + }; +} + // eslint-disable-next-line sonarjs/cognitive-complexity function DashboardDescription(props: DashboardDescriptionProps): JSX.Element { const { handle } = props; @@ -407,7 +435,10 @@ function DashboardDescription(props: DashboardDescriptionProps): JSX.Element { type="text" icon={} onClick={(): void => { - downloadObjectAsJson(selectedData, selectedData.title); + downloadObjectAsJson( + sanitizeDashboardData(selectedData), + selectedData.title, + ); setIsDashbordSettingsOpen(false); }} > @@ -417,7 +448,9 @@ function DashboardDescription(props: DashboardDescriptionProps): JSX.Element { type="text" icon={} onClick={(): void => { - setCopy(JSON.stringify(selectedData, null, 2)); + setCopy( + JSON.stringify(sanitizeDashboardData(selectedData), null, 2), + ); setIsDashbordSettingsOpen(false); }} >