refactored eventcollector & index page
This commit is contained in:
@@ -3,11 +3,12 @@ import { RelayContext } from '@components/relaysProvider';
|
||||
import { hasNewerNoteAtom } from '@stores/note';
|
||||
|
||||
import { dateToUnix } from '@utils/getDate';
|
||||
import { fetchMetadata } from '@utils/metadata';
|
||||
import { getParentID, pubkeyArray } from '@utils/transform';
|
||||
|
||||
import useLocalStorage, { writeStorage } from '@rehooks/local-storage';
|
||||
import { window } from '@tauri-apps/api';
|
||||
import { TauriEvent } from '@tauri-apps/api/event';
|
||||
import { appWindow, getCurrent } from '@tauri-apps/api/window';
|
||||
import { useSetAtom } from 'jotai';
|
||||
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||
|
||||
@@ -22,6 +23,26 @@ export default function EventCollector() {
|
||||
|
||||
const now = useRef(new Date());
|
||||
const unsubscribe = useRef(null);
|
||||
const unlisten = useRef(null);
|
||||
|
||||
const createFollowingPlebs = useCallback(
|
||||
async (tags) => {
|
||||
const { createPleb } = await import('@utils/bindings');
|
||||
for (const tag of tags) {
|
||||
const pubkey = tag[1];
|
||||
const metadata: any = await fetchMetadata(pubkey);
|
||||
|
||||
createPleb({
|
||||
pleb_id: pubkey + '-lume' + activeAccount.id.toString(),
|
||||
pubkey: pubkey,
|
||||
kind: 0,
|
||||
metadata: metadata.content,
|
||||
account_id: activeAccount.id,
|
||||
}).catch(console.error);
|
||||
}
|
||||
},
|
||||
[activeAccount.id]
|
||||
);
|
||||
|
||||
const subscribe = useCallback(async () => {
|
||||
const { createNote } = await import('@utils/bindings');
|
||||
@@ -35,14 +56,18 @@ export default function EventCollector() {
|
||||
authors: pubkeyArray(follows),
|
||||
since: dateToUnix(now.current),
|
||||
},
|
||||
{
|
||||
kinds: [3],
|
||||
authors: [activeAccount.pubkey],
|
||||
},
|
||||
{
|
||||
kinds: [4],
|
||||
'#p': [activeAccount.pubkey],
|
||||
since: 0,
|
||||
since: dateToUnix(now.current),
|
||||
},
|
||||
{
|
||||
kinds: [40],
|
||||
since: 0,
|
||||
since: dateToUnix(now.current),
|
||||
},
|
||||
],
|
||||
relays,
|
||||
@@ -66,30 +91,43 @@ export default function EventCollector() {
|
||||
setHasNewerNote(true)
|
||||
)
|
||||
.catch(console.error);
|
||||
} else if (event.kind === 3) {
|
||||
createFollowingPlebs(event.tags);
|
||||
} else if (event.kind === 4) {
|
||||
if (event.pubkey !== activeAccount.pubkey) {
|
||||
createChat({ pubkey: event.pubkey, created_at: event.created_at, account_id: activeAccount.id });
|
||||
createChat({ pubkey: event.pubkey, created_at: event.created_at, account_id: activeAccount.id }).catch(
|
||||
console.error
|
||||
);
|
||||
}
|
||||
} else if (event.kind === 40) {
|
||||
createChannel({ event_id: event.id, content: event.content, account_id: activeAccount.id });
|
||||
createChannel({ event_id: event.id, content: event.content, account_id: activeAccount.id }).catch(
|
||||
console.error
|
||||
);
|
||||
} else {
|
||||
console.error;
|
||||
}
|
||||
}
|
||||
);
|
||||
}, [activeAccount.id, activeAccount.pubkey, follows, pool, relays, setHasNewerNote]);
|
||||
}, [pool, relays, activeAccount.id, activeAccount.pubkey, follows, setHasNewerNote, createFollowingPlebs]);
|
||||
|
||||
const listenWindowClose = useCallback(async () => {
|
||||
unlisten.current = window.getCurrent().listen(TauriEvent.WINDOW_CLOSE_REQUESTED, () => {
|
||||
writeStorage('lastLogin', now.current);
|
||||
window.getCurrent().close();
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
subscribe();
|
||||
getCurrent().listen(TauriEvent.WINDOW_CLOSE_REQUESTED, () => {
|
||||
writeStorage('lastLogin', now.current);
|
||||
appWindow.close();
|
||||
});
|
||||
listenWindowClose();
|
||||
|
||||
return () => {
|
||||
unsubscribe.current;
|
||||
if (unsubscribe.current) {
|
||||
unsubscribe.current();
|
||||
}
|
||||
unlisten.current();
|
||||
};
|
||||
}, [setHasNewerNote, subscribe]);
|
||||
}, [setHasNewerNote, subscribe, listenWindowClose]);
|
||||
|
||||
return (
|
||||
<div className="inline-flex items-center gap-1 rounded-md px-1.5 py-1 hover:bg-zinc-900">
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
import { RelayContext } from '@components/relaysProvider';
|
||||
|
||||
import { DEFAULT_AVATAR } from '@stores/constants';
|
||||
|
||||
import { fetchMetadata } from '@utils/metadata';
|
||||
|
||||
import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
|
||||
import { AvatarIcon, ExitIcon, GearIcon } from '@radix-ui/react-icons';
|
||||
import { writeText } from '@tauri-apps/api/clipboard';
|
||||
import Image from 'next/image';
|
||||
import { useRouter } from 'next/router';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import { memo, useCallback, useContext, useEffect } from 'react';
|
||||
|
||||
export const ActiveAccount = memo(function ActiveAccount({ user }: { user: any }) {
|
||||
const [pool, relays]: any = useContext(RelayContext);
|
||||
|
||||
export const ActiveAccount = ({ user }: { user: any }) => {
|
||||
const router = useRouter();
|
||||
const userData = JSON.parse(user.metadata);
|
||||
|
||||
@@ -26,50 +19,6 @@ export const ActiveAccount = memo(function ActiveAccount({ user }: { user: any }
|
||||
await writeText(nip19.npubEncode(user.pubkey));
|
||||
};
|
||||
|
||||
const insertFollowsToStorage = useCallback(
|
||||
async (tags) => {
|
||||
const { createPleb } = await import('@utils/bindings');
|
||||
|
||||
for (const tag of tags) {
|
||||
const metadata: any = await fetchMetadata(tag[1]);
|
||||
createPleb({
|
||||
pleb_id: tag[1] + '-lume' + user.id.toString(),
|
||||
pubkey: tag[1],
|
||||
kind: 0,
|
||||
metadata: metadata.content,
|
||||
account_id: user.id,
|
||||
}).catch(console.error);
|
||||
}
|
||||
},
|
||||
[user.id]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
kinds: [3],
|
||||
authors: [user.pubkey],
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event: any) => {
|
||||
if (event.tags.length > 0) {
|
||||
//insertFollowsToStorage(event.tags);
|
||||
}
|
||||
},
|
||||
20000,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe;
|
||||
};
|
||||
}, [insertFollowsToStorage, pool, relays, user.pubkey]);
|
||||
|
||||
return (
|
||||
<DropdownMenu.Root>
|
||||
<DropdownMenu.Trigger asChild>
|
||||
@@ -125,4 +74,4 @@ export const ActiveAccount = memo(function ActiveAccount({ user }: { user: any }
|
||||
</DropdownMenu.Portal>
|
||||
</DropdownMenu.Root>
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -52,7 +52,7 @@ export default function NoteMetadata({
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe;
|
||||
unsubscribe();
|
||||
};
|
||||
}, [eventID, eventTime, pool, relays]);
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
|
||||
account_id: activeAccount.id,
|
||||
}).catch(console.error);
|
||||
},
|
||||
undefined,
|
||||
100,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
@@ -75,7 +75,9 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
|
||||
checkNoteExist();
|
||||
|
||||
return () => {
|
||||
unsubscribe.current;
|
||||
if (unsubscribe.current) {
|
||||
unsubscribe.current();
|
||||
}
|
||||
};
|
||||
}, [checkNoteExist]);
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ export const NoteRepost = memo(function NoteRepost({ id }: { id: string }) {
|
||||
account_id: activeAccount.id,
|
||||
}).catch(console.error);
|
||||
},
|
||||
undefined,
|
||||
100,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
@@ -71,7 +71,9 @@ export const NoteRepost = memo(function NoteRepost({ id }: { id: string }) {
|
||||
checkNoteExist();
|
||||
|
||||
return () => {
|
||||
unsubscribe.current;
|
||||
if (unsubscribe.current) {
|
||||
unsubscribe.current();
|
||||
}
|
||||
};
|
||||
}, [checkNoteExist]);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ export const RelayContext = createContext({});
|
||||
const relays = [
|
||||
'wss://relay.damus.io',
|
||||
'wss://nostr-pub.wellorder.net',
|
||||
//'wss://nostr.bongbong.com',
|
||||
'wss://nostr.bongbong.com',
|
||||
'wss://nostr.zebedee.cloud',
|
||||
'wss://nostr.fmt.wiz.biz',
|
||||
'wss://relay.snort.social',
|
||||
|
||||
Reference in New Issue
Block a user