-
Notifications
You must be signed in to change notification settings - Fork 45
/
EditProjectView.tsx
212 lines (202 loc) · 7.93 KB
/
EditProjectView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
* Copyright (c) 2019, 2023 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, useQuery } from '@apollo/client';
import { Representation, Toast, Workbench, WorkbenchViewContribution } from '@eclipse-sirius/sirius-components-core';
import { DetailsView, RelatedElementsView, RepresentationsView } from '@eclipse-sirius/sirius-components-forms';
import {
ExplorerView,
GQLTreeItem,
TreeItemContextMenuContext,
TreeItemContextMenuContribution,
} from '@eclipse-sirius/sirius-components-trees';
import { ValidationView } from '@eclipse-sirius/sirius-components-validation';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import AccountTreeIcon from '@material-ui/icons/AccountTree';
import Filter from '@material-ui/icons/Filter';
import LinkIcon from '@material-ui/icons/Link';
import MenuIcon from '@material-ui/icons/Menu';
import WarningIcon from '@material-ui/icons/Warning';
import { useMachine } from '@xstate/react';
import { useEffect } from 'react';
import { generatePath, useHistory, useParams, useRouteMatch } from 'react-router-dom';
import { NavigationBar } from '../../navigationBar/NavigationBar';
import { OnboardArea } from '../../onboarding/OnboardArea';
import { DiagramTreeItemContextMenuContribution } from './DiagramTreeItemContextMenuContribution';
import { DocumentTreeItemContextMenuContribution } from './DocumentTreeItemContextMenuContribution';
import { EditProjectNavbar } from './EditProjectNavbar/EditProjectNavbar';
import { EditProjectViewParams, GQLGetProjectQueryData, GQLGetProjectQueryVariables } from './EditProjectView.types';
import {
EditProjectViewContext,
EditProjectViewEvent,
HandleFetchedProjectEvent,
HideToastEvent,
SchemaValue,
SelectRepresentationEvent,
ShowToastEvent,
editProjectViewMachine,
} from './EditProjectViewMachine';
import { ObjectTreeItemContextMenuContribution } from './ObjectTreeItemContextMenuContribution';
const getProjectQuery = gql`
query getRepresentation($projectId: ID!, $representationId: ID!, $includeRepresentation: Boolean!) {
viewer {
project(projectId: $projectId) {
id
name
currentEditingContext {
id
representation(representationId: $representationId) @include(if: $includeRepresentation) {
id
label
kind
}
}
}
}
}
`;
const useEditProjectViewStyles = makeStyles((theme) => ({
editProjectView: {
display: 'grid',
gridTemplateRows: 'min-content minmax(0, 1fr)',
gridTemplateColumns: '1fr',
height: '100vh',
width: '100vw',
},
}));
export const EditProjectView = () => {
const history = useHistory();
const routeMatch = useRouteMatch();
const { projectId, representationId } = useParams<EditProjectViewParams>();
const classes = useEditProjectViewStyles();
const [{ value, context }, dispatch] = useMachine<EditProjectViewContext, EditProjectViewEvent>(
editProjectViewMachine
);
const { toast, editProjectView } = value as SchemaValue;
const { project, representation, message } = context;
const { loading, data, error } = useQuery<GQLGetProjectQueryData, GQLGetProjectQueryVariables>(getProjectQuery, {
variables: {
projectId,
representationId: representationId ?? '',
includeRepresentation: !!representationId,
},
});
useEffect(() => {
if (!loading) {
if (error) {
const showToastEvent: ShowToastEvent = {
type: 'SHOW_TOAST',
message: 'An unexpected error has occurred, please refresh the page',
};
dispatch(showToastEvent);
}
if (data) {
const fetchProjectEvent: HandleFetchedProjectEvent = { type: 'HANDLE_FETCHED_PROJECT', data };
dispatch(fetchProjectEvent);
}
}
}, [loading, data, error, dispatch]);
useEffect(() => {
if (representation && representation.id !== representationId) {
const pathname = generatePath(routeMatch.path, { projectId, representationId: representation.id });
history.push({ pathname });
} else if (editProjectView === 'loaded' && representation === null && representationId) {
const pathname = generatePath(routeMatch.path, { projectId, representationId: null });
history.push({ pathname });
}
}, [editProjectView, projectId, routeMatch, history, representation, representationId]);
let main = null;
if (editProjectView === 'loaded' && project) {
const onRepresentationSelected = (representationSelected: Representation) => {
const selectRepresentationEvent: SelectRepresentationEvent = {
type: 'SELECT_REPRESENTATION',
representation: representationSelected,
};
dispatch(selectRepresentationEvent);
};
const treeItemContextMenuContributions = [
<TreeItemContextMenuContribution
canHandle={(treeId: string, item: GQLTreeItem) =>
treeId?.startsWith('explorer://') && item.kind.startsWith('siriusWeb://document')
}
component={DocumentTreeItemContextMenuContribution}
/>,
<TreeItemContextMenuContribution
canHandle={(treeId: string, item: GQLTreeItem) =>
treeId?.startsWith('explorer://') && item.kind.startsWith('siriusComponents://semantic')
}
component={ObjectTreeItemContextMenuContribution}
/>,
<TreeItemContextMenuContribution
canHandle={(treeId: string, item: GQLTreeItem) =>
treeId?.startsWith('explorer://') && item.kind === 'siriusComponents://representation?type=Diagram'
}
component={DiagramTreeItemContextMenuContribution}
/>,
];
main = (
<TreeItemContextMenuContext.Provider value={treeItemContextMenuContributions}>
<Workbench
editingContextId={project.currentEditingContext.id}
initialRepresentationSelected={representation}
onRepresentationSelected={onRepresentationSelected}
mainAreaComponent={OnboardArea}
readOnly={false}>
<WorkbenchViewContribution side="left" title="Explorer" icon={<AccountTreeIcon />} component={ExplorerView} />
<WorkbenchViewContribution side="left" title="Validation" icon={<WarningIcon />} component={ValidationView} />
<WorkbenchViewContribution side="right" title="Details" icon={<MenuIcon />} component={DetailsView} />
<WorkbenchViewContribution
side="right"
title="Representations"
icon={<Filter />}
component={RepresentationsView}
/>
<WorkbenchViewContribution
side="right"
title="Related Elements"
icon={<LinkIcon />}
component={RelatedElementsView}
/>
</Workbench>
</TreeItemContextMenuContext.Provider>
);
} else if (editProjectView === 'missing') {
main = (
<Grid container justifyContent="center" alignItems="center">
<Typography variant="h4" align="center" gutterBottom>
The project does not exist
</Typography>
</Grid>
);
}
let navbar = null;
if (editProjectView === 'missing' || editProjectView === 'loading') {
navbar = <NavigationBar />;
} else if (editProjectView === 'loaded') {
navbar = <EditProjectNavbar project={project} />;
}
return (
<>
<div className={classes.editProjectView}>
{navbar}
{main}
</div>
<Toast
message={message}
open={toast === 'visible'}
onClose={() => dispatch({ type: 'HIDE_TOAST' } as HideToastEvent)}
/>
</>
);
};