Skip to content

Commit

Permalink
feat: colored execution tags and tags filter
Browse files Browse the repository at this point in the history
Signed-off-by: Lyon Lu <[email protected]>
  • Loading branch information
lyonlu13 committed Feb 1, 2024
1 parent ec124c9 commit cac3afe
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/console/src/components/Executions/ExecutionFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { MultiSelectForm } from 'components/common/MultiSelectForm';
import { SearchInputForm } from 'components/common/SearchInputForm';
import { SingleSelectForm } from 'components/common/SingleSelectForm';
import { FilterPopoverButton } from 'components/Tables/filters/FilterPopoverButton';
import { TagsInputForm } from 'components/common/TagsInputForm';
import {
FilterState,
MultiFilterState,
SearchFilterState,
SingleFilterState,
BooleanFilterState,
TagsFilterState,
} from './filters/types';

const useStyles = makeStyles((theme: Theme) => ({
Expand Down Expand Up @@ -49,6 +51,7 @@ export interface ExecutionFiltersProps {

const RenderFilter: React.FC<{ filter: FilterState }> = ({ filter }) => {
const searchFilterState = filter as SearchFilterState;
const tagsFilterState = filter as TagsFilterState;
switch (filter.type) {
case 'single':
return <SingleSelectForm {...(filter as SingleFilterState<any>)} />;
Expand All @@ -61,6 +64,13 @@ const RenderFilter: React.FC<{ filter: FilterState }> = ({ filter }) => {
defaultValue={searchFilterState.value}
/>
);
case 'tags':
return (

Check warning on line 68 in packages/console/src/components/Executions/ExecutionFilters.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/ExecutionFilters.tsx#L67-L68

Added lines #L67 - L68 were not covered by tests
<TagsInputForm
{...tagsFilterState}
defaultValue={tagsFilterState.tags}
/>
);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Execution } from 'models/Execution/types';
import { ExecutionState, WorkflowExecutionPhase } from 'models/Execution/enums';
import classnames from 'classnames';
import { LaunchPlanLink } from 'components/LaunchPlan/LaunchPlanLink';
import { getColorFromString } from 'components/utils';
import { WorkflowExecutionsTableState } from '../types';
import { WorkflowExecutionLink } from '../WorkflowExecutionLink';
import { getWorkflowExecutionTimingMS, isExecutionArchived } from '../../utils';
Expand Down Expand Up @@ -115,7 +116,10 @@ export function getExecutionTagsCell(
key={tag}
label={tag}
size="small"
color={isArchived ? 'default' : 'primary'}
color="default"
style={{
backgroundColor: isArchived ? undefined : getColorFromString(tag),

Check warning on line 121 in packages/console/src/components/Executions/Tables/WorkflowExecutionTable/cells.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/Tables/WorkflowExecutionTable/cells.tsx#L121

Added line #L121 was not covered by tests
}}
/>
);
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export const filterLabels = {
startTime: 'Start Time',
status: 'Status',
version: 'Version',
tags: 'Tags',
};
14 changes: 13 additions & 1 deletion packages/console/src/components/Executions/filters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export interface FilterButtonState {
onClick: () => void;
}

export type FilterStateType = 'single' | 'multi' | 'search' | 'boolean';
export type FilterStateType =
| 'single'
| 'multi'
| 'search'
| 'boolean'
| 'tags';

export interface FilterState {
active: boolean;
Expand Down Expand Up @@ -60,3 +65,10 @@ export interface BooleanFilterState extends FilterState {
setActive: (active: boolean) => void;
type: 'boolean';
}

export interface TagsFilterState extends FilterState {
onChange: (newTags: string[]) => void;
placeholder: string;
type: 'tags';
tags: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FilterState } from './types';
import { useMultiFilterState } from './useMultiFilterState';
import { useSearchFilterState } from './useSearchFilterState';
import { useSingleFilterState } from './useSingleFilterState';
import { useTagsFilterState } from './useTagsFilterState';

export interface ExecutionFiltersState {
appliedFilters: FilterOperation[];
Expand Down Expand Up @@ -45,6 +46,12 @@ export function useWorkflowExecutionFiltersState() {
listHeader: 'Filter By',
queryStateKey: 'status',
}),
useTagsFilterState({
filterKey: 'admin_tag.name',
label: filterLabels.tags,
placeholder: 'Enter Tags String',
queryStateKey: 'tags',
}),
useSearchFilterState({
filterKey: 'workflow.version',
label: filterLabels.version,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useQueryState } from 'components/hooks/useQueryState';
import { FilterOperationName } from 'models/AdminEntity/types';
import { useEffect, useState } from 'react';
import { TagsFilterState } from './types';
import { useFilterButtonState } from './useFilterButtonState';

function serializeForQueryState(values: any[]) {
return values.join(';');

Check warning on line 8 in packages/console/src/components/Executions/filters/useTagsFilterState.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/filters/useTagsFilterState.ts#L7-L8

Added lines #L7 - L8 were not covered by tests
}
function deserializeFromQueryState(stateValue = '') {
return stateValue.split(';');

Check warning on line 11 in packages/console/src/components/Executions/filters/useTagsFilterState.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/filters/useTagsFilterState.ts#L10-L11

Added lines #L10 - L11 were not covered by tests
}

interface TagsFilterStateStateArgs {
defaultValue?: string[];
filterKey: string;
filterOperation?: FilterOperationName;
label: string;
placeholder: string;
queryStateKey: string;
}

/** Maintains the state for a `TagsInputForm` filter.
* The generated `FilterOperation` will use the provided `key` and `operation`
* (defaults to `VALUE_IN`)
* The current search value will be synced to the query string using the
* provided `queryStateKey` value.
*/
export function useTagsFilterState({
defaultValue = [],
filterKey,
filterOperation = FilterOperationName.VALUE_IN,
label,
placeholder,
queryStateKey,
}: TagsFilterStateStateArgs): TagsFilterState {
const { params, setQueryStateValue } =
useQueryState<Record<string, string>>();
const queryStateValue = params[queryStateKey];

const [tags, setTags] = useState(defaultValue);
const active = tags.length !== 0;

const button = useFilterButtonState();
const onChange = (newValue: string[]) => {
setTags(newValue);

Check warning on line 46 in packages/console/src/components/Executions/filters/useTagsFilterState.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/filters/useTagsFilterState.ts#L46

Added line #L46 was not covered by tests
};

const onReset = () => {
setTags(defaultValue);
button.setOpen(false);

Check warning on line 51 in packages/console/src/components/Executions/filters/useTagsFilterState.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/filters/useTagsFilterState.ts#L50-L51

Added lines #L50 - L51 were not covered by tests
};

useEffect(() => {
const queryValue = tags.length ? serializeForQueryState(tags) : undefined;
setQueryStateValue(queryStateKey, queryValue);
}, [tags.join(), queryStateKey]);

useEffect(() => {
if (queryStateValue) {
setTags(deserializeFromQueryState(queryStateValue));

Check warning on line 61 in packages/console/src/components/Executions/filters/useTagsFilterState.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/filters/useTagsFilterState.ts#L61

Added line #L61 was not covered by tests
}
}, [queryStateValue]);

const getFilter = () =>
tags.length
? [

Check warning on line 67 in packages/console/src/components/Executions/filters/useTagsFilterState.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/Executions/filters/useTagsFilterState.ts#L67

Added line #L67 was not covered by tests
{
value: tags,
key: filterKey,
operation: filterOperation,
},
]
: [];

return {
active,
button,
getFilter,
onChange,
onReset,
label,
placeholder,
tags,
type: 'tags',
};
}
154 changes: 154 additions & 0 deletions packages/console/src/components/common/TagsInputForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import {
Chip,
FormControl,
FormLabel,
IconButton,
InputAdornment,
makeStyles,
OutlinedInput,
Theme,
Link,
} from '@material-ui/core';
import { Add } from '@material-ui/icons';
import { getColorFromString } from 'components/utils';
import * as React from 'react';

const useStyles = makeStyles((theme: Theme) => ({
input: {
margin: `${theme.spacing(1)}px 0`,
},
listHeader: {
color: theme.palette.text.secondary,
lineHeight: 1.5,
textTransform: 'uppercase',
},
resetLink: {
marginLeft: theme.spacing(4),
width: theme.spacing(5),
},
title: {
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
margin: 0,
textTransform: 'uppercase',
color: theme.palette.text.secondary,
},
tagStack: {
display: 'flex',
gap: theme.spacing(0.5),
flexWrap: 'wrap',
width: '240px',
margin: `${theme.spacing(1)}px 0`,
},
}));

export interface TagsInputFormProps {
label: string;
placeholder?: string;
onChange: (tags: string[]) => void;
defaultValue: string[];
}

/** Form content for rendering a header and search input. The value is applied
* on submission of the form.
*/
export const TagsInputForm: React.FC<TagsInputFormProps> = ({
label,
placeholder,
onChange,
defaultValue,
}) => {
const [tags, setTags] = React.useState<string[]>(defaultValue);
const [value, setValue] = React.useState('');
const composition = React.useRef(false);

Check warning on line 64 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L61-L64

Added lines #L61 - L64 were not covered by tests

const styles = useStyles();
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = ({

Check warning on line 67 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L66-L67

Added lines #L66 - L67 were not covered by tests
target: { value },
}) => setValue(value);

Check warning on line 69 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L69

Added line #L69 was not covered by tests

const addTag = () => {
const newTag = value.trim();
setValue('');
if (!tags.includes(newTag) && newTag !== '') {
const newTags = [...tags, newTag];
setTags(newTags);
onChange(newTags);

Check warning on line 77 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L71-L77

Added lines #L71 - L77 were not covered by tests
}
};

const removeTag = (tag: string) => {
const newTags = tags.filter(t => t !== tag);
setTags(newTags);
onChange(newTags);

Check warning on line 84 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L81-L84

Added lines #L81 - L84 were not covered by tests
};

const handleClickReset = () => {
setTags([]);
onChange([]);

Check warning on line 89 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L87-L89

Added lines #L87 - L89 were not covered by tests
};

const resetControl = tags.length ? (
<Link

Check warning on line 93 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L92-L93

Added lines #L92 - L93 were not covered by tests
className={styles.resetLink}
component="button"
variant="body1"
onClick={handleClickReset}
>
Reset
</Link>
) : (
<div className={styles.resetLink} />

Check warning on line 102 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L102

Added line #L102 was not covered by tests
);

return (

Check warning on line 105 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L105

Added line #L105 was not covered by tests
<div>
<div className={styles.title}>
<FormLabel className={styles.listHeader}>{label}</FormLabel>
{resetControl}
</div>
<FormControl margin="dense" variant="outlined" fullWidth={true}>
<OutlinedInput
autoFocus={true}
className={styles.input}
labelWidth={0}
onChange={onInputChange}
onCompositionStart={() => (composition.current = true)}
onCompositionEnd={() => (composition.current = false)}

Check warning on line 118 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L117-L118

Added lines #L117 - L118 were not covered by tests
placeholder={placeholder}
type="text"
value={value}
onKeyDown={e => {
if (e.key === 'Enter' && !composition.current) {
addTag();

Check warning on line 124 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L122-L124

Added lines #L122 - L124 were not covered by tests
}
}}
endAdornment={
<InputAdornment position="end">
<IconButton component="span" size="small" onClick={addTag}>
<Add />
</IconButton>
</InputAdornment>
}
/>
<div className={styles.tagStack}>
{tags.map(tag => {
return (

Check warning on line 137 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L136-L137

Added lines #L136 - L137 were not covered by tests
<Chip
key={tag}
label={tag}
color="default"
size="small"
onDelete={() => removeTag(tag)}

Check warning on line 143 in packages/console/src/components/common/TagsInputForm.tsx

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/common/TagsInputForm.tsx#L143

Added line #L143 was not covered by tests
style={{
backgroundColor: getColorFromString(tag),
}}
/>
);
})}
</div>
</FormControl>
</div>
);
};
19 changes: 19 additions & 0 deletions packages/console/src/components/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
export const removeLeadingSlash = (pathName: string | null): string => {
return pathName?.replace(/^\//, '') || '';
};

export const getColorFromString = (str: string) => {
const strToDec = (string: string) => {
return (

Check warning on line 7 in packages/console/src/components/utils/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/utils/index.ts#L6-L7

Added lines #L6 - L7 were not covered by tests
Array.from<string>(string)
.map(c => c.codePointAt(0) || 0)
.reduce((sum, char, i) => sum + ((i + 1) * char) / 256, 0) % 1

Check warning on line 10 in packages/console/src/components/utils/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/utils/index.ts#L9-L10

Added lines #L9 - L10 were not covered by tests
);
};
return (

Check warning on line 13 in packages/console/src/components/utils/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/console/src/components/utils/index.ts#L13

Added line #L13 was not covered by tests
'hsl(' +
360 * strToDec(str) +
',' +
(40 + 60 * strToDec(str)) +
'%,' +
(75 + 10 * strToDec(str)) +
'%)'
);
};

0 comments on commit cac3afe

Please sign in to comment.