Skip to content

Commit

Permalink
refactor: change filter strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ourai committed Nov 5, 2024
1 parent ed1b2a1 commit 2984a3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/domain/project/widgets/project-list/ProjectList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useState } from 'react';
import { Button } from '@/components/react';
import { Filter } from '@/components/icon';

import { type TagId, type TagTypeId, TagFilterWidget } from '../../../tag';
import { type TagId, type TagTypeId, getTagTypes, getTypeMap, TagFilterWidget } from '../../../tag';
import type { Project } from '../../typing';
import ProjectCardWidget from '../project-card';

Expand All @@ -14,10 +14,22 @@ type ProjectListWidgetProps = {

type FilterTagMap = Partial<Record<TagTypeId, TagId[]>>;

const typeMap = getTypeMap();

function filterByTags(dataSource: Project[], filters: FilterTagMap) {
const tagMap: Record<string, true> = Object.values(filters).reduce((p, tags) => ({ ...p, ...tags.reduce((_p, tag) => ({ ..._p, [tag]: true }), {}) }), {});

return Object.keys(tagMap).length > 0 ? dataSource.filter(({ tags }) => tags && tags.some(tag => tagMap[tag])) : dataSource;
let filtered = dataSource.slice();

getTagTypes().forEach(({ id }) => {
if ((filters[id] || []).length > 0) {
const availableTagMap: Record<string, true> = typeMap[id].reduce((p, t) => ({ ...p, [t.id]: true }), {});

filtered = filtered.filter(({ tags }) => !!tags && tags.some(tag => availableTagMap[tag] && tagMap[tag]));
}
});

return filtered;
}

function ProjectListWidget({ dataSource }: ProjectListWidgetProps) {
Expand All @@ -36,7 +48,11 @@ function ProjectListWidget({ dataSource }: ProjectListWidgetProps) {
<div className="flex items-center justify-between mb-4">
<div><span className="font-semibold">{count}</span> item{count !== 1 ? 's' : ''} found</div>
<div>
<Button variant="light" isIconOnly onClick={() => setFilterable(!filterable)}>
<Button
variant={filterable ? 'solid' : 'light'}
isIconOnly
onClick={() => setFilterable(!filterable)}
>
<Filter className="size-4" />
</Button>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/domain/tag/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type { TagId, TagTypeId, InternalTag, Tag } from './typing';
export { getTagTypes } from './helper';
export { getTypeMap } from './repository';
export { default as TagGroupWidget } from './widgets/tag-group';
export { default as TagFilterWidget } from './widgets/tag-filter';

0 comments on commit 2984a3e

Please sign in to comment.