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

[4348] Move quick tools declaration on the backend #4349

Open
wants to merge 1 commit into
base: master
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 CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Note that you may need to encode special characters like `[`(by `%5B`) and `]` (
- https://github.com/eclipse-sirius/sirius-web/issues/4345[#4345] [table] Make table cells independently selectable
- https://github.com/eclipse-sirius/sirius-web/issues/4352[#4352] [table] Add support to range column filters in table
- https://github.com/eclipse-sirius/sirius-web/issues/4474[#4474] [query] Add EMF Services (eSet, eUnset) for the Query View.
- https://github.com/eclipse-sirius/sirius-web/issues/4348[#4348] [sirius-web] Move quick tools declaration on the backend


== v2025.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
public final class DiagramImageConstants {

public static final String HIDE_SVG = "/icons/full/obj16/HideTool.svg";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this image coming from? Do not add coupling between unrelated modules. Is that coming from the diagram view DSL? If so, sirius-components-collaborative-diagrams can be reused with the view DSL so it cannot be referenced this way. Since I don't see any reference to this constant anywhere in sirius-components-collaborative-diagrams, you can remove it entirely


private static final String IMAGES_ROOT_FOLDER = "/diagram-images";

public static final String EDIT_SVG = IMAGES_ROOT_FOLDER + "/edit.svg";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type {
DiagramPaletteToolContributionProps,
} from './renderer/palette/extensions/DiagramPaletteToolContribution.types';
export { diagramPaletteToolExtensionPoint } from './renderer/palette/extensions/DiagramPaletteToolExtensionPoints';
export type { GQLToolVariable, GQLToolVariableType } from './renderer/palette/usePalette.types';
export type { GQLToolVariable, GQLToolVariableType } from './renderer/palette/tools/useInvokeTool.types';
export type { DiagramPanelActionProps } from './renderer/panel/DiagramPanel.types';
export { diagramPanelActionExtensionPoint } from './renderer/panel/DiagramPanelExtensionPoints';
export { DiagramRepresentation } from './representation/DiagramRepresentation';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { gql, useMutation } from '@apollo/client';
import { useMultiToast } from '@eclipse-sirius/sirius-components-core';
import { useCallback, useContext, useEffect } from 'react';
import { DiagramContext } from '../../contexts/DiagramContext';
import { DiagramContextValue } from '../../contexts/DiagramContext.types';
import {
GQLCollapsingState,
GQLErrorPayload,
GQLUpdateCollapsingStateData,
GQLUpdateCollapsingStateInput,
GQLUpdateCollapsingStatePayload,
GQLUpdateCollapsingStateVariables,
UseCollapseExpandElement,
} from './useCollapseExpandElement.types';

const updateCollapsingStateMutation = gql`
mutation updateCollapsingState($input: UpdateCollapsingStateInput!) {
updateCollapsingState(input: $input) {
__typename
... on SuccessPayload {
id
}
... on ErrorPayload {
message
}
}
}
`;

const isErrorPayload = (payload: GQLUpdateCollapsingStatePayload): payload is GQLErrorPayload =>
payload.__typename === 'ErrorPayload';

export const useCollapseExpandElement = (): UseCollapseExpandElement => {
const { addMessages, addErrorMessage } = useMultiToast();
const { diagramId, editingContextId } = useContext<DiagramContextValue>(DiagramContext);

const [collapseExpandMutation, { error: collpaseNodeError }] = useMutation<
GQLUpdateCollapsingStateData,
GQLUpdateCollapsingStateVariables
>(updateCollapsingStateMutation);

const collapseExpandElement = useCallback(
async (nodeId: string, collapsingState: GQLCollapsingState) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use async not await

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it was already used so I did not change it but if there was no reason to use async I can remove it.

const input: GQLUpdateCollapsingStateInput = {
id: crypto.randomUUID(),
editingContextId,
representationId: diagramId,
diagramElementId: nodeId,
collapsingState,
};
const { data: collapseNodeData } = await collapseExpandMutation({ variables: { input } });
if (collapseNodeData) {
const { collapseExpandDiagramElement } = collapseNodeData;
if (isErrorPayload(collapseExpandDiagramElement)) {
addMessages(collapseExpandDiagramElement.messages);
}
}
Comment on lines +63 to +69
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just write this kind of code as we always do

},
[editingContextId, diagramId, collapseExpandMutation]
);

useEffect(() => {
if (collpaseNodeError) {
addErrorMessage('An unexpected error has occurred, please refresh the page');
}
}, [collpaseNodeError]);

return { collapseExpandElement };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { GQLMessage } from '../Tool.types';

export interface UseCollapseExpandElement {
collapseExpandElement: (nodeId: string, collapsingState: GQLCollapsingState) => void;
}

export interface GQLUpdateCollapsingStateData {
collapseExpandDiagramElement: GQLUpdateCollapsingStatePayload;
}

export interface GQLUpdateCollapsingStatePayload {
__typename: string;
}

export interface GQLUpdateCollapsingStateVariables {
input: GQLUpdateCollapsingStateInput;
}

export interface GQLUpdateCollapsingStateInput {
id: string;
editingContextId: string;
representationId: string;
diagramElementId: string;
collapsingState: GQLCollapsingState;
}

export enum GQLCollapsingState {
EXPANDED = 'EXPANDED',
COLLAPSED = 'COLLAPSED',
}

export interface GQLErrorPayload extends GQLUpdateCollapsingStatePayload {
message: string;
messages: GQLMessage[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,21 @@
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { gql, useMutation } from '@apollo/client';
import { useDeletionConfirmationDialog, useMultiToast } from '@eclipse-sirius/sirius-components-core';
import { useDeletionConfirmationDialog } from '@eclipse-sirius/sirius-components-core';
import { Edge, Node, useReactFlow } from '@xyflow/react';
import { useCallback, useContext, useEffect } from 'react';
import { useCallback, useContext } from 'react';
import { DiagramContext } from '../../contexts/DiagramContext';
import { DiagramContextValue } from '../../contexts/DiagramContext.types';
import { EdgeData, NodeData } from '../DiagramRenderer.types';
import {
GQLDeleteFromDiagramData,
GQLDeleteFromDiagramInput,
GQLDeleteFromDiagramPayload,
GQLDeleteFromDiagramSuccessPayload,
GQLDeleteFromDiagramVariables,
GQLDeletionPolicy,
GQLErrorPayload,
} from '../palette/usePalette.types';
import { UseDiagramDeleteValue } from './useDiagramDelete.types';

export const deleteFromDiagramMutation = gql`
mutation deleteFromDiagram($input: DeleteFromDiagramInput!) {
deleteFromDiagram(input: $input) {
__typename
... on ErrorPayload {
messages {
body
level
}
}
... on DeleteFromDiagramSuccessPayload {
messages {
body
level
}
}
}
}
`;

const isErrorPayload = (payload: GQLDeleteFromDiagramPayload): payload is GQLErrorPayload =>
payload.__typename === 'ErrorPayload';
const isSuccessPayload = (payload: GQLDeleteFromDiagramPayload): payload is GQLDeleteFromDiagramSuccessPayload =>
payload.__typename === 'DeleteFromDiagramSuccessPayload';
import { useDiagramDeleteMutation } from './useDiagramDeleteMutation';
import { GQLDeletionPolicy } from './useDiagramDeleteMutation.types';

export const useDiagramDelete = (): UseDiagramDeleteValue => {
const { addErrorMessage, addMessages } = useMultiToast();
const { showDeletionConfirmation } = useDeletionConfirmationDialog();
const { diagramId, editingContextId, readOnly } = useContext<DiagramContextValue>(DiagramContext);
const { getNodes } = useReactFlow<Node<NodeData>, Edge<EdgeData>>();

const [deleteElementsMutation, { data: deleteElementsData, error: deleteElementsError }] = useMutation<
GQLDeleteFromDiagramData,
GQLDeleteFromDiagramVariables
>(deleteFromDiagramMutation);

useEffect(() => {
if (deleteElementsError) {
addErrorMessage('An unexpected error has occurred, please refresh the page');
}
if (deleteElementsData) {
const { deleteFromDiagram } = deleteElementsData;
if (isErrorPayload(deleteFromDiagram) || isSuccessPayload(deleteFromDiagram)) {
addMessages(deleteFromDiagram.messages);
}
}
}, [deleteElementsData, deleteElementsError]);
const { diagramId, editingContextId, readOnly } = useContext<DiagramContextValue>(DiagramContext);
const { deleteElements } = useDiagramDeleteMutation();
const { showDeletionConfirmation } = useDeletionConfirmationDialog();

const onDelete = useCallback((event: React.KeyboardEvent<Element>) => {
const { key } = event;
Expand All @@ -89,16 +39,9 @@ export const useDiagramDelete = (): UseDiagramDeleteValue => {
const nodeToDeleteIds: string[] = getNodes()
.filter((node) => node.selected)
.map((node) => node.id);
const input: GQLDeleteFromDiagramInput = {
id: crypto.randomUUID(),
editingContextId,
representationId: diagramId,
nodeIds: nodeToDeleteIds,
edgeIds: [],
deletionPolicy: GQLDeletionPolicy.SEMANTIC,
};

showDeletionConfirmation(() => {
deleteElementsMutation({ variables: { input } });
deleteElements(nodeToDeleteIds, [], GQLDeletionPolicy.SEMANTIC);
});
}
}, []);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { gql, useMutation } from '@apollo/client';
import { useMultiToast } from '@eclipse-sirius/sirius-components-core';
import { useCallback, useContext, useEffect } from 'react';
import { DiagramContext } from '../../contexts/DiagramContext';
import { DiagramContextValue } from '../../contexts/DiagramContext.types';
import {
GQLDeleteFromDiagramData,
GQLDeleteFromDiagramInput,
GQLDeleteFromDiagramPayload,
GQLDeleteFromDiagramSuccessPayload,
GQLDeleteFromDiagramVariables,
GQLDeletionPolicy,
GQLErrorPayload,
UseDiagramDeleteMutation,
} from './useDiagramDeleteMutation.types';

export const deleteFromDiagramMutation = gql`
mutation deleteFromDiagram($input: DeleteFromDiagramInput!) {
deleteFromDiagram(input: $input) {
__typename
... on ErrorPayload {
messages {
body
level
}
}
... on DeleteFromDiagramSuccessPayload {
messages {
body
level
}
}
}
}
`;

const isErrorPayload = (payload: GQLDeleteFromDiagramPayload): payload is GQLErrorPayload =>
payload.__typename === 'ErrorPayload';

const isSuccessPayload = (payload: GQLDeleteFromDiagramPayload): payload is GQLDeleteFromDiagramSuccessPayload =>
payload.__typename === 'DeleteFromDiagramSuccessPayload';

export const useDiagramDeleteMutation = (): UseDiagramDeleteMutation => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hook should probably be named useDeleteFromDiagram to stay consistent with our naming conventions

const { addMessages, addErrorMessage } = useMultiToast();
const { diagramId, editingContextId } = useContext<DiagramContextValue>(DiagramContext);

const [deleteElementsMutation, { data: deleteElementsData, error: deleteElementsError }] = useMutation<
GQLDeleteFromDiagramData,
GQLDeleteFromDiagramVariables
>(deleteFromDiagramMutation);

const deleteElements = useCallback(
async (nodeIds: string[], edgesId: string[], deletePocily: GQLDeletionPolicy) => {
const input: GQLDeleteFromDiagramInput = {
id: crypto.randomUUID(),
editingContextId,
representationId: diagramId,
nodeIds: nodeIds,
edgeIds: edgesId,
deletionPolicy: deletePocily,
};

const { data } = await deleteElementsMutation({ variables: { input } });
if (data) {
const { deleteFromDiagram } = data;
if (isErrorPayload(deleteFromDiagram) || isSuccessPayload(deleteFromDiagram)) {
addMessages(deleteFromDiagram.messages);
}
}
},
[editingContextId, diagramId, deleteElementsMutation]
);
Comment on lines +64 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use async / await, we have tons of hooks using mutations, just do the same thing


useEffect(() => {
if (deleteElementsError) {
addErrorMessage('An unexpected error has occurred, please refresh the page');
}
if (deleteElementsData) {
const { deleteFromDiagram } = deleteElementsData;
if (isErrorPayload(deleteFromDiagram) || isSuccessPayload(deleteFromDiagram)) {
addMessages(deleteFromDiagram.messages);
}
}
}, [deleteElementsData, deleteElementsError]);

return { deleteElements };
};
Loading
Loading