Skip to content

Commit

Permalink
Add KeywordList option to SearchAttributeInput
Browse files Browse the repository at this point in the history
  • Loading branch information
laurakwhit committed Nov 6, 2024
1 parent 0de2c30 commit 4a72641
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/lib/components/schedule/schedule-search-attributes.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import Accordion from '$lib/holocene/accordion/accordion.svelte';
import { translate } from '$lib/i18n/translate';
import type { SearchAttribute } from '$lib/types';
import type { Payload, SearchAttribute } from '$lib/types';
import { decodePayloadAttributes } from '$lib/utilities/decode-payload';
import { pluralize } from '$lib/utilities/pluralize';
Expand All @@ -11,6 +11,13 @@
$: indexedFields =
decodedSearchAttributes?.searchAttributes.indexedFields ?? {};
$: searchAttributeCount = Object.keys(indexedFields).length;
const formatValue = (value: Payload) => {
if (Array.isArray(value)) {
return value.join(', ');
}
return value;
};
</script>

<Accordion
Expand All @@ -23,12 +30,13 @@
{#if searchAttributeCount}
<ul class="w-full">
{#each Object.entries(indexedFields) as [searchAttrName, searchAttrValue]}
{@const value = formatValue(searchAttrValue)}
<li
class="flex flex-wrap items-center gap-2 border-b py-2 last-of-type:border-b-0"
>
<span class="break-all">{searchAttrName}</span>
<span class="surface-subtle select-all rounded-sm p-1 leading-4"
>{searchAttrValue}</span
>{value}</span
>
</li>
{/each}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/components/workflow/add-search-attributes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
on:click={addSearchAttribute}
disabled={!searchAttributes.length ||
attributesToAdd.length === searchAttributes.length ||
attributesToAdd.filter((a) => a.value === '' || a.value === null).length >
0}>{translate('workflows.add-search-attribute')}</Button
attributesToAdd.filter(
(a) =>
a.value === '' ||
a.value === null ||
(Array.isArray(a.value) && a.value.length === 0),
).length > 0}>{translate('workflows.add-search-attribute')}</Button
>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let second = '';
onMount(() => {
if (value) {
if (value && (typeof value === 'string' || typeof value === 'number')) {
const datetime = new Date(value);
const utcDate = new Date(
datetime.getUTCFullYear(),
Expand Down
11 changes: 7 additions & 4 deletions src/lib/components/workflow/search-attribute-input/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
} from '$lib/types/workflows';
import DatetimeInput from './datetime-input.svelte';
import ListInput from './list-input.svelte';
import NumberInput from './number-input.svelte';
import TextInput from './text-input.svelte';
Expand All @@ -22,9 +23,9 @@
export let onRemove: (attribute: string) => void;
$: type = searchAttributes[attribute.attribute];
$: searchAttributesOptions = [...Object.entries(searchAttributes)]
.map(([key, value]) => ({ label: key, value: key, type: value }))
.filter(({ type }) => type !== 'KeywordList');
$: searchAttributesOptions = [...Object.entries(searchAttributes)].map(
([key, value]) => ({ label: key, value: key, type: value }),
);
$: isDisabled = (value: string) => {
return !!attributesToAdd.find((a) => a.attribute === value);
Expand All @@ -37,7 +38,7 @@
};
</script>

<div class="flex items-start gap-2">
<div class="flex items-end gap-2">
<div class="min-w-fit">
<Select
id="search-attribute"
Expand Down Expand Up @@ -67,6 +68,8 @@
<DatetimeInput bind:value={attribute.value} />
{:else if type === SEARCH_ATTRIBUTE_TYPE.INT || type === SEARCH_ATTRIBUTE_TYPE.DOUBLE}
<NumberInput bind:value={attribute.value} />
{:else if type === SEARCH_ATTRIBUTE_TYPE.KEYWORDLIST}
<ListInput bind:value={attribute.value} />
{:else}
<TextInput bind:value={attribute.value} />
{/if}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script lang="ts">
import { afterUpdate, onMount } from 'svelte';
import ChipInput from '$lib/holocene/input/chip-input.svelte';
import { translate } from '$lib/i18n/translate';
import type { SearchAttributeInputValue } from '$lib/stores/search-attributes';
export let value: SearchAttributeInputValue;
let _value = [];
afterUpdate(() => {
value = _value;
});
onMount(() => {
_value = getInitialValue(value);
});
function getInitialValue(value: SearchAttributeInputValue) {
if (!value) return [];
if (typeof value === 'string' || typeof value === 'number') return [];
return value;
}
</script>

<ChipInput
label={translate('common.value')}
id="attribute-value"
bind:chips={_value}
class="w-full"
removeChipButtonLabel={(chip) =>
translate('workflows.remove-keyword-label', { keyword: chip })}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
value = _value;
});
function getValue(value: number | string) {
function getValue(value: SearchAttributeInputValue) {
const _numValue = Number(value);
const isZero = value === 0 || value === '0';
if (_numValue || isZero) return _numValue;
Expand Down
4 changes: 1 addition & 3 deletions src/lib/services/workflow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,7 @@ export const setSearchAttributes = (

const searchAttributes: SearchAttribute = {};
attributes.forEach((attribute) => {
searchAttributes[attribute.attribute] = setBase64Payload(
String(attribute.value),
);
searchAttributes[attribute.attribute] = setBase64Payload(attribute.value);
});

return searchAttributes;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stores/search-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type SearchAttributeOption = {
type: SearchAttributeType;
};

export type SearchAttributeInputValue = string | number;
export type SearchAttributeInputValue = string | number | string[];

export type SearchAttributeInput = {
attribute: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utilities/encode-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const getSinglePayload = (decodedValue: string): string => {
return '';
};

export const setBase64Payload = (payload: string, encoding = 'json/plain') => {
export const setBase64Payload = (payload: unknown, encoding = 'json/plain') => {
return {
metadata: {
encoding: btoa(encoding),
Expand Down

0 comments on commit 4a72641

Please sign in to comment.