-
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
9 changed files
with
295 additions
and
153 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
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
72 changes: 72 additions & 0 deletions
72
...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,72 @@ | ||
/******************************************************************************* | ||
* 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 { 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 { loading, complete, payload } = useDiagramSubscription(editingContextId, diagramId); | ||
|
||
useEffect(() => { | ||
if (isDiagramRefreshedEventPayload(payload)) { | ||
setState((prevState) => ({ ...prevState, diagramRefreshedEventPayload: payload })); | ||
} | ||
}, [payload]); | ||
|
||
if (loading || !state.diagramRefreshedEventPayload) { | ||
return <LinearProgress />; | ||
} | ||
|
||
if (complete) { | ||
return ( | ||
<div> | ||
<Typography variant="subtitle2">The representation is not available anymore</Typography> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<DiagramContext.Provider | ||
value={{ | ||
editingContextId, | ||
diagramId: diagramId, | ||
refreshEventPayloadId: state.diagramRefreshedEventPayload.id, | ||
payload: payload, | ||
readOnly, | ||
}}> | ||
<DiagramRenderer diagramRefreshedEventPayload={state.diagramRefreshedEventPayload} /> | ||
</DiagramContext.Provider> | ||
); | ||
} | ||
); |
Oops, something went wrong.