/** * 行业技能词云 hook:按行业拉取技能词数据,统一管理 loading / error / refresh。 * 切换行业时自动重新取数(fetcher 依赖 industry); * 搜索关键词的过滤规则放在接口层(filterSkillWords),这里只编排状态。 */ import { useCallback, useState } from 'react'; import { fetchSkillCloud, filterSkillWords } from '../api'; import type { SkillCloudData, SkillWord } from '../types'; import { useAsyncData } from './useAsyncData'; export function useSkillCloud(initialIndustry = '互联网/IT') { const [industry, setIndustry] = useState(initialIndustry); const [keyword, setKeyword] = useState(''); const fetcher = useCallback(() => fetchSkillCloud(industry), [industry]); const { data, loading, error, refresh } = useAsyncData(fetcher); const cloud = (data as SkillCloudData | null) ?? null; /** 按关键词过滤后的技能词;未输入关键词时即全量 */ const words: SkillWord[] = cloud ? filterSkillWords(cloud.words, keyword) : []; return { industry, setIndustry, cloud, /** 搜索关键词 */ keyword, setKeyword, /** 过滤后的技能词(渲染词云用) */ words, /** 有关键词但一条没命中 */ isEmpty: cloud !== null && words.length === 0, loading, error, refresh, }; }