Files
desktop/app/providers/InformationProvider/useUserCache.ts
rosetta 83f38dc63f 'init'
2026-01-30 05:01:05 +02:00

36 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
import { useEffect, useState } from "react";
import { UserInformation } from "./InformationProvider";
import { OnlineState } from "@/app/providers/ProtocolProvider/protocol/packets/packet.onlinestate";
/**
* Информация запрашивается только из кэша, и в отличии от вызова
* хука useUserInformation не использует подгрузку с сервера,
* то есть если в кэше информации нет то вернет пустое значение
* @param publicKey публичный ключ
*/
export function useUserCache(publicKey : string) {
const {getQuery} = useDatabase();
const [userInfo, setUserInfo] =
useState<UserInformation|undefined>(undefined);
useEffect(() => {
loadFromCacheDatabase();
}, [publicKey]);
const loadFromCacheDatabase = async () => {
const result = await getQuery("SELECT * FROM `cached_users` WHERE `public_key` = ?", [publicKey]);
if(!result){
return;
}
setUserInfo({
publicKey: result.public_key,
verified: result.verified,
title: result.title,
username: result.username,
online: OnlineState.OFFLINE
});
}
return userInfo;
}