Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for spreadsheet config collections #578

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/directory-content-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const getClickableRowStyle = (cellData: RowClassParams<ElementAttributes>) => {
ElementType.VOLTAGE_INIT_PARAMETERS,
ElementType.SHORT_CIRCUIT_PARAMETERS,
ElementType.SPREADSHEET_CONFIG,
ElementType.SPREADSHEET_CONFIG_COLLECTION,
].includes(cellData.data.type)
) {
style.cursor = 'pointer';
Expand Down
14 changes: 13 additions & 1 deletion src/components/menus/content-contextual-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
deleteElements,
duplicateElement,
duplicateSpreadsheetConfig,
duplicateSpreadsheetConfigCollection,
elementExists,
moveElementsToDirectory,
newScriptFromFilter,
Expand Down Expand Up @@ -169,6 +170,7 @@ export default function ContentContextualMenu(props: Readonly<ContentContextualM
case ElementType.LOADFLOW_PARAMETERS:
case ElementType.SHORT_CIRCUIT_PARAMETERS:
case ElementType.SPREADSHEET_CONFIG:
case ElementType.SPREADSHEET_CONFIG_COLLECTION:
console.info(
`${activeElement.type} with uuid ${activeElement.elementUuid} from directory ${selectedDirectory?.elementUuid} selected for copy`
);
Expand Down Expand Up @@ -241,6 +243,11 @@ export default function ContentContextualMenu(props: Readonly<ContentContextualM
handleDuplicateError(error.message);
});
break;
case ElementType.SPREADSHEET_CONFIG_COLLECTION:
duplicateSpreadsheetConfigCollection(activeElement.elementUuid).catch((error) => {
handleDuplicateError(error.message);
});
break;
default: {
handleLastError(intl.formatMessage({ id: 'unsupportedItem' }));
}
Expand Down Expand Up @@ -409,6 +416,7 @@ export default function ContentContextualMenu(props: Readonly<ContentContextualM
ElementType.SHORT_CIRCUIT_PARAMETERS,
ElementType.LOADFLOW_PARAMETERS,
ElementType.SPREADSHEET_CONFIG,
ElementType.SPREADSHEET_CONFIG_COLLECTION,
];

const hasMetadata = selectedElements[0]?.hasMetadata;
Expand Down Expand Up @@ -453,7 +461,11 @@ export default function ContentContextualMenu(props: Readonly<ContentContextualM
);

const allowsDownload = useCallback(() => {
const allowedTypes = [ElementType.CASE, ElementType.SPREADSHEET_CONFIG];
const allowedTypes = [
ElementType.CASE,
ElementType.SPREADSHEET_CONFIG,
ElementType.SPREADSHEET_CONFIG_COLLECTION,
];
// if selectedElements contains at least one of the allowed types
return selectedElements.some((element) => allowedTypes.includes(element.type)) && noCreationInProgress();
}, [selectedElements, noCreationInProgress]);
Expand Down
6 changes: 6 additions & 0 deletions src/components/menus/directory-tree-contextual-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
deleteElement,
duplicateElement,
duplicateSpreadsheetConfig,
duplicateSpreadsheetConfigCollection,
elementExists,
insertDirectory,
insertRootDirectory,
Expand Down Expand Up @@ -172,6 +173,11 @@ export default function DirectoryTreeContextualMenu(props: Readonly<DirectoryTre
handlePasteError(error)
);
break;
case ElementType.SPREADSHEET_CONFIG_COLLECTION:
duplicateSpreadsheetConfigCollection(selectionForPaste.sourceItemUuid, directoryUuid).catch(
(error: any) => handlePasteError(error)
);
break;
default:
handleError(
intl.formatMessage({
Expand Down
6 changes: 5 additions & 1 deletion src/components/toolbars/content-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export default function ContentToolbar(props: Readonly<ContentToolbarProps>) {
);

const allowsDownload = useMemo(() => {
const allowedTypes = [ElementType.CASE, ElementType.SPREADSHEET_CONFIG];
const allowedTypes = [
ElementType.CASE,
ElementType.SPREADSHEET_CONFIG,
ElementType.SPREADSHEET_CONFIG_COLLECTION,
];
// if selectedElements contains at least one of the allowed types
return selectedElements.some((element) => allowedTypes.includes(element.type)) && noCreationInProgress;
}, [selectedElements, noCreationInProgress]);
Expand Down
25 changes: 24 additions & 1 deletion src/components/utils/downloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import { useIntl } from 'react-intl';
import { ElementAttributes, ElementType, useSnackMessage } from '@gridsuite/commons-ui';
import { useCallback, useState } from 'react';
import { UUID } from 'crypto';
import { downloadCase, downloadSpreadsheetConfig, fetchConvertedCase, getCaseOriginalName } from '../../utils/rest-api';
import {
downloadCase,
downloadSpreadsheetConfig,
downloadSpreadsheetConfigCollection,
fetchConvertedCase,
getCaseOriginalName,
} from '../../utils/rest-api';

interface DownloadData {
blob: Blob;
Expand Down Expand Up @@ -56,6 +62,23 @@ const downloadStrategies: { [key in ElementType]?: (element: ElementAttributes)
return { blob: await result.blob(), filename };
}
},
[ElementType.SPREADSHEET_CONFIG_COLLECTION]: async (element: ElementAttributes) => {
const result = await downloadSpreadsheetConfigCollection(element.elementUuid);
const filename = `${element.elementName}.json`;
try {
// Parse the JSON to ensure it's valid
const jsonContent = await result.json();
// Stringify with indentation for pretty formatting
const prettyJson = JSON.stringify(jsonContent, null, 2);
// Create a new Blob with the pretty-formatted JSON
const blob = new Blob([prettyJson], { type: 'application/json' });
return { blob, filename };
} catch (error) {
// If parsing fails, fall back to the original blob
console.error('Error parsing JSON:', error);
return { blob: await result.blob(), filename };
}
},
};

export function useDownloadUtils() {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"editFilter": "Edit filter",
"STUDY": "Study",
"SPREADSHEET_CONFIG": "Spreadsheet model",
"SPREADSHEET_CONFIG_COLLECTION": "Spreadsheet model collection",
"VOLTAGE_INIT_PARAMETERS": "Settings (Voltage profile initialization)",
"SECURITY_ANALYSIS_PARAMETERS": "Settings (Security analysis)",
"SENSITIVITY_PARAMETERS": "Settings (Sensitivity analysis)",
Expand Down
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"editFilter": "Éditer le filtre",
"STUDY": "Étude",
"SPREADSHEET_CONFIG": "Modèle de tableur",
"SPREADSHEET_CONFIG_COLLECTION": "Modèles de tableur",
"VOLTAGE_INIT_PARAMETERS": "Paramètres (Initialisation du plan de tension)",
"SECURITY_ANALYSIS_PARAMETERS": "Paramètres (Analyse de sécurité)",
"SENSITIVITY_PARAMETERS": "Paramètres (Analyse de sensibilité)",
Expand Down
28 changes: 28 additions & 0 deletions src/utils/rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,22 @@ export function duplicateSpreadsheetConfig(sourceCaseUuid: UUID, parentDirectory
});
}

export function duplicateSpreadsheetConfigCollection(sourceCaseUuid: UUID, parentDirectoryUuid?: UUID) {
console.info('Duplicating a spreadsheet config collection...');
const queryParams = new URLSearchParams();
queryParams.append('duplicateFrom', sourceCaseUuid);
if (parentDirectoryUuid) {
queryParams.append('parentDirectoryUuid', parentDirectoryUuid);
}
const url = `${PREFIX_EXPLORE_SERVER_QUERIES}/v1/explore/spreadsheet-config-collections/duplicate?${queryParams.toString()}`;

console.debug(url);

return backendFetch(url, {
method: 'post',
});
}

export function downloadSpreadsheetConfig(configId: string) {
console.info(`Downloading spreadsheet config with id: ${configId}`);
const fetchUrl = `${PREFIX_SPREADSHEET_CONFIG_QUERIES}/v1/spreadsheet-configs/${configId}`;
Expand All @@ -444,6 +460,18 @@ export function downloadSpreadsheetConfig(configId: string) {
});
}

export function downloadSpreadsheetConfigCollection(collectionId: string) {
console.info(`Downloading spreadsheet config collection with id: ${collectionId}`);
const fetchUrl = `${PREFIX_SPREADSHEET_CONFIG_QUERIES}/v1/spreadsheet-config-collections/${collectionId}`;

return backendFetch(fetchUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
}

export function elementExists(directoryUuid: UUID | null | undefined, elementName: string, type: string) {
const elementNameEncoded = encodeURIComponent(elementName);
const existsElementUrl = `${PREFIX_DIRECTORY_SERVER_QUERIES}/v1/directories/${directoryUuid}/elements/${elementNameEncoded}/types/${type}`;
Expand Down
Loading