Qortal-Hub/src/hooks/useHandleUserInfo.tsx
2025-05-18 19:22:27 +02:00

31 lines
823 B
TypeScript

import { useCallback, useRef } from 'react';
import { getBaseApiReact } from '../App';
export const useHandleUserInfo = () => {
const userInfoRef = useRef({});
const getIndividualUserInfo = useCallback(async (address) => {
try {
if (!address) return null;
if (userInfoRef.current[address] !== undefined)
return userInfoRef.current[address];
const url = `${getBaseApiReact()}/addresses/${address}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error('network error');
}
const data = await response.json();
userInfoRef.current = {
...userInfoRef.current,
[address]: data?.level,
};
return data?.level;
} catch (error) {
console.log(error);
}
}, []);
return getIndividualUserInfo;
};