fixed ssr errors

This commit is contained in:
Ren Amamiya
2023-04-23 08:25:27 +07:00
parent b9bafc851e
commit 8b6bffcff2
14 changed files with 101 additions and 45 deletions

View File

@@ -18,7 +18,7 @@ import LumeSymbol from '@assets/icons/Lume';
import { writeStorage } from '@rehooks/local-storage';
import { useCallback, useContext, useEffect, useRef } from 'react';
import { navigate } from 'vite-plugin-ssr/client/router';
import { navigate, prefetch } from 'vite-plugin-ssr/client/router';
export function Page() {
const [pool, relays]: any = useContext(RelayContext);
@@ -123,7 +123,8 @@ export function Page() {
() => {
updateLastLogin(dateToUnix(now.current));
timeout.current = setTimeout(() => {
navigate('/newsfeed/following', { overwriteLastHistoryEntry: true });
prefetch('/newsfeed/following');
navigate('/newsfeed/following');
}, 5000);
},
{
@@ -152,6 +153,7 @@ export function Page() {
// fetch data
fetchData(account, account.follows);
} else {
prefetch('/onboarding');
navigate('/onboarding', { overwriteLastHistoryEntry: true });
}
})

View File

@@ -10,20 +10,22 @@ import { createAccount, createPleb, updateAccount } from '@utils/storage';
import { nip02ToArray } from '@utils/transform';
import { getPublicKey } from 'nostr-tools';
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { navigate } from 'vite-plugin-ssr/client/router';
export function Page() {
const pageContext = usePageContext();
const searchParams = pageContext.urlParsed.search;
const privkey = searchParams.privkey;
const pubkey = getPublicKey(privkey);
const pubkey = useMemo(() => getPublicKey(privkey), [privkey]);
const [pool, relays]: any = useContext(RelayContext);
const [profile, setProfile] = useState({ metadata: null });
const [done, setDone] = useState(false);
const timeout = useRef(null);
const nip02 = useRef(null);
const createPlebs = useCallback(async (tags: string[]) => {
for (const tag of tags) {
@@ -33,6 +35,16 @@ export function Page() {
}
}, []);
const submit = () => {
// update account's folllows with NIP-02 tag list
const arr = nip02ToArray(nip02.current);
updateAccount('follows', arr, pubkey);
// create plebs (saved nostr profile)
createPlebs(nip02.current);
// redirect to splashscreen
navigate('/', { overwriteLastHistoryEntry: true });
};
useEffect(() => {
const unsubscribe = pool.subscribe(
[
@@ -43,20 +55,20 @@ export function Page() {
],
relays,
(event: any) => {
if (event.kind === 0) {
// create account
createAccount(pubkey, privkey, event.content);
// update state
setProfile({
metadata: JSON.parse(event.content),
});
} else {
if (event.tags.length > 0) {
createPlebs(event.tags);
const arr = nip02ToArray(event.tags);
// update account's folllows with NIP-02 tag list
updateAccount('follows', arr, pubkey);
}
switch (event.kind) {
case 0:
// create account
createAccount(pubkey, privkey, event.content);
// update state
setProfile({
metadata: JSON.parse(event.content),
});
break;
case 3:
nip02.current = event.tags;
break;
default:
break;
}
},
undefined,
@@ -73,7 +85,7 @@ export function Page() {
unsubscribe();
clearTimeout(timeout.current);
};
}, [pool, relays, pubkey, privkey, createPlebs]);
}, [pool, relays, pubkey, privkey]);
return (
<OnboardingLayout>
@@ -128,12 +140,12 @@ export function Page() {
></path>
</svg>
) : (
<a
href="/"
<button
onClick={() => submit()}
className="inline-flex w-full transform items-center justify-center rounded-lg bg-gradient-to-r from-fuchsia-300 via-orange-100 to-amber-300 px-3.5 py-2.5 font-medium text-zinc-800 active:translate-y-1 disabled:cursor-not-allowed disabled:opacity-30"
>
<span className="drop-shadow-lg">Done! Go to newsfeed</span>
</a>
</button>
)}
</div>
</div>