From b64ed8b58790e1cedb3800fe231c99014eb70f3d Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Fri, 21 Apr 2023 16:41:04 +0700 Subject: [PATCH] fixed nextjs ssg build issue --- .../nostr/{channels/[id] => channel}/page.tsx | 12 ++++-- .../nostr/{chats/[pubkey] => chat}/page.tsx | 14 ++++--- src/app/nostr/newsfeed/[id]/page.tsx | 5 --- src/app/nostr/newsfeed/note/page.tsx | 10 +++++ src/app/nostr/{users/[id] => user}/page.tsx | 14 ++++--- src/app/onboarding/create/page.tsx | 2 +- .../create/{[...slug] => step-2}/page.tsx | 9 +++-- src/app/onboarding/login/page.tsx | 2 +- .../login/{[privkey] => step-2}/page.tsx | 13 +++--- src/components/channels/browseChannelItem.tsx | 40 ------------------- src/components/channels/channelListItem.tsx | 2 +- src/components/chats/chatList.tsx | 2 +- src/components/chats/chatListItem.tsx | 2 +- src/components/note/base.tsx | 6 +-- src/components/note/parent.tsx | 23 ++++++----- src/components/note/quote.tsx | 23 ++++++----- src/components/note/quoteRepost.tsx | 2 +- src/components/note/rootNote.tsx | 32 ++++++++++----- 18 files changed, 107 insertions(+), 106 deletions(-) rename src/app/nostr/{channels/[id] => channel}/page.tsx (87%) rename src/app/nostr/{chats/[pubkey] => chat}/page.tsx (80%) delete mode 100644 src/app/nostr/newsfeed/[id]/page.tsx create mode 100644 src/app/nostr/newsfeed/note/page.tsx rename src/app/nostr/{users/[id] => user}/page.tsx (85%) rename src/app/onboarding/create/{[...slug] => step-2}/page.tsx (97%) rename src/app/onboarding/login/{[privkey] => step-2}/page.tsx (93%) delete mode 100644 src/components/channels/browseChannelItem.tsx diff --git a/src/app/nostr/channels/[id]/page.tsx b/src/app/nostr/channel/page.tsx similarity index 87% rename from src/app/nostr/channels/[id]/page.tsx rename to src/app/nostr/channel/page.tsx index a1711cff..ce87d1af 100644 --- a/src/app/nostr/channels/[id]/page.tsx +++ b/src/app/nostr/channel/page.tsx @@ -12,9 +12,13 @@ import { dateToUnix, hoursAgo } from '@utils/getDate'; import useLocalStorage from '@rehooks/local-storage'; import { useSetAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; +import { useSearchParams } from 'next/navigation'; import { useContext, useEffect, useRef } from 'react'; -export default function Page({ params }: { params: { id: string } }) { +export default function Page() { + const searchParams = useSearchParams(); + const id = searchParams.get('channel-id'); + const [pool]: any = useContext(RelayContext); const [activeAccount]: any = useLocalStorage('account', {}); @@ -40,7 +44,7 @@ export default function Page({ params }: { params: { id: string } }) { since: dateToUnix(hoursAgo(24, now.current)), }, { - '#e': [params.id], + '#e': [id], kinds: [42], since: dateToUnix(hoursAgo(24, now.current)), }, @@ -66,13 +70,13 @@ export default function Page({ params }: { params: { id: string } }) { return () => { unsubscribe(); }; - }, [pool, activeAccount.pubkey, params.id, setChannelMessages, resetChannelReply, resetChannelMessages]); + }, [pool, id, activeAccount.pubkey, setChannelMessages, resetChannelReply, resetChannelMessages]); return (
- +
); diff --git a/src/app/nostr/chats/[pubkey]/page.tsx b/src/app/nostr/chat/page.tsx similarity index 80% rename from src/app/nostr/chats/[pubkey]/page.tsx rename to src/app/nostr/chat/page.tsx index af6e573e..c918f89c 100644 --- a/src/app/nostr/chats/[pubkey]/page.tsx +++ b/src/app/nostr/chat/page.tsx @@ -10,9 +10,13 @@ import { FULL_RELAYS } from '@stores/constants'; import useLocalStorage from '@rehooks/local-storage'; import { useSetAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; +import { useSearchParams } from 'next/navigation'; import { useContext, useEffect } from 'react'; -export default function Page({ params }: { params: { pubkey: string } }) { +export default function Page() { + const searchParams = useSearchParams(); + const pubkey = searchParams.get('pubkey'); + const [pool]: any = useContext(RelayContext); const [activeAccount]: any = useLocalStorage('account', {}); @@ -27,13 +31,13 @@ export default function Page({ params }: { params: { pubkey: string } }) { [ { kinds: [4], - authors: [params.pubkey], + authors: [pubkey], '#p': [activeAccount.pubkey], }, { kinds: [4], authors: [activeAccount.pubkey], - '#p': [params.pubkey], + '#p': [pubkey], }, ], FULL_RELAYS, @@ -45,13 +49,13 @@ export default function Page({ params }: { params: { pubkey: string } }) { return () => { unsubscribe(); }; - }, [params.pubkey, activeAccount.pubkey, setChatMessages, pool]); + }, [pubkey, activeAccount.pubkey, setChatMessages, pool]); return (
- +
); diff --git a/src/app/nostr/newsfeed/[id]/page.tsx b/src/app/nostr/newsfeed/[id]/page.tsx deleted file mode 100644 index 83ac2fba..00000000 --- a/src/app/nostr/newsfeed/[id]/page.tsx +++ /dev/null @@ -1,5 +0,0 @@ -'use client'; - -export default function Page({ params }: { params: { id: string } }) { - return
{params.id}
; -} diff --git a/src/app/nostr/newsfeed/note/page.tsx b/src/app/nostr/newsfeed/note/page.tsx new file mode 100644 index 00000000..276b0122 --- /dev/null +++ b/src/app/nostr/newsfeed/note/page.tsx @@ -0,0 +1,10 @@ +'use client'; + +import { useSearchParams } from 'next/navigation'; + +export default function Page() { + const searchParams = useSearchParams(); + const id = searchParams.get('event-id'); + + return
{id}
; +} diff --git a/src/app/nostr/users/[id]/page.tsx b/src/app/nostr/user/page.tsx similarity index 85% rename from src/app/nostr/users/[id]/page.tsx rename to src/app/nostr/user/page.tsx index 227fc741..31252c80 100644 --- a/src/app/nostr/users/[id]/page.tsx +++ b/src/app/nostr/user/page.tsx @@ -6,11 +6,15 @@ import ProfileMetadata from '@components/profile/metadata'; import ProfileNotes from '@components/profile/notes'; import * as Tabs from '@radix-ui/react-tabs'; +import { useSearchParams } from 'next/navigation'; + +export default function Page() { + const searchParams = useSearchParams(); + const pubkey = searchParams.get('pubkey'); -export default function Page({ params }: { params: { id: string } }) { return (
- + - + - + - +
diff --git a/src/app/onboarding/create/page.tsx b/src/app/onboarding/create/page.tsx index d38e5882..8d213bd5 100644 --- a/src/app/onboarding/create/page.tsx +++ b/src/app/onboarding/create/page.tsx @@ -68,7 +68,7 @@ export default function Page() { // broadcast pool.publish(event, relays); // redirect to next step - router.push(`/onboarding/create/${pubkey}/${privkey}`); + router.push(`/onboarding/create/step-2?pubkey=${pubkey}&privkey=${privkey}`); }, [pool, pubkey, privkey, metadata, relays, router]); return ( diff --git a/src/app/onboarding/create/[...slug]/page.tsx b/src/app/onboarding/create/step-2/page.tsx similarity index 97% rename from src/app/onboarding/create/[...slug]/page.tsx rename to src/app/onboarding/create/step-2/page.tsx index 6c06391f..fd8b992d 100644 --- a/src/app/onboarding/create/[...slug]/page.tsx +++ b/src/app/onboarding/create/step-2/page.tsx @@ -9,7 +9,7 @@ import { arrayToNIP02 } from '@utils/transform'; import { createClient } from '@supabase/supabase-js'; import { CheckCircle } from 'iconoir-react'; -import { useRouter } from 'next/navigation'; +import { useRouter, useSearchParams } from 'next/navigation'; import { getEventHash, signEvent } from 'nostr-tools'; import { Key, useCallback, useContext, useEffect, useState } from 'react'; @@ -53,11 +53,12 @@ const initialList = [ { pubkey: 'ff04a0e6cd80c141b0b55825fed127d4532a6eecdb7e743a38a3c28bf9f44609' }, ]; -export default function Page({ params }: { params: { slug: string } }) { +export default function Page() { const router = useRouter(); + const searchParams = useSearchParams(); - const pubkey = params.slug[0]; - const privkey = params.slug[1]; + const pubkey = searchParams.get('pubkey'); + const privkey = searchParams.get('privkey'); const [pool, relays]: any = useContext(RelayContext); const [loading, setLoading] = useState(false); diff --git a/src/app/onboarding/login/page.tsx b/src/app/onboarding/login/page.tsx index dc2adca1..013dfec4 100644 --- a/src/app/onboarding/login/page.tsx +++ b/src/app/onboarding/login/page.tsx @@ -40,7 +40,7 @@ export default function Page() { privkey = nip19.decode(privkey).data; } if (typeof getPublicKey(privkey) === 'string') { - router.push(`/onboarding/login/${privkey}`); + router.push(`/onboarding/login/step-2?privkey=${privkey}`); } } catch (error) { setError('key', { diff --git a/src/app/onboarding/login/[privkey]/page.tsx b/src/app/onboarding/login/step-2/page.tsx similarity index 93% rename from src/app/onboarding/login/[privkey]/page.tsx rename to src/app/onboarding/login/step-2/page.tsx index 148b6e59..b9f23ee4 100644 --- a/src/app/onboarding/login/[privkey]/page.tsx +++ b/src/app/onboarding/login/step-2/page.tsx @@ -11,17 +11,20 @@ import { nip02ToArray } from '@utils/transform'; import Image from 'next/image'; import Link from 'next/link'; +import { useSearchParams } from 'next/navigation'; import { getPublicKey } from 'nostr-tools'; import { useCallback, useContext, useEffect, useRef, useState } from 'react'; -export default function Page({ params }: { params: { privkey: string } }) { - const [pool, relays]: any = useContext(RelayContext); +export default function Page() { + const searchParams = useSearchParams(); + const privkey = searchParams.get('privkey'); + const [pool, relays]: any = useContext(RelayContext); const [profile, setProfile] = useState({ metadata: null }); const [done, setDone] = useState(false); const timeout = useRef(null); - const pubkey = getPublicKey(params.privkey); + const pubkey = getPublicKey(privkey); const createPlebs = useCallback(async (tags: string[]) => { for (const tag of tags) { @@ -43,7 +46,7 @@ export default function Page({ params }: { params: { privkey: string } }) { (event: any) => { if (event.kind === 0) { // create account - createAccount(pubkey, params.privkey, event.content); + createAccount(pubkey, privkey, event.content); // update state setProfile({ metadata: JSON.parse(event.content), @@ -71,7 +74,7 @@ export default function Page({ params }: { params: { privkey: string } }) { unsubscribe(); clearTimeout(timeout.current); }; - }, [pool, relays, pubkey, params.privkey, createPlebs]); + }, [pool, relays, pubkey, privkey, createPlebs]); return (
diff --git a/src/components/channels/browseChannelItem.tsx b/src/components/channels/browseChannelItem.tsx deleted file mode 100644 index df47815b..00000000 --- a/src/components/channels/browseChannelItem.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import { ImageWithFallback } from '@components/imageWithFallback'; - -import { DEFAULT_AVATAR, DEFAULT_CHANNEL_BANNER } from '@stores/constants'; - -import { useChannelMetadata } from '@utils/hooks/useChannelMetadata'; - -export const BrowseChannelItem = ({ data }: { data: any }) => { - const channel = useChannelMetadata(data.event_id, data.metadata); - - return ( -
-
-
- -
-
-
- -
-
-
-
-
-

{channel?.name}

-
-
{channel?.about}
-
-
- ); -}; diff --git a/src/components/channels/channelListItem.tsx b/src/components/channels/channelListItem.tsx index c687c811..c5571ae6 100644 --- a/src/components/channels/channelListItem.tsx +++ b/src/components/channels/channelListItem.tsx @@ -10,7 +10,7 @@ export const ChannelListItem = ({ data }: { data: any }) => { return ( diff --git a/src/components/chats/chatList.tsx b/src/components/chats/chatList.tsx index 5fc443f7..5e6798bb 100644 --- a/src/components/chats/chatList.tsx +++ b/src/components/chats/chatList.tsx @@ -18,7 +18,7 @@ export default function ChatList() { const profile = JSON.parse(activeAccount.metadata); const openSelfChat = () => { - router.push(`/nostr/chats/${activeAccount.pubkey}`); + router.push(`/nostr/chat?pubkey=${activeAccount.pubkey}`); }; useEffect(() => { diff --git a/src/components/chats/chatListItem.tsx b/src/components/chats/chatListItem.tsx index c5f29ad7..aa77deea 100644 --- a/src/components/chats/chatListItem.tsx +++ b/src/components/chats/chatListItem.tsx @@ -11,7 +11,7 @@ export const ChatListItem = ({ pubkey }: { pubkey: string }) => { return ( diff --git a/src/components/note/base.tsx b/src/components/note/base.tsx index e7103609..e9e146a9 100644 --- a/src/components/note/base.tsx +++ b/src/components/note/base.tsx @@ -22,13 +22,13 @@ export const NoteBase = memo(function NoteBase({ event }: { event: any }) { const openUserPage = (e) => { e.stopPropagation(); - router.push(`/nostr/users/${event.pubkey}`); + router.push(`/nostr/user?pubkey=${event.pubkey}`); }; const openThread = (e) => { const selection = window.getSelection(); if (selection.toString().length === 0) { - router.push(`/newsfeed/${event.parent_id}`); + router.push(`/nostr/newsfeed/note?id=${event.parent_id}`); } else { e.stopPropagation(); } @@ -37,7 +37,7 @@ export const NoteBase = memo(function NoteBase({ event }: { event: any }) { return (
openThread(e)} - className="relative z-10 m-0 flex h-min min-h-min w-full select-text flex-col border-b border-zinc-800 px-3 py-5 hover:bg-black/20" + className="relative z-10 flex h-min min-h-min w-full select-text flex-col border-b border-zinc-800 px-3 py-5 hover:bg-black/20" > {parentNote()}
diff --git a/src/components/note/parent.tsx b/src/components/note/parent.tsx index 311dfc48..cf163462 100644 --- a/src/components/note/parent.tsx +++ b/src/components/note/parent.tsx @@ -7,19 +7,18 @@ import { createNote, getNoteByID } from '@utils/storage'; import { getParentID } from '@utils/transform'; import useLocalStorage from '@rehooks/local-storage'; -import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react'; +import { memo, useCallback, useContext, useEffect, useState } from 'react'; export const NoteParent = memo(function NoteParent({ id }: { id: string }) { const [pool, relays]: any = useContext(RelayContext); const [activeAccount]: any = useLocalStorage('account', {}); const [event, setEvent] = useState(null); - const unsubscribe = useRef(null); const content = event ? contentParser(event.content, event.tags) : ''; const fetchEvent = useCallback(async () => { - unsubscribe.current = pool.subscribe( + const unsubscribe = pool.subscribe( [ { ids: [id], @@ -50,9 +49,13 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) { unsubscribeOnEose: true, } ); + + return () => { + unsubscribe(); + }; }, [activeAccount.id, id, pool, relays]); - const checkNoteExist = useCallback(async () => { + const checkNoteIsSaved = useCallback(async () => { getNoteByID(id) .then((res) => { if (res) { @@ -65,14 +68,16 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) { }, [fetchEvent, id]); useEffect(() => { - checkNoteExist(); + let ignore = false; + + if (!ignore) { + checkNoteIsSaved(); + } return () => { - if (unsubscribe.current) { - unsubscribe.current(); - } + ignore = true; }; - }, [checkNoteExist]); + }, [checkNoteIsSaved]); if (event) { return ( diff --git a/src/components/note/quote.tsx b/src/components/note/quote.tsx index 10a07514..671e236d 100644 --- a/src/components/note/quote.tsx +++ b/src/components/note/quote.tsx @@ -6,19 +6,18 @@ import { createNote, getNoteByID } from '@utils/storage'; import { getParentID } from '@utils/transform'; import useLocalStorage from '@rehooks/local-storage'; -import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react'; +import { memo, useCallback, useContext, useEffect, useState } from 'react'; export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) { const [pool, relays]: any = useContext(RelayContext); const [activeAccount]: any = useLocalStorage('account', {}); const [event, setEvent] = useState(null); - const unsubscribe = useRef(null); const content = event ? contentParser(event.content, event.tags) : ''; const fetchEvent = useCallback(async () => { - unsubscribe.current = pool.subscribe( + const unsubscribe = pool.subscribe( [ { ids: [id], @@ -48,9 +47,13 @@ export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) { unsubscribeOnEose: true, } ); + + return () => { + unsubscribe(); + }; }, [activeAccount.id, id, pool, relays]); - const checkNoteExist = useCallback(async () => { + const checkNoteIsSaved = useCallback(async () => { getNoteByID(id) .then((res) => { if (res) { @@ -63,14 +66,16 @@ export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) { }, [fetchEvent, id]); useEffect(() => { - checkNoteExist(); + let ignore = false; + + if (!ignore) { + checkNoteIsSaved(); + } return () => { - if (unsubscribe.current) { - unsubscribe.current(); - } + ignore = true; }; - }, [checkNoteExist]); + }, [checkNoteIsSaved]); if (event) { return ( diff --git a/src/components/note/quoteRepost.tsx b/src/components/note/quoteRepost.tsx index 32ae36f9..e1d57c0d 100644 --- a/src/components/note/quoteRepost.tsx +++ b/src/components/note/quoteRepost.tsx @@ -21,7 +21,7 @@ export const NoteQuoteRepost = memo(function NoteQuoteRepost({ event }: { event: }; return ( -
+
diff --git a/src/components/note/rootNote.tsx b/src/components/note/rootNote.tsx index 3d760b89..f09d582d 100644 --- a/src/components/note/rootNote.tsx +++ b/src/components/note/rootNote.tsx @@ -5,7 +5,7 @@ import { UserExtend } from '@components/user/extend'; import { contentParser } from '@utils/parser'; import { useRouter } from 'next/navigation'; -import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react'; +import { memo, useCallback, useContext, useEffect, useState } from 'react'; export const RootNote = memo(function RootNote({ event }: { event: any }) { const router = useRouter(); @@ -14,17 +14,15 @@ export const RootNote = memo(function RootNote({ event }: { event: any }) { const [data, setData] = useState(null); const [content, setContent] = useState(''); - const unsubscribe = useRef(null); - const openUserPage = (e) => { e.stopPropagation(); - router.push(`/nostr/users/${event.pubkey}`); + router.push(`/nostr/user?pubkey=${event.pubkey}`); }; const openThread = (e) => { const selection = window.getSelection(); if (selection.toString().length === 0) { - router.push(`/nostr/newsfeed/${event.id}`); + router.push(`/nostr/newsfeed/note?id=${event.parent_id}`); } else { e.stopPropagation(); } @@ -32,7 +30,7 @@ export const RootNote = memo(function RootNote({ event }: { event: any }) { const fetchEvent = useCallback( async (id: string) => { - unsubscribe.current = pool.subscribe( + const unsubscribe = pool.subscribe( [ { ids: [id], @@ -50,17 +48,29 @@ export const RootNote = memo(function RootNote({ event }: { event: any }) { unsubscribeOnEose: true, } ); + + return () => { + unsubscribe(); + }; }, [pool, relays] ); useEffect(() => { - if (typeof event === 'object') { - setData(event); - setContent(contentParser(event.content, event.tags)); - } else { - fetchEvent(event); + let ignore = false; + + if (!ignore) { + if (typeof event === 'object') { + setData(event); + setContent(contentParser(event.content, event.tags)); + } else { + fetchEvent(event); + } } + + return () => { + ignore = true; + }; }, [event, fetchEvent]); if (data) {