32 lines
902 B
TypeScript
Executable File
32 lines
902 B
TypeScript
Executable File
import { api } from "@nice/client/"
|
|
import { TaxonomySlug, TermDto } from "@nice/common"
|
|
import { useMemo } from 'react';
|
|
|
|
export interface GetTaxonomyProps {
|
|
categories: string[];
|
|
isLoading: boolean;
|
|
}
|
|
|
|
export function useGetTaxonomy({type}) : GetTaxonomyProps {
|
|
const {data,isLoading} :{data:TermDto[],isLoading:boolean}= api.term.findMany.useQuery({
|
|
where:{
|
|
taxonomy: {
|
|
//TaxonomySlug.CATEGORY
|
|
slug:type
|
|
}
|
|
},
|
|
include:{
|
|
children :true
|
|
},
|
|
take:10, // 只取前10个
|
|
orderBy: {
|
|
createdAt: 'desc', // 按创建时间降序排列
|
|
},
|
|
})
|
|
const categories = useMemo(() => {
|
|
const allCategories = isLoading ? [] : data?.map((course) => course.name);
|
|
return [...Array.from(new Set(allCategories))];
|
|
}, [data]);
|
|
return {categories,isLoading}
|
|
}
|