This commit is contained in:
Ren Amamiya
2023-08-09 13:17:07 +07:00
parent d1d0a462f4
commit edf56bc97b
7 changed files with 27 additions and 26 deletions

View File

@@ -47,7 +47,8 @@ export function OnboardStep1Screen() {
const event = await publish({ content: '', kind: 3, tags: tags }); const event = await publish({ content: '', kind: 3, tags: tags });
await updateAccount('follows', follows); await updateAccount('follows', follows);
const notes = await fetchNotes(); // prefetch notes with current follows
const notes = await fetchNotes(follows);
// redirect to next step // redirect to next step
if (event && notes) { if (event && notes) {
@@ -103,7 +104,7 @@ export function OnboardStep1Screen() {
{loading ? ( {loading ? (
<> <>
<span className="w-5" /> <span className="w-5" />
<span>Creating...</span> <span>It might take a bit, please patient...</span>
<LoaderIcon className="h-5 w-5 animate-spin text-white" /> <LoaderIcon className="h-5 w-5 animate-spin text-white" />
</> </>
) : ( ) : (

View File

@@ -2,7 +2,7 @@
import NDK from '@nostr-dev-kit/ndk'; import NDK from '@nostr-dev-kit/ndk';
import { ndkAdapter } from '@nostr-fetch/adapter-ndk'; import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
import { NostrFetcher } from 'nostr-fetch'; import { NostrFetcher } from 'nostr-fetch';
import { useEffect, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import TauriAdapter from '@libs/ndk/cache'; import TauriAdapter from '@libs/ndk/cache';
import { getExplicitRelayUrls } from '@libs/storage'; import { getExplicitRelayUrls } from '@libs/storage';
@@ -10,13 +10,14 @@ import { getExplicitRelayUrls } from '@libs/storage';
import { FULL_RELAYS } from '@stores/constants'; import { FULL_RELAYS } from '@stores/constants';
export const NDKInstance = () => { export const NDKInstance = () => {
const cacheAdapter = useMemo(() => new TauriAdapter(), []);
const [ndk, setNDK] = useState<NDK | undefined>(undefined); const [ndk, setNDK] = useState<NDK | undefined>(undefined);
const [relayUrls, setRelayUrls] = useState<string[]>([]); const [relayUrls, setRelayUrls] = useState<string[]>([]);
const [fetcher, setFetcher] = useState<NostrFetcher>(undefined); const [fetcher, setFetcher] = useState<NostrFetcher>(undefined);
const [cacheAdapter] = useState(new TauriAdapter());
useEffect(() => { useEffect(() => {
loadNdk(); if (!ndk) loadNdk();
return () => { return () => {
cacheAdapter.save(); cacheAdapter.save();

View File

@@ -34,10 +34,10 @@ export async function getActiveAccount() {
if (result.length > 0) { if (result.length > 0) {
result[0]['follows'] = result[0].follows result[0]['follows'] = result[0].follows
? JSON.parse(result[0].follows as unknown as string) ? JSON.parse(result[0].follows as unknown as string)
: []; : null;
result[0]['network'] = result[0].network result[0]['network'] = result[0].network
? JSON.parse(result[0].network as unknown as string) ? JSON.parse(result[0].network as unknown as string)
: []; : null;
return result[0]; return result[0];
} else { } else {
return null; return null;

View File

@@ -9,14 +9,12 @@ export function AvatarUploader({ setPicture }: { setPicture: any }) {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const uploadAvatar = async () => { const uploadAvatar = async () => {
setLoading(true);
const image = await upload(null); const image = await upload(null);
if (image.url) { if (image.url) {
// update parent state
setPicture(image.url); setPicture(image.url);
// disable loader
setLoading(false);
} }
setLoading(false);
}; };
return ( return (

View File

@@ -9,14 +9,12 @@ export function BannerUploader({ setBanner }: { setBanner: any }) {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const uploadBanner = async () => { const uploadBanner = async () => {
setLoading(true);
const image = await upload(null); const image = await upload(null);
if (image.url) { if (image.url) {
// update parent state
setBanner(image); setBanner(image);
// disable loader
setLoading(false);
} }
setLoading(false);
}; };
return ( return (

View File

@@ -18,22 +18,25 @@ import { nHoursAgo } from '@utils/date';
export function useNostr() { export function useNostr() {
const { ndk, relayUrls, fetcher } = useNDK(); const { ndk, relayUrls, fetcher } = useNDK();
async function fetchNetwork() { async function fetchNetwork(prevFollow?: string[]) {
const account = await getActiveAccount(); const account = await getActiveAccount();
const follows = new Set<string>(); const follows = new Set<string>(prevFollow || []);
const lruNetwork = new LRUCache<string, string, void>({ max: 300 }); const lruNetwork = new LRUCache<string, string, void>({ max: 300 });
let network: string[]; let network: string[];
// fetch user's follows // fetch user's follows
if (!prevFollow) {
const user = ndk.getUser({ hexpubkey: account.pubkey }); const user = ndk.getUser({ hexpubkey: account.pubkey });
const list = await user.follows(); const list = await user.follows();
list.forEach((item: NDKUser) => { list.forEach((item: NDKUser) => {
follows.add(nip19.decode(item.npub).data as string); follows.add(nip19.decode(item.npub).data as string);
}); });
}
// fetch network // fetch network
if (!account.network) { if (!account.network) {
console.log('fetching network...', follows.size);
const events = await fetcher.fetchAllEvents( const events = await fetcher.fetchAllEvents(
relayUrls, relayUrls,
{ kinds: [3], authors: [...follows] }, { kinds: [3], authors: [...follows] },
@@ -59,9 +62,9 @@ export function useNostr() {
return [...new Set([...follows, ...network])]; return [...new Set([...follows, ...network])];
} }
const fetchNotes = async () => { const fetchNotes = async (prevFollow?: string[]) => {
try { try {
const network = (await fetchNetwork()) as string[]; const network = (await fetchNetwork(prevFollow)) as string[];
const totalNotes = await countTotalNotes(); const totalNotes = await countTotalNotes();
const lastLogin = await getLastLogin(); const lastLogin = await getLastLogin();

View File

@@ -20,8 +20,8 @@ export interface Account extends NDKUserProfile {
id: number; id: number;
npub: string; npub: string;
pubkey: string; pubkey: string;
follows: string[]; follows: null | string[];
network: string[]; network: null | string[];
is_active: number; is_active: number;
privkey?: string; // deprecated privkey?: string; // deprecated
} }