diff --git a/src/app.tsx b/src/app.tsx index 48865958..c13e9ce1 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -15,6 +15,7 @@ import { ErrorScreen } from "@app/error"; import { Root } from "@app/root"; import { SpaceScreen } from "@app/space"; import { TrendingScreen } from "@app/trending"; +import { UserScreen } from "@app/user"; import { AppLayout } from "@shared/appLayout"; import { AuthLayout } from "@shared/authLayout"; import { Protected } from "@shared/protected"; @@ -66,6 +67,7 @@ const router = createBrowserRouter([ children: [ { path: "space", element: }, { path: "trending", element: }, + { path: "user/:pubkey", element: }, { path: "chat/:pubkey", element: }, { path: "channel/:id", element: }, ], diff --git a/src/app/space/components/blocks/thread.tsx b/src/app/space/components/blocks/thread.tsx index 74f485f8..bac42f46 100644 --- a/src/app/space/components/blocks/thread.tsx +++ b/src/app/space/components/blocks/thread.tsx @@ -55,7 +55,7 @@ export function ThreadBlock({ params }: { params: any }) {
- +
)} diff --git a/src/app/trending/components/trendingNotes.tsx b/src/app/trending/components/trendingNotes.tsx index 6613142a..2f31c7fe 100644 --- a/src/app/trending/components/trendingNotes.tsx +++ b/src/app/trending/components/trendingNotes.tsx @@ -4,8 +4,11 @@ import { TitleBar } from "@shared/titleBar"; import { useQuery } from "@tanstack/react-query"; export function TrendingNotes() { - const { status, data } = useQuery(["trending-notes"], async () => { + const { status, data, error } = useQuery(["trending-notes"], async () => { const res = await fetch("https://api.nostr.band/v0/trending/notes"); + if (!res.ok) { + throw new Error("Error"); + } return res.json(); }); @@ -13,6 +16,7 @@ export function TrendingNotes() {
+ {error &&

Failed to fetch

} {status === "loading" ? (
diff --git a/src/app/trending/components/trendingProfiles.tsx b/src/app/trending/components/trendingProfiles.tsx index e6c23092..55b389af 100644 --- a/src/app/trending/components/trendingProfiles.tsx +++ b/src/app/trending/components/trendingProfiles.tsx @@ -4,8 +4,11 @@ import { TitleBar } from "@shared/titleBar"; import { useQuery } from "@tanstack/react-query"; export function TrendingProfiles() { - const { status, data } = useQuery(["trending-profiles"], async () => { + const { status, data, error } = useQuery(["trending-profiles"], async () => { const res = await fetch("https://api.nostr.band/v0/trending/profiles"); + if (!res.ok) { + throw new Error("Error"); + } return res.json(); }); @@ -13,6 +16,7 @@ export function TrendingProfiles() {
+ {error &&

Failed to fetch

} {status === "loading" ? (
diff --git a/src/app/user/index.tsx b/src/app/user/index.tsx index 36aaf09a..dc252b32 100644 --- a/src/app/user/index.tsx +++ b/src/app/user/index.tsx @@ -1,57 +1,50 @@ -import { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; +import { usePublish } from "@libs/ndk"; import { Image } from "@shared/image"; -import { RelayContext } from "@shared/relayProvider"; import { DEFAULT_AVATAR } from "@stores/constants"; import { useQuery } from "@tanstack/react-query"; -import { dateToUnix } from "@utils/date"; -import { useAccount } from "@utils/hooks/useAccount"; -import { usePageContext } from "@utils/hooks/usePageContext"; +import { useFollows } from "@utils/hooks/useFollows"; import { useProfile } from "@utils/hooks/useProfile"; import { compactNumber } from "@utils/number"; import { shortenKey } from "@utils/shortenKey"; -import { useContext } from "react"; -import { Link } from "react-router-dom"; +import { useEffect, useState } from "react"; +import { Link, useParams } from "react-router-dom"; export function UserScreen() { - const ndk = useContext(RelayContext); - const pageContext = usePageContext(); - const searchParams: any = pageContext.urlParsed.search; - const pubkey = searchParams.pubkey || ""; + const publish = usePublish(); + const [followed, setFollowed] = useState(false); - const { account } = useAccount(); + const { pubkey } = useParams(); const { user } = useProfile(pubkey); - const { data: userStats, error } = useQuery(["user", pubkey], async () => { + const { status: followsStatus, follows } = useFollows(); + const { + status: userStatsStatus, + data: userStats, + error, + } = useQuery(["user", pubkey], async () => { const res = await fetch( `https://api.nostr.band/v0/stats/profile/${pubkey}`, ); - if (res.ok) { - return await res.json(); + if (!res.ok) { + throw new Error("Error"); } + return await res.json(); }); - const follows = account ? JSON.parse(account.follows) : []; - const follow = (pubkey: string) => { try { const followsAsSet = new Set(follows); followsAsSet.add(pubkey); - const signer = new NDKPrivateKeySigner(account.privkey); - ndk.signer = signer; - const tags = []; followsAsSet.forEach((item) => { tags.push(["p", item]); }); - const event = new NDKEvent(ndk); - event.content = ""; - event.created_at = dateToUnix(); - event.pubkey = pubkey; - event.kind = 3; - event.tags = tags; // publish event - event.publish(); + publish({ content: "", kind: 3, tags: tags }); + + // update state + setFollowed(true); } catch (error) { console.log(error); } @@ -62,27 +55,29 @@ export function UserScreen() { const followsAsSet = new Set(follows); followsAsSet.delete(pubkey); - const signer = new NDKPrivateKeySigner(account.privkey); - ndk.signer = signer; - const tags = []; followsAsSet.forEach((item) => { tags.push(["p", item]); }); - const event = new NDKEvent(ndk); - event.content = ""; - event.created_at = dateToUnix(); - event.pubkey = pubkey; - event.kind = 3; - event.tags = tags; // publish event - event.publish(); + publish({ content: "", kind: 3, tags: tags }); + + // update state + setFollowed(false); } catch (error) { console.log(error); } }; + useEffect(() => { + if (followsStatus === "success" && follows) { + if (follows.includes(pubkey)) { + setFollowed(true); + } + } + }, [followsStatus]); + return (
{error &&

Failed to fetch user stats

} - {!userStats ? ( + {userStatsStatus === "loading" ? (

Loading...

) : (
@@ -166,7 +161,14 @@ export function UserScreen() {
)}
- {follows.includes(pubkey) ? ( + {followsStatus === "loading" ? ( + + ) : followed ? (