-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathViewControls.tsx
More file actions
349 lines (317 loc) · 9.77 KB
/
ViewControls.tsx
File metadata and controls
349 lines (317 loc) · 9.77 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
import {
IconBoundingBox,
IconClockTimeFourOutline,
IconCursor,
IconList,
IconOutlinerEyeClosed,
IconOutlinerEyeOpened,
IconPredictions,
IconSortDown,
IconSortUp,
IconTimelineRegion,
} from "@humansignal/icons";
import { Button } from "@humansignal/ui";
import { type FC, useCallback, useContext, useEffect, useMemo } from "react";
import { Dropdown } from "@humansignal/ui";
// eslint-disable-next-line
// @ts-ignore
import { Menu } from "../../../common/Menu/Menu";
// local merge icon
import MergeIcon from "./MergeIcon";
import { cn } from "../../../utils/bem";
import { SidePanelsContext } from "../SidePanelsContext";
import "./ViewControls.prefix.css";
import { observer } from "mobx-react";
export type GroupingOptions = "manual" | "label" | "type";
export type OrderingOptions = "score" | "date" | "mediaStartTime";
export type OrderingDirection = "asc" | "desc";
interface ViewControlsProps {
ordering: OrderingOptions;
orderingDirection?: OrderingDirection;
regions: any;
onOrderingChange: (ordering: OrderingOptions) => void;
onGroupingChange: (grouping: GroupingOptions) => void;
}
const mediaStartTimeSupportedTags = [
["labels", "audio"],
["labels", "videorectangle", "video"],
["timelinelabels", "video"],
["timeserieslabels", "timeseries"],
];
export const ViewControls: FC<ViewControlsProps> = observer(
({ ordering, regions, orderingDirection, onOrderingChange, onGroupingChange }) => {
const grouping = regions.group;
const context = useContext(SidePanelsContext);
// Check labeling configuration for media-time-capable object tags
const mediaTimeSupport: boolean | null = useMemo(() => {
const names = regions.annotation?.names;
if (!names || names.size === 0) return null;
const tags = Array.from(names.values());
// Check if all tag types from the tuple exist in the configuration
return mediaStartTimeSupportedTags.some((requiredTagTypes) => {
return requiredTagTypes.every((requiredType) => tags.some((tag: any) => tag?.type === requiredType));
});
}, [regions.annotation?.names]);
// Auto-fallback to "date" if current ordering is "mediaStartTime" but no media-time support in config
useEffect(() => {
if (ordering === "mediaStartTime" && mediaTimeSupport === false) {
onOrderingChange("date");
}
}, [ordering, mediaTimeSupport, onOrderingChange]);
const getGroupingLabels = useCallback((value: GroupingOptions): LabelInfo => {
switch (value) {
case "manual":
return {
label: (
<>
<IconList /> Group Manually
</>
),
selectedLabel: "Manual",
icon: <IconList width={16} height={16} />,
tooltip: "Manually Grouped",
};
case "label":
return {
label: (
<>
<IconBoundingBox /> Group by Label
</>
),
selectedLabel: "By Label",
icon: <IconBoundingBox width={16} height={16} />,
tooltip: "Grouped by Label",
};
case "type":
return {
label: (
<>
<IconCursor /> Group by Tool
</>
),
selectedLabel: "By Tool",
icon: <IconCursor width={16} height={16} />,
tooltip: "Grouped by Tool",
};
}
}, []);
const getOrderingLabels = useCallback((value: OrderingOptions): LabelInfo => {
switch (value) {
case "date":
return {
label: (
<>
<IconClockTimeFourOutline /> Order by Time
</>
),
selectedLabel: "By Time",
icon: <IconClockTimeFourOutline width={16} height={16} />,
};
case "score":
return {
label: (
<>
<IconPredictions /> Order by Score
</>
),
selectedLabel: "By Score",
icon: <IconPredictions width={16} height={16} />,
};
case "mediaStartTime":
return {
label: (
<>
<IconTimelineRegion /> Order by Media Start Time
</>
),
selectedLabel: "By Media Start Time",
icon: <IconTimelineRegion width={16} height={16} />,
};
}
}, []);
const renderOrderingDirectionIcon = orderingDirection === "asc" ? <IconSortUp /> : <IconSortDown />;
return (
<div className={cn("view-controls").mod({ collapsed: context.locked }).toClassName()}>
<Grouping
value={grouping}
options={["manual", "type", "label"]}
onChange={(value) => onGroupingChange(value)}
readableValueForKey={getGroupingLabels}
/>
{grouping === "manual" && (
<div className={cn("view-controls").elem("sort").toClassName()}>
<Grouping
value={ordering}
direction={orderingDirection}
options={mediaTimeSupport ? ["score", "date", "mediaStartTime"] : ["score", "date"]}
onChange={(value) => onOrderingChange(value)}
readableValueForKey={getOrderingLabels}
allowClickSelected
extraIcon={renderOrderingDirectionIcon}
width={230}
/>
</div>
)}
<MergeRegionsButton regions={regions} />
<ToggleRegionsVisibilityButton regions={regions} />
</div>
);
},
);
interface LabelInfo {
label: string | React.ReactNode | JSX.Element;
selectedLabel: string;
icon: JSX.Element;
tooltip?: string;
}
interface GroupingProps<T extends string> {
value: T;
options: T[];
direction?: OrderingDirection;
allowClickSelected?: boolean;
onChange: (value: T) => void;
readableValueForKey: (value: T) => LabelInfo;
extraIcon?: JSX.Element;
width?: number;
}
const Grouping = <T extends string>({
value,
options,
direction,
allowClickSelected,
onChange,
readableValueForKey,
extraIcon,
width = 200,
}: GroupingProps<T>) => {
const readableValue = useMemo(() => {
return readableValueForKey(value);
}, [value]);
const optionsList: [T, LabelInfo][] = useMemo(() => {
return options.map((key) => [key, readableValueForKey(key)]);
}, [options, readableValueForKey]);
const dropdownContent = useMemo(() => {
return (
<Menu
size="medium"
style={{
width,
minWidth: width,
borderRadius: 4,
}}
selectedKeys={[value]}
allowClickSelected={allowClickSelected}
>
{optionsList.map(([key, label]) => (
<GroupingMenuItem
key={key}
name={key}
value={value}
direction={direction}
label={label}
onChange={(value) => onChange(value)}
/>
))}
</Menu>
);
}, [value, optionsList, readableValue, direction, onChange]);
return (
<Dropdown.Trigger content={dropdownContent} style={{ width }}>
<Button
variant="neutral"
size="smaller"
data-testid={`grouping-${value}`}
look="string"
leading={readableValue.icon}
trailing={extraIcon}
>
{readableValue.selectedLabel}
</Button>
</Dropdown.Trigger>
);
};
interface GroupingMenuItemProps<T extends string> {
name: T;
label: LabelInfo;
value: T;
direction?: OrderingDirection;
onChange: (key: T) => void;
}
const GroupingMenuItem = <T extends string>({ value, name, label, direction, onChange }: GroupingMenuItemProps<T>) => {
return (
<Menu.Item name={name} onClick={() => onChange(name)}>
<div className={cn("view-controls").elem("label").toClassName()}>
{label.label}
<DirectionIndicator direction={direction} name={name} value={value} />
</div>
</Menu.Item>
);
};
interface DirectionIndicator {
direction?: OrderingDirection;
value: string;
name: string;
wrap?: boolean;
}
const DirectionIndicator: FC<DirectionIndicator> = ({ direction, value, name, wrap = true }) => {
const content = direction === "asc" ? <IconSortUp /> : <IconSortDown />;
if (!direction || value !== name) return null;
if (!wrap) return content;
return <span>{content}</span>;
};
interface ToggleRegionsVisibilityButton {
regions: any;
}
const ToggleRegionsVisibilityButton = observer<FC<ToggleRegionsVisibilityButton>>(({ regions }) => {
const toggleRegionsVisibility = useCallback(
(e) => {
e.preventDefault();
e.stopPropagation();
regions.toggleVisibility();
},
[regions],
);
const isDisabled = !regions?.regions?.length;
const isAllHidden = !isDisabled && regions.isAllHidden;
return (
<Button
variant="neutral"
size="smaller"
look="string"
disabled={isDisabled}
onClick={toggleRegionsVisibility}
aria-label={isAllHidden ? "Show all regions" : "Hide all regions"}
tooltip={isAllHidden ? "Show all regions" : "Hide all regions"}
>
{isAllHidden ? (
<IconOutlinerEyeClosed width={16} height={16} />
) : (
<IconOutlinerEyeOpened width={16} height={16} />
)}
</Button>
);
});
const MergeRegionsButton = observer<FC<ToggleRegionsVisibilityButton>>(({ regions }) => {
const merge = useCallback(
(e) => {
e.preventDefault();
e.stopPropagation();
regions.annotation.mergeSelectedRegions?.();
},
[regions],
);
const isDisabled = !regions?.selection || regions.selection.size < 2;
return (
<Button
variant="neutral"
size="smaller"
look="string"
disabled={isDisabled}
onClick={merge}
aria-label={"Merge selected regions"}
tooltip={"Merge selected regions"}
>
<MergeIcon width={16} height={16} />
</Button>
);
});