refactor user component and updated onboarding

This commit is contained in:
Ren Amamiya
2023-04-03 08:43:37 +07:00
parent 39e7c9bf34
commit 33000979ed
11 changed files with 89 additions and 129 deletions

View File

@@ -1,44 +1,27 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants';
import { createCacheProfile, getCacheProfile } from '@utils/storage';
import { truncate } from '@utils/truncate';
import { DotsHorizontalIcon } from '@radix-ui/react-icons';
import { fetch } from '@tauri-apps/api/http';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import destr from 'destr';
import { memo, useCallback, useEffect, useState } from 'react';
import { Author } from 'nostr-relaypool';
import { memo, useContext, useEffect, useMemo, useState } from 'react';
dayjs.extend(relativeTime);
export const UserLarge = memo(function UserLarge({ pubkey, time }: { pubkey: string; time: any }) {
const [profile, setProfile] = useState(null);
const [pool, relays]: any = useContext(RelayContext);
const fetchProfile = useCallback(async (id: string) => {
const res = await fetch(`https://rbr.bio/${id}/metadata.json`, {
method: 'GET',
timeout: 30,
});
return res.data;
}, []);
const [profile, setProfile] = useState(null);
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
useEffect(() => {
getCacheProfile(pubkey).then((res) => {
if (res) {
setProfile(destr(res.metadata));
} else {
fetchProfile(pubkey)
.then((res: any) => {
setProfile(destr(res.content));
createCacheProfile(pubkey, res.content);
})
.catch(console.error);
}
});
}, [fetchProfile, pubkey]);
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
}, [user]);
return (
<div className="flex items-center gap-2">