Files
explore/lib/hooks/useAddressProfileApiQuery.tsx
tom goriunovandGitHub da6b2f2b87 Migrate from Sentry.io to Rollbar monitoring solution (#2403)
* basic Rollbar setup

* remove sentry code

* add rollback env to values for demo

* [skip ci] adjust instance name

* fix validator

* don't log errors from API by default and change error levels

* [skip ci] change default value for environment property

* [skip ci] do not log api errors of address endpoint
2024-11-29 15:25:59 +01:00

45 lines
1.2 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import * as v from 'valibot';
import config from 'configs/app';
import type { ResourceError } from 'lib/api/resources';
import useFetch from 'lib/hooks/useFetch';
const feature = config.features.addressProfileAPI;
type AddressInfoApiQueryResponse = v.InferOutput<typeof AddressInfoSchema>;
const AddressInfoSchema = v.object({
user_profile: v.object({
username: v.union([ v.string(), v.null() ]),
}),
});
const ERROR_NAME = 'Invalid response schema';
export default function useAddressProfileApiQuery(hash: string | undefined, isEnabled = true) {
const fetch = useFetch();
return useQuery<unknown, ResourceError<unknown>, AddressInfoApiQueryResponse>({
queryKey: [ 'username_api', hash ],
queryFn: async() => {
if (!feature.isEnabled || !hash) {
return Promise.reject();
}
return fetch(feature.apiUrlTemplate.replace('{address}', hash));
},
enabled: isEnabled && Boolean(hash),
refetchOnMount: false,
select: (response) => {
const parsedResponse = v.safeParse(AddressInfoSchema, response);
if (!parsedResponse.success) {
throw Error(ERROR_NAME);
}
return parsedResponse.output;
},
});
}