update useProfile hook

This commit is contained in:
Ren Amamiya
2023-07-10 14:20:36 +07:00
parent 4e36b2d902
commit 2a4c3dd7c8
8 changed files with 19 additions and 12 deletions

View File

@@ -60,7 +60,7 @@ export function ImportStep2Screen() {
setPassword(data.password);
// save privkey to secure storage
await save(pubkey, privkey);
await save(pubkey, privkey, data.password);
// redirect to next step
navigate('/auth/import/step-3', { replace: true });

View File

@@ -33,7 +33,7 @@ export function ChatsListItem({ data }: { data: any }) {
>
<div className="inline-flex h-6 w-6 shrink-0 items-center justify-center rounded border-t border-zinc-800/50 bg-zinc-900">
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={data.sender_pubkey}
className="h-6 w-6 rounded object-cover"

View File

@@ -35,7 +35,7 @@ export function ChatsListSelfItem({ data }: { data: any }) {
>
<div className="inline-flex h-6 w-6 shrink-0 items-center justify-center rounded border-t border-zinc-800/50 bg-zinc-900">
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={data.pubkey}
className="h-6 w-6 rounded bg-white object-cover"

View File

@@ -15,7 +15,7 @@ export function ChatSidebar({ pubkey }: { pubkey: string }) {
<div className="flex flex-col gap-3">
<div className="relative h-11 w-11 shrink rounded-md">
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={pubkey}
className="h-11 w-11 rounded-md object-cover"

View File

@@ -71,7 +71,7 @@ export function UserScreen() {
<div className="-mt-7 w-full">
<div className="px-5">
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={pubkey}
className="h-14 w-14 rounded-md ring-2 ring-black"

View File

@@ -11,7 +11,7 @@ export function User({ pubkey }: { pubkey: string }) {
<div className="flex items-center gap-2">
<div className="h-8 w-8 shrink-0 overflow-hidden rounded bg-zinc-900">
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={pubkey}
className="h-8 w-8 object-cover"

View File

@@ -58,7 +58,7 @@ export function User({
className={`${avatarWidth} ${avatarHeight} relative z-10 shrink-0 overflow-hidden bg-zinc-900`}
>
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={pubkey}
className={`${avatarWidth} ${avatarHeight} ${
@@ -93,7 +93,7 @@ export function User({
<div className="w-[250px] overflow-hidden rounded-md border border-zinc-800/50 bg-zinc-900/90 backdrop-blur-lg">
<div className="flex gap-2.5 border-b border-zinc-800 px-3 py-3">
<Image
src={user?.image}
src={user?.picture || user?.image}
fallback={DEFAULT_AVATAR}
alt={pubkey}
className="h-11 w-11 shrink-0 rounded-lg object-cover"

View File

@@ -1,3 +1,4 @@
import { NDKFilter } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
@@ -17,10 +18,16 @@ export function useProfile(pubkey: string, fallback?: string) {
if (cache && parseInt(cache.created_at) + 86400 >= current) {
return cache;
} else {
const user = ndk.getUser({ hexpubkey: pubkey });
await user.fetchProfile();
await createMetadata(pubkey, pubkey, JSON.stringify(user.profile));
return user.profile;
const filter: NDKFilter = { kinds: [0], authors: [pubkey] };
const events = await ndk.fetchEvents(filter);
const latest = [...events].slice(-1)[0];
if (latest) {
await createMetadata(pubkey, pubkey, latest.content);
return JSON.parse(latest.content);
} else {
await createMetadata(pubkey, pubkey, [...events][0].content);
return JSON.parse([...events][0].content);
}
}
} else {
const profile = JSON.parse(fallback);