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
This commit is contained in:
SagarRajput-7 2024-10-21 15:51:43 +05:30 committed by GitHub
parent 503ed45a99
commit e5be431f18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 4 deletions

View File

@ -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'),
});
}
};

View File

@ -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<DashboardData, 'uuid'> {
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<string, IDashboardVariable>,
);
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={<FileJson size={14} />}
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={<ClipboardCopy size={14} />}
onClick={(): void => {
setCopy(JSON.stringify(selectedData, null, 2));
setCopy(
JSON.stringify(sanitizeDashboardData(selectedData), null, 2),
);
setIsDashbordSettingsOpen(false);
}}
>