-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3533] Add a dedicated component to handle diagram subscription
Bug: #3533 Signed-off-by: Michaël Charfadi <[email protected]>
- Loading branch information
Showing
7 changed files
with
246 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...ms/frontend/sirius-components-diagrams/src/representation/DiagramSubscriptionProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 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 LinearProgress from '@mui/material/LinearProgress'; | ||
import Typography from '@mui/material/Typography'; | ||
import { memo, useEffect, useState } from 'react'; | ||
import { DiagramContext } from '../contexts/DiagramContext'; | ||
import { | ||
GQLDiagramEventPayload, | ||
GQLDiagramRefreshedEventPayload, | ||
} from '../graphql/subscription/diagramEventSubscription.types'; | ||
import { DiagramRenderer } from '../renderer/DiagramRenderer'; | ||
import { DiagramSubscriptionProviderProps, DiagramSubscriptionState } from './DiagramSubscriptionProvider.types'; | ||
import { StoreContextProvider } from './StoreContext'; | ||
import { useDiagramSubscription } from './useDiagramSubscription'; | ||
|
||
const isDiagramRefreshedEventPayload = ( | ||
payload: GQLDiagramEventPayload | null | ||
): payload is GQLDiagramRefreshedEventPayload => !!payload && payload.__typename === 'DiagramRefreshedEventPayload'; | ||
|
||
export const DiagramSubscriptionProvider = memo( | ||
({ diagramId, editingContextId, readOnly }: DiagramSubscriptionProviderProps) => { | ||
const [state, setState] = useState<DiagramSubscriptionState>({ | ||
id: crypto.randomUUID(), | ||
diagramRefreshedEventPayload: null, | ||
complete: false, | ||
message: '', | ||
}); | ||
|
||
const { complete, payload } = useDiagramSubscription(editingContextId, diagramId); | ||
|
||
useEffect(() => { | ||
if (isDiagramRefreshedEventPayload(payload)) { | ||
setState((prevState) => ({ ...prevState, diagramRefreshedEventPayload: payload })); | ||
} | ||
}, [payload]); | ||
|
||
if (complete) { | ||
return ( | ||
<div> | ||
<Typography variant="subtitle2">The representation is not available anymore</Typography> | ||
</div> | ||
); | ||
} | ||
|
||
if (!state.diagramRefreshedEventPayload) { | ||
return <LinearProgress />; | ||
} | ||
|
||
return ( | ||
<StoreContextProvider> | ||
<DiagramContext.Provider | ||
value={{ | ||
editingContextId, | ||
diagramId: diagramId, | ||
refreshEventPayloadId: state.diagramRefreshedEventPayload.id, | ||
payload: payload, | ||
readOnly, | ||
}}> | ||
<div | ||
style={{ display: 'inline-block', position: 'relative' }} | ||
data-representation-kind="diagram" | ||
data-representation-label={state.diagramRefreshedEventPayload.diagram.metadata.label}> | ||
<DiagramRenderer | ||
key={state.diagramRefreshedEventPayload.diagram.id} | ||
diagramRefreshedEventPayload={state.diagramRefreshedEventPayload} | ||
/> | ||
</div> | ||
</DiagramContext.Provider> | ||
</StoreContextProvider> | ||
); | ||
} | ||
); |
31 changes: 31 additions & 0 deletions
31
...ontend/sirius-components-diagrams/src/representation/DiagramSubscriptionProvider.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 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 { GQLDiagramRefreshedEventPayload } from '../graphql/subscription/diagramEventSubscription.types'; | ||
|
||
export type DiagramSubscriptionContextValue = { | ||
diagramRefreshedEventPayload: GQLDiagramRefreshedEventPayload | null; | ||
refreshEventPayloadId: string; | ||
}; | ||
|
||
export type DiagramSubscriptionState = { | ||
id: string; | ||
diagramRefreshedEventPayload: GQLDiagramRefreshedEventPayload | null; | ||
complete: boolean; | ||
message: string; | ||
}; | ||
|
||
export interface DiagramSubscriptionProviderProps { | ||
editingContextId: string; | ||
diagramId: string; | ||
readOnly: boolean; | ||
} |
65 changes: 65 additions & 0 deletions
65
...iagrams/frontend/sirius-components-diagrams/src/representation/useDiagramSubscription.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 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, useSubscription } from '@apollo/client'; | ||
import { useMultiToast } from '@eclipse-sirius/sirius-components-core'; | ||
import { useEffect, useState } from 'react'; | ||
import { diagramEventSubscription } from '../graphql/subscription/diagramEventSubscription'; | ||
import { | ||
GQLDiagramEventSubscription, | ||
GQLDiagramEventVariables, | ||
UseDiagramSubscriptionState, | ||
UseDiagramSubscriptionValue, | ||
} from './useDiagramSubscription.types'; | ||
|
||
export const useDiagramSubscription = (editingContextId: string, diagramId: string): UseDiagramSubscriptionValue => { | ||
const [state, setState] = useState<UseDiagramSubscriptionState>({ | ||
id: crypto.randomUUID(), | ||
complete: false, | ||
payload: null, | ||
}); | ||
|
||
const variables: GQLDiagramEventVariables = { | ||
input: { | ||
id: state.id, | ||
editingContextId, | ||
diagramId, | ||
}, | ||
}; | ||
|
||
const onComplete = () => { | ||
setState((prevState) => ({ ...prevState, diagramRefreshedEventPayload: null, complete: true })); | ||
}; | ||
|
||
const { error, loading, data } = useSubscription<GQLDiagramEventSubscription, GQLDiagramEventVariables>( | ||
gql(diagramEventSubscription), | ||
{ | ||
variables, | ||
fetchPolicy: 'no-cache', | ||
onComplete, | ||
} | ||
); | ||
|
||
const { addErrorMessage } = useMultiToast(); | ||
useEffect(() => { | ||
if (error) { | ||
addErrorMessage('An unexpected error has occurred, please refresh the page'); | ||
} | ||
}, [error]); | ||
|
||
return { | ||
loading, | ||
payload: data?.diagramEvent ?? null, | ||
complete: state.complete, | ||
}; | ||
}; |
Oops, something went wrong.