added event collector
This commit is contained in:
@@ -4,7 +4,7 @@ const AppActions = dynamic(() => import('@components/appHeader/actions'), {
|
||||
ssr: false,
|
||||
});
|
||||
|
||||
const NoteConnector = dynamic(() => import('@components/note/connector'), {
|
||||
const EventCollector = dynamic(() => import('@components/eventCollector'), {
|
||||
ssr: false,
|
||||
});
|
||||
|
||||
@@ -15,7 +15,7 @@ export default function AppHeader() {
|
||||
<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>
|
||||
<div>
|
||||
<NoteConnector />
|
||||
<EventCollector />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
import { ChatListItem } from '@components/chats/chatListItem';
|
||||
import { ChatModal } from '@components/chats/chatModal';
|
||||
import { ImageWithFallback } from '@components/imageWithFallback';
|
||||
import { RelayContext } from '@components/relaysProvider';
|
||||
|
||||
import { activeAccountAtom } from '@stores/account';
|
||||
import { DEFAULT_AVATAR } from '@stores/constants';
|
||||
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useContext, useEffect, useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export default function ChatList() {
|
||||
const [pool, relays]: any = useContext(RelayContext);
|
||||
const router = useRouter();
|
||||
|
||||
const activeAccount: any = useAtomValue(activeAccountAtom);
|
||||
const accountProfile = JSON.parse(activeAccount.metadata);
|
||||
|
||||
const [list, setList] = useState(new Set());
|
||||
const [list, setList] = useState([]);
|
||||
|
||||
const openSelfChat = () => {
|
||||
router.push({
|
||||
@@ -27,26 +25,15 @@ export default function ChatList() {
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
kinds: [4],
|
||||
'#p': [activeAccount.pubkey],
|
||||
since: 0,
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event: any) => {
|
||||
if (event.pubkey !== activeAccount.pubkey) {
|
||||
setList((list) => new Set(list).add(event.pubkey));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe;
|
||||
const fetchChats = async () => {
|
||||
const { getChats } = await import('@utils/bindings');
|
||||
return await getChats({ account_id: activeAccount.id });
|
||||
};
|
||||
}, [pool, relays, activeAccount.pubkey]);
|
||||
|
||||
fetchChats()
|
||||
.then((res) => setList(res))
|
||||
.catch(console.error);
|
||||
}, [activeAccount.id]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-px">
|
||||
@@ -68,8 +55,8 @@ export default function ChatList() {
|
||||
</h5>
|
||||
</div>
|
||||
</div>
|
||||
{[...list].map((item: string, index) => (
|
||||
<ChatListItem key={index} pubkey={item} />
|
||||
{list.map((item) => (
|
||||
<ChatListItem key={item.id} pubkey={item.pubkey} />
|
||||
))}
|
||||
<ChatModal />
|
||||
</div>
|
||||
|
||||
@@ -11,7 +11,7 @@ import { appWindow, getCurrent } from '@tauri-apps/api/window';
|
||||
import { useSetAtom } from 'jotai';
|
||||
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||
|
||||
export default function NoteConnector() {
|
||||
export default function EventCollector() {
|
||||
const [pool, relays]: any = useContext(RelayContext);
|
||||
|
||||
const setLastLoginAtom = useSetAtom(lastLoginAtom);
|
||||
@@ -24,6 +24,9 @@ export default function NoteConnector() {
|
||||
|
||||
const subscribe = useCallback(async () => {
|
||||
const { createNote } = await import('@utils/bindings');
|
||||
const { createChat } = await import('@utils/bindings');
|
||||
const { createChannel } = await import('@utils/bindings');
|
||||
|
||||
const activeAccount = JSON.parse(localStorage.getItem('activeAccount'));
|
||||
const follows = JSON.parse(localStorage.getItem('activeAccountFollows'));
|
||||
|
||||
@@ -34,29 +37,47 @@ export default function NoteConnector() {
|
||||
authors: pubkeyArray(follows),
|
||||
since: dateToUnix(now.current),
|
||||
},
|
||||
{
|
||||
kinds: [4],
|
||||
'#p': [activeAccount.pubkey],
|
||||
since: 0,
|
||||
},
|
||||
{
|
||||
kinds: [40],
|
||||
since: 0,
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event) => {
|
||||
const parentID = getParentID(event.tags, event.id);
|
||||
// insert event to local database
|
||||
createNote({
|
||||
event_id: event.id,
|
||||
pubkey: event.pubkey,
|
||||
kind: event.kind,
|
||||
tags: JSON.stringify(event.tags),
|
||||
content: event.content,
|
||||
parent_id: parentID,
|
||||
parent_comment_id: '',
|
||||
created_at: event.created_at,
|
||||
account_id: activeAccount.id,
|
||||
})
|
||||
.then(() =>
|
||||
// notify user reload to get newer note
|
||||
setHasNewerNote(true)
|
||||
)
|
||||
.catch(console.error);
|
||||
},
|
||||
10000
|
||||
if (event.kind === 1) {
|
||||
const parentID = getParentID(event.tags, event.id);
|
||||
// insert event to local database
|
||||
createNote({
|
||||
event_id: event.id,
|
||||
pubkey: event.pubkey,
|
||||
kind: event.kind,
|
||||
tags: JSON.stringify(event.tags),
|
||||
content: event.content,
|
||||
parent_id: parentID,
|
||||
parent_comment_id: '',
|
||||
created_at: event.created_at,
|
||||
account_id: activeAccount.id,
|
||||
})
|
||||
.then(() =>
|
||||
// notify user reload to get newer note
|
||||
setHasNewerNote(true)
|
||||
)
|
||||
.catch(console.error);
|
||||
} else if (event.kind === 4) {
|
||||
if (event.pubkey !== activeAccount.pubkey) {
|
||||
createChat({ pubkey: event.pubkey, created_at: event.created_at, account_id: activeAccount.id });
|
||||
}
|
||||
} else if (event.kind === 40) {
|
||||
createChannel({ event_id: event.id, content: event.content, account_id: activeAccount.id });
|
||||
} else {
|
||||
console.error;
|
||||
}
|
||||
}
|
||||
);
|
||||
}, [pool, relays, setHasNewerNote]);
|
||||
|
||||
Reference in New Issue
Block a user