From 2984a3ed7908deed857ed2c528d659f084184344 Mon Sep 17 00:00:00 2001 From: Ourai Lin Date: Tue, 5 Nov 2024 17:07:46 +0800 Subject: [PATCH] refactor: change filter strategy --- .../widgets/project-list/ProjectList.tsx | 22 ++++++++++++++++--- src/domain/tag/index.ts | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/domain/project/widgets/project-list/ProjectList.tsx b/src/domain/project/widgets/project-list/ProjectList.tsx index 273249b..33535d6 100644 --- a/src/domain/project/widgets/project-list/ProjectList.tsx +++ b/src/domain/project/widgets/project-list/ProjectList.tsx @@ -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'; @@ -14,10 +14,22 @@ type ProjectListWidgetProps = { type FilterTagMap = Partial>; +const typeMap = getTypeMap(); + function filterByTags(dataSource: Project[], filters: FilterTagMap) { const tagMap: Record = 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 = 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) { @@ -36,7 +48,11 @@ function ProjectListWidget({ dataSource }: ProjectListWidgetProps) {
{count} item{count !== 1 ? 's' : ''} found
-
diff --git a/src/domain/tag/index.ts b/src/domain/tag/index.ts index 5168e3f..77a526c 100644 --- a/src/domain/tag/index.ts +++ b/src/domain/tag/index.ts @@ -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';