1111 * Obeo - initial API and implementation
1212 *******************************************************************************/
1313
14- import { gql , useQuery } from '@apollo/client' ;
1514import { IconOverlay , useMultiToast } from '@eclipse-sirius/sirius-components-core' ;
1615import ListItemIcon from '@mui/material/ListItemIcon' ;
1716import Menu from '@mui/material/Menu' ;
1817import MenuItem from '@mui/material/MenuItem' ;
1918import Typography from '@mui/material/Typography' ;
2019import { Edge , Node , useReactFlow } from '@xyflow/react' ;
21- import { memo , useContext , useEffect } from 'react' ;
22- import { DiagramContext } from '../../contexts/DiagramContext' ;
23- import { DiagramContextValue } from '../../contexts/DiagramContext.types' ;
20+ import { memo , useEffect } from 'react' ;
2421import { EdgeData , NodeData } from '../DiagramRenderer.types' ;
25- import {
26- ConnectorContextualMenuProps ,
27- GetConnectorToolsData ,
28- GetConnectorToolsVariables ,
29- GQLDiagramDescription ,
30- GQLRepresentationDescription ,
31- GQLTool ,
32- } from './ConnectorContextualMenu.types' ;
22+ import { ConnectorContextualMenuProps , GQLTool } from './ConnectorContextualMenu.types' ;
3323import { useConnector } from './useConnector' ;
24+ import { useConnectorPaletteContents } from './useConnectorPaletteContents' ;
3425import { useSingleClickOnTwoDiagramElementTool } from './useSingleClickOnTwoDiagramElementTool' ;
3526import { useTemporaryEdge } from './useTemporaryEdge' ;
3627
37- export const getConnectorToolsQuery = gql `
38- query getConnectorTools(
39- $editingContextId: ID!
40- $representationId: ID!
41- $sourceDiagramElementId: ID!
42- $targetDiagramElementId: ID!
43- ) {
44- viewer {
45- editingContext(editingContextId: $editingContextId) {
46- representation(representationId: $representationId) {
47- description {
48- ... on DiagramDescription {
49- connectorTools(
50- sourceDiagramElementId: $sourceDiagramElementId
51- targetDiagramElementId: $targetDiagramElementId
52- ) {
53- id
54- label
55- iconURL
56- ... on SingleClickOnTwoDiagramElementsTool {
57- dialogDescriptionId
58- }
59- }
60- }
61- }
62- }
63- }
64- }
65- }
66- ` ;
67-
68- const isDiagramDescription = (
69- representationDescription : GQLRepresentationDescription
70- ) : representationDescription is GQLDiagramDescription => representationDescription . __typename === 'DiagramDescription' ;
71-
7228const ConnectorContextualMenuComponent = memo ( ( { } : ConnectorContextualMenuProps ) => {
73- const { editingContextId, diagramId } = useContext < DiagramContextValue > ( DiagramContext ) ;
7429 const { connection, position, onConnectorContextualMenuClose } = useConnector ( ) ;
7530 const { addTempConnectionLine, removeTempConnectionLine } = useTemporaryEdge ( ) ;
76- const { addMessages, addErrorMessage } = useMultiToast ( ) ;
31+ const { addMessages } = useMultiToast ( ) ;
7732 const { screenToFlowPosition } = useReactFlow < Node < NodeData > , Edge < EdgeData > > ( ) ;
7833 const { invokeConnectorTool, data : invokeSingleClickOnTwoDiagramElementToolCalled } =
7934 useSingleClickOnTwoDiagramElementTool ( ) ;
@@ -89,29 +44,7 @@ const ConnectorContextualMenuComponent = memo(({}: ConnectorContextualMenuProps)
8944 const sourceDiagramElementId = connectionSource ?. dataset . id ?? '' ;
9045 const targetDiagramElementId = connectionTarget ?. dataset . id ?? '' ;
9146
92- const variables : GetConnectorToolsVariables = {
93- editingContextId,
94- representationId : diagramId ,
95- sourceDiagramElementId,
96- targetDiagramElementId,
97- } ;
98- const { loading, data, error } = useQuery < GetConnectorToolsData , GetConnectorToolsVariables > ( getConnectorToolsQuery , {
99- variables,
100- skip : ! connectionSource || ! connectionTarget ,
101- } ) ;
102-
103- useEffect ( ( ) => {
104- if ( error ) {
105- addErrorMessage ( error . message ) ;
106- }
107- } , [ error ] ) ;
108-
109- const connectorTools : GQLTool [ ] = [ ] ;
110- const representationDescription : GQLRepresentationDescription | null | undefined =
111- data ?. viewer . editingContext ?. representation ?. description ;
112- if ( representationDescription && isDiagramDescription ( representationDescription ) ) {
113- representationDescription . connectorTools . forEach ( ( tool ) => connectorTools . push ( tool ) ) ;
114- }
47+ const { connectorTools, loading } = useConnectorPaletteContents ( sourceDiagramElementId , targetDiagramElementId ) ;
11548
11649 useEffect ( ( ) => {
11750 if ( connectorTools . length > 1 ) {
@@ -120,11 +53,10 @@ const ConnectorContextualMenuComponent = memo(({}: ConnectorContextualMenuProps)
12053 } , [ connection , connectorTools . length ] ) ;
12154
12255 useEffect ( ( ) => {
123- if ( ! loading && connection && data && connectorTools . length === 0 ) {
124- onConnectorContextualMenuClose ( ) ;
56+ if ( ! loading && connection && connectorTools . length === 0 ) {
12557 addMessages ( [ { body : 'No edge found between source and target selected' , level : 'WARNING' } ] ) ;
12658 }
127- } , [ loading , data , connection , connectorTools . length ] ) ;
59+ } , [ loading , connectorTools , connection , connectorTools . length ] ) ;
12860
12961 useEffect ( ( ) => {
13062 return ( ) => removeTempConnectionLine ( ) ;
@@ -141,7 +73,7 @@ const ConnectorContextualMenuComponent = memo(({}: ConnectorContextualMenuProps)
14173 invokeConnectorTool ( tool , sourceDiagramElementId , targetDiagramElementId , cursorPositionX , cursorPositionY ) ;
14274 } ;
14375
144- if ( ! data || connectorTools . length <= 1 ) {
76+ if ( ! connectorTools || connectorTools . length <= 1 ) {
14577 return null ;
14678 }
14779
0 commit comments