-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
488 lines (451 loc) · 13.6 KB
/
index.tsx
File metadata and controls
488 lines (451 loc) · 13.6 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import React, { Key, useCallback, useEffect, useMemo, useState } from 'react'
import { Alert, Button, Flex, Modal, Tooltip, Tree } from 'antd'
import type { GetProps, TreeDataNode } from 'antd'
import './index.css'
import {
PlusCircleOutlined,
DeleteOutlined,
CloseCircleOutlined,
ShrinkOutlined,
FolderOutlined,
LineChartOutlined,
} from '@ant-design/icons'
import { Feature, Geometry, MultiPoint, Point } from 'geojson'
import {
BUOY_FIELD_TYPE,
GROUP_TYPE,
MULTI_ZONE_TYPE,
REFERENCE_POINT_TYPE,
TRACK_TYPE,
ZONE_TYPE,
} from '../../constants'
import { useDocContext } from '../../state/DocContext'
import { useAppSelector, useAppDispatch } from '../../state/hooks'
import { LoadTrackModel } from '../LoadTrackModal'
import {
NewTrackProps,
TrackProps,
PointProps,
GroupProps,
BuoyFieldProps,
} from '../../types'
import { CopyButton } from './CopyButton'
import { PasteButton } from './PasteButton'
import { AddZoneShape } from './AddZoneShape'
import { zoneFeatureFor } from '../../helpers/zoneShapePropsFor'
import { getFeatureIcon } from '../../helpers/getFeatureIcon'
import { noop } from 'lodash'
type DirectoryTreeProps = GetProps<typeof Tree.DirectoryTree>
const { DirectoryTree } = Tree
type FieldDataNode = {
title: string
key: string
children: FieldDataNode[]
}
interface LayerProps {
openGraph: { (): void }
}
const notGroups = (key: Key) => {
return (key as string).indexOf(':') === -1
}
const findChildrenOfType = (
features: Feature[],
dType: string
): FieldDataNode[] => {
const items = features.filter(
(feature) => feature.properties?.dataType === dType
)
return items.map((item) => ({
title: nameFor(item),
key: item.id as string,
children: [],
}))
}
const findChildrenOfGroup = (features: Feature[]): TreeDataNode[] => {
const items = features.filter(
(feature) => feature.properties?.dataType === GROUP_TYPE
)
return items.map((item): TreeDataNode => {
const props = item.properties as GroupProps
const children = features.filter((feature) =>
props.units.includes(feature.id as string)
)
const childFeatures = children.map(child => features.find(f => idFor(f) === child.id)) as Feature[]
return {
title: nameFor(item),
key: item.id as string,
children: childFeatures.map((child: Feature): TreeDataNode => {
return {
title: nameFor(child),
key: groupIdFor(item, child.id as string),
icon: getIcon(child, groupIdFor(item, child.id as string), nameFor(child), noop, undefined),
children: [],
}
}),
}
})
}
const groupIdFor = (parent: Feature, child?: string): string => {
return `${parent.id || 'unknown'}:${child || 'unknown'}`
}
const getIcon = (feature: Feature | undefined,
key:string, title: string,
handleAdd: (e: React.MouseEvent, key: string, title: string) => void, button?: React.ReactNode) => {
// If no feature is provided, this is a parent node - show plus icon
if (!feature) {
return button || <PlusCircleOutlined
style={{ cursor: 'copy' }}
onClick={(e) => handleAdd(e, key, title)}
/>
}
// For leaf nodes, show type-specific icon based on dataType
const dataType = feature.properties?.dataType
const color = feature.properties?.stroke || feature.properties?.color || feature.properties?.['marker-color']
const environment = feature.properties?.env
return getFeatureIcon({ dataType, color, environment }) || <FolderOutlined />
}
const mapFunc = (
features: Feature[],
title: string,
key: string,
dType: string,
handleAdd: (e: React.MouseEvent, key: string, title: string) => void,
button?: React.ReactNode
): TreeDataNode => {
const children = features
? dType === GROUP_TYPE
? findChildrenOfGroup(features).map(child => {
// For group children, we need to find the actual feature they reference
const referencedFeature = features.find(f => idFor(f) === child.key)
// and the child units of this group
return {
...child,
icon: getIcon(referencedFeature, child.key as string, child.title as string, handleAdd, button),
}
})
: findChildrenOfType(features, dType).map(child => {
// Find the corresponding feature for this child
const feature = features.find(f => idFor(f) === child.key)
return {
...child,
icon: getIcon(feature, child.key, child.title, handleAdd, button),
}
})
: []
return {
title: (
<span>
{title}
</span>
),
key,
icon: getIcon(undefined, key, title, handleAdd, button), // Parent node gets plus icon
children,
}
}
const idFor = (feature: Feature): string => {
return `${feature.id || 'unknown'}`
}
const nameFor = (feature: Feature): string => {
return (feature.properties?.name || feature.id) + ' : ' + feature.id
}
// filter out the branches, just leave the leaves
const justLeaves = (id: Key): boolean => {
return !(id as string).startsWith('node-')
}
interface ToolProps {
onClick: () => void
icon: React.ReactNode
title: string
disabled: boolean
}
export const ToolButton: React.FC<ToolProps> = ({
onClick,
icon,
title,
disabled,
}) => {
return (
<Tooltip title={title}>
<Button
size={'middle'}
onClick={onClick}
disabled={disabled}
type='primary'
icon={icon}
/>
</Tooltip>
)
}
const Layers: React.FC<LayerProps> = ({ openGraph }) => {
const { selection, setSelection, setNewFeature } = useDocContext()
const features = useAppSelector((state) => state.fColl.features)
const selectedFeatures = features.filter((feature) =>
selection.includes(feature.id as string)
)
const dispatch = useAppDispatch()
const NODE_TRACKS = 'node-tracks'
const NODE_FIELDS = 'node-fields'
const [model, setModel] = React.useState<TreeDataNode[]>([])
const [message, setMessage] = React.useState<string>('')
const [createTrackDialogVisible, setcreateTrackDialogVisible] =
useState(false)
const [expandedKeys, setExpandedKeys] = useState<string[]>([NODE_TRACKS])
const clearSelection = () => {
setSelection([])
}
const temporalFeatureSelected = useMemo(
() => selectedFeatures.some((feature) => feature.properties?.times),
[selectedFeatures]
)
const onGraphClick = () => {
openGraph()
}
const selectionWithGroups = useMemo(() => {
const fullList = [...selection]
const groups = features.filter(
(feature) => feature.properties?.dataType === GROUP_TYPE
) as unknown as Feature<Geometry, GroupProps>[]
selection.forEach((id: string) => {
// find the groups that include this feature id
const groupsContainingFeature = groups.filter((group) =>
group.properties.units.some((unit: string | number) => unit === id)
)
const groupIds = groupsContainingFeature.map(
(group) => (group.id + ':' + id) as string
)
fullList.push(...groupIds)
})
return fullList
}, [selection, features])
const isExpanded = useMemo(() => expandedKeys.length, [expandedKeys])
const localSetNewFeature = useCallback((feature: Feature) => {
setSelection([])
setNewFeature(feature)
}, [setNewFeature, setSelection])
const addPoint = useCallback(() => {
const point: Feature<Point, PointProps> = {
type: 'Feature',
properties: {
name: '',
dataType: REFERENCE_POINT_TYPE,
'marker-color': '#FF0000',
visible: true,
},
geometry: {
type: 'Point',
coordinates: [],
},
}
localSetNewFeature(point)
}, [localSetNewFeature])
const addZone = useCallback((key: string): void => {
const zone = zoneFeatureFor(key)
localSetNewFeature(zone)
}, [localSetNewFeature])
const addBuoyField = useCallback(() => {
const buoyField: Feature<MultiPoint, BuoyFieldProps> = {
type: 'Feature',
properties: {
name: '',
shortName: '',
dataType: BUOY_FIELD_TYPE,
'marker-color': '#FF0000',
visible: true,
},
geometry: {
type: 'MultiPoint',
coordinates: [],
},
}
localSetNewFeature(buoyField)
}, [localSetNewFeature])
const addGroup = useCallback(() => {
const group: Feature<Geometry, GroupProps> = {
type: 'Feature',
properties: {
name: '',
dataType: GROUP_TYPE,
units: [],
visible: true,
},
geometry: {
type: 'GeometryCollection',
geometries: [],
},
}
localSetNewFeature(group)
}, [localSetNewFeature])
const handleAdd = useCallback(
(e: React.MouseEvent, key: string, title: string) => {
if (key === NODE_TRACKS) {
setcreateTrackDialogVisible(true)
} else if (key === NODE_FIELDS) {
addBuoyField()
} else if (key === 'node-points') {
addPoint()
} else if (key === 'node-zones') {
throw new Error('This method not responsible for adding zones')
} else if (key === 'node-groups') {
addGroup()
} else {
console.error(
'unknown key for create new item ' + key + ' for ' + title
)
}
e.stopPropagation()
}, [addGroup, addBuoyField, addPoint])
useEffect(() => {
const items: TreeDataNode[] = []
items.push(mapFunc(features, 'Tracks', NODE_TRACKS, TRACK_TYPE, handleAdd))
items.push(
mapFunc(features, 'Buoy Fields', NODE_FIELDS, BUOY_FIELD_TYPE, handleAdd)
)
items.push(mapFunc(features, 'Multi Zones', 'node-multi-zones', MULTI_ZONE_TYPE, handleAdd,
<AddZoneShape addZone={addZone} />))
items.push(mapFunc(features, 'Zones', 'node-zones', ZONE_TYPE, handleAdd,
<AddZoneShape addZone={addZone} />))
items.push(
mapFunc(
features,
'Points',
'node-points',
REFERENCE_POINT_TYPE,
handleAdd
)
)
items.push(
mapFunc(features, 'Groups', 'node-groups', GROUP_TYPE, handleAdd)
)
const modelData = items
setModel(modelData)
}, [features, handleAdd, addZone])
const onSelect: DirectoryTreeProps['onSelect'] = (selectedKeys) => {
const newKeysArr = selectedKeys as string[]
const cleaned = newKeysArr.filter(justLeaves).filter(notGroups)
if (JSON.stringify(cleaned) !== JSON.stringify(selection)) {
setSelection(cleaned as string[])
}
}
const onDeleteClick = () => {
dispatch({
type: 'fColl/featuresDeleted',
payload: { ids: selection },
})
setSelection([])
}
const setLoadTrackResults = async (values: NewTrackProps) => {
setcreateTrackDialogVisible(false)
// props in NewTrackProps format to TrackProps format, where they have different type
const newValues = values as unknown as TrackProps
newValues.labelInterval = parseInt(values.labelInterval)
newValues.symbolInterval = parseInt(values.symbolInterval)
const newTrack = {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [],
},
properties: {
...newValues,
dataType: TRACK_TYPE,
times: [],
courses: [],
speeds: [],
},
}
dispatch({
type: 'fColl/featureAdded',
payload: newTrack,
})
}
const handleDialogCancel = () => {
setcreateTrackDialogVisible(false)
}
return (
<>
<Modal
title='Message'
open={message !== ''}
onOk={() => setMessage('')}
onCancel={() => setMessage('')}
>
<Alert type='info' description={message} />
</Modal>
<div
style={{ position: 'sticky', top: 0, zIndex: 1, background: '#fff' }}
>
<Flex
className='toolbar'
gap='small'
justify='end'
wrap
style={{ height: '1em' }}
>
<Button.Group>
<ToolButton
onClick={() => setExpandedKeys([])}
icon={<ShrinkOutlined />}
title='Collapse All'
disabled={!isExpanded}
/>
<ToolButton
onClick={clearSelection}
disabled={selection.length === 0}
icon={<CloseCircleOutlined />}
title={'Clear selection'}
/>
<ToolButton
onClick={onDeleteClick}
disabled={selection.length === 0}
icon={<DeleteOutlined />}
title={
selection.length > 0
? 'Delete selected items'
: 'Select items to enable delete'
}
/>
<CopyButton />
<PasteButton />
<ToolButton
onClick={onGraphClick}
disabled={!temporalFeatureSelected}
icon={<LineChartOutlined />}
title={
temporalFeatureSelected
? 'View graph of selected features'
: 'Select a time-related feature to enable graphs'
}
/>
</Button.Group>
</Flex>
</div>
{model.length > 0 && (
<DirectoryTree
showLine={true}
style={{ textAlign: 'left', height: '100%' }}
defaultSelectedKeys={[]}
multiple={true}
onSelect={onSelect}
showIcon={true}
selectedKeys={selectionWithGroups || []}
expandedKeys={expandedKeys}
onExpand={(keys) => {
setExpandedKeys(keys as string[])
}}
treeData={model}
/>
)}
{createTrackDialogVisible && (
<LoadTrackModel
visible={createTrackDialogVisible}
cancel={handleDialogCancel}
newTrack={setLoadTrackResults}
addToTrack={() => {}}
createTrackOnly={true}
/>
)}
</>
)
}
export default Layers