added init data page
This commit is contained in:
@@ -4,12 +4,14 @@ import { activeAccountAtom } from '@stores/account';
|
||||
import { hasNewerNoteAtom } from '@stores/note';
|
||||
import { relaysAtom } from '@stores/relays';
|
||||
|
||||
import { dateToUnix, hoursAgo } from '@utils/getDate';
|
||||
import { createCacheNote, getAllFollowsByID } from '@utils/storage';
|
||||
import { dateToUnix } from '@utils/getDate';
|
||||
import { createCacheNote, getAllFollowsByID, updateLastLoginTime } from '@utils/storage';
|
||||
import { pubkeyArray } from '@utils/transform';
|
||||
|
||||
import { window } from '@tauri-apps/api';
|
||||
import { TauriEvent } from '@tauri-apps/api/event';
|
||||
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { memo, useContext, useEffect, useRef, useState } from 'react';
|
||||
import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||
|
||||
export const NoteConnector = memo(function NoteConnector() {
|
||||
const pool: any = useContext(RelayContext);
|
||||
@@ -21,29 +23,34 @@ export const NoteConnector = memo(function NoteConnector() {
|
||||
const [isOnline] = useState(true);
|
||||
const now = useRef(new Date());
|
||||
|
||||
useEffect(() => {
|
||||
const subscribe = useCallback(() => {
|
||||
getAllFollowsByID(activeAccount.id).then((follows) => {
|
||||
pool.subscribe(
|
||||
[
|
||||
{
|
||||
kinds: [1],
|
||||
authors: pubkeyArray(follows),
|
||||
since: dateToUnix(hoursAgo(12, now.current)),
|
||||
since: dateToUnix(now.current),
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event: any) => {
|
||||
// insert event to local database
|
||||
createCacheNote(event);
|
||||
// ask user load newer note
|
||||
if (event.created_at > dateToUnix(now.current)) {
|
||||
setHasNewerNote(true);
|
||||
}
|
||||
setHasNewerNote(true);
|
||||
}
|
||||
);
|
||||
});
|
||||
}, [activeAccount.id, pool, relays, setHasNewerNote]);
|
||||
|
||||
useEffect(() => {
|
||||
subscribe();
|
||||
window.getCurrent().listen(TauriEvent.WINDOW_CLOSE_REQUESTED, () => {
|
||||
updateLastLoginTime(now.current);
|
||||
window.appWindow.close();
|
||||
});
|
||||
}, [activeAccount.id, pool, relays, setHasNewerNote, subscribe]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="inline-flex items-center gap-1 rounded-md py-1 px-1.5 hover:bg-zinc-900">
|
||||
|
||||
Reference in New Issue
Block a user