/
index.tsx
184 lines (164 loc) · 3.95 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
/** @module Components.Project.Simulation.Materials.List */
import { useCallback, useContext, useMemo, useRef } from 'react'
import { Card, Typography } from 'antd'
import { WarningOutlined } from '@ant-design/icons'
import { IModelMaterialsValue } from '@/models/index.d'
import {
IFrontSimulationsItem,
IFrontMutateSimulationsItem,
IFrontGeometriesItem
} from '@/api/index.d'
import { SelectContext } from '@/context/select'
import {
enable,
disable,
select,
setPart,
setType
} from '@/context/select/actions'
import { EditButton } from '@/components/assets/button'
import Delete from '../delete'
import globalStyle from '@/styles/index.module.css'
import style from '../../index.module.css'
/**
* Props
*/
export type Geometry = Pick<IFrontGeometriesItem, 'id' | 'summary'>
export type Simulation = Pick<IFrontSimulationsItem, 'id' | 'scheme'>
export type Swr = {
mutateOneSimulation: (
simulation: IFrontMutateSimulationsItem
) => Promise<void>
}
export interface Props {
geometries: Geometry[]
simulation: Simulation
swr: Swr
onEdit: (index: number) => void
}
export interface ListItemProps {
geometries: Geometry[]
simulation: Pick<IFrontSimulationsItem, 'id' | 'scheme'>
material: IModelMaterialsValue
index: number
swr: Swr
_onEdit: (index: number) => void
}
/**
* List item
* @param props Props
* @returns ListItem
*/
const ListItem: React.FunctionComponent<ListItemProps> = ({
geometries,
simulation,
material,
index,
swr,
_onEdit
}) => {
// Ref
const disableDispatch = useRef<boolean>(false)
// Context
const { dispatch } = useContext(SelectContext)
// Geometry
const geometry = useMemo(
() => geometries.find((geometry) => geometry.id === material.geometry),
[geometries, material]
)
// Selected
const selected = useMemo(() => material.selected, [material.selected])
// Type
const type = useMemo(
() => (geometry?.summary.dimension === 2 ? 'faces' : 'solids'),
[geometry]
)
/**
* Highlight
*/
const highlight = useCallback((): void => {
// Distpatch
dispatch(enable())
dispatch(setType(type))
dispatch(setPart(geometry?.summary.uuid))
dispatch(select(selected))
}, [geometry, selected, type, dispatch])
/**
* Unhighlight
*/
const unhighlight = useCallback((): void => {
if (disableDispatch.current) return
dispatch(disable())
}, [dispatch])
/**
* On edit
*/
const onEdit = useCallback((): void => {
disableDispatch.current = true
_onEdit(index)
setTimeout(() => (disableDispatch.current = false), 1_000)
}, [index, _onEdit])
/**
* Render
*/
return (
<Card
className={`${globalStyle.textAlignCenter} ${style.listItem}`}
hoverable
onMouseMove={highlight}
onMouseLeave={unhighlight}
actions={[
<EditButton key="edit" onEdit={onEdit} />,
<Delete key="delete" index={index} simulation={simulation} swr={swr} />
]}
>
{geometry ? null : (
<>
<Typography.Text type="danger" strong>
<WarningOutlined /> Geometry not found
</Typography.Text>
<br />
</>
)}
<Typography.Text strong className={globalStyle.textAlignCenter}>
{material.material.label}
</Typography.Text>
</Card>
)
}
/**
* List materials
* @param props Props
* @returns List
*/
const List: React.FunctionComponent<Props> = ({
geometries,
simulation,
swr,
onEdit
}) => {
// Materials
const materials = useMemo(
() => simulation.scheme.configuration.materials!,
[simulation]
)
/**
* Render
*/
return materials.values
?.map((material: IModelMaterialsValue, index: number) => {
return (
<ListItem
key={material.uuid}
geometries={geometries}
simulation={simulation}
material={material}
index={index}
swr={swr}
_onEdit={onEdit}
/>
)
})
.filter((l: any) => l)
}
export default List