/
index.tsx
408 lines (363 loc) · 9.34 KB
/
index.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/** @module Components.Project.View */
import { useCallback, useContext, useEffect, useState } from 'react'
import dynamic from 'next/dynamic'
import { Spin } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'
import {
Tanatloc3DPart,
Tanatloc3DSelectionPoint,
Tanatloc3DSelectionValue
} from '@airthium/tanatloc-3d'
import { v4 } from 'uuid'
import {
IFrontGeometriesItem,
IFrontProject,
IFrontSimulationsItem,
IFrontResult,
IFrontSimulation
} from '@/api/index.d'
import useCustomEffect from '@/components/utils/useCustomEffect'
import { NotificationContext } from '@/context/notification'
import { addError } from '@/context/notification/actions'
import { SelectContext } from '@/context/select'
import {
highlight,
select,
setData,
setPoint,
setPostProcessing
} from '@/context/select/actions'
import { IGeometryPart } from '@/lib/index.d'
import SimulationAPI from '@/api/simulation'
import AvatarAPI from '@/api/avatar'
import GeometryAPI from '@/api/geometry'
import ResultAPI from '@/api/result'
import style from './index.module.css'
import theme from '@/styles/theme'
// Tanatloc3D Renderer
const Renderer = dynamic(
() => import('@airthium/tanatloc-3d').then((mod) => mod.default.Renderer),
{ ssr: false }
)
/**
* Props
*/
export type Project = Pick<IFrontProject, 'id' | 'title'>
export type Simulation = Pick<IFrontSimulationsItem, 'id' | 'scheme'>
export type Geometry = Pick<IFrontGeometriesItem, 'id' | 'needCleanup'> & {
visible?: boolean
}
export type Result = Pick<IFrontResult, 'name' | 'glb' | 'originPath' | 'extra'>
export interface IProps {
project: Project
simulation?: Simulation
geometries: Geometry[]
results: Result[]
postprocessing?: Result
}
/**
* Errors
*/
export const errors = {
part: 'Unable to load part',
snapshot: 'Unable to save snapshot'
}
/**
* Load part
* @param simulation Simulation
* @param file File
* @param type Type
* @returns Part
*/
export const _loadPart = async (
simulation: Pick<IFrontSimulationsItem, 'id'> | undefined,
file: Pick<IFrontGeometriesItem, 'id'> | Result,
type: 'geometry' | 'result'
): Promise<IGeometryPart> => {
if (type === 'geometry') {
const geometry = file as Pick<IFrontGeometriesItem, 'id'>
const part = await GeometryAPI.getPart({ id: geometry.id })
return { ...part, extra: { id: geometry.id } }
} else {
const result = file as Pick<
IFrontResult,
'name' | 'glb' | 'originPath' | 'extra'
>
const part = await ResultAPI.load(
{ id: simulation!.id },
{ originPath: result.originPath, glb: result.glb! }
)
return {
...part,
extra: {
name: result.name,
glb: result.glb,
fields: result.extra
}
}
}
}
/**
* Load result
* @param simulation Simulation
* @param parts Parts
* @param results Results
* @returns Result
*/
export const _loadResults = async (
simulation: Pick<IFrontSimulationsItem, 'id'>,
parts: IGeometryPart[],
results: Result[]
): Promise<IGeometryPart[]> => {
const partsContent = []
for (const result of results) {
const prevPart = parts.find((part) => part.extra?.glb === result.glb)
if (prevPart) {
partsContent.push(prevPart)
continue
}
const partContent = await _loadPart(simulation, result, 'result')
partsContent.push(partContent)
}
return partsContent
}
/**
* Load postprocessing
* @param simulation Simulation
* @param parts Parts
* @param postprocessing Postprocessing
* @returns Postprocessing
*/
export const _loadPostprocessing = async (
simulation: Pick<IFrontSimulationsItem, 'id'>,
parts: IGeometryPart[],
postprocessing?: Result
): Promise<IGeometryPart | undefined> => {
if (!postprocessing) return
const prevPart = parts.find((part) => part.extra?.glb === postprocessing.glb)
if (prevPart) return prevPart
const partContent = await _loadPart(simulation, postprocessing, 'result')
return partContent
}
/**
* Load geometries
* @param simulation Simulation
* @param parts Parts
* @param geometries Geometries
* @returns Geometries
*/
export const _loadGeometries = async (
simulation: Pick<IFrontSimulationsItem, 'id'> | undefined,
parts: IGeometryPart[],
geometries: Geometry[]
): Promise<IGeometryPart[]> => {
const partsContent = []
for (const geometry of geometries) {
const prevPart = parts.find((part) => part.extra?.id === geometry.id)
if (prevPart) {
partsContent.push(prevPart)
continue
}
const partContent = await _loadPart(simulation, geometry, 'geometry')
partsContent.push(partContent)
}
return partsContent
}
/**
* View
* @param props Props
* @returns View
*/
const View: React.FunctionComponent<IProps> = ({
project,
simulation,
geometries,
results,
postprocessing
}) => {
// State
const [parts, setParts] = useState<IGeometryPart[]>([])
const [loading, setLoading] = useState<boolean>(false)
const [dataEnabled, setDataEnabled] = useState<boolean>(false)
const [postProcessingEnabled, setPostProcessingEnabled] =
useState<boolean>(false)
// Context
const { enabled, part, highlighted, selected, point, type, dispatch } =
useContext(SelectContext)
const { dispatch: notificationDispatch } = useContext(NotificationContext)
// Data
const [currentSimulation] = SimulationAPI.useSimulation(simulation?.id)
// Parts
useCustomEffect(() => {
const load = async () => {
setLoading(true)
const newParts = []
// Results
if (simulation && !postprocessing) {
const newResults = await _loadResults(simulation, parts, results)
newParts.push(...newResults)
}
// Postprocessing
if (simulation) {
const newPostprocessing = await _loadPostprocessing(
simulation,
parts,
postprocessing
)
newPostprocessing && newParts.push(newPostprocessing)
}
// Geometries
if (!results.length && !postprocessing) {
const newGeometries = await _loadGeometries(
simulation,
parts,
geometries
)
newParts.push(...newGeometries)
}
setParts(newParts)
}
load()
.catch((err): any => {
notificationDispatch(addError({ title: errors.part, err }))
setLoading(false)
})
.finally(() => setLoading(false))
}, [
simulation,
geometries,
results,
postprocessing,
parts,
notificationDispatch
])
// Data enabled
useEffect(() => {
if (currentSimulation.id === '0') {
setDataEnabled(false)
return
}
const validSimulation = currentSimulation as IFrontSimulation
const tasks = validSimulation.tasks
const datas = tasks
?.map((task) => task.datas)
.filter((d) => d)
.flat()
if (datas?.length) setDataEnabled(true)
else setDataEnabled(false)
}, [currentSimulation])
// Postprocessing enabled
useEffect(() => {
if (simulation?.scheme.configuration.run.postprocessing) {
let oneResult = false
parts.forEach((part) => {
if (part.summary.type === 'result') oneResult = true
})
if (oneResult) setPostProcessingEnabled(true)
else setPostProcessingEnabled(false)
} else setPostProcessingEnabled(false)
}, [simulation, parts])
/**
* Snapshot
* @param image Image
*/
const snapshot = useCallback(
async (image: string): Promise<void> => {
try {
await AvatarAPI.add(
{
name: 'snapshot',
uid: 'snapshot_' + v4(),
data: image
},
{ id: project.id }
)
} catch (err: any) {
notificationDispatch(addError({ title: errors.snapshot, err }))
}
},
[project.id, notificationDispatch]
)
/**
* On highlight
* @param value Value
*/
const onHighlight = useCallback(
(value?: Tanatloc3DSelectionValue) => {
dispatch(highlight(value))
},
[dispatch]
)
/**
* On select
* @param value Value
*/
const onSelect = useCallback(
(value: Tanatloc3DSelectionValue[]) => {
dispatch(select(value))
},
[dispatch]
)
/**
* On point
* @param value Value
*/
const onPoint = useCallback(
(value: Tanatloc3DSelectionPoint) => {
dispatch(setPoint(value))
},
[dispatch]
)
/**
* On data
*/
const onData = useCallback(() => {
dispatch(setData(true))
}, [dispatch])
/**
* On postprocessing
*/
const onPostProcessing = useCallback(() => {
dispatch(setPostProcessing(true))
}, [dispatch])
/**
* Render
*/
return (
<>
<div
style={{ display: loading ? 'flex' : 'none' }}
className={style.loading}
>
<Spin indicator={<LoadingOutlined style={{ fontSize: 80 }} spin />} />
</div>
<Renderer
theme={theme}
style={{ width: 'calc(100vw - 256px)' }}
parts={parts as Tanatloc3DPart[]}
selection={{
enabled,
part,
type,
highlighted,
selected,
point,
onHighlight,
onSelect,
onPoint
}}
data={dataEnabled}
postProcessing={postProcessingEnabled}
snapshot={{
project: {
apiRoute: snapshot,
size: { width: 2 * 260, height: 2 * 156 }
}
}}
onData={onData}
onPostProcessing={onPostProcessing}
/>
</>
)
}
export default View