'use client'; import { RelayContext } from '@components/relaysProvider'; import { dateToUnix, hoursAgo } from '@utils/getDate'; import { countTotalChannels, countTotalNotes, createChannel, createChat, createNote, getActiveAccount, getLastLogin, getPlebs, updateLastLogin, } from '@utils/storage'; import { getParentID } from '@utils/transform'; import LumeSymbol from '@assets/icons/Lume'; import { writeStorage } from '@rehooks/local-storage'; import { useRouter } from 'next/navigation'; import { useCallback, useContext, useEffect, useRef } from 'react'; export default function Page() { const router = useRouter(); const [pool, relays]: any = useContext(RelayContext); const now = useRef(new Date()); const timeout = useRef(null); const unsubscribe = useRef(null); const fetchData = useCallback( async (account: { id: number; pubkey: string; chats: string[] }, tags: any) => { const lastLogin = await getLastLogin(); const notes = await countTotalNotes(); const channels = await countTotalChannels(); const chats = account.chats?.length || 0; const follows = JSON.parse(tags); const query = []; let since: number; // kind 1 (notes) query if (notes.total === 0) { since = dateToUnix(hoursAgo(24, now.current)); } else { if (parseInt(lastLogin) > 0) { since = parseInt(lastLogin); } else { since = dateToUnix(hoursAgo(24, now.current)); } } query.push({ kinds: [1, 6], authors: follows, since: since, until: dateToUnix(now.current), }); // kind 4 (chats) query if (chats === 0) { query.push({ kinds: [4], '#p': [account.pubkey], since: 0, until: dateToUnix(now.current), }); } // kind 40 (channels) query if (channels.total === 0) { query.push({ kinds: [40], since: 0, until: dateToUnix(now.current), }); } // subscribe relays unsubscribe.current = pool.subscribe( query, relays, (event: { kind: number; tags: string[]; id: string; pubkey: string; content: string; created_at: number }) => { switch (event.kind) { // short text note case 1: const parentID = getParentID(event.tags, event.id); // insert event to local database createNote( event.id, account.id, event.pubkey, event.kind, event.tags, event.content, event.created_at, parentID ); break; // chat case 4: if (event.pubkey !== account.pubkey) { createChat(account.id, event.pubkey, event.created_at); } break; // repost case 6: createNote( event.id, account.id, event.pubkey, event.kind, event.tags, event.content, event.created_at, '' ); break; // channel case 40: createChannel(event.id, event.content, event.created_at); break; default: break; } }, undefined, () => { updateLastLogin(dateToUnix(now.current)); //timeout.current = setTimeout(() => router.replace('/nostr/newsfeed/following'), 5000); }, { unsubscribeOnEose: true, logAllEvents: false, } ); }, [router, pool, relays] ); useEffect(() => { getPlebs() .then((res) => { if (res) { writeStorage('plebs', res); } }) .catch(console.error); getActiveAccount() .then((res: any) => { if (res) { const account = res; // update local storage writeStorage('account', account); // fetch data fetchData(account, account.follows); } else { router.replace('/onboarding'); } }) .catch(console.error); return () => { if (unsubscribe.current) { unsubscribe.current(); } clearTimeout(timeout.current); }; }, [fetchData, router]); return (
Bitcoin and Nostr can be used by anyone, and no one can stop you!