import useSWR from 'swr'; import { convertWPThematicToThematic, fetchThematic, } from '../../../services/graphql'; import type { Maybe, Thematic, WPThematic } from '../../../types'; export type UseThematicReturn> = { isError: boolean; isLoading: boolean; isValidating: boolean; thematic: T extends undefined ? Maybe : Thematic; }; export const useThematic = >( slug: string, fallback?: T ): UseThematicReturn => { const { data, error, isLoading, isValidating } = useSWR(slug, fetchThematic, { fallbackData: fallback, }); if (error) console.error(error); return { isError: !!error, isLoading, isValidating, thematic: data ? convertWPThematicToThematic(data) : undefined, } as UseThematicReturn; };