-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathworkflows-summary-configurable-table.svelte
More file actions
323 lines (278 loc) · 9.89 KB
/
workflows-summary-configurable-table.svelte
File metadata and controls
323 lines (278 loc) · 9.89 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
<script lang="ts">
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
import { getContext, type Snippet } from 'svelte';
import { page } from '$app/state';
import TableEmptyState from '$lib/components/workflow/workflows-summary-configurable-table/table-empty-state.svelte';
import Button from '$lib/holocene/button.svelte';
import FeatureTag from '$lib/holocene/feature-tag.svelte';
import Icon from '$lib/holocene/icon/icon.svelte';
import PaginatedTable from '$lib/holocene/table/paginated-table/api-paginated.svelte';
import Tooltip from '$lib/holocene/tooltip.svelte';
import { translate } from '$lib/i18n/translate';
import {
BATCH_OPERATION_CONTEXT,
type BatchOperationContext,
} from '$lib/pages/workflows-with-new-search.svelte';
import {
fetchAllChildWorkflows,
fetchPaginatedWorkflows,
} from '$lib/services/workflow-service';
import { configurableTableColumns } from '$lib/stores/configurable-table-columns';
import { viewFeature } from '$lib/stores/new-feature-tags';
import { tableDensity } from '$lib/stores/table-density';
import { refresh, workflowCount } from '$lib/stores/workflows';
import type { WorkflowExecution } from '$lib/types/workflows';
import { exportWorkflows } from '$lib/utilities/export-workflows';
import TableBodyCell from './workflows-summary-configurable-table/table-body-cell.svelte';
import TableHeaderCell from './workflows-summary-configurable-table/table-header-cell.svelte';
import TableHeaderRow from './workflows-summary-configurable-table/table-header-row.svelte';
import TableRow from './workflows-summary-configurable-table/table-row.svelte';
interface Props {
onClickConfigure: () => void;
cloud?: Snippet;
}
let { onClickConfigure, cloud }: Props = $props();
const { allSelected, selectedWorkflows, selectWorkflows } =
getContext<BatchOperationContext>(BATCH_OPERATION_CONTEXT);
const namespace = $derived(page.params.namespace);
const baseColumns = $derived(
$configurableTableColumns?.[namespace]?.workflows ?? [],
);
const query = $derived(page.url.searchParams.get('query') ?? '');
const hasVersioningFilter = $derived(
query?.includes('TemporalWorkerDeploymentVersion') ?? false,
);
const hasVersioningBehaviorColumn = $derived(
baseColumns.some((col) => col.label === 'Versioning Behavior'),
);
const columns = $derived(
hasVersioningFilter && !hasVersioningBehaviorColumn
? [...baseColumns, { label: 'Versioning Behavior' }]
: baseColumns,
);
const visibleChildrenMap = new SvelteMap<string, WorkflowExecution[]>();
$effect(() => {
void $refresh;
void query;
visibleChildrenMap.clear();
inFlightChildRequests.clear();
});
const inFlightChildRequests = new SvelteSet<string>();
const toggleChildrenVisibility = async (workflow: WorkflowExecution) => {
const visibleChildren = visibleChildrenMap.get(workflow.runId);
if (visibleChildren?.length) {
// we are collapsing the children so if there is an inflight request
// we don't want its resolution to reopen the children.
inFlightChildRequests.delete(workflow.runId);
visibleChildrenMap.delete(workflow.runId);
// deselect children when collapsing
selectWorkflows(false, visibleChildren);
// clear prevClickedRow if row is collapsing
if (
prevClickedRow?.rowType === 'child' &&
prevClickedRow.parentRow.value.runId === workflow.runId
) {
prevClickedRow = prevClickedRow.parentRow;
}
return;
}
if (inFlightChildRequests.has(workflow.runId)) return;
inFlightChildRequests.add(workflow.runId);
try {
const children = await fetchAllChildWorkflows(
namespace,
workflow.id,
workflow.runId,
);
if (inFlightChildRequests.has(workflow.runId)) {
visibleChildrenMap.set(workflow.runId, children);
}
} finally {
inFlightChildRequests.delete(workflow.runId);
}
};
const onFetch = $derived(() => fetchPaginatedWorkflows(namespace, query));
const dense = $derived($tableDensity === 'dense');
const setTableDensity = () => {
$tableDensity = dense ? 'comfortable' : 'dense';
viewFeature('tableDensity');
};
let visiblePaginatedItems: WorkflowExecution[] = $state([]);
type VisibleRow =
| {
rowType: 'root';
childCount: number;
value: WorkflowExecution;
}
| {
rowType: 'child';
parentRow: Extract<VisibleRow, { rowType: 'root' }>;
value: WorkflowExecution;
};
const visibleRows: VisibleRow[] = $derived.by(() => {
return visiblePaginatedItems.flatMap((workflow) => {
const visibleChildren = visibleChildrenMap.get(workflow.runId) ?? [];
const rootRow = {
rowType: 'root' as const,
childCount: visibleChildren.length,
value: workflow,
};
return [
rootRow,
...visibleChildren.map((c) => ({
rowType: 'child' as const,
parentRow: rootRow,
value: c,
})),
];
});
});
let prevClickedRow = $state<VisibleRow | null>(null);
type PageSelectionStatus = 'checked' | 'unchecked' | 'partial';
const pageSelectionStatus: PageSelectionStatus = $derived.by(() => {
const selectedRunIdSet = new Set($selectedWorkflows.map((w) => w.runId));
if ($allSelected) {
return 'checked';
}
const visibleItemsNotSelected = visiblePaginatedItems.filter(
(i) => !selectedRunIdSet.has(i.runId),
);
if (visibleItemsNotSelected.length === visiblePaginatedItems.length) {
return 'unchecked';
}
if (visibleItemsNotSelected.length === 0) {
return 'checked';
}
return 'partial';
});
const handleSelectPage = (
isSelected: boolean,
workflows: WorkflowExecution[],
) => {
selectWorkflows(isSelected, workflows);
prevClickedRow = null;
if (!isSelected) {
allSelected.set(false);
}
};
$effect(() => {
void visiblePaginatedItems;
void $allSelected;
prevClickedRow = null;
});
</script>
{#key [namespace, query, $refresh]}
<PaginatedTable
total={$workflowCount.count}
{onFetch}
onItemsChange={(items) => {
visiblePaginatedItems = items;
}}
aria-label={translate('common.workflows')}
pageSizeSelectLabel={translate('common.per-page')}
nextButtonLabel={translate('common.next')}
previousButtonLabel={translate('common.previous')}
emptyStateMessage={translate('workflows.empty-state-title')}
maxHeight="var(--panel-h)"
>
<caption class="sr-only" slot="caption">
{translate('common.workflows')}
</caption>
<TableHeaderRow
columnsCount={columns.length}
empty={visiblePaginatedItems.length === 0}
slot="headers"
workflows={visiblePaginatedItems}
{pageSelectionStatus}
onSelectPage={handleSelectPage}
>
{#each columns as column (column)}
<TableHeaderCell {column} />
{/each}
</TableHeaderRow>
{#each visibleRows as row, visibleRowIndex (row.value.runId)}
{@const isChildRow = row.rowType === 'child'}
<TableRow
workflow={row.value}
{toggleChildrenVisibility}
childCount={!isChildRow && row.childCount > 0
? row.childCount
: undefined}
child={isChildRow}
onClickBatchSelect={(event) => {
// this is required due to how the underlying Checkbox component
// gets its onclick type from svelte event forwarding. It does not
// know what the current event target type is a checkbox input
if (!(event.currentTarget instanceof HTMLInputElement)) {
return;
}
const isChecked = event.currentTarget.checked;
let targetedWorkflows = [row.value];
const prevClickedRowIndex = visibleRows.findIndex(
(r) => r.value.runId === prevClickedRow?.value.runId,
);
if (event.shiftKey && prevClickedRowIndex >= 0) {
const rangeStartInclusive = Math.min(
prevClickedRowIndex,
visibleRowIndex,
);
const rangeEndInclusive = Math.max(
prevClickedRowIndex,
visibleRowIndex,
);
// end of the slice range is exclusive, so add 1 to include the full range
targetedWorkflows = visibleRows
.slice(rangeStartInclusive, rangeEndInclusive + 1)
.map((r) => r.value);
}
selectWorkflows(isChecked, targetedWorkflows);
prevClickedRow = row;
}}
>
{#each columns as column (column)}
<TableBodyCell workflow={row.value} {column} truncate={dense} />
{/each}
</TableRow>
{/each}
<svelte:fragment slot="empty">
<TableEmptyState {cloud} />
</svelte:fragment>
<svelte:fragment slot="actions-end-additional" let:visibleItems let:page>
<Tooltip
text={dense
? translate('common.dense')
: translate('common.comfortable')}
top
>
<FeatureTag feature="tableDensity" alpha />
<Button
on:click={setTableDensity}
data-testid="table-density-button"
size="xs"
variant="ghost"
leadingIcon={dense ? 'table-dense' : 'table-comfy'}
></Button>
</Tooltip>
<Tooltip text={translate('common.download-json')} top>
<Button
on:click={() => exportWorkflows(visibleItems, page)}
data-testid="export-history-button"
size="xs"
variant="ghost"
>
<Icon name="download" />
</Button>
</Tooltip>
<Tooltip text="Configure Columns" top>
<Button
on:click={onClickConfigure}
data-testid="workflows-summary-table-configuration-button"
size="xs"
variant="ghost"
>
<Icon name="settings" />
</Button>
</Tooltip>
</svelte:fragment>
</PaginatedTable>
{/key}