-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusePredictAccountState.ts
More file actions
58 lines (50 loc) · 1.52 KB
/
usePredictAccountState.ts
File metadata and controls
58 lines (50 loc) · 1.52 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { useEffect, useRef } from 'react';
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import Logger from '../../../../util/Logger';
import { PREDICT_CONSTANTS } from '../constants/errors';
import { ensureError } from '../utils/predictErrorHandler';
import { predictQueries } from '../queries';
import type { AccountState } from '../types';
interface UsePredictAccountStateOptions {
/**
* Whether the query is enabled.
* @default true
*/
enabled?: boolean;
}
/**
* Fetches the Predict account state (address and deployment status).
*/
export function usePredictAccountState(
options: UsePredictAccountStateOptions = {},
): UseQueryResult<AccountState, Error> {
const { enabled = true } = options;
const queryResult = useQuery({
...predictQueries.accountState.options(),
enabled,
});
const reportedErrorRef = useRef<Error | null>(null);
useEffect(() => {
if (!queryResult.error) {
reportedErrorRef.current = null;
return;
}
if (reportedErrorRef.current === queryResult.error) return;
reportedErrorRef.current = queryResult.error;
Logger.error(ensureError(queryResult.error), {
tags: {
feature: PREDICT_CONSTANTS.FEATURE_NAME,
component: 'usePredictAccountState',
},
context: {
name: 'usePredictAccountState',
data: {
method: 'queryFn',
action: 'account_state_load',
operation: 'data_fetching',
},
},
});
}, [queryResult.error]);
return queryResult;
}