47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { ApiError } from "@/lib/types"
|
|
import { useState, useCallback } from "react"
|
|
import { root } from '@repo/core'
|
|
import { RecommendedTemplateController, ListPublicRecommendedTemplatesInput, ListPublicRecommendedTemplatesResult } from "@repo/sdk"
|
|
import { useError } from "./use-error"
|
|
|
|
export const useRecommendedTemplates = () => {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<ApiError | null>(null)
|
|
const [data, setData] = useState<ListPublicRecommendedTemplatesResult | undefined>()
|
|
|
|
const execute = useCallback(async (params?: ListPublicRecommendedTemplatesInput) => {
|
|
setLoading(true)
|
|
setError(null)
|
|
|
|
const recommendedTemplate = root.get(RecommendedTemplateController)
|
|
const { data, error } = await useError(async () => await recommendedTemplate.listPublic({
|
|
limit: params?.limit || 20,
|
|
sortBy: 'sortOrder',
|
|
sortOrder: 'desc'
|
|
}))
|
|
|
|
if (error) {
|
|
setError(error)
|
|
setLoading(false)
|
|
return { data: undefined, error }
|
|
}
|
|
|
|
setData(data)
|
|
setLoading(false)
|
|
return { data, error: null }
|
|
}, [])
|
|
|
|
const refetch = useCallback((params?: ListPublicRecommendedTemplatesInput) => {
|
|
return execute(params)
|
|
}, [execute])
|
|
|
|
return {
|
|
data,
|
|
loading,
|
|
error,
|
|
execute,
|
|
refetch,
|
|
}
|
|
}
|
|
|