import { ApiError } from "@/lib/types" import { useState } from "react" import { OWNER_ID } from "@/hooks/constants" import { root } from '@repo/core' import { CategoryController, ListCategoriesResult } from "@repo/sdk" import { useError } from "./use-error" export const useCategories = () => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [data, setData] = useState() const load = async (params?: { page?: number limit?: number isActive?: boolean search?: string categoryIds?: string[] withChildren?: boolean ownerId?: string }) => { try { setLoading(true) const category = root.get(CategoryController) const { data, error } = await useError(async () => await category.list({ page: params?.page || 1, limit: params?.limit || 20, orderBy: 'createdAt', order: 'desc', ...params, ownerId: OWNER_ID })) if (error) { setError(error) return } setData(data) } catch (e) { setError(e as ApiError) } finally { setLoading(false) } } return { load, loading, error, data } }