feat: 分类数据统一
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { CATEGORY_LIST_WITH_ALL } from '../../core/data/categories';
|
||||
import { CATEGORY_DATA, type CategoryItem } from './category.data';
|
||||
|
||||
type CategoryTab = {
|
||||
@@ -6,54 +7,37 @@ type CategoryTab = {
|
||||
icon: string;
|
||||
};
|
||||
|
||||
type DisplayItem = CategoryItem & {
|
||||
thumbBg: string;
|
||||
};
|
||||
const SIDEBAR_TABS: CategoryTab[] = CATEGORY_LIST_WITH_ALL.map(({ id, name, icon }) => ({
|
||||
id,
|
||||
name,
|
||||
icon,
|
||||
}));
|
||||
|
||||
const CATEGORY_BG: Record<string, string> = {
|
||||
math: '#fff3c4',
|
||||
pinyin: '#d8f0d0',
|
||||
puzzle: '#f5e6dc',
|
||||
chinese: '#d8f0d0',
|
||||
english: '#e8f4ff',
|
||||
craft: '#fce4ec',
|
||||
};
|
||||
|
||||
const ALL_TAB: CategoryTab = { id: 'all', name: '全部', icon: '📋' };
|
||||
|
||||
const SIDEBAR_TABS: CategoryTab[] = [
|
||||
ALL_TAB,
|
||||
...CATEGORY_DATA.categories.map((c) => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
icon: c.icon,
|
||||
})),
|
||||
];
|
||||
|
||||
function buildAllItems(): DisplayItem[] {
|
||||
const items: DisplayItem[] = [];
|
||||
function buildAllItems(): CategoryItem[] {
|
||||
const items: CategoryItem[] = [];
|
||||
for (const group of CATEGORY_DATA.categories) {
|
||||
for (const item of group.items) {
|
||||
items.push({
|
||||
...item,
|
||||
thumbBg: CATEGORY_BG[group.id] ?? '#f0e7d6',
|
||||
});
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
function getItemsByCategory(categoryId: string): DisplayItem[] {
|
||||
function getItemsByCategory(categoryId: string): CategoryItem[] {
|
||||
if (categoryId === 'all') return buildAllItems();
|
||||
const group = CATEGORY_DATA.categories.find((c) => c.id === categoryId);
|
||||
if (!group) return [];
|
||||
return group.items.map((item) => ({
|
||||
...item,
|
||||
thumbBg: CATEGORY_BG[group.id] ?? '#f0e7d6',
|
||||
}));
|
||||
}
|
||||
|
||||
function filterByKeyword(items: DisplayItem[], keyword: string): DisplayItem[] {
|
||||
function filterByKeyword(
|
||||
items: CategoryItem[],
|
||||
keyword: string,
|
||||
): CategoryItem[] {
|
||||
if (!keyword) return items;
|
||||
const kw = keyword.toLowerCase();
|
||||
return items.filter(
|
||||
@@ -77,7 +61,7 @@ Page({
|
||||
searchKeyword: '',
|
||||
sidebarTabs: SIDEBAR_TABS,
|
||||
activeCategoryId: 'all',
|
||||
displayItems: buildAllItems() as DisplayItem[],
|
||||
displayItems: buildAllItems() as CategoryItem[],
|
||||
scrollIntoViewId: '',
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user