From e5be431f183580070c3f15bee4c0a0697b540e69 Mon Sep 17 00:00:00 2001 From: SagarRajput-7 <162284829+SagarRajput-7@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:51:43 +0530 Subject: [PATCH] fix: removed selectedValue & uuid from exported and copied dashboard json (#6145) * fix: removed selectedValue from exported and copied dashboard json * fix: added uuid validation to not expect empty uuid * fix: removed uuid from export and copied dashboard json * fix: resolved comment and removed uuid when empty * fix: renamed function --- .../ListOfDashboard/ImportJSON/index.tsx | 11 +++++- .../DashboardDescription/index.tsx | 39 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) 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); }} >