fixed errors

This commit is contained in:
Ren Amamiya
2023-04-05 15:24:44 +07:00
parent 0d94313b45
commit 5da94e091f
10 changed files with 147 additions and 53 deletions

View File

@@ -1,23 +1,20 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants';
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 { useRouter } from 'next/router';
import { Author } from 'nostr-relaypool';
import { memo, useContext, useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
dayjs.extend(relativeTime);
export const UserExtend = memo(function UserExtend({ pubkey, time }: { pubkey: string; time: any }) {
export const UserExtend = ({ pubkey, time }: { pubkey: string; time: number }) => {
const router = useRouter();
const [pool, relays]: any = useContext(RelayContext);
const [profile, setProfile] = useState(null);
const openUserPage = (e) => {
@@ -25,10 +22,36 @@ export const UserExtend = memo(function UserExtend({ pubkey, time }: { pubkey: s
router.push(`/users/${pubkey}`);
};
const fetchMetadata = useCallback(async (pubkey: string) => {
const res = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
method: 'GET',
timeout: 5,
});
return res.data;
}, []);
const getCachedMetadata = useCallback(async () => {
const { getFollowByPubkey } = await import('@utils/bindings');
getFollowByPubkey({ pubkey: pubkey })
.then((res) => {
if (res) {
const metadata = JSON.parse(res.metadata);
setProfile(metadata);
} else {
fetchMetadata(pubkey).then((res: any) => {
if (res.content) {
const metadata = JSON.parse(res.content);
setProfile(metadata);
}
});
}
})
.catch(console.error);
}, [fetchMetadata, pubkey]);
useEffect(() => {
const user = new Author(pool, relays, pubkey);
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
}, [pool, relays, pubkey]);
getCachedMetadata().catch(console.error);
}, [getCachedMetadata]);
return (
<div className="group flex items-start gap-2">
@@ -61,4 +84,4 @@ export const UserExtend = memo(function UserExtend({ pubkey, time }: { pubkey: s
</div>
</div>
);
});
};

View File

@@ -1,22 +1,29 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants';
import { truncate } from '@utils/truncate';
import { Author } from 'nostr-relaypool';
import { memo, useContext, useEffect, useMemo, useState } from 'react';
export const UserFollow = memo(function UserFollow({ pubkey }: { pubkey: string }) {
const [pool, relays]: any = useContext(RelayContext);
import { useCallback, useEffect, useState } from 'react';
export const UserFollow = ({ pubkey }: { pubkey: string }) => {
const [profile, setProfile] = useState(null);
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
const getCachedMetadata = useCallback(async () => {
const { getFollowByPubkey } = await import('@utils/bindings');
getFollowByPubkey({ pubkey: pubkey })
.then((res) => {
if (res) {
const metadata = JSON.parse(res.metadata);
setProfile(metadata);
}
})
.catch(console.error);
}, [pubkey]);
useEffect(() => {
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
}, [user]);
getCachedMetadata().catch(console.error);
}, [getCachedMetadata]);
return (
<div className="flex items-center gap-2">
@@ -36,4 +43,4 @@ export const UserFollow = memo(function UserFollow({ pubkey }: { pubkey: string
</div>
</div>
);
});
};

View File

@@ -1,5 +1,4 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants';
@@ -8,19 +7,28 @@ import { truncate } from '@utils/truncate';
import { DotsHorizontalIcon } from '@radix-ui/react-icons';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { Author } from 'nostr-relaypool';
import { memo, useContext, useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
dayjs.extend(relativeTime);
export const UserLarge = memo(function UserLarge({ pubkey, time }: { pubkey: string; time: any }) {
const [pool, relays]: any = useContext(RelayContext);
export const UserLarge = ({ pubkey, time }: { pubkey: string; time: number }) => {
const [profile, setProfile] = useState(null);
const getCachedMetadata = useCallback(async () => {
const { getFollowByPubkey } = await import('@utils/bindings');
getFollowByPubkey({ pubkey: pubkey })
.then((res) => {
if (res) {
const metadata = JSON.parse(res.metadata);
setProfile(metadata);
}
})
.catch(console.error);
}, [pubkey]);
useEffect(() => {
const user = new Author(pool, relays, pubkey);
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
}, [pool, relays, pubkey]);
getCachedMetadata().catch(console.error);
}, [getCachedMetadata]);
return (
<div className="flex items-center gap-2">
@@ -51,4 +59,4 @@ export const UserLarge = memo(function UserLarge({ pubkey, time }: { pubkey: str
</div>
</div>
);
});
};

View File

@@ -1,18 +1,41 @@
import { RelayContext } from '@components/relaysProvider';
import { truncate } from '@utils/truncate';
import { Author } from 'nostr-relaypool';
import { memo, useContext, useEffect, useState } from 'react';
import { fetch } from '@tauri-apps/api/http';
import { memo, useCallback, useEffect, useState } from 'react';
export const UserMention = memo(function UserMention({ pubkey }: { pubkey: string }) {
const [pool, relays]: any = useContext(RelayContext);
const [profile, setProfile] = useState(null);
const fetchMetadata = useCallback(async (pubkey: string) => {
const res = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
method: 'GET',
timeout: 5,
});
return res.data;
}, []);
const getCachedMetadata = useCallback(async () => {
const { getFollowByPubkey } = await import('@utils/bindings');
getFollowByPubkey({ pubkey: pubkey })
.then((res) => {
if (res) {
const metadata = JSON.parse(res.metadata);
setProfile(metadata);
} else {
fetchMetadata(pubkey).then((res: any) => {
if (res.content) {
const metadata = JSON.parse(res.content);
setProfile(metadata);
}
});
}
})
.catch(console.error);
}, [fetchMetadata, pubkey]);
useEffect(() => {
const user = new Author(pool, relays, pubkey);
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
}, [pool, relays, pubkey]);
getCachedMetadata().catch(console.error);
}, [getCachedMetadata]);
return <span className="cursor-pointer text-fuchsia-500">@{profile?.name || truncate(pubkey, 16, ' .... ')}</span>;
});

View File

@@ -1,21 +1,29 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants';
import { truncate } from '@utils/truncate';
import { Author } from 'nostr-relaypool';
import { useContext, useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
export const UserMini = ({ pubkey }: { pubkey: string }) => {
const [pool, relays]: any = useContext(RelayContext);
const [profile, setProfile] = useState(null);
const getCachedMetadata = useCallback(async () => {
const { getFollowByPubkey } = await import('@utils/bindings');
getFollowByPubkey({ pubkey: pubkey })
.then((res) => {
if (res) {
const metadata = JSON.parse(res.metadata);
setProfile(metadata);
}
})
.catch(console.error);
}, [pubkey]);
useEffect(() => {
const user = new Author(pool, relays, pubkey);
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
}, [pool, relays, pubkey]);
getCachedMetadata().catch(console.error);
}, [getCachedMetadata]);
if (profile) {
return (