1
1
import { useCallback } from "react" ;
2
2
import useSWR , { type KeyedMutator } from "swr" ;
3
3
4
+ import { useLogger } from "./use-logger" ;
5
+
4
6
export const useData = < T > ( ...args : Parameters < typeof useSWR < T > > ) => {
5
7
const { data, isLoading, mutate, ...rest } = useSWR ( ...args ) ;
6
8
7
9
const error = rest . error as unknown ;
10
+ const logger = useLogger ( ) ;
8
11
9
12
const reset = useCallback ( ( ) => {
10
13
mutate ( undefined ) . catch ( ( ) => {
@@ -13,7 +16,8 @@ export const useData = <T>(...args: Parameters<typeof useSWR<T>>) => {
13
16
} , [ mutate ] ) ;
14
17
15
18
if ( error ) {
16
- return State . ErrorState ( new LoadDashboardDataError ( error ) , reset ) ;
19
+ logger . error ( error ) ;
20
+ return State . ErrorState ( new UseDataError ( error ) , reset ) ;
17
21
} else if ( isLoading ) {
18
22
return State . Loading ( ) ;
19
23
} else if ( data ) {
@@ -38,17 +42,17 @@ const State = {
38
42
mutate,
39
43
data,
40
44
} ) ,
41
- ErrorState : ( error : LoadDashboardDataError , reset : ( ) => void ) => ( {
45
+ ErrorState : ( error : UseDataError , reset : ( ) => void ) => ( {
42
46
type : StateType . Error as const ,
43
47
error,
44
48
reset,
45
49
} ) ,
46
50
} ;
47
51
48
- class LoadDashboardDataError extends Error {
52
+ class UseDataError extends Error {
49
53
constructor ( cause : unknown ) {
50
54
super ( cause instanceof Error ? cause . message : "" ) ;
51
- this . name = "LoadDashboardDataError " ;
55
+ this . name = "UseDataError " ;
52
56
this . cause = cause ;
53
57
}
54
58
}
0 commit comments