forked from xivanalysis/xivanalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotationTable.tsx
267 lines (244 loc) · 7.23 KB
/
RotationTable.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
import {Trans} from '@lingui/react'
import Rotation, {RotationEvent} from 'components/ui/Rotation'
import React from 'react'
import {Button, Table} from 'semantic-ui-react'
import {isDefined, formatDuration} from 'utilities'
export interface RotationTarget {
/**
* Displayed header
*/
header: React.ReactNode
/**
* Accessor can either be a string, in which case this will resolve to the value assigned to the same key in the `targetsData` field in each entry,
* or a function resolving the entry to the `RotationTargetData`.
*/
accessor: string | ((entry: RotationTableEntry) => RotationTargetData)
}
export interface RotationNotes {
/**
* Displayed header
*/
header: React.ReactNode
/**
* Accessor can either be a string, in which case this will resolve to the value assigned to the same key in the `targetsData` field in each entry,
* or a function resolving the entry to the `RotationTargetData`.
*/
accessor: string | ((entry: RotationTableEntry) => React.ReactNode)
}
/**
* Determines how a rotation target gets highlighted (negative = red, positive = green)
*/
export enum RotationTargetOutcome { NEGATIVE, NEUTRAL, POSITIVE }
export interface RotationTargetData {
/**
* Expected target number
*/
expected?: number
/**
* Recorded number
*/
actual: number
/**
* Optional function to override the default positive/negative highlighting
*/
targetComparator?: (actual: number, expected?: number) => RotationTargetOutcome
}
export interface RotationTableTargetData {
/**
* Identifier to Target Data mapping
*/
[id: string]: RotationTargetData
}
export interface RotationTableNotesMap {
/**
* Identifier to Notes mapping
*/
[id: string]: React.ReactNode
}
export interface RotationTableEntry {
/**
* Start point relative to fight start
*/
start: number
/**
* End point relative to fight start
*/
end: number
/**
* Map of pre calculated target data
*/
targetsData?: RotationTableTargetData
/**
* Map of pre calculated target data
*/
notesMap?: RotationTableNotesMap
/**
* Rotation to display that occurs during this entry
*/
rotation: RotationEvent[]
}
interface RotationTableProps {
/**
* List of Targets to display, consisting of the displayed header and the accessor to resolve the actual and expected values
*/
targets?: RotationTarget[]
/**
* List of Notes to display, consisting of the displayed header and the accessor to resolve the value
*/
notes?: RotationNotes[]
/**
* List of table entries, consisting of a time frame and the rotation, with optionally a pre calculated target data
*/
data: RotationTableEntry[]
/**
* Optional Callback to display the jump to time button.
* Usually this should be a pass through of the `Timeline.show` function.
* @param start
* @param end
* @param scrollTo
*/
onGoto?: (start: number, end: number, scrollTo?: boolean) => void
/**
* Optional property to provide a JSX.Element (translation tag) for the header value.
* Defaults to "Rotation"
*/
headerTitle?: JSX.Element
}
interface RotationTableRowProps {
/**
* List of Targets to display, consisting of the displayed header and the accessor to resolve the actual and expected values
*/
targets: RotationTarget[]
/**
* List of Notes to display, consisting of the displayed header and the accessor to resolve the value
*/
notes: RotationNotes[]
/**
* Optional Callback to display the jump to time button.
* Usually this should be a pass through of the `Timeline.show` function.
* @param start
* @param end
* @param scrollTo
*/
onGoto?: (start: number, end: number, scrollTo?: boolean) => void
}
export class RotationTable extends React.Component<RotationTableProps> {
static defaultTargetComparator(actual: number, expected?: number): RotationTargetOutcome {
if (!isDefined(expected)) {
return RotationTargetOutcome.NEUTRAL
}
if (actual >= expected) {
return RotationTargetOutcome.POSITIVE
}
return RotationTargetOutcome.NEGATIVE
}
static targetAccessorResolver = (entry: RotationTableEntry, target: RotationTarget): RotationTargetData => {
if (typeof target.accessor === 'string' && entry.targetsData != null) {
return entry.targetsData[target.accessor]
}
if (typeof target.accessor === 'function') {
return target.accessor(entry)
}
return {
actual: 0,
expected: 0,
}
}
static notesAccessorResolver = (entry: RotationTableEntry, note: RotationNotes): React.ReactNode => {
if (typeof note.accessor === 'string' && entry.notesMap != null) {
return entry.notesMap[note.accessor]
}
if (typeof note.accessor === 'function') {
return note.accessor(entry)
}
return null
}
static TargetCell = ({actual, expected, targetComparator}: RotationTargetData) => {
if (targetComparator === undefined) {
targetComparator = RotationTable.defaultTargetComparator
}
const targetOutcome = targetComparator(actual, expected)
return <Table.Cell
textAlign="center"
positive={targetOutcome === RotationTargetOutcome.POSITIVE}
negative={targetOutcome === RotationTargetOutcome.NEGATIVE}
>
{actual}/{expected === undefined ? '-' : expected}
</Table.Cell>
}
static Row = ({onGoto, targets, notes, notesMap, start, end, targetsData, rotation}: RotationTableRowProps & RotationTableEntry) =>
<Table.Row>
<Table.Cell textAlign="center">
<span style={{marginRight: 5}}>{formatDuration(start, {secondPrecision: 0})}</span>
{typeof onGoto === 'function' && <Button
circular
compact
size="mini"
icon="time"
onClick={() => onGoto(start, end)}
/>}
</Table.Cell>
{
targets
.map(target => RotationTable.targetAccessorResolver({start, end, targetsData, rotation}, target))
.map((targetEntry, i) => <RotationTable.TargetCell key={`target_${i}`} {...targetEntry}/>)
}
<Table.Cell>
<Rotation events={rotation}/>
</Table.Cell>
{
notes
.map(note => RotationTable.notesAccessorResolver({start, end, targetsData, notesMap, rotation}, note))
.map((noteEntry, i) =>
<Table.Cell
key={`notes_${i}`}
textAlign="center"
>
{noteEntry}
</Table.Cell>,
)
}
</Table.Row>
override render(): React.ReactNode {
const {
targets,
notes,
data,
onGoto,
headerTitle,
} = this.props
return <Table compact unstackable celled>
<Table.Header>
<Table.Row>
<Table.HeaderCell collapsing>
<strong><Trans id="core.ui.rotation-table.header.time">Time</Trans></strong>
</Table.HeaderCell>
{
(targets || []).map((target, i) =>
<Table.HeaderCell key={`target_header_${i}`} textAlign="center" collapsing>
<strong>{target.header}</strong>
</Table.HeaderCell>,
)
}
<Table.HeaderCell>
<strong>{(headerTitle)? headerTitle : <Trans id="core.ui.rotation-table.header.rotation">Rotation</Trans>}</strong>
</Table.HeaderCell>
{
(notes || []).map((note, i) =>
<Table.HeaderCell key={`note_header_${i}`} textAlign="center" collapsing>
<strong>{note.header}</strong>
</Table.HeaderCell>,
)
}
</Table.Row>
</Table.Header>
<Table.Body>
{
data.map((entry) =>
<RotationTable.Row key={entry.start} onGoto={onGoto} targets={targets || []} notes={notes || []} {...entry}/>,
)
}
</Table.Body>
</Table>
}
}