-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseShapeDiverStargate.ts
More file actions
367 lines (354 loc) · 10.7 KB
/
Copy pathuseShapeDiverStargate.ts
File metadata and controls
367 lines (354 loc) · 10.7 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import {
Configuration,
ResCreateSessionByTicket,
SessionApi,
} from "@shapediver/sdk.geometry-api-sdk-v2";
import {
SdPlatformModelGetEmbeddableFields,
SdPlatformSdk,
} from "@shapediver/sdk.platform-api-sdk-v1";
import {
createSdk,
ISdStargateBakeDataCommandDto,
ISdStargateBakeDataReplyDto,
ISdStargateBakeDataResultEnum,
ISdStargateExportFileCommandDto,
ISdStargateExportFileReplyDto,
ISdStargateExportFileResultEnum,
ISdStargateGetDataCommandDto,
ISdStargateGetDataReplyDto,
ISdStargateGetDataResultEnum,
ISdStargateGetSupportedDataReplyDto,
ISdStargatePrepareModelCommandDto,
ISdStargatePrepareModelReplyDto,
ISdStargatePrepareModelResultEnum,
ISdStargateSdk,
ISdStargateStatusReplyDto,
SdStargateBakeDataCommand,
SdStargateExportFileCommand,
SdStargateGetDataCommand,
SdStargateGetSupportedDataCommand,
SdStargatePrepareModelCommand,
SdStargateStatusCommand,
} from "@shapediver/sdk.stargate-sdk-v1";
import {useCallback, useEffect, useRef, useState} from "react";
import packagejson from "../../package.json";
const firstActivity = Math.floor(Date.now() / 1000);
export type SessionData = {
config: Configuration;
session: ResCreateSessionByTicket;
};
type ModelIdSessionMapType = {
[key: string]: Promise<SessionData>;
};
interface Props {
/** The access token to use. */
accessToken: string | undefined;
/** The platform SDK to use. */
platformSdk: SdPlatformSdk;
/**
* Supported data. Use this to specify which ShapeDiver parameter types,
* file endings, and content types are supported by the handlers.
*/
supportedData: Partial<ISdStargateGetSupportedDataReplyDto>;
/**
* Handler for command messages for which no handler is registered.
* Typically there is no need to provide this handler.
* The default handler logs to the browser console.
*/
serverCommandHandler?: (payload: unknown) => void;
/**
* Handler for connection errors, called if an error message has been
* received from the Stargate server.
* The default handler logs to the browser console.
*/
connectionErrorHandler?: (msg: string) => void;
/**
* Handler called when the established connection is closed by the Stargate server
* or other external circumstances.
* The default handler logs to the browser console.
*/
disconnectHandler?: (msg: string) => void;
/**
* Handler for the GET DATA command. The GET DATA command handler
* is called whenever the user requests data for an input (for a parameter)
* from a client application, by clicking on the corresponding UI element.
* @see https://help.shapediver.com/doc/inputs-and-outputs
*
* This extends the default handler by providing session data,
* which can be used to interact with the corresponding
* ShapeDiver model via the Geometry API.
* @param data
* @param sessionData
* @returns
*/
getDataCommandHandler?: (
data: ISdStargateGetDataCommandDto,
sessionData: SessionData,
) => Promise<ISdStargateGetDataReplyDto>;
/**
* Handler for the BAKE DATA command. The BAKE DATA command handler
* is called whenever the user requests baking of data from an output
* to a client application, by clicking on the corresponding UI element.
* @see https://help.shapediver.com/doc/shapediver-output#ShapeDiverOutput-clientUsagewithdesktopclients
*
* This extends the default handler by providing session data,
* which can be used to interact with the corresponding
* ShapeDiver model via the Geometry API.
* @param data
* @param sessionData
* @returns
*/
bakeDataCommandHandler?: (
data: ISdStargateBakeDataCommandDto,
sessionData: SessionData,
) => Promise<ISdStargateBakeDataReplyDto>;
/**
* Handler for the EXPORT FILE command. The EXPORT FILE command handler
* is called whenever the user requests the export of a file
* via a client application, by clicking on the corresponding UI element.
*
* This extends the default handler by providing session data,
* which can be used to interact with the corresponding
* ShapeDiver model via the Geometry API.
* @param data
* @param sessionData
* @returns
*/
exportFileCommandHandler?: (
data: ISdStargateExportFileCommandDto,
sessionData: SessionData,
) => Promise<ISdStargateExportFileReplyDto>;
}
/**
* Hook providing a ShapeDiver Stargate Client implementation.
* @returns
*/
export default function useShapeDiverStargate(props: Props): {
/**
* The Stargate SDK. Typically there is no need to register further
* command handlers, as this is already taken care of by this hook.
*/
stargateSdk: ISdStargateSdk | null;
/**
* True if this Stargate client is currently being used by the user
* from a ShapeDiver App. This is based on the STATUS command,
* which is sent by the App every 30 seconds. If no STATUS command
* has been received for 35 seconds, this property will be set to false.
*/
isActive: boolean;
} {
const {
accessToken,
platformSdk,
supportedData,
serverCommandHandler,
connectionErrorHandler,
disconnectHandler,
getDataCommandHandler,
bakeDataCommandHandler,
exportFileCommandHandler,
} = props;
const [stargateSdk, setStargateSdk] = useState<ISdStargateSdk | null>(null);
const [isActive, setIsActive_] = useState(false);
const timeoutRef = useRef<number | undefined>();
const setIsActive = useCallback(() => {
if (typeof timeoutRef.current === "number")
clearTimeout(timeoutRef.current);
setIsActive_(true);
timeoutRef.current = window.setTimeout(
() => setIsActive_(false),
35000,
);
}, []);
const modelIdSessionMapRef = useRef<ModelIdSessionMapType>({});
const getSessionDataForModelId = useCallback(
async (modelId: string) => {
const get = async (modelId: string) => {
const model = (
await platformSdk.models.get(modelId, [
SdPlatformModelGetEmbeddableFields.BackendSystem,
SdPlatformModelGetEmbeddableFields.Ticket,
SdPlatformModelGetEmbeddableFields.TokenExport,
])
).data;
const config = new Configuration({
accessToken: model.access_token,
basePath: model.backend_system!.model_view_url,
});
const session = (
await new SessionApi(config).createSessionByTicket(
model.ticket!.ticket!,
)
).data;
return {config, session};
};
// create a session for the model if none exists yet
if (!modelIdSessionMapRef.current[modelId]) {
modelIdSessionMapRef.current[modelId] = get(modelId);
}
return modelIdSessionMapRef.current[modelId];
},
[platformSdk],
);
useEffect(() => {
const init = async (jwt: string, platformSdk: SdPlatformSdk) => {
// get Stargate endpoint to use
const endpoints = (await platformSdk.stargate.getConfig())?.data
.endpoint;
const endpoint = endpoints
? endpoints[Object.keys(endpoints)[0]]
: "prod-sg.eu-central-1.shapediver.com";
// create and configure the SDK
const sdk = await createSdk()
.setBaseUrl(endpoint)
.setServerCommandHandler(
serverCommandHandler ??
((payload: unknown) => {
console.log("Received Stargate command:", payload);
}),
)
.setConnectionErrorHandler(
connectionErrorHandler ??
((msg: string) =>
console.error(`Stargate connection error: ${msg}`)),
)
.setDisconnectHandler(
disconnectHandler ??
((msg: string) =>
console.error(`Stargate disconnected: ${msg}`)),
)
.build();
// register the client
await sdk.register(
jwt,
"Stargate Web Client",
packagejson.version,
navigator.platform || "",
window.location.hostname,
"",
);
// register a handler for the status command
new SdStargateStatusCommand(sdk).registerHandler(
async (): Promise<ISdStargateStatusReplyDto> => {
setIsActive();
return {
firstActivity,
latestActivity: Math.floor(Date.now() / 1000),
};
},
);
// register a handler for the get supported data command
new SdStargateGetSupportedDataCommand(sdk).registerHandler(
async (): Promise<ISdStargateGetSupportedDataReplyDto> => ({
parameterTypes: [],
typeHints: [],
contentTypes: [],
fileExtensions: [],
...supportedData,
}),
);
// register a handler for the prepare model command
new SdStargatePrepareModelCommand(sdk).registerHandler(
async (
data: ISdStargatePrepareModelCommandDto,
): Promise<ISdStargatePrepareModelReplyDto> => {
await getSessionDataForModelId(data.model.id);
return {
info: {
result: ISdStargatePrepareModelResultEnum.SUCCESS,
},
};
},
);
// register a handler for the get data command
new SdStargateGetDataCommand(sdk).registerHandler(
async (
data: ISdStargateGetDataCommandDto,
): Promise<ISdStargateGetDataReplyDto> => {
const sessionData = await getSessionDataForModelId(
data.model.id,
);
if (getDataCommandHandler) {
return getDataCommandHandler(data, sessionData);
}
console.warn(
"Received get data command, but no handler is registered.",
data,
);
return {
info: {
message: "No handler registered.",
result: ISdStargateGetDataResultEnum.NOTHING,
count: 0,
},
};
},
);
// register a handler for the bake data command
new SdStargateBakeDataCommand(sdk).registerHandler(
async (
data: ISdStargateBakeDataCommandDto,
): Promise<ISdStargateBakeDataReplyDto> => {
const sessionData = await getSessionDataForModelId(
data.model.id,
);
if (bakeDataCommandHandler) {
return bakeDataCommandHandler(data, sessionData);
}
console.warn(
"Received bake data command, but no handler is registered.",
data,
);
return {
info: {
message: "No handler registered.",
result: ISdStargateBakeDataResultEnum.NOTHING,
count: 0,
},
};
},
);
// register a handler for the export file command
new SdStargateExportFileCommand(sdk).registerHandler(
async (
data: ISdStargateExportFileCommandDto,
): Promise<ISdStargateExportFileReplyDto> => {
const sessionData = await getSessionDataForModelId(
data.model.id,
);
if (exportFileCommandHandler) {
return exportFileCommandHandler(data, sessionData);
}
console.warn(
"Received export file command, but no handler is registered.",
data,
);
return {
info: {
message: "No handler registered.",
result: ISdStargateExportFileResultEnum.NOTHING,
},
};
},
);
// store the SDK in state
setStargateSdk(sdk);
};
if (accessToken && platformSdk) init(accessToken, platformSdk);
}, [
accessToken,
platformSdk,
supportedData,
serverCommandHandler,
connectionErrorHandler,
disconnectHandler,
getDataCommandHandler,
bakeDataCommandHandler,
exportFileCommandHandler,
]);
return {
stargateSdk,
isActive,
};
}