123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import {useCallback, useEffect, useRef} from 'react';
- import {Client} from 'sentry/api';
- type Options = {
-
- api?: Client;
-
- persistInFlight?: boolean;
- };
- function useApi({persistInFlight, api: providedApi}: Options = {}) {
- const localApi = useRef<Client>();
-
- if (localApi.current === undefined && providedApi === undefined) {
- localApi.current = new Client();
- }
-
- const api = providedApi ?? localApi.current!;
-
- const clearOnUnmount = useCallback(() => {
- if (!persistInFlight) {
- api.clear();
- }
- }, [api, persistInFlight]);
- useEffect(() => clearOnUnmount, [clearOnUnmount]);
- return api;
- }
- export default useApi;
|