-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: colored execution tags and tags filter
Signed-off-by: lyonlu13 <[email protected]>
- Loading branch information
Showing
8 changed files
with
296 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ export const filterLabels = { | |
startTime: 'Start Time', | ||
status: 'Status', | ||
version: 'Version', | ||
tags: 'Tags', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/console/src/components/Executions/filters/useTagsFilterState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(';'); | ||
} | ||
function deserializeFromQueryState(stateValue = '') { | ||
return stateValue.split(';'); | ||
} | ||
|
||
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); | ||
}; | ||
|
||
const onReset = () => { | ||
setTags(defaultValue); | ||
button.setOpen(false); | ||
}; | ||
|
||
useEffect(() => { | ||
const queryValue = tags.length ? serializeForQueryState(tags) : undefined; | ||
setQueryStateValue(queryStateKey, queryValue); | ||
}, [tags.join(), queryStateKey]); | ||
|
||
useEffect(() => { | ||
if (queryStateValue) { | ||
setTags(deserializeFromQueryState(queryStateValue)); | ||
} | ||
}, [queryStateValue]); | ||
|
||
const getFilter = () => | ||
tags.length | ||
? [ | ||
{ | ||
value: tags, | ||
key: filterKey, | ||
operation: filterOperation, | ||
}, | ||
] | ||
: []; | ||
|
||
return { | ||
active, | ||
button, | ||
getFilter, | ||
onChange, | ||
onReset, | ||
label, | ||
placeholder, | ||
tags, | ||
type: 'tags', | ||
}; | ||
} |
154 changes: 154 additions & 0 deletions
154
packages/console/src/components/common/TagsInputForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
const styles = useStyles(); | ||
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = ({ | ||
target: { value }, | ||
}) => setValue(value); | ||
|
||
const addTag = () => { | ||
const newTag = value.trim(); | ||
setValue(''); | ||
if (!tags.includes(newTag) && newTag !== '') { | ||
const newTags = [...tags, newTag]; | ||
setTags(newTags); | ||
onChange(newTags); | ||
} | ||
}; | ||
|
||
const removeTag = (tag: string) => { | ||
const newTags = tags.filter(t => t !== tag); | ||
setTags(newTags); | ||
onChange(newTags); | ||
}; | ||
|
||
const handleClickReset = () => { | ||
setTags([]); | ||
onChange([]); | ||
}; | ||
|
||
const resetControl = tags.length ? ( | ||
<Link | ||
className={styles.resetLink} | ||
component="button" | ||
variant="body1" | ||
onClick={handleClickReset} | ||
> | ||
Reset | ||
</Link> | ||
) : ( | ||
<div className={styles.resetLink} /> | ||
); | ||
|
||
return ( | ||
<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)} | ||
placeholder={placeholder} | ||
type="text" | ||
value={value} | ||
onKeyDown={e => { | ||
if (e.key === 'Enter' && !composition.current) { | ||
addTag(); | ||
} | ||
}} | ||
endAdornment={ | ||
<InputAdornment position="end"> | ||
<IconButton component="span" size="small" onClick={addTag}> | ||
<Add /> | ||
</IconButton> | ||
</InputAdornment> | ||
} | ||
/> | ||
<div className={styles.tagStack}> | ||
{tags.map(tag => { | ||
return ( | ||
<Chip | ||
key={tag} | ||
label={tag} | ||
color="default" | ||
size="small" | ||
onDelete={() => removeTag(tag)} | ||
style={{ | ||
backgroundColor: getColorFromString(tag), | ||
}} | ||
/> | ||
); | ||
})} | ||
</div> | ||
</FormControl> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
Array.from<string>(string) | ||
.map(c => c.codePointAt(0) || 0) | ||
.reduce((sum, char, i) => sum + ((i + 1) * char) / 256, 0) % 1 | ||
); | ||
}; | ||
return ( | ||
'hsl(' + | ||
360 * strToDec(str) + | ||
',' + | ||
(40 + 60 * strToDec(str)) + | ||
'%,' + | ||
(75 + 10 * strToDec(str)) + | ||
'%)' | ||
); | ||
}; |