refactor relay provider

This commit is contained in:
Ren Amamiya
2023-04-24 08:17:26 +07:00
parent f671521356
commit d52410b857
27 changed files with 113 additions and 87 deletions

View File

@@ -4,7 +4,7 @@ import NewsfeedLayout from '@components/layouts/newsfeed';
import { RelayContext } from '@components/relaysProvider';
import { channelMessagesAtom, channelReplyAtom } from '@stores/channel';
import { FULL_RELAYS } from '@stores/constants';
import { MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix, hoursAgo } from '@utils/getDate';
import { usePageContext } from '@utils/hooks/usePageContext';
@@ -21,7 +21,7 @@ export function Page() {
const id = searchParams.id;
const [pool]: any = useContext(RelayContext);
const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {});
const setChannelMessages = useSetAtom(channelMessagesAtom);
@@ -46,7 +46,7 @@ export function Page() {
since: dateToUnix(hoursAgo(48, now.current)),
},
],
FULL_RELAYS,
MESSAGE_RELAYS,
(event: { kind: number; tags: string[][]; pubkey: string; id: string }) => {
if (event.kind === 44) {
muted.current = muted.current.add(event.tags[0][1]);

View File

@@ -4,7 +4,7 @@ import NewsfeedLayout from '@components/layouts/newsfeed';
import { RelayContext } from '@components/relaysProvider';
import { chatMessagesAtom } from '@stores/chat';
import { FULL_RELAYS } from '@stores/constants';
import { MESSAGE_RELAYS } from '@stores/constants';
import { usePageContext } from '@utils/hooks/usePageContext';
@@ -20,7 +20,7 @@ export function Page() {
const pubkey = searchParams.pubkey;
const [pool]: any = useContext(RelayContext);
const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {});
const setChatMessages = useSetAtom(chatMessagesAtom);
@@ -40,7 +40,7 @@ export function Page() {
'#p': [pubkey],
},
],
FULL_RELAYS,
MESSAGE_RELAYS,
(event: any) => {
setChatMessages((prev) => [...prev, event]);
}

View File

@@ -1,5 +1,7 @@
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { dateToUnix, hoursAgo } from '@utils/getDate';
import {
countTotalNotes,
@@ -20,7 +22,7 @@ import { useCallback, useContext, useEffect, useRef } from 'react';
import { navigate } from 'vite-plugin-ssr/client/router';
export function Page() {
const [pool, relays]: any = useContext(RelayContext);
const pool: any = useContext(RelayContext);
const now = useRef(new Date());
const timeout = useRef(null);
@@ -74,7 +76,7 @@ export function Page() {
// subscribe relays
const unsubscribe = pool.subscribe(
query,
relays,
DEFAULT_RELAYS,
(event: { kind: number; tags: string[]; id: string; pubkey: string; content: string; created_at: number }) => {
switch (event.kind) {
// short text note
@@ -136,7 +138,7 @@ export function Page() {
unsubscribe();
};
},
[pool, relays]
[pool]
);
useEffect(() => {

View File

@@ -1,6 +1,8 @@
import OnboardingLayout from '@components/layouts/onboarding';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { createAccount } from '@utils/storage';
import { EyeClose, EyeEmpty } from 'iconoir-react';
@@ -9,7 +11,7 @@ import { useCallback, useContext, useMemo, useState } from 'react';
import { navigate } from 'vite-plugin-ssr/client/router';
export function Page() {
const [pool, relays]: any = useContext(RelayContext);
const pool: any = useContext(RelayContext);
const [type, setType] = useState('password');
const [loading, setLoading] = useState(false);
@@ -58,10 +60,10 @@ export function Page() {
// insert to database
createAccount(pubkey, privkey, metadata);
// broadcast
pool.publish(event, relays);
pool.publish(event, DEFAULT_RELAYS);
// redirect to next step
navigate(`/onboarding/create/step-2?pubkey=${pubkey}&privkey=${privkey}`, { overwriteLastHistoryEntry: true });
}, [pool, pubkey, privkey, metadata, relays]);
}, [pool, pubkey, privkey, metadata]);
return (
<OnboardingLayout>

View File

@@ -2,6 +2,8 @@ import OnboardingLayout from '@components/layouts/onboarding';
import { RelayContext } from '@components/relaysProvider';
import { UserBase } from '@components/user/base';
import { DEFAULT_RELAYS } from '@stores/constants';
import { usePageContext } from '@utils/hooks/usePageContext';
import { fetchProfileMetadata } from '@utils/hooks/useProfileMetadata';
import { createPleb, updateAccount } from '@utils/storage';
@@ -60,7 +62,7 @@ export function Page() {
const pubkey = searchParams.pubkey;
const privkey = searchParams.privkey;
const [pool, relays]: any = useContext(RelayContext);
const pool: any = useContext(RelayContext);
const [loading, setLoading] = useState(false);
const [list, setList]: any = useState(initialList);
const [follows, setFollows] = useState([]);
@@ -98,10 +100,10 @@ export function Page() {
event.id = getEventHash(event);
event.sig = signEvent(event, privkey);
// broadcast
pool.publish(event, relays);
pool.publish(event, DEFAULT_RELAYS);
// redirect to splashscreen
navigate('/');
}, [pubkey, privkey, follows, pool, relays]);
}, [pubkey, privkey, follows, pool]);
useEffect(() => {
let ignore = false;

View File

@@ -1,7 +1,7 @@
import OnboardingLayout from '@components/layouts/onboarding';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants';
import { DEFAULT_AVATAR, DEFAULT_RELAYS } from '@stores/constants';
import { usePageContext } from '@utils/hooks/usePageContext';
import { fetchProfileMetadata } from '@utils/hooks/useProfileMetadata';
@@ -20,7 +20,7 @@ export function Page() {
const privkey = searchParams.privkey;
const pubkey = useMemo(() => getPublicKey(privkey), [privkey]);
const [pool, relays]: any = useContext(RelayContext);
const pool: any = useContext(RelayContext);
const [profile, setProfile] = useState({ metadata: null });
const [done, setDone] = useState(false);
@@ -53,7 +53,7 @@ export function Page() {
authors: [pubkey],
},
],
relays,
DEFAULT_RELAYS,
(event: any) => {
switch (event.kind) {
case 0:
@@ -85,7 +85,7 @@ export function Page() {
unsubscribe();
clearTimeout(timeout.current);
};
}, [pool, relays, pubkey, privkey]);
}, [pool, pubkey, privkey]);
return (
<OnboardingLayout>