- 统计页面添加分类多选筛选功能 - 点击分类可跳转到搜索页面查看详情 - 搜索页面添加 URL 同步与自动搜索 - 提取 useCategories hook 消除重复代码 - 修复自定义分类图标显示问题 - 添加 useMemo 优化计算性能 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
import { useState, useEffect } from 'react';
|
|
import { getCategories, isLoggedIn } from '../lib/api';
|
|
import { DEFAULT_CATEGORIES } from '../lib/categories';
|
|
|
|
export function useCategories() {
|
|
const [categories, setCategories] = useState(DEFAULT_CATEGORIES);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
if (!isLoggedIn()) {
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
const loadCategories = async () => {
|
|
try {
|
|
const categoriesData = await getCategories();
|
|
if (categoriesData) {
|
|
setCategories({
|
|
expense: [
|
|
...DEFAULT_CATEGORIES.expense,
|
|
...(categoriesData.expense || []).filter(
|
|
c => !DEFAULT_CATEGORIES.expense.some(d => d.name === c.name)
|
|
)
|
|
],
|
|
income: [
|
|
...DEFAULT_CATEGORIES.income,
|
|
...(categoriesData.income || []).filter(
|
|
c => !DEFAULT_CATEGORIES.income.some(d => d.name === c.name)
|
|
)
|
|
],
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
loadCategories();
|
|
}, []);
|
|
|
|
return { categories, loading };
|
|
}
|