Skip to content

Commit 6911471

Browse files
authored
Refactor SearchAttributeInput to include type (#2439)
* Refactor SearchAttributeInput to include type * Fix initial search attributes on Start Workflow
1 parent 0a9875f commit 6911471

13 files changed

+125
-138
lines changed

Diff for: src/lib/components/schedule/schedule-form-view.svelte

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import Loading from '$lib/holocene/loading.svelte';
1414
import { translate } from '$lib/i18n/translate';
1515
import { error, loading } from '$lib/stores/schedules';
16-
import type { SearchAttributeInput } from '$lib/stores/search-attributes';
16+
import {
17+
customSearchAttributes,
18+
type SearchAttributeInput,
19+
} from '$lib/stores/search-attributes';
1720
import type {
1821
FullSchedule,
1922
ScheduleParameters,
@@ -84,9 +87,13 @@
8487
let second = '';
8588
let phase = '';
8689
let cronString = '';
87-
let searchAttributesInput: SearchAttributeInput[] = Object.entries(
88-
indexedFields,
89-
).map(([attribute, value]) => ({ attribute, value }));
90+
let searchAttributesInput = Object.entries(indexedFields).map(
91+
([label, value]) => ({
92+
label,
93+
value,
94+
type: $customSearchAttributes[label],
95+
}),
96+
) as SearchAttributeInput[];
9097
9198
const handleConfirm = (preset: SchedulePreset, schedule?: Schedule) => {
9299
const args: Partial<ScheduleParameters> = {

Diff for: src/lib/components/workflow/add-search-attributes.svelte

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
customSearchAttributes,
66
type SearchAttributeInput as SAInput,
77
} from '$lib/stores/search-attributes';
8+
import { SEARCH_ATTRIBUTE_TYPE } from '$lib/types/workflows';
89
910
import SearchAttributeInput from './search-attribute-input/index.svelte';
1011
@@ -13,13 +14,16 @@
1314
export let attributesToAdd: SAInput[] = [];
1415
1516
const addSearchAttribute = () => {
16-
attributesToAdd = [...attributesToAdd, { attribute: '', value: null }];
17+
attributesToAdd = [
18+
...attributesToAdd,
19+
{ label: null, value: null, type: SEARCH_ATTRIBUTE_TYPE.UNSPECIFIED },
20+
];
1721
};
1822
1923
$: searchAttributes = Object.keys($customSearchAttributes);
2024
2125
const onRemove = (attribute: string) => {
22-
attributesToAdd = attributesToAdd.filter((a) => a.attribute !== attribute);
26+
attributesToAdd = attributesToAdd.filter((a) => a.label !== attribute);
2327
};
2428
</script>
2529

Diff for: src/lib/components/workflow/search-attribute-input/datetime-input.svelte

+12-20
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,23 @@
55
import DatePicker from '$lib/holocene/date-picker.svelte';
66
import TimePicker from '$lib/holocene/time-picker.svelte';
77
import { translate } from '$lib/i18n/translate';
8-
import type { SearchAttributeInputValue } from '$lib/stores/search-attributes';
98
import { getUTCString } from '$lib/utilities/format-date';
109
11-
export let value: SearchAttributeInputValue;
10+
export let value: string | null;
1211
13-
let date = startOfDay(new Date());
14-
let hour = '';
15-
let minute = '';
16-
let second = '';
12+
const datetime = value ? new Date(value) : new Date();
13+
const utcDate = new Date(
14+
datetime.getUTCFullYear(),
15+
datetime.getUTCMonth(),
16+
datetime.getUTCDate(),
17+
);
18+
let date = startOfDay(utcDate);
19+
let hour = value ? String(datetime.getUTCHours()) : '';
20+
let minute = value ? String(datetime.getUTCMinutes()) : '';
21+
let second = value ? String(datetime.getUTCSeconds()) : '';
1722
1823
onMount(() => {
19-
if (value && (typeof value === 'string' || typeof value === 'number')) {
20-
const datetime = new Date(value);
21-
const utcDate = new Date(
22-
datetime.getUTCFullYear(),
23-
datetime.getUTCMonth(),
24-
datetime.getUTCDate(),
25-
);
26-
date = startOfDay(utcDate);
27-
hour = String(datetime.getUTCHours());
28-
minute = String(datetime.getUTCMinutes());
29-
second = String(datetime.getUTCSeconds());
30-
} else {
31-
updateDatetime();
32-
}
24+
if (!value) updateDatetime();
3325
});
3426
3527
const onDateChange = (d: CustomEvent) => {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<script lang="ts">
22
import Button from '$lib/holocene/button.svelte';
3+
import ChipInput from '$lib/holocene/input/chip-input.svelte';
4+
import Input from '$lib/holocene/input/input.svelte';
5+
import NumberInput from '$lib/holocene/input/number-input.svelte';
36
import Option from '$lib/holocene/select/option.svelte';
47
import Select from '$lib/holocene/select/select.svelte';
58
import { translate } from '$lib/i18n/translate';
@@ -11,23 +14,27 @@
1114
import { SEARCH_ATTRIBUTE_TYPE } from '$lib/types/workflows';
1215
1316
import DatetimeInput from './datetime-input.svelte';
14-
import ListInput from './list-input.svelte';
15-
import NumberInput from './number-input.svelte';
16-
import TextInput from './text-input.svelte';
1717
1818
export let attributesToAdd: SearchAttributeInput[] = [];
1919
export let attribute: SearchAttributeInput;
2020
export let onRemove: (attribute: string) => void;
2121
22-
$: type = $customSearchAttributes[attribute.attribute];
2322
$: isDisabled = (value: string) => {
24-
return !!attributesToAdd.find((a) => a.attribute === value);
23+
return !!attributesToAdd.find((a) => a.label === value);
2524
};
2625
26+
const getType = (attr: string) => $customSearchAttributes[attr];
27+
2728
const handleAttributeChange = (attr: string) => {
28-
if (type !== $customSearchAttributes[attr]) {
29+
const type = getType(attr);
30+
31+
if (type === SEARCH_ATTRIBUTE_TYPE.KEYWORDLIST) {
32+
attribute.value = [];
33+
} else if (attribute.type !== type) {
2934
attribute.value = null;
3035
}
36+
37+
attribute.type = type;
3138
};
3239
</script>
3340

@@ -38,7 +45,7 @@
3845
label={translate('workflows.custom-search-attribute')}
3946
class="w-full"
4047
placeholder={translate('workflows.select-attribute')}
41-
bind:value={attribute.attribute}
48+
bind:value={attribute.label}
4249
onChange={handleAttributeChange}
4350
>
4451
{#each $customSearchAttributeOptions as { value, label, type }}
@@ -48,7 +55,7 @@
4855
{/each}
4956
</Select>
5057
</div>
51-
{#if type === SEARCH_ATTRIBUTE_TYPE.BOOL}
58+
{#if attribute.type === SEARCH_ATTRIBUTE_TYPE.BOOL}
5259
<Select
5360
label={translate('common.value')}
5461
id="attribute-value"
@@ -57,19 +64,45 @@
5764
<Option value={true}>{translate('common.true')}</Option>
5865
<Option value={false}>{translate('common.false')}</Option>
5966
</Select>
60-
{:else if type === SEARCH_ATTRIBUTE_TYPE.DATETIME}
67+
{:else if attribute.type === SEARCH_ATTRIBUTE_TYPE.DATETIME}
6168
<DatetimeInput bind:value={attribute.value} />
62-
{:else if type === SEARCH_ATTRIBUTE_TYPE.INT || type === SEARCH_ATTRIBUTE_TYPE.DOUBLE}
63-
<NumberInput bind:value={attribute.value} />
64-
{:else if type === SEARCH_ATTRIBUTE_TYPE.KEYWORDLIST}
65-
<ListInput bind:value={attribute.value} />
69+
{:else if attribute.type === SEARCH_ATTRIBUTE_TYPE.INT || attribute.type === SEARCH_ATTRIBUTE_TYPE.DOUBLE}
70+
<NumberInput
71+
label={translate('common.value')}
72+
id="attribute-value"
73+
bind:value={attribute.value}
74+
/>
75+
{:else if attribute.type === SEARCH_ATTRIBUTE_TYPE.KEYWORDLIST}
76+
<ChipInput
77+
label={translate('common.value')}
78+
id="attribute-value"
79+
bind:chips={attribute.value}
80+
class="w-full"
81+
removeChipButtonLabel={(chip) =>
82+
translate('workflows.remove-keyword-label', { keyword: chip })}
83+
/>
84+
{:else if attribute.type === SEARCH_ATTRIBUTE_TYPE.TEXT || attribute.type === SEARCH_ATTRIBUTE_TYPE.KEYWORD || attribute.type === SEARCH_ATTRIBUTE_TYPE.UNSPECIFIED}
85+
<Input
86+
label={translate('common.value')}
87+
id="attribute-value"
88+
class="grow"
89+
bind:value={attribute.value}
90+
/>
6691
{:else}
67-
<TextInput bind:value={attribute.value} />
92+
<Input
93+
label={translate('common.value')}
94+
id="attribute-value"
95+
class="grow"
96+
placeholder={translate('workflows.unsupported-attribute')}
97+
error
98+
disabled
99+
value=""
100+
/>
68101
{/if}
69102
<Button
70103
variant="ghost"
71104
leadingIcon="close"
72105
class="mt-6 w-10 rounded-full"
73-
on:click={() => onRemove(attribute.attribute)}
106+
on:click={() => onRemove(attribute.label)}
74107
/>
75108
</div>

Diff for: src/lib/components/workflow/search-attribute-input/list-input.svelte

-33
This file was deleted.

Diff for: src/lib/components/workflow/search-attribute-input/number-input.svelte

-27
This file was deleted.

Diff for: src/lib/components/workflow/search-attribute-input/text-input.svelte

-21
This file was deleted.

Diff for: src/lib/i18n/locales/en/workflows.ts

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ export const Strings = {
248248
'start-workflow-like-this-one': 'Start Workflow Like This One',
249249
'custom-search-attribute': 'Custom Search Attribute',
250250
'select-attribute': 'Select Attribute',
251+
'unsupported-attribute': 'Unsupported attribute type',
251252
'add-search-attribute': 'Add a Search Attribute',
252253
'pending-workflow-task': 'Pending Workflow Task',
253254
'original-scheduled-time': 'Original Scheduled Time',

Diff for: src/lib/models/workflow-execution.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
PendingNexusOperation,
88
} from '$lib/types/events';
99
import type {
10-
DecodedWorkflowSearchAttributes,
1110
ListWorkflowExecutionsResponse,
1211
WorkflowExecution,
1312
WorkflowExecutionAPIResponse,
@@ -58,7 +57,7 @@ const toCallbacks = (callbacks?: Callbacks): Callbacks => {
5857

5958
const toSearchAttributes = (
6059
apiSearchAttributes: WorkflowSearchAttributes,
61-
): DecodedWorkflowSearchAttributes => {
60+
): WorkflowSearchAttributes => {
6261
if (!apiSearchAttributes || !apiSearchAttributes.indexedFields) return {};
6362
const decoded = Object.entries(apiSearchAttributes.indexedFields).reduce(
6463
(searchAttributes, [searchAttributeName, payload]) => {

Diff for: src/lib/pages/start-workflow.svelte

+6-2
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,18 @@
142142
if (customSAKeys.includes(key)) {
143143
searchAttributes = [
144144
...searchAttributes,
145-
{ attribute: key, value: String(value) },
145+
{
146+
label: key,
147+
value,
148+
type: $customSearchAttributes[key],
149+
} as SearchAttributeInput,
146150
];
147151
}
148152
});
149153
}
150154
151155
if (
152-
initialValues?.searchAttributes?.length ||
156+
Object.keys(initialValues?.searchAttributes ?? {}).length ||
153157
initialValues?.summary ||
154158
initialValues?.details
155159
) {

Diff for: src/lib/services/workflow-service.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ export const setSearchAttributes = (
488488

489489
const searchAttributes: SearchAttribute = {};
490490
attributes.forEach((attribute) => {
491-
searchAttributes[attribute.attribute] = setBase64Payload(attribute.value);
491+
searchAttributes[attribute.label] = setBase64Payload(attribute.value);
492492
});
493493

494494
return searchAttributes;
@@ -648,8 +648,9 @@ export const fetchInitialValuesForStartWorkflow = async ({
648648
details = decodedDetails;
649649
}
650650
}
651-
652-
const input = stringifyWithBigInt(convertedAttributes?.payloads[0]);
651+
const input = convertedAttributes?.payloads
652+
? stringifyWithBigInt(convertedAttributes.payloads[0])
653+
: '';
653654
return {
654655
input,
655656
searchAttributes: workflow?.searchAttributes?.indexedFields,

0 commit comments

Comments
 (0)