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

@@ -2,7 +2,7 @@ import { AvatarUploader } from '@components/avatarUploader';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { defaultChannelsAtom } from '@stores/channel'; import { defaultChannelsAtom } from '@stores/channel';
import { DEFAULT_AVATAR, FULL_RELAYS } from '@stores/constants'; import { DEFAULT_AVATAR, MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
import { createChannel } from '@utils/storage'; import { createChannel } from '@utils/storage';
@@ -17,7 +17,7 @@ import { useForm } from 'react-hook-form';
import { navigate } from 'vite-plugin-ssr/client/router'; import { navigate } from 'vite-plugin-ssr/client/router';
export const CreateChannelModal = () => { export const CreateChannelModal = () => {
const [pool]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [image, setImage] = useState(DEFAULT_AVATAR); const [image, setImage] = useState(DEFAULT_AVATAR);
@@ -47,7 +47,7 @@ export const CreateChannelModal = () => {
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish channel // publish channel
pool.publish(event, FULL_RELAYS); pool.publish(event, MESSAGE_RELAYS);
// insert to database // insert to database
createChannel(event.id, event.content, event.created_at); createChannel(event.id, event.content, event.created_at);
// update jotai state // update jotai state

View File

@@ -1,5 +1,7 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
import * as AlertDialog from '@radix-ui/react-alert-dialog'; import * as AlertDialog from '@radix-ui/react-alert-dialog';
@@ -10,7 +12,7 @@ import { getEventHash, signEvent } from 'nostr-tools';
import { useCallback, useContext } from 'react'; import { useCallback, useContext } from 'react';
export const HideMessageButton = ({ id }: { id: string }) => { export const HideMessageButton = ({ id }: { id: string }) => {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const hideMessage = useCallback(() => { const hideMessage = useCallback(() => {
@@ -25,8 +27,8 @@ export const HideMessageButton = ({ id }: { id: string }) => {
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish note // publish note
pool.publish(event, relays); pool.publish(event, MESSAGE_RELAYS);
}, [id, activeAccount.privkey, activeAccount.pubkey, pool, relays]); }, [id, activeAccount.privkey, activeAccount.pubkey, pool, MESSAGE_RELAYS]);
return ( return (
<AlertDialog.Root> <AlertDialog.Root>

View File

@@ -1,5 +1,7 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
import * as AlertDialog from '@radix-ui/react-alert-dialog'; import * as AlertDialog from '@radix-ui/react-alert-dialog';
@@ -7,13 +9,13 @@ import * as Tooltip from '@radix-ui/react-tooltip';
import useLocalStorage from '@rehooks/local-storage'; import useLocalStorage from '@rehooks/local-storage';
import { MicMute } from 'iconoir-react'; import { MicMute } from 'iconoir-react';
import { getEventHash, signEvent } from 'nostr-tools'; import { getEventHash, signEvent } from 'nostr-tools';
import { useCallback, useContext } from 'react'; import { useContext } from 'react';
export const MuteButton = ({ pubkey }: { pubkey: string }) => { export const MuteButton = ({ pubkey }: { pubkey: string }) => {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const muteUser = useCallback(() => { const muteUser = () => {
const event: any = { const event: any = {
content: '', content: '',
created_at: dateToUnix(), created_at: dateToUnix(),
@@ -25,8 +27,8 @@ export const MuteButton = ({ pubkey }: { pubkey: string }) => {
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish note // publish note
pool.publish(event, relays); pool.publish(event, MESSAGE_RELAYS);
}, [pubkey, activeAccount.privkey, activeAccount.pubkey, pool, relays]); };
return ( return (
<AlertDialog.Root> <AlertDialog.Root>

View File

@@ -1,6 +1,7 @@
import { NetworkStatusIndicator } from '@components/networkStatusIndicator'; import { NetworkStatusIndicator } from '@components/networkStatusIndicator';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { hasNewerNoteAtom } from '@stores/note'; import { hasNewerNoteAtom } from '@stores/note';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
@@ -12,7 +13,7 @@ import { useSetAtom } from 'jotai';
import { useCallback, useContext, useEffect, useRef } from 'react'; import { useCallback, useContext, useEffect, useRef } from 'react';
export default function EventCollector() { export default function EventCollector() {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', null); const [activeAccount]: any = useLocalStorage('account', null);
const setHasNewerNote = useSetAtom(hasNewerNoteAtom); const setHasNewerNote = useSetAtom(hasNewerNoteAtom);
@@ -42,7 +43,7 @@ export default function EventCollector() {
since: dateToUnix(now.current), since: dateToUnix(now.current),
}, },
], ],
relays, DEFAULT_RELAYS,
(event: { kind: number; tags: string[]; id: string; pubkey: string; content: string; created_at: number }) => { (event: { kind: number; tags: string[]; id: string; pubkey: string; content: string; created_at: number }) => {
switch (event.kind) { switch (event.kind) {
// metadata // metadata
@@ -103,7 +104,7 @@ export default function EventCollector() {
return () => { return () => {
unsubscribe(); unsubscribe();
}; };
}, [activeAccount.pubkey, activeAccount.id, follows, pool, relays, setHasNewerNote]); }, [activeAccount.pubkey, activeAccount.id, follows, pool, setHasNewerNote]);
useEffect(() => { useEffect(() => {
let ignore = false; let ignore = false;

View File

@@ -1,6 +1,7 @@
import { ImagePicker } from '@components/form/imagePicker'; import { ImagePicker } from '@components/form/imagePicker';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { noteContentAtom } from '@stores/note'; import { noteContentAtom } from '@stores/note';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
@@ -12,7 +13,7 @@ import { getEventHash, signEvent } from 'nostr-tools';
import { useContext } from 'react'; import { useContext } from 'react';
export default function FormBase() { export default function FormBase() {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [value, setValue] = useAtom(noteContentAtom); const [value, setValue] = useAtom(noteContentAtom);
const resetValue = useResetAtom(noteContentAtom); const resetValue = useResetAtom(noteContentAtom);
@@ -31,7 +32,7 @@ export default function FormBase() {
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish note // publish note
pool.publish(event, relays); pool.publish(event, DEFAULT_RELAYS);
// reset form // reset form
resetValue(); resetValue();
// send notification // send notification

View File

@@ -3,7 +3,7 @@ import { RelayContext } from '@components/relaysProvider';
import { UserMini } from '@components/user/mini'; import { UserMini } from '@components/user/mini';
import { channelContentAtom, channelReplyAtom } from '@stores/channel'; import { channelContentAtom, channelReplyAtom } from '@stores/channel';
import { FULL_RELAYS } from '@stores/constants'; import { MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
@@ -15,7 +15,7 @@ import { getEventHash, signEvent } from 'nostr-tools';
import { useCallback, useContext } from 'react'; import { useCallback, useContext } from 'react';
export const FormChannel = ({ eventId }: { eventId: string | string[] }) => { export const FormChannel = ({ eventId }: { eventId: string | string[] }) => {
const [pool]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [value, setValue] = useAtom(channelContentAtom); const [value, setValue] = useAtom(channelContentAtom);
@@ -48,7 +48,7 @@ export const FormChannel = ({ eventId }: { eventId: string | string[] }) => {
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish note // publish note
pool.publish(event, FULL_RELAYS); pool.publish(event, MESSAGE_RELAYS);
// reset state // reset state
resetValue(); resetValue();
// reset channel reply // reset channel reply

View File

@@ -2,7 +2,7 @@ import { ImagePicker } from '@components/form/imagePicker';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { chatContentAtom } from '@stores/chat'; import { chatContentAtom } from '@stores/chat';
import { FULL_RELAYS } from '@stores/constants'; import { MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
@@ -13,7 +13,7 @@ import { getEventHash, nip04, signEvent } from 'nostr-tools';
import { useCallback, useContext } from 'react'; import { useCallback, useContext } from 'react';
export default function FormChat({ receiverPubkey }: { receiverPubkey: string }) { export default function FormChat({ receiverPubkey }: { receiverPubkey: string }) {
const [pool]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [value, setValue] = useAtom(chatContentAtom); const [value, setValue] = useAtom(chatContentAtom);
@@ -39,7 +39,7 @@ export default function FormChat({ receiverPubkey }: { receiverPubkey: string })
event.id = getEventHash(event); event.id = getEventHash(event);
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish note // publish note
pool.publish(event, FULL_RELAYS); pool.publish(event, MESSAGE_RELAYS);
// reset state // reset state
resetValue(); resetValue();
}) })

View File

@@ -1,6 +1,7 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
import useLocalStorage from '@rehooks/local-storage'; import useLocalStorage from '@rehooks/local-storage';
@@ -8,7 +9,7 @@ import { getEventHash, signEvent } from 'nostr-tools';
import { useContext, useState } from 'react'; import { useContext, useState } from 'react';
export default function FormComment({ eventID }: { eventID: any }) { export default function FormComment({ eventID }: { eventID: any }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [value, setValue] = useState(''); const [value, setValue] = useState('');
@@ -27,7 +28,7 @@ export default function FormComment({ eventID }: { eventID: any }) {
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish note // publish note
pool.publish(event, relays); pool.publish(event, DEFAULT_RELAYS);
// send notification // send notification
// sendNotification('Comment has been published successfully'); // sendNotification('Comment has been published successfully');
}; };
@@ -37,12 +38,7 @@ export default function FormComment({ eventID }: { eventID: any }) {
<div className="flex gap-1"> <div className="flex gap-1">
<div> <div>
<div className="relative h-11 w-11 shrink-0 overflow-hidden rounded-md border border-white/10"> <div className="relative h-11 w-11 shrink-0 overflow-hidden rounded-md border border-white/10">
<ImageWithFallback <img src={profile?.picture} alt={activeAccount.pubkey} className="h-11 w-11 rounded-md object-cover" />
src={profile?.picture}
alt={activeAccount.pubkey}
fill={true}
className="rounded-md object-cover"
/>
</div> </div>
</div> </div>
<div className="relative h-24 w-full flex-1 overflow-hidden before:pointer-events-none before:absolute before:-inset-1 before:rounded-[11px] before:border before:border-fuchsia-500 before:opacity-0 before:ring-2 before:ring-fuchsia-500/20 before:transition after:pointer-events-none after:absolute after:inset-px after:rounded-[7px] after:shadow-highlight after:shadow-white/5 after:transition focus-within:before:opacity-100 focus-within:after:shadow-fuchsia-500/100 dark:focus-within:after:shadow-fuchsia-500/20"> <div className="relative h-24 w-full flex-1 overflow-hidden before:pointer-events-none before:absolute before:-inset-1 before:rounded-[11px] before:border before:border-fuchsia-500 before:opacity-0 before:ring-2 before:ring-fuchsia-500/20 before:transition after:pointer-events-none after:absolute after:inset-px after:rounded-[7px] after:shadow-highlight after:shadow-white/5 after:transition focus-within:before:opacity-100 focus-within:after:shadow-fuchsia-500/100 dark:focus-within:after:shadow-fuchsia-500/20">

View File

@@ -1,6 +1,8 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend'; import { UserExtend } from '@components/user/extend';
import { DEFAULT_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
import * as Dialog from '@radix-ui/react-dialog'; import * as Dialog from '@radix-ui/react-dialog';
@@ -23,7 +25,7 @@ export const NoteComment = ({
eventTime: number; eventTime: number;
eventContent: any; eventContent: any;
}) => { }) => {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [value, setValue] = useState(''); const [value, setValue] = useState('');
@@ -46,7 +48,7 @@ export const NoteComment = ({
event.id = getEventHash(event); event.id = getEventHash(event);
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
pool.publish(event, relays); pool.publish(event, DEFAULT_RELAYS);
setOpen(false); setOpen(false);
}; };

View File

@@ -1,5 +1,7 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate'; import { dateToUnix } from '@utils/getDate';
import useLocalStorage from '@rehooks/local-storage'; import useLocalStorage from '@rehooks/local-storage';
@@ -18,7 +20,7 @@ export const NoteReaction = ({
eventID: string; eventID: string;
eventPubkey: string; eventPubkey: string;
}) => { }) => {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [isReact, setIsReact] = useState(false); const [isReact, setIsReact] = useState(false);
@@ -41,7 +43,7 @@ export const NoteReaction = ({
event.id = getEventHash(event); event.id = getEventHash(event);
event.sig = signEvent(event, activeAccount.privkey); event.sig = signEvent(event, activeAccount.privkey);
// publish event to all relays // publish event to all relays
pool.publish(event, relays); pool.publish(event, DEFAULT_RELAYS);
// update state to change icon to filled heart // update state to change icon to filled heart
setIsReact(true); setIsReact(true);
// update counter // update counter

View File

@@ -2,6 +2,8 @@ import { NoteComment } from '@components/note/meta/comment';
import { NoteReaction } from '@components/note/meta/reaction'; import { NoteReaction } from '@components/note/meta/reaction';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import useLocalStorage from '@rehooks/local-storage'; import useLocalStorage from '@rehooks/local-storage';
import { memo, useContext, useEffect, useState } from 'react'; import { memo, useContext, useEffect, useState } from 'react';
@@ -16,7 +18,7 @@ export const NoteMetadata = memo(function NoteMetadata({
eventTime: any; eventTime: any;
eventContent: any; eventContent: any;
}) { }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [liked, setLiked] = useState(false); const [liked, setLiked] = useState(false);
@@ -32,7 +34,7 @@ export const NoteMetadata = memo(function NoteMetadata({
kinds: [1, 7], kinds: [1, 7],
}, },
], ],
relays, DEFAULT_RELAYS,
(event: any) => { (event: any) => {
switch (event.kind) { switch (event.kind) {
case 1: case 1:
@@ -64,7 +66,7 @@ export const NoteMetadata = memo(function NoteMetadata({
return () => { return () => {
unsubscribe(); unsubscribe();
}; };
}, [eventID, eventTime, pool, relays, activeAccount.pubkey]); }, [eventID, eventTime, pool, activeAccount.pubkey]);
return ( return (
<div className="relative z-10 -ml-1 flex items-center gap-8"> <div className="relative z-10 -ml-1 flex items-center gap-8">

View File

@@ -2,6 +2,8 @@ import { NoteMetadata } from '@components/note/metadata';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend'; import { UserExtend } from '@components/user/extend';
import { DEFAULT_RELAYS } from '@stores/constants';
import { contentParser } from '@utils/parser'; import { contentParser } from '@utils/parser';
import { createNote, getNoteByID } from '@utils/storage'; import { createNote, getNoteByID } from '@utils/storage';
import { getParentID } from '@utils/transform'; import { getParentID } from '@utils/transform';
@@ -10,7 +12,7 @@ import useLocalStorage from '@rehooks/local-storage';
import { memo, useCallback, useContext, useEffect, useState } from 'react'; import { memo, useCallback, useContext, useEffect, useState } from 'react';
export const NoteParent = memo(function NoteParent({ id }: { id: string }) { export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [event, setEvent] = useState(null); const [event, setEvent] = useState(null);
@@ -25,7 +27,7 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
kinds: [1], kinds: [1],
}, },
], ],
relays, DEFAULT_RELAYS,
(event: any) => { (event: any) => {
// update state // update state
setEvent(event); setEvent(event);
@@ -53,7 +55,7 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
return () => { return () => {
unsubscribe(); unsubscribe();
}; };
}, [activeAccount.id, id, pool, relays]); }, [activeAccount.id, id, pool]);
const checkNoteIsSaved = useCallback(async () => { const checkNoteIsSaved = useCallback(async () => {
getNoteByID(id) getNoteByID(id)

View File

@@ -1,6 +1,8 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend'; import { UserExtend } from '@components/user/extend';
import { DEFAULT_RELAYS } from '@stores/constants';
import { contentParser } from '@utils/parser'; import { contentParser } from '@utils/parser';
import { createNote, getNoteByID } from '@utils/storage'; import { createNote, getNoteByID } from '@utils/storage';
import { getParentID } from '@utils/transform'; import { getParentID } from '@utils/transform';
@@ -9,7 +11,7 @@ import useLocalStorage from '@rehooks/local-storage';
import { memo, useCallback, useContext, useEffect, useState } from 'react'; import { memo, useCallback, useContext, useEffect, useState } from 'react';
export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) { export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [activeAccount]: any = useLocalStorage('account', {}); const [activeAccount]: any = useLocalStorage('account', {});
const [event, setEvent] = useState(null); const [event, setEvent] = useState(null);
@@ -24,7 +26,7 @@ export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) {
kinds: [1], kinds: [1],
}, },
], ],
relays, DEFAULT_RELAYS,
(event: any) => { (event: any) => {
// update state // update state
setEvent(event); setEvent(event);
@@ -51,7 +53,7 @@ export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) {
return () => { return () => {
unsubscribe(); unsubscribe();
}; };
}, [activeAccount.id, id, pool, relays]); }, [activeAccount.id, id, pool]);
const checkNoteIsSaved = useCallback(async () => { const checkNoteIsSaved = useCallback(async () => {
getNoteByID(id) getNoteByID(id)

View File

@@ -2,13 +2,15 @@ import { NoteMetadata } from '@components/note/metadata';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend'; import { UserExtend } from '@components/user/extend';
import { DEFAULT_RELAYS } from '@stores/constants';
import { contentParser } from '@utils/parser'; import { contentParser } from '@utils/parser';
import { memo, useCallback, useContext, useEffect, useState } from 'react'; import { memo, useCallback, useContext, useEffect, useState } from 'react';
import { navigate } from 'vite-plugin-ssr/client/router'; import { navigate } from 'vite-plugin-ssr/client/router';
export const RootNote = memo(function RootNote({ event }: { event: any }) { export const RootNote = memo(function RootNote({ event }: { event: any }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [data, setData] = useState(null); const [data, setData] = useState(null);
const [content, setContent] = useState(''); const [content, setContent] = useState('');
@@ -36,7 +38,7 @@ export const RootNote = memo(function RootNote({ event }: { event: any }) {
kinds: [1], kinds: [1],
}, },
], ],
relays, DEFAULT_RELAYS,
(event: any) => { (event: any) => {
setData(event); setData(event);
setContent(contentParser(event.content, event.tags)); setContent(contentParser(event.content, event.tags));
@@ -52,7 +54,7 @@ export const RootNote = memo(function RootNote({ event }: { event: any }) {
unsubscribe(); unsubscribe();
}; };
}, },
[pool, relays] [pool]
); );
useEffect(() => { useEffect(() => {

View File

@@ -1,18 +1,20 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { UserFollow } from '@components/user/follow'; import { UserFollow } from '@components/user/follow';
import { DEFAULT_RELAYS } from '@stores/constants';
import destr from 'destr'; import destr from 'destr';
import { Author } from 'nostr-relaypool'; import { Author } from 'nostr-relaypool';
import { useContext, useEffect, useState } from 'react'; import { useContext, useEffect, useState } from 'react';
export default function ProfileFollowers({ id }: { id: string }) { export default function ProfileFollowers({ id }: { id: string }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [followers, setFollowers] = useState(null); const [followers, setFollowers] = useState(null);
useEffect(() => { useEffect(() => {
const user = new Author(pool, relays, id); const user = new Author(pool, DEFAULT_RELAYS, id);
user.followers((res) => setFollowers(destr(res.tags)), 0, 100); user.followers((res) => setFollowers(destr(res.tags)), 0, 100);
}, [id, pool, relays]); }, [id, pool]);
return ( return (
<div className="flex flex-col gap-3 px-3 py-5"> <div className="flex flex-col gap-3 px-3 py-5">

View File

@@ -1,17 +1,19 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { UserFollow } from '@components/user/follow'; import { UserFollow } from '@components/user/follow';
import { DEFAULT_RELAYS } from '@stores/constants';
import { Author } from 'nostr-relaypool'; import { Author } from 'nostr-relaypool';
import { useContext, useEffect, useState } from 'react'; import { useContext, useEffect, useState } from 'react';
export default function ProfileFollows({ id }: { id: string }) { export default function ProfileFollows({ id }: { id: string }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [follows, setFollows] = useState(null); const [follows, setFollows] = useState(null);
useEffect(() => { useEffect(() => {
const user = new Author(pool, relays, id); const user = new Author(pool, DEFAULT_RELAYS, id);
user.follows((res) => setFollows(res), 0); user.follows((res) => setFollows(res), 0);
}, [id, pool, relays]); }, [id, pool]);
return ( return (
<div className="flex flex-col gap-3 px-3 py-5"> <div className="flex flex-col gap-3 px-3 py-5">

View File

@@ -1,6 +1,6 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_AVATAR } from '@stores/constants'; import { DEFAULT_AVATAR, DEFAULT_RELAYS } from '@stores/constants';
import { shortenKey } from '@utils/shortenKey'; import { shortenKey } from '@utils/shortenKey';
@@ -11,13 +11,13 @@ import { useContext, useEffect, useState } from 'react';
const DEFAULT_BANNER = 'https://bafybeiacwit7hjmdefqggxqtgh6ht5dhth7ndptwn2msl5kpkodudsr7py.ipfs.w3s.link/banner-1.jpg'; const DEFAULT_BANNER = 'https://bafybeiacwit7hjmdefqggxqtgh6ht5dhth7ndptwn2msl5kpkodudsr7py.ipfs.w3s.link/banner-1.jpg';
export default function ProfileMetadata({ id }: { id: string }) { export default function ProfileMetadata({ id }: { id: string }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [profile, setProfile] = useState(null); const [profile, setProfile] = useState(null);
useEffect(() => { useEffect(() => {
const user = new Author(pool, relays, id); const user = new Author(pool, DEFAULT_RELAYS, id);
user.metaData((res) => setProfile(destr(res.content)), 0); user.metaData((res) => setProfile(destr(res.content)), 0);
}, [id, pool, relays]); }, [id, pool]);
return ( return (
<> <>

View File

@@ -1,17 +1,19 @@
import { NoteBase } from '@components/note/base'; import { NoteBase } from '@components/note/base';
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { Author } from 'nostr-relaypool'; import { Author } from 'nostr-relaypool';
import { useContext, useEffect, useState } from 'react'; import { useContext, useEffect, useState } from 'react';
export default function ProfileNotes({ id }: { id: string }) { export default function ProfileNotes({ id }: { id: string }) {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [data, setData] = useState([]); const [data, setData] = useState([]);
useEffect(() => { useEffect(() => {
const user = new Author(pool, relays, id); const user = new Author(pool, DEFAULT_RELAYS, id);
user.text((res) => setData((data) => [...data, res]), 100, 0); user.text((res) => setData((data) => [...data, res]), 100, 0);
}, [id, pool, relays]); }, [id, pool]);
return ( return (
<div className="flex flex-col"> <div className="flex flex-col">

View File

@@ -5,15 +5,13 @@ import { createContext, useMemo } from 'react';
export const RelayContext = createContext({}); export const RelayContext = createContext({});
const relays = DEFAULT_RELAYS;
export default function RelayProvider({ children }: { children: React.ReactNode }) { export default function RelayProvider({ children }: { children: React.ReactNode }) {
const pool = useMemo(() => { const pool = useMemo(() => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
return new RelayPool(relays, { useEventCache: false, logSubscriptions: false }); return new RelayPool(DEFAULT_RELAYS, { useEventCache: false, logSubscriptions: false });
} else { } else {
return null; return null;
} }
}, []); }, []);
return <RelayContext.Provider value={[pool, relays]}>{children}</RelayContext.Provider>; return <RelayContext.Provider value={pool}>{children}</RelayContext.Provider>;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -23,7 +23,7 @@ export const DEFAULT_CHANNELS = [
}, },
]; ];
export const DEFAULT_RELAYS = ['wss://welcome.nostr.wine', 'wss://relay.nostr.band', 'wss://nostr.mutinywallet.com']; export const DEFAULT_RELAYS = ['wss://welcome.nostr.wine', 'wss://relay.nostr.band', 'wss://nostr.mutinywallet.com'];
export const FULL_RELAYS = [ export const MESSAGE_RELAYS = [
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://nostr-pub.wellorder.net', 'wss://nostr-pub.wellorder.net',
'wss://nostr.zebedee.cloud', 'wss://nostr.zebedee.cloud',

View File

@@ -1,11 +1,13 @@
import { RelayContext } from '@components/relaysProvider'; import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { updateChannelMetadata } from '@utils/storage'; import { updateChannelMetadata } from '@utils/storage';
import { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { useCallback, useContext, useEffect, useRef, useState } from 'react';
export const useChannelMetadata = (id: string, fallback: string) => { export const useChannelMetadata = (id: string, fallback: string) => {
const [pool, relays]: any = useContext(RelayContext); const pool: any = useContext(RelayContext);
const [metadata, setMetadata] = useState(null); const [metadata, setMetadata] = useState(null);
const unsubscribe = useRef(null); const unsubscribe = useRef(null);
@@ -17,7 +19,7 @@ export const useChannelMetadata = (id: string, fallback: string) => {
'#e': [id], '#e': [id],
}, },
], ],
relays, DEFAULT_RELAYS,
(event: { content: string }) => { (event: { content: string }) => {
const json = JSON.parse(event.content); const json = JSON.parse(event.content);
// update state // update state
@@ -32,7 +34,7 @@ export const useChannelMetadata = (id: string, fallback: string) => {
logAllEvents: false, logAllEvents: false,
} }
); );
}, [id, pool, relays]); }, [id, pool]);
useEffect(() => { useEffect(() => {
let ignore = false; let ignore = false;