|
1 | 1 | import { CUIAutoComplete } from 'chakra-ui-autocomplete'
|
2 |
| -import React, { useState } from 'react' |
| 2 | +import React, { useContext, useState } from 'react' |
| 3 | +import { ThemeContext } from '../../util/themecontext' |
3 | 4 | import { initialFilter } from '../config'
|
4 | 5 |
|
5 |
| -export interface TagPanelProps { |
6 |
| - tags: string[] |
| 6 | +export interface OptionPanelProps { |
| 7 | + options: string[] |
7 | 8 | filter: typeof initialFilter
|
8 | 9 | setFilter: any
|
9 |
| - highlightColor: string |
10 |
| - mode: string |
| 10 | + listName: 'tagsBlacklist' | 'tagsWhitelist' | 'dirsAllowlist' | 'dirsBlocklist' |
| 11 | + displayName: string |
| 12 | + labelFilter?: string |
11 | 13 | }
|
12 | 14 |
|
13 |
| -export const TagPanel = (props: TagPanelProps) => { |
14 |
| - const { filter, setFilter, tags, highlightColor, mode } = props |
15 |
| - const tagArray = tags.map((tag) => { |
16 |
| - return { value: tag, label: tag } |
| 15 | +export const OptionPanel = (props: OptionPanelProps) => { |
| 16 | + const { filter, listName, labelFilter, displayName, setFilter, options = [] } = props |
| 17 | + const { highlightColor } = useContext(ThemeContext) |
| 18 | + const optionArray = options.map((option) => { |
| 19 | + return { value: option, label: labelFilter ? option.replace(labelFilter, '') : option } |
17 | 20 | })
|
18 | 21 |
|
19 |
| - const currentTags = mode === 'blacklist' ? 'tagsBlacklist' : 'tagsWhitelist' |
20 |
| - const name = mode === 'blacklist' ? 'blocklist' : 'allowlist' |
21 |
| - const [selectedItems, setSelectedItems] = useState<typeof tagArray>( |
22 |
| - filter[currentTags].map((tag) => { |
23 |
| - return { value: tag, label: tag } |
| 22 | + const [selectedItems, setSelectedItems] = useState<typeof optionArray>( |
| 23 | + filter[listName].map((option) => { |
| 24 | + return { |
| 25 | + value: option, |
| 26 | + label: labelFilter ? (option as string)?.replace(labelFilter, '') : option, |
| 27 | + } |
24 | 28 | }),
|
25 | 29 | )
|
26 | 30 |
|
27 | 31 | return (
|
28 | 32 | <CUIAutoComplete
|
29 | 33 | labelStyleProps={{ fontWeight: 300, fontSize: 14 }}
|
30 |
| - items={tagArray} |
31 |
| - label={`Add tag to ${name}`} |
| 34 | + items={optionArray} |
| 35 | + label={`Add tag to ${displayName}`} |
32 | 36 | placeholder=" "
|
33 | 37 | onCreateItem={(item) => null}
|
34 | 38 | disableCreateItem={true}
|
35 | 39 | selectedItems={selectedItems}
|
36 | 40 | onSelectedItemsChange={(changes) => {
|
37 | 41 | if (changes.selectedItems) {
|
38 | 42 | setSelectedItems(changes.selectedItems)
|
39 |
| - setFilter({ ...filter, [currentTags]: changes.selectedItems.map((item) => item.value) }) |
| 43 | + setFilter({ ...filter, [listName]: changes.selectedItems.map((item) => item.value) }) |
40 | 44 | }
|
41 | 45 | }}
|
42 | 46 | listItemStyleProps={{ overflow: 'hidden' }}
|
|
0 commit comments