refactor: tags for constistency

This commit is contained in:
Arman committed 2024-07-25 14:43:04 +02:00
1 parent 6c32340f47
commit 7895f37ad0
3 files changed
+101 -82

No files matched your search

+14 -12
View File
@@ -13,27 +13,23 @@ export type TagValue = {
};
export type Operator = {
toTag: (
attribute: string,
input?: string | number | string[],
type?: string
) => string | TagValue;
toTag: (attribute: string, input?: string | number | string[], type?: string) => TagValue;
toQuery: (attribute: string, input?: string | number | string[]) => string;
types: ColumnType[];
hideInput?: boolean;
};
export function mapToQueryParams(map: Map<string | TagValue, string>) {
export function mapToQueryParams(map: Map<TagValue, string>) {
return encodeURIComponent(JSON.stringify(Array.from(map.entries())));
}
export function queryParamToMap(queryParam: string) {
const decodedQueryParam = decodeURIComponent(queryParam);
const queries = JSON.parse(decodedQueryParam) as [string, string][];
const queries = JSON.parse(decodedQueryParam) as [TagValue, string][];
return new Map(queries);
}
function initQueries(initialValue = new Map<string | TagValue, string>()) {
function initQueries(initialValue = new Map<TagValue, string>()) {
const queries = writable(initialValue);
type AddFilterArgs = {
@@ -52,7 +48,7 @@ function initQueries(initialValue = new Map<string | TagValue, string>()) {
});
}
function removeFilter(tag: string | TagValue) {
function removeFilter(tag: TagValue) {
queries.update((map) => {
map.delete(tag);
return map;
@@ -256,16 +252,22 @@ function generateDefaultOperators() {
toQuery: operator.query,
toTag: (attribute, input = null, type = null) => {
if (input === null) {
return `**${attribute}** ${operatorName}`;
return {
value: '',
tag: `**${attribute}** ${operatorName}`
};
} else if (Array.isArray(input) && input.length > 2) {
return {
value: input,
tag: `**${attribute}** ${operatorName} **${formatArray(input)}** `
};
} else if (type === ValidTypes.Datetime) {
return `**${attribute}** ${operatorName} **${toLocaleDateTime(input.toString())}**`;
return {
value: input,
tag: `**${attribute}** ${operatorName} **${toLocaleDateTime(input.toString())}**`
};
} else {
return `**${attribute}** ${operatorName} **${input}**`;
return { value: input, tag: `**${attribute}** ${operatorName} **${input}**` };
}
},
types: operator.types,
+16 -42
View File
@@ -1,49 +1,23 @@
<script lang="ts">
import { tooltip } from '$lib/actions/tooltip';
import { queries, tagFormat, tags, type TagValue } from './store';
// We cast to any to not cause type errors in the input components
/* eslint @typescript-eslint/no-explicit-any: 'off' */
function isTypeTagValue(obj: any): obj is TagValue {
if (typeof obj === 'string') return false;
return (
obj &&
typeof obj.tag === 'string' &&
(typeof obj.value === 'string' ||
typeof obj.value === 'number' ||
Array.isArray(obj.value))
);
}
import { queries, tagFormat, tags } from './store';
</script>
{#each $tags as tag (tag)}
{#if isTypeTagValue(tag)}
<button
use:tooltip={{
content: tag?.value?.toString()
}}
class="tag"
on:click={() => {
queries.removeFilter(tag);
queries.apply();
}}>
<span class="text" use:tagFormat>
{tag.tag}
</span>
<i class="icon-x" />
</button>
{:else}
<button
class="tag"
on:click={() => {
queries.removeFilter(tag);
queries.apply();
}}>
<span class="text" use:tagFormat>
{tag}
</span>
<i class="icon-x" />
</button>
{/if}
<button
use:tooltip={{
content: tag?.value?.toString(),
disabled: Array.isArray(tag.value) ? tag.value?.length < 3 : true
}}
class="tag"
on:click={() => {
queries.removeFilter(tag);
queries.apply();
}}>
<span class="text" use:tagFormat>
{tag.tag}
</span>
<i class="icon-x" />
</button>
{/each}
@@ -13,10 +13,21 @@
export let columns: Writable<Column[]>;
const colStatus = $columns.find((col) => col.id === 'status');
const statusCol = $columns.find((col) => col.id === 'status');
let showStatusFilter = false;
let statusTag: string = null;
let statusOptions = colStatus?.elements?.map((element) => {
let statusOptions = statusCol?.elements?.map((element) => {
return {
value: (element?.value ?? element) as string,
label: (element?.label ?? element) as string,
checked: false
};
});
const triggerCol = $columns.find((col) => col.id === 'trigger');
let showTriggerFilter = false;
let triggerTag: string = null;
let triggerOptions = triggerCol?.elements?.map((element) => {
return {
value: (element?.value ?? element) as string,
label: (element?.label ?? element) as string,
@@ -30,25 +41,15 @@
} else {
const list = [...tagList].reverse();
list.forEach((tag) => {
if (typeof tag === 'string') {
// Status
if (tag.includes(`**${colStatus.title}**`)) {
statusTag = tag;
// Status
if (tag.tag.includes(`**${statusCol.title}**`)) {
statusTag = tag.tag;
if (Array.isArray(tag.value) && tag.value?.length) {
const values = tag.value as string[];
statusOptions.forEach((option) => {
option.checked = tag.toLowerCase().includes(option.value);
option.checked = values.includes(option.value);
});
}
} else if (typeof tag !== 'string') {
// Status
if (tag.tag.includes(`**${colStatus.title}**`)) {
statusTag = tag.tag;
if (Array.isArray(tag.value) && tag.value?.length) {
const values = tag.value as string[];
statusOptions.forEach((option) => {
option.checked = values.includes(option.value);
});
}
}
} else {
statusTag = null;
}
@@ -62,11 +63,7 @@
value: unknown,
arrayValues: string[] = []
) {
const tagsList = $tags.filter((tag) =>
typeof tag === 'string'
? tag.toLowerCase().includes(colId)
: tag.tag.toLowerCase().includes(colId)
);
const tagsList = $tags.filter((tag) => tag.tag.toLowerCase().includes(colId));
tagsList.forEach((tag) => queries.removeFilter(tag));
if (value || arrayValues?.length) {
addFilter($columns, colId, operator, value, arrayValues);
@@ -74,8 +71,14 @@
queries.apply();
}
$: multipleStatusQueries = [...$queries.values()].reduce((acc, query) => {
if (query.includes(`"attribute":"${colStatus.id}"`)) {
$: statusQueriesTotal = [...$queries.values()].reduce((acc, query) => {
if (query.includes(`"attribute":"${statusCol.id}"`)) {
acc++;
}
return acc;
}, 0);
$: triggerQueriesTotal = [...$queries.values()].reduce((acc, query) => {
if (query.includes(`"attribute":"${triggerCol.id}"`)) {
acc++;
}
return acc;
@@ -92,7 +95,7 @@
{/key}
{:else}
<span class="text">
{colStatus.title}
{statusCol.title}
</span>
{/if}
<span
@@ -101,7 +104,7 @@
</span>
</Pill>
<svelte:fragment slot="list">
{#if multipleStatusQueries > 1}
{#if statusQueriesTotal > 1}
<li class="u-padding-inline-12">This will override your previous query.</li>
{/if}
{#each statusOptions as option (option.value + option.checked)}
@@ -111,7 +114,7 @@
option.checked = !option.checked;
statusOptions = statusOptions;
addFilterAndApply(
colStatus.id,
statusCol.id,
ValidOperators.Equal,
null,
statusOptions.filter((opt) => opt.checked).map((opt) => opt.value) ?? []
@@ -122,3 +125,43 @@
{/each}
</svelte:fragment>
</DropList>
<DropList bind:show={showTriggerFilter} width="11">
<Pill button on:click={() => (showTriggerFilter = !showTriggerFilter)}>
{#if triggerTag}
{#key triggerTag}
<span use:tagFormat>
{triggerTag}
</span>
{/key}
{:else}
<span class="text">
{triggerCol.title}
</span>
{/if}
<span
class:icon-cheveron-down={!showTriggerFilter}
class:icon-cheveron-up={showTriggerFilter}>
</span>
</Pill>
<svelte:fragment slot="list">
{#if triggerQueriesTotal > 1}
<li class="u-padding-inline-12">This will override your previous query.</li>
{/if}
{#each triggerOptions as option (option.value + option.checked)}
<SelectSearchCheckbox
bind:value={option.checked}
on:click={() => {
option.checked = !option.checked;
triggerOptions = triggerOptions;
addFilterAndApply(
triggerCol.id,
ValidOperators.Equal,
null,
triggerOptions.filter((opt) => opt.checked).map((opt) => opt.value) ?? []
);
}}>
{option.label}
</SelectSearchCheckbox>
{/each}
</svelte:fragment>
</DropList>