reuse relayprovider

This commit is contained in:
Ren Amamiya
2023-04-29 10:25:40 +07:00
parent be8b40c86d
commit 3f06193c59
27 changed files with 151 additions and 156 deletions

View File

@@ -1,14 +0,0 @@
import { createContext } from 'react';
export const AccountContext = createContext({});
let activeAccount: any = { id: '', pubkey: '', follows: null, metadata: {} };
if (typeof window !== 'undefined') {
const { getActiveAccount } = await import('@lume/utils/storage');
activeAccount = await getActiveAccount();
}
export default function AccountProvider({ children }: { children: React.ReactNode }) {
return <AccountContext.Provider value={activeAccount}>{children}</AccountContext.Provider>;
}

View File

@@ -1,5 +1,3 @@
import EventCollector from '@lume/shared/eventCollector';
import { ArrowLeft, ArrowRight, Refresh } from 'iconoir-react';
let platformName = 'darwin';
@@ -9,7 +7,7 @@ if (typeof window !== 'undefined') {
platformName = await platform();
}
export default function AppHeader({ collector }: { collector: boolean }) {
export default function AppHeader() {
const goBack = () => {
window.history.back();
};
@@ -46,7 +44,6 @@ export default function AppHeader({ collector }: { collector: boolean }) {
</div>
<div data-tauri-drag-region className="flex h-full w-full items-center justify-between">
<div className="flex h-full items-center divide-x divide-zinc-900 px-4 pt-px"></div>
{collector && <EventCollector />}
</div>
</div>
);

View File

@@ -1,4 +1,5 @@
import { NetworkStatusIndicator } from '@lume/shared/networkStatusIndicator';
import { RelayContext } from '@lume/shared/relayProvider';
import { READONLY_RELAYS } from '@lume/stores/constants';
import { hasNewerNoteAtom } from '@lume/stores/note';
import { dateToUnix } from '@lume/utils/getDate';
@@ -7,20 +8,19 @@ import { createChat, createNote, updateAccount } from '@lume/utils/storage';
import { getParentID, nip02ToArray } from '@lume/utils/transform';
import { useSetAtom } from 'jotai';
import { RelayPool } from 'nostr-relaypool';
import { useRef } from 'react';
import { useContext, useRef } from 'react';
import useSWRSubscription from 'swr/subscription';
export default function EventCollector() {
const pool: any = useContext(RelayContext);
const setHasNewerNote = useSetAtom(hasNewerNoteAtom);
const now = useRef(new Date());
const { account, isLoading, isError } = useActiveAccount();
useSWRSubscription(!isLoading && !isError ? account : null, () => {
const follows = nip02ToArray(JSON.parse(account.follows));
const pool = new RelayPool(READONLY_RELAYS);
useSWRSubscription(!isLoading && !isError && account ? ['eventCollector', account] : null, ([, key], {}) => {
const follows = nip02ToArray(JSON.parse(key.follows));
const unsubscribe = pool.subscribe(
[
{
@@ -30,11 +30,11 @@ export default function EventCollector() {
},
{
kinds: [0, 3],
authors: [account.pubkey],
authors: [key.pubkey],
},
{
kinds: [4],
'#p': [account.pubkey],
'#p': [key.pubkey],
since: dateToUnix(now.current),
},
],
@@ -68,15 +68,15 @@ export default function EventCollector() {
break;
// chat
case 4:
if (event.pubkey !== account.pubkey) {
createChat(account.id, event.pubkey, event.created_at);
if (event.pubkey !== key.pubkey) {
createChat(key.id, event.pubkey, event.created_at);
}
break;
// repost
case 6:
createNote(
event.id,
account.id,
key.id,
event.pubkey,
event.kind,
event.tags,

View File

@@ -1,20 +1,19 @@
import { RelayContext } from '@lume/shared/relayProvider';
import { WRITEONLY_RELAYS } from '@lume/stores/constants';
import { dateToUnix } from '@lume/utils/getDate';
import { useActiveAccount } from '@lume/utils/hooks/useActiveAccount';
import { RelayPool } from 'nostr-relaypool';
import { getEventHash, signEvent } from 'nostr-tools';
import { useState } from 'react';
import { useContext, useState } from 'react';
export default function FormComment({ eventID }: { eventID: any }) {
const pool: any = useContext(RelayContext);
const { account } = useActiveAccount();
const [value, setValue] = useState('');
const profile = JSON.parse(account.metadata);
const submitEvent = () => {
const pool = new RelayPool(WRITEONLY_RELAYS);
const event: any = {
content: value,
created_at: dateToUnix(),

View File

@@ -0,0 +1,17 @@
import { FULL_RELAYS } from '@lume/stores/constants';
import { RelayPool } from 'nostr-relaypool';
import { createContext } from 'react';
export const RelayContext = createContext({});
const pool = new RelayPool(FULL_RELAYS, {
useEventCache: false,
subscriptionCache: true,
logErrorsAndNotices: false,
logSubscriptions: false,
});
export default function RelayProvider({ children }: { children: React.ReactNode }) {
return <RelayContext.Provider value={pool}>{children}</RelayContext.Provider>;
}