Skip to content

Commit

Permalink
Issue: [BUG] Filtering on field with multiple values does not work as…
Browse files Browse the repository at this point in the history
… expected #895
  • Loading branch information
estruyf committed Dec 30, 2024
1 parent 1ed5131 commit 1c269db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### 🐞 Fixes

- [#895](https://github.com/estruyf/vscode-front-matter/issues/895): Fix issue with array values in filters

## [10.6.0] - 2024-11-06 - [Release notes](https://beta.frontmatter.codes/updates/v10.6.0)

### 🎨 Enhancements
Expand Down
22 changes: 20 additions & 2 deletions src/dashboardWebView/components/Header/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,35 @@ export const Filters: React.FunctionComponent<IFiltersProps> = () => {
return otherFilters?.map((filter) => {
const filterName = typeof filter === "string" ? filter : filter.name;
const filterTitle = typeof filter === "string" ? firstToUpper(filter) : filter.title;
const values = filterValues?.[filterName];
let values = filterValues?.[filterName];
if (!values || values.length === 0) {
return null;
}

// Get all the unique values
const individualValues = new Set<string>();
values.forEach((value) => {
if (value.length === 0) {
return;
}

if (Array.isArray(value)) {
value.forEach((v) => individualValues.add(v));
}

if (typeof value === "string") {
individualValues.add(value);
}
});

values = Array.from(individualValues);

return (
<Filter
key={filterName}
label={filterTitle}
activeItem={crntFilters[filterName]}
items={values}
items={values as string[]}
onClick={(value) => setCrntFilters((prev) => {
const clone = Object.assign({}, prev);
if (!clone[filterName] && value) {
Expand Down
9 changes: 8 additions & 1 deletion src/dashboardWebView/hooks/usePages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ export default function usePages(pages: Page[]) {
for (const filter of filterNames) {
const filterValue = filters[filter];
if (filterValue) {
pagesSorted = pagesSorted.filter((page) => page[filter] === filterValue);
pagesSorted = pagesSorted.filter((page) => {
const value = page[filter];
if (Array.isArray(value)) {
return value.includes(filterValue);
} else {
return value === filterValue;
}
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dashboardWebView/state/atom/FilterValuesAtom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { atom } from 'recoil';

export const FilterValuesAtom = atom<{ [filter: string]: string[] }>({
export const FilterValuesAtom = atom<{ [filter: string]: string[] | string[][] }>({
key: 'FilterValuesAtom',
default: {}
});

0 comments on commit 1c269db

Please sign in to comment.