From edf56bc97bd3d9550cd2bb1efb945442d7b2e543 Mon Sep 17 00:00:00 2001
From: Ren Amamiya <123083837+reyamir@users.noreply.github.com>
Date: Wed, 9 Aug 2023 13:17:07 +0700
Subject: [PATCH] fix bugs
---
src/app/auth/onboarding/step-1.tsx | 5 +++--
src/libs/ndk/instance.ts | 7 ++++---
src/libs/storage.tsx | 4 ++--
src/shared/avatarUploader.tsx | 6 ++----
src/shared/bannerUploader.tsx | 6 ++----
src/utils/hooks/useNostr.tsx | 21 ++++++++++++---------
src/utils/types.d.ts | 4 ++--
7 files changed, 27 insertions(+), 26 deletions(-)
diff --git a/src/app/auth/onboarding/step-1.tsx b/src/app/auth/onboarding/step-1.tsx
index 4f9fba2f..7267cdb3 100644
--- a/src/app/auth/onboarding/step-1.tsx
+++ b/src/app/auth/onboarding/step-1.tsx
@@ -47,7 +47,8 @@ export function OnboardStep1Screen() {
const event = await publish({ content: '', kind: 3, tags: tags });
await updateAccount('follows', follows);
- const notes = await fetchNotes();
+ // prefetch notes with current follows
+ const notes = await fetchNotes(follows);
// redirect to next step
if (event && notes) {
@@ -103,7 +104,7 @@ export function OnboardStep1Screen() {
{loading ? (
<>
- Creating...
+ It might take a bit, please patient...
>
) : (
diff --git a/src/libs/ndk/instance.ts b/src/libs/ndk/instance.ts
index 7d406c45..9404215c 100644
--- a/src/libs/ndk/instance.ts
+++ b/src/libs/ndk/instance.ts
@@ -2,7 +2,7 @@
import NDK from '@nostr-dev-kit/ndk';
import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
import { NostrFetcher } from 'nostr-fetch';
-import { useEffect, useState } from 'react';
+import { useEffect, useMemo, useState } from 'react';
import TauriAdapter from '@libs/ndk/cache';
import { getExplicitRelayUrls } from '@libs/storage';
@@ -10,13 +10,14 @@ import { getExplicitRelayUrls } from '@libs/storage';
import { FULL_RELAYS } from '@stores/constants';
export const NDKInstance = () => {
+ const cacheAdapter = useMemo(() => new TauriAdapter(), []);
+
const [ndk, setNDK] = useState(undefined);
const [relayUrls, setRelayUrls] = useState([]);
const [fetcher, setFetcher] = useState(undefined);
- const [cacheAdapter] = useState(new TauriAdapter());
useEffect(() => {
- loadNdk();
+ if (!ndk) loadNdk();
return () => {
cacheAdapter.save();
diff --git a/src/libs/storage.tsx b/src/libs/storage.tsx
index 2f907b0c..2784e800 100644
--- a/src/libs/storage.tsx
+++ b/src/libs/storage.tsx
@@ -34,10 +34,10 @@ export async function getActiveAccount() {
if (result.length > 0) {
result[0]['follows'] = result[0].follows
? JSON.parse(result[0].follows as unknown as string)
- : [];
+ : null;
result[0]['network'] = result[0].network
? JSON.parse(result[0].network as unknown as string)
- : [];
+ : null;
return result[0];
} else {
return null;
diff --git a/src/shared/avatarUploader.tsx b/src/shared/avatarUploader.tsx
index a815ca86..6e73b825 100644
--- a/src/shared/avatarUploader.tsx
+++ b/src/shared/avatarUploader.tsx
@@ -9,14 +9,12 @@ export function AvatarUploader({ setPicture }: { setPicture: any }) {
const [loading, setLoading] = useState(false);
const uploadAvatar = async () => {
+ setLoading(true);
const image = await upload(null);
if (image.url) {
- // update parent state
setPicture(image.url);
-
- // disable loader
- setLoading(false);
}
+ setLoading(false);
};
return (
diff --git a/src/shared/bannerUploader.tsx b/src/shared/bannerUploader.tsx
index 0a54403e..2a048381 100644
--- a/src/shared/bannerUploader.tsx
+++ b/src/shared/bannerUploader.tsx
@@ -9,14 +9,12 @@ export function BannerUploader({ setBanner }: { setBanner: any }) {
const [loading, setLoading] = useState(false);
const uploadBanner = async () => {
+ setLoading(true);
const image = await upload(null);
if (image.url) {
- // update parent state
setBanner(image);
-
- // disable loader
- setLoading(false);
}
+ setLoading(false);
};
return (
diff --git a/src/utils/hooks/useNostr.tsx b/src/utils/hooks/useNostr.tsx
index a24c915c..faef06b0 100644
--- a/src/utils/hooks/useNostr.tsx
+++ b/src/utils/hooks/useNostr.tsx
@@ -18,22 +18,25 @@ import { nHoursAgo } from '@utils/date';
export function useNostr() {
const { ndk, relayUrls, fetcher } = useNDK();
- async function fetchNetwork() {
+ async function fetchNetwork(prevFollow?: string[]) {
const account = await getActiveAccount();
- const follows = new Set();
+ const follows = new Set(prevFollow || []);
const lruNetwork = new LRUCache({ max: 300 });
let network: string[];
// fetch user's follows
- const user = ndk.getUser({ hexpubkey: account.pubkey });
- const list = await user.follows();
- list.forEach((item: NDKUser) => {
- follows.add(nip19.decode(item.npub).data as string);
- });
+ if (!prevFollow) {
+ const user = ndk.getUser({ hexpubkey: account.pubkey });
+ const list = await user.follows();
+ list.forEach((item: NDKUser) => {
+ follows.add(nip19.decode(item.npub).data as string);
+ });
+ }
// fetch network
if (!account.network) {
+ console.log('fetching network...', follows.size);
const events = await fetcher.fetchAllEvents(
relayUrls,
{ kinds: [3], authors: [...follows] },
@@ -59,9 +62,9 @@ export function useNostr() {
return [...new Set([...follows, ...network])];
}
- const fetchNotes = async () => {
+ const fetchNotes = async (prevFollow?: string[]) => {
try {
- const network = (await fetchNetwork()) as string[];
+ const network = (await fetchNetwork(prevFollow)) as string[];
const totalNotes = await countTotalNotes();
const lastLogin = await getLastLogin();
diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts
index da977d63..23767001 100644
--- a/src/utils/types.d.ts
+++ b/src/utils/types.d.ts
@@ -20,8 +20,8 @@ export interface Account extends NDKUserProfile {
id: number;
npub: string;
pubkey: string;
- follows: string[];
- network: string[];
+ follows: null | string[];
+ network: null | string[];
is_active: number;
privkey?: string; // deprecated
}