1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import useSWR from 'swr';
import {
convertWPThematicToThematic,
fetchThematic,
} from '../../../services/graphql';
import type { Maybe, Thematic, WPThematic } from '../../../types';
export type UseThematicReturn<T extends Maybe<WPThematic>> = {
isError: boolean;
isLoading: boolean;
isValidating: boolean;
thematic: T extends undefined ? Maybe<Thematic> : Thematic;
};
export const useThematic = <T extends Maybe<WPThematic>>(
slug: string,
fallback?: T
): UseThematicReturn<T> => {
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<T>;
};
|