-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug: #3675 Signed-off-by: Guillaume Coutable <[email protected]>
- Loading branch information
Showing
25 changed files
with
174 additions
and
57 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
74 changes: 74 additions & 0 deletions
74
.../frontend/sirius-components-core/src/representationmetadata/useRepresentationMetadata.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,74 @@ | ||
/******************************************************************************* | ||
* 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, useLazyQuery } from '@apollo/client'; | ||
import { useMultiToast } from '../toast/MultiToast'; | ||
import { RepresentationMetadata } from '../workbench/Workbench.types'; | ||
import { | ||
GQLRepresentationMetadata, | ||
GQLRepresentationMetadataQueryData, | ||
GQLRepresentationMetadataQueryVariables, | ||
UseRepresentationMetadataValue, | ||
} from './useRepresentationMetadata.types'; | ||
|
||
const getRepresentationMetadata = gql` | ||
query getRepresentationMetadata($editingContextId: ID!, $representationId: ID!) { | ||
viewer { | ||
editingContext(editingContextId: $editingContextId) { | ||
representation(representationId: $representationId) { | ||
id | ||
label | ||
kind | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const notNullNorUndefined = ( | ||
metadata: GQLRepresentationMetadata | null | undefined | ||
): metadata is GQLRepresentationMetadata => !!metadata; | ||
|
||
export const useRepresentationMetadata = (): UseRepresentationMetadataValue => { | ||
const { addErrorMessage } = useMultiToast(); | ||
const [fetch] = useLazyQuery<GQLRepresentationMetadataQueryData, GQLRepresentationMetadataQueryVariables>( | ||
getRepresentationMetadata | ||
); | ||
|
||
const representationMetadata = ( | ||
editingContextId: string, | ||
representationIds: string[], | ||
onRetrieveRepresentationMetadataSuccess: (representationMetadata: RepresentationMetadata[]) => void | ||
): void => { | ||
Promise.all( | ||
representationIds.map((representationId) => { | ||
const variables: GQLRepresentationMetadataQueryVariables = { | ||
editingContextId, | ||
representationId, | ||
}; | ||
return fetch({ variables }); | ||
}) | ||
).then( | ||
(result) => { | ||
const representationMetadata: RepresentationMetadata[] = result | ||
.map((queryResult) => queryResult.data?.viewer.editingContext.representation) | ||
.filter(notNullNorUndefined); | ||
onRetrieveRepresentationMetadataSuccess(representationMetadata); | ||
}, | ||
() => { | ||
addErrorMessage('An error occurred while fetching representation metadata, please refresh.'); | ||
} | ||
); | ||
}; | ||
|
||
return { representationMetadata }; | ||
}; |
43 changes: 43 additions & 0 deletions
43
...end/sirius-components-core/src/representationmetadata/useRepresentationMetadata.types.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,43 @@ | ||
/******************************************************************************* | ||
* 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 { RepresentationMetadata } from '../workbench/Workbench.types'; | ||
|
||
export interface UseRepresentationMetadataValue { | ||
representationMetadata: ( | ||
editingContextId: string, | ||
representationIds: string[], | ||
onRetrieveRepresentationMetadataSuccess: (representationMetadata: RepresentationMetadata[]) => void | ||
) => void; | ||
} | ||
|
||
export interface GQLRepresentationMetadataQueryData { | ||
viewer: GQLViewer; | ||
} | ||
|
||
export interface GQLViewer { | ||
editingContext: GQLEditingContext; | ||
} | ||
export interface GQLEditingContext { | ||
representation: GQLRepresentationMetadata | null; | ||
} | ||
|
||
export interface GQLRepresentationMetadata { | ||
id: string; | ||
label: string; | ||
kind: string; | ||
} | ||
|
||
export interface GQLRepresentationMetadataQueryVariables { | ||
editingContextId: string; | ||
representationId: string; | ||
} |
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
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
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
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
Oops, something went wrong.