-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
132 lines (114 loc) · 4.17 KB
/
Copy pathApp.tsx
File metadata and controls
132 lines (114 loc) · 4.17 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { Config, Id64, Id64String } from "@bentley/bentleyjs-core";
import "@bentley/icons-generic-webfont/dist/bentley-icons-generic-webfont.css";
import { DrawingViewState, IModelConnection, SnapshotConnection, SpatialViewState } from "@bentley/imodeljs-frontend";
import { ViewportComponent } from "@bentley/ui-components";
import * as React from "react";
import "./App.css";
import Toolbar from "./Toolbar";
// cSpell:ignore imodels
/** React state of the App component */
export interface AppState {
user: {
isAuthorized: boolean;
isLoading: boolean;
};
imodel?: IModelConnection;
viewDefinitionId?: Id64String;
}
/** A component the renders the whole application UI */
export default class App extends React.Component<{}, AppState> {
/** Creates an App instance */
constructor(props?: any, context?: any) {
super(props, context);
this.state = {
user: {
isAuthorized: true,
isLoading: false,
}
};
this.loadSnapshot();
}
/** Pick the first available spatial view definition in the imodel */
private async getFirstViewDefinitionId(imodel: IModelConnection): Promise<Id64String> {
// Return default view definition (if any)
const defaultViewId = await imodel.views.queryDefaultViewId();
if (Id64.isValid(defaultViewId))
return defaultViewId;
// Return first spatial view definition (if any)
const spatialViews: IModelConnection.ViewSpec[] = await imodel.views.getViewList({ from: SpatialViewState.classFullName });
if (spatialViews.length > 0)
return spatialViews[0].id;
// Return first drawing view definition (if any)
const drawingViews: IModelConnection.ViewSpec[] = await imodel.views.getViewList({ from: DrawingViewState.classFullName });
if (drawingViews.length > 0)
return drawingViews[0].id;
throw new Error("No valid view definitions in imodel");
}
/** Handle iModel open event */
private onIModelSelected = async (imodel: IModelConnection | undefined) => {
if (!imodel) {
// reset the state when imodel is closed
this.setState({ imodel: undefined, viewDefinitionId: undefined });
return;
}
try {
// attempt to get a view definition
const viewDefinitionId = await this.getFirstViewDefinitionId(imodel);
this.setState({ imodel, viewDefinitionId });
} catch (e) {
// if failed, close the imodel and reset the state
await imodel.close();
this.setState({ imodel: undefined, viewDefinitionId: undefined });
alert(e.message);
}
};
private async loadSnapshot() {
//!!OA this.setState({ user.isLoading: true });
let imodel: IModelConnection | undefined;
const imodelName = Config.App.get("imjs_snapshot");
try {
// attempt to open the snapshot imodel
imodel = await SnapshotConnection.openFile(imodelName);
} catch (e) {
alert(e.message);
}
await this.onIModelSelected(imodel);
};
/** The component's render method */
public render() {
let ui: React.ReactNode;
if (this.state.imodel && this.state.viewDefinitionId) {
// if we do have an imodel and view definition id - render imodel components
ui = (<IModelComponents imodel={this.state.imodel} viewDefinitionId={this.state.viewDefinitionId} />);
}
// render the app
return (
<div className="app">
{ui}
</div>
);
}
}
/** React props for [[IModelComponents]] component */
interface IModelComponentsProps {
imodel: IModelConnection;
viewDefinitionId: Id64String;
}
/** Renders a viewport */
class IModelComponents extends React.PureComponent<IModelComponentsProps> {
public render() {
return (
<>
<ViewportComponent
style={{ height: "100vh" }}
imodel={this.props.imodel}
viewDefinitionId={this.props.viewDefinitionId} />
<Toolbar />
</>
);
}
}