fixed my buggy codes

This commit is contained in:
Ren Amamiya
2023-03-21 13:49:00 +07:00
parent 3ad1fff4f8
commit 283931bfa1
8 changed files with 108 additions and 107 deletions

View File

@@ -3,13 +3,27 @@ import { ImageWithFallback } from '@components/imageWithFallback';
import { truncate } from '@utils/truncate';
import { fetch } from '@tauri-apps/api/http';
import Avatar from 'boring-avatars';
import { memo, useCallback, useContext, useMemo, useState } from 'react';
import { memo, useCallback, useContext, useEffect, useState } from 'react';
export const UserMini = memo(function UserMini({ pubkey }: { pubkey: string }) {
const { db }: any = useContext(DatabaseContext);
const [profile, setProfile] = useState({ picture: null, name: null });
const fetchProfile = useCallback(async (id: string) => {
const res = await fetch(`https://rbr.bio/${id}/metadata.json`, {
method: 'GET',
timeout: 30,
});
return res.data;
}, []);
const getCacheProfile = useCallback(async () => {
const result: any = await db.select(`SELECT metadata FROM cache_profiles WHERE id = "${pubkey}"`);
return result[0];
}, [db, pubkey]);
const insertCacheProfile = useCallback(
async (event) => {
// insert to database
@@ -20,33 +34,19 @@ export const UserMini = memo(function UserMini({ pubkey }: { pubkey: string }) {
[db, pubkey]
);
const getCacheProfile = useCallback(async () => {
const result: any = await db.select(`SELECT metadata FROM cache_profiles WHERE id = "${pubkey}"`);
return result[0];
}, [db, pubkey]);
useMemo(() => {
useEffect(() => {
getCacheProfile()
.then((res) => {
if (res !== undefined) {
setProfile(JSON.parse(res.metadata));
} else {
fetch(`https://rbr.bio/${pubkey}/metadata.json`, { redirect: 'follow' })
.then((response) => {
if (response.ok) {
return response.json();
} else if (response.status === 404) {
return Promise.reject('error 404');
} else {
return Promise.reject('some other error: ' + response.status);
}
})
.then((data) => insertCacheProfile(data))
.catch((error) => console.log('error is', error));
fetchProfile(pubkey)
.then((res) => insertCacheProfile(res))
.catch(console.error);
}
})
.catch(console.error);
}, [getCacheProfile, insertCacheProfile, pubkey]);
}, [fetchProfile, getCacheProfile, insertCacheProfile, pubkey]);
return (
<div className="flex cursor-pointer items-center gap-2.5 rounded-md px-2.5 py-1.5 text-sm font-medium hover:bg-zinc-900">