Skip to content

Commit

Permalink
allow removing of custom filters
Browse files Browse the repository at this point in the history
Signed-off-by: GRBurst <[email protected]>
  • Loading branch information
GRBurst committed May 25, 2024
1 parent 781184e commit ff2558a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/CustomFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CustomTagFilter = ({ onTagAdd }: CustomTagFilterProps) => {
const addNewTag = () => {
if (tagName !== undefined && tagName != "") {
const newFlags = (tagPatternFlags !== undefined && tagPatternFlags != "") ? tagPattern : "gmi"
const newTag = (tagPattern !== undefined && tagPattern != "") ? TagFilter({ name: tagName, pattern: RegExp(tagPattern, newFlags) }) : TagFilterSimple(tagName)
const newTag = (tagPattern !== undefined && tagPattern != "") ? TagFilter({ name: tagName, pattern: RegExp(`(${tagPattern})`, newFlags) }) : TagFilterSimple(tagName)
onTagAdd(AppConfig.tagFilters.custom.sectionName, newTag)
setTagName("")
setTagPattern("")
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/components/FilterableJobList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ const FilterableJobList = ({
JSON.stringify(Array.from(allTagFilters.get(AppConfig.tagFilters.custom.sectionName) ?? []).map(f => tagFilterToString(f)))
)
}}
onTagRemove={(key: string, tag: TagFilter) => {
removeFilters(key, tag, allTagFilters, setAllTagFilters)
localStorage.setItem(
AppConfig.tagFilters.custom.localStorageKey,
JSON.stringify(Array.from(allTagFilters.get(AppConfig.tagFilters.custom.sectionName) ?? []).map(f => tagFilterToString(f)))
)
}}
onSearch={(needle: string | undefined) => setSearchFilter(needle)}
/>
<ItemList
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/HnJobs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ const HnJobs = () => {
if (customFilters) {
const restoredFilters = JSON.parse(customFilters).map((f: string) => tagFilterFromString(f))
console.log("Restoring custom filters: ", restoredFilters)
if (restoredFilters?.length) {
predefinedFilterTags.set(AppConfig.tagFilters.custom.sectionName, HSet.fromIterable(restoredFilters));
}
}

}, []);

Expand Down
24 changes: 17 additions & 7 deletions frontend/src/components/TagFilterBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CSSProperties, Fragment, ReactNode, useContext, useState } from 'react';
import { Collapse, Drawer, Flex, Button, FloatButton } from "antd";
import { Collapse, Drawer, Flex, FloatButton, Tag } from "antd";
import type { CollapseProps } from 'antd';
import { EditFilled, FilterTwoTone } from '@ant-design/icons';

Expand All @@ -16,15 +16,24 @@ interface TagProps {
filterKey: string
isActive: boolean
onActiveChange: (filterKey: string, tag: TagF) => void
deletable: boolean
onTagRemove: (filterKey: string, tag: TagF) => void
}

function TagButton({ tagFilter, filterKey, isActive, onActiveChange }: TagProps): ReactNode {
return <Button type={isActive ? "primary" : "default"} onClick={() => onActiveChange(filterKey, tagFilter)}>{tagFilter.name}</Button>
function TagButton({ tagFilter, filterKey, isActive, onActiveChange, deletable, onTagRemove }: TagProps): ReactNode {
return <Tag
color={isActive ? AppConfig.colors.primary : "default"}
closable={deletable}
onClick={() => onActiveChange(filterKey, tagFilter)}
onClose={() => deletable ? onTagRemove(filterKey, tagFilter) : {}}
>{tagFilter.name}</Tag>
}

TagButton.defaultProps = {
isActive: false,
onActiveChange: () => { }
onActiveChange: () => { },
deletable: false,
onTagRemove: () => { },
}

interface TagFilterProps {
Expand All @@ -33,9 +42,10 @@ interface TagFilterProps {
onActive: (key: string, tag: TagF) => void
onInactive: (key: string, tag: TagF) => void
onTagAdd: (key: string, tag: TagF) => void
onTagRemove: (key: string, tag: TagF) => void
onSearch: (needle: string | undefined) => void
}
const TagFilterDrawer = ({ allTags, activeTags, onActive, onInactive, onTagAdd, onSearch }: TagFilterProps) => {
const TagFilterDrawer = ({ allTags, activeTags, onActive, onInactive, onTagAdd, onTagRemove, onSearch }: TagFilterProps) => {
const [open, setOpen] = useState(false);
const appContext = useContext(ConfigContext)
const flatActive = flatFilters(activeTags)
Expand Down Expand Up @@ -69,7 +79,7 @@ const TagFilterDrawer = ({ allTags, activeTags, onActive, onInactive, onTagAdd,
children: (
<Flex key={tagKey} wrap="wrap" gap="small" className="filter-list">
{Array.from(HSet.difference(allTags.get(tagKey) ?? HSet.empty(), activeTags.get(tagKey) ?? HSet.empty())).sort(tagSort).map(tag =>
<TagButton key={tagKey + tag.name} filterKey={tagKey} tagFilter={tag} isActive={false} onActiveChange={onActive} />)
<TagButton key={tagKey + tag.name} filterKey={tagKey} tagFilter={tag} isActive={false} onActiveChange={onActive} deletable={tagKey == AppConfig.tagFilters.custom.sectionName} onTagRemove={onTagRemove} />)
}
</Flex>
),
Expand All @@ -80,7 +90,7 @@ const TagFilterDrawer = ({ allTags, activeTags, onActive, onInactive, onTagAdd,

const collapsableCustomFilter: CollapseProps['items'] = [
{
key: AppConfig.tagFilters.custom.sectionName,
key: `Add${AppConfig.tagFilters.custom.sectionName}`,
label: <h3 className="filter">{AppConfig.tagFilters.custom.name}</h3>,
children: <CustomFilters onTagAdd={onTagAdd} onSearch={onSearch} />,
style: panelStyle
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ h3.filter {
}

.filter-list {
min-height: 40px;
min-height: 22px;
display: flex;
}

Expand Down

0 comments on commit ff2558a

Please sign in to comment.