forked from konveyor/tackle2-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration-targets.tsx
More file actions
325 lines (293 loc) · 9.87 KB
/
migration-targets.tsx
File metadata and controls
325 lines (293 loc) · 9.87 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
import React, { useMemo, useRef, useState } from "react";
import {
DndContext,
DragOverEvent,
DragOverlay,
DragStartEvent,
MouseSensor,
TouchSensor,
closestCenter,
useSensor,
useSensors,
} from "@dnd-kit/core";
import {
SortableContext,
arrayMove,
rectSortingStrategy,
} from "@dnd-kit/sortable";
import { AxiosError, AxiosResponse } from "axios";
import { unique } from "radash";
import { useTranslation } from "react-i18next";
import {
Button,
Gallery,
Modal,
PageSection,
PageSectionVariants,
Text,
TextContent,
Toolbar,
ToolbarContent,
ToolbarGroup,
ToolbarItem,
} from "@patternfly/react-core";
import spacing from "@patternfly/react-styles/css/utilities/Spacing/spacing";
import { Target } from "@app/api/models";
import { FilterToolbar, FilterType } from "@app/components/FilterToolbar";
import { NotificationsContext } from "@app/components/NotificationsContext";
import { useLocalTableControls } from "@app/hooks/table-controls";
import { useSetting, useSettingMutation } from "@app/queries/settings";
import { useDeleteTargetMutation, useFetchTargets } from "@app/queries/targets";
import { getAxiosErrorMessage } from "@app/utils/utils";
import { CustomTargetForm } from "./components/custom-target-form";
import { SortableTargetItem } from "./components/dnd/sortable-target-item";
import { TargetItem } from "./components/dnd/target-item";
import { DEFAULT_PROVIDER } from "./useMigrationProviderList";
export const MigrationTargets: React.FC = () => {
const { t } = useTranslation();
const { pushNotification } = React.useContext(NotificationsContext);
const { targetsInOrder } = useFetchTargets();
const targetOrderSetting = useSetting("ui.target.order");
const targetOrderSettingMutation = useSettingMutation("ui.target.order");
// Create and update modal
const [createUpdateModalState, setCreateUpdateModalState] = React.useState<
"create" | Target | null
>(null);
const isCreateUpdateModalOpen = createUpdateModalState !== null;
const targetToUpdate =
createUpdateModalState !== "create" ? createUpdateModalState : null;
const targetsEndRef = useRef<null | HTMLDivElement>(null);
const scrollToBottom = () => {
targetsEndRef.current?.scrollIntoView({ behavior: "smooth" });
};
const [activeTarget, setActiveTarget] = useState<Target | null>(null);
const onDeleteTargetSuccess = (target: Target, id: number) => {
pushNotification({
title: "Custom target deleted",
variant: "success",
});
if (targetOrderSetting.isSuccess)
targetOrderSettingMutation.mutate(
targetOrderSetting.data.filter((targetID: number) => targetID !== id)
);
};
const onDeleteTargetError = (error: AxiosError) => {
pushNotification({
title: getAxiosErrorMessage(error),
variant: "danger",
});
};
const { mutate: deleteTarget } = useDeleteTargetMutation(
onDeleteTargetSuccess,
onDeleteTargetError
);
const onCustomTargetModalSaved = async (response: AxiosResponse<Target>) => {
if (targetToUpdate) {
pushNotification({
title: t("toastr.success.saveWhat", {
what: response.data.name,
type: t("terms.customTarget"),
}),
variant: "success",
});
} else {
pushNotification({
title: t("toastr.success.createWhat", {
what: response.data.name,
type: t("terms.customTarget"),
}),
variant: "success",
message: (
<Button
variant="link"
onClick={scrollToBottom}
className={spacing.pl_0}
>
Take me there
</Button>
),
});
// Make sure the new target's provider is part of the providers filter so it can be seen
if (filterState.filterValues["provider"]) {
const targetProvider = response.data.provider;
const fv = filterState.filterValues["provider"];
const newFv = !targetProvider
? null
: fv.includes(targetProvider)
? fv
: [...fv, targetProvider];
filterState.setFilterValues({
...filterState.filterValues,
provider: newFv,
});
}
}
setCreateUpdateModalState(null);
};
const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor));
function handleDragOver(event: DragOverEvent) {
if (targetOrderSetting.isSuccess) {
const { active, over } = event;
if (over && active.id !== over.id) {
const reorderTarget = (items: number[]) => {
const oldIndex = items.indexOf(active.id as number);
const newIndex = items.indexOf(over.id as number);
return arrayMove(items, oldIndex, newIndex);
};
targetOrderSettingMutation.mutate(
reorderTarget(targetOrderSetting.data)
);
}
}
}
const handleDragEnd = () => {
setActiveTarget(null);
};
const handleDragStart = ({ active }: DragStartEvent) => {
const activeId = active.id as number;
const activeTarget = targetsInOrder.find(
(target) => target.id === activeId
);
setActiveTarget(activeTarget ?? null);
};
const tableControls = useLocalTableControls({
tableName: "target-cards",
items: targetsInOrder,
idProperty: "name",
initialFilterValues: { provider: [DEFAULT_PROVIDER] },
columnNames: {
name: "name",
provider: "provider",
},
isFilterEnabled: true,
isPaginationEnabled: false,
isSortEnabled: false,
// TODO: Add `persistTo` handling if needed.
filterCategories: [
{
selectOptions: unique(
targetsInOrder.map((target) => target.provider).filter(Boolean)
).map((target) => ({ value: target })),
placeholderText: "Filter by language...",
categoryKey: "provider",
title: "Languages",
type: FilterType.multiselect,
matcher: (filter, target) => !!target.provider?.includes(filter),
},
{
placeholderText: "Filter by name...",
categoryKey: "name",
title: "Name",
type: FilterType.search,
matcher: (filter, target) =>
!!target.name?.toLowerCase().includes(filter.toLowerCase()),
},
],
});
const {
filterState,
currentPageItems: filteredTargets,
propHelpers: { toolbarProps, filterToolbarProps },
} = tableControls;
const filteredTargetsInOrder = useMemo(() => {
if (!targetOrderSetting.isSuccess) {
return [];
}
// Get targets in the order specified by the setting
const orderedTargets = targetOrderSetting.data
.map((id) => filteredTargets.find((target) => target.id === id))
.filter(Boolean);
// Find targets that are in filteredTargets but not in the ordered list
const orderedTargetIds = new Set(orderedTargets.map((target) => target.id));
const unorderedTargets = filteredTargets.filter(
(target) => !orderedTargetIds.has(target.id)
);
// Combine ordered targets with unordered ones at the end
return [...orderedTargets, ...unorderedTargets];
}, [filteredTargets, targetOrderSetting.data, targetOrderSetting.isSuccess]);
return (
<>
<PageSection variant={PageSectionVariants.light}>
<TextContent>
<Text component="h1">{t("terms.customTargets")}</Text>
</TextContent>
<TextContent>
<Text>{t("terms.customTargetsDetails")}</Text>
</TextContent>
</PageSection>
<PageSection>
<Toolbar
{...toolbarProps}
clearAllFilters={() => filterToolbarProps.setFilterValues({})}
>
<ToolbarContent>
<FilterToolbar {...filterToolbarProps} breakpoint="md" />
<ToolbarGroup variant="button-group">
<ToolbarItem>
<Button
id="create-target"
isInline
className={spacing.mlMd}
onClick={() => setCreateUpdateModalState("create")}
>
Create new
</Button>
</ToolbarItem>
</ToolbarGroup>
</ToolbarContent>
</Toolbar>
</PageSection>
<PageSection style={{ paddingBlockStart: 0 }}>
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={handleDragStart}
onDragOver={handleDragOver}
onDragEnd={handleDragEnd}
>
<SortableContext
items={targetOrderSetting.isSuccess ? targetOrderSetting.data : []}
strategy={rectSortingStrategy}
>
<Gallery hasGutter minWidths={{ default: "20em" }}>
{filteredTargetsInOrder.map((target) => (
<SortableTargetItem
key={target.id}
target={target}
style={{ height: "410px" }}
onEdit={() => {
setCreateUpdateModalState(target);
}}
onDelete={() => {
// TODO: Add a delete confirmation modal.
deleteTarget(target.id);
}}
/>
))}
<div ref={targetsEndRef} />
</Gallery>
<DragOverlay>
{activeTarget ? <TargetItem target={activeTarget} /> : null}
</DragOverlay>
</SortableContext>
</DndContext>
</PageSection>
<Modal
id="create-edit-custom-target-modal"
title={t(targetToUpdate ? "dialog.title.update" : "dialog.title.new", {
what: `${t("terms.customTarget")}`,
})}
variant="medium"
isOpen={isCreateUpdateModalOpen}
onClose={() => setCreateUpdateModalState(null)}
>
<CustomTargetForm
target={targetToUpdate}
targetOrder={targetOrderSetting.data ?? []}
onSaved={onCustomTargetModalSaved}
onCancel={() => setCreateUpdateModalState(null)}
/>
</Modal>
</>
);
};