converted full sql from onboarding flow to prisma
This commit is contained in:
+53
-44
@@ -2,12 +2,10 @@ import BaseLayout from '@layouts/base';
|
||||
|
||||
import { RelayContext } from '@components/relaysProvider';
|
||||
|
||||
import { activeAccountAtom } from '@stores/account';
|
||||
import { relaysAtom } from '@stores/relays';
|
||||
import { activeAccountAtom, activeAccountFollowsAtom, lastLoginAtom } from '@stores/account';
|
||||
|
||||
import { dateToUnix, hoursAgo } from '@utils/getDate';
|
||||
import { countTotalNotes, createCacheNote, getAllFollowsByID, getLastLoginTime } from '@utils/storage';
|
||||
import { pubkeyArray } from '@utils/transform';
|
||||
import { getParentID, pubkeyArray } from '@utils/transform';
|
||||
|
||||
import LumeSymbol from '@assets/icons/Lume';
|
||||
|
||||
@@ -30,64 +28,75 @@ export default function Page() {
|
||||
const [pool, relays]: any = useContext(RelayContext);
|
||||
|
||||
const activeAccount: any = useAtomValue(activeAccountAtom);
|
||||
const [done, setDone] = useState(false);
|
||||
const activeAccountFollows: any = useAtomValue(activeAccountFollowsAtom);
|
||||
const lastLogin: any = useAtomValue(lastLoginAtom);
|
||||
|
||||
const now = useRef(new Date());
|
||||
const unsubscribe = useRef(null);
|
||||
const timer = useRef(null);
|
||||
|
||||
const [eose, setEose] = useState(false);
|
||||
|
||||
const fetchData = useCallback(
|
||||
(since) => {
|
||||
getAllFollowsByID(activeAccount.id).then((follows) => {
|
||||
unsubscribe.current = pool.subscribe(
|
||||
[
|
||||
{
|
||||
kinds: [1],
|
||||
authors: pubkeyArray(follows),
|
||||
since: dateToUnix(since),
|
||||
until: dateToUnix(now.current),
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event) => {
|
||||
// insert event to local database
|
||||
createCacheNote(event);
|
||||
},
|
||||
undefined,
|
||||
() => {
|
||||
// wait for 8 seconds
|
||||
timer.current = setTimeout(() => setDone(true), 8000);
|
||||
},
|
||||
async (since) => {
|
||||
const { createNote } = await import('@utils/bindings');
|
||||
unsubscribe.current = pool.subscribe(
|
||||
[
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
}
|
||||
);
|
||||
});
|
||||
kinds: [1],
|
||||
authors: pubkeyArray(activeAccountFollows),
|
||||
since: dateToUnix(since),
|
||||
until: dateToUnix(now.current),
|
||||
},
|
||||
],
|
||||
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: 'aaa',
|
||||
account_id: activeAccount.id,
|
||||
}).catch(console.error);
|
||||
},
|
||||
undefined,
|
||||
() => {
|
||||
setEose(true);
|
||||
}
|
||||
);
|
||||
},
|
||||
[activeAccount.id, pool, relays]
|
||||
[activeAccount.id, activeAccountFollows, pool, relays]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!done) {
|
||||
countTotalNotes().then((count) => {
|
||||
if (count.total === 0) {
|
||||
fetchData(hoursAgo(24, now.current));
|
||||
const isNoteExist = useCallback(async () => {
|
||||
const { checkNote } = await import('@utils/bindings');
|
||||
checkNote()
|
||||
.then((res) => {
|
||||
if (res.length === 5) {
|
||||
const parseDate = new Date(lastLogin);
|
||||
fetchData(parseDate);
|
||||
} else {
|
||||
getLastLoginTime().then((time) => {
|
||||
const parseDate = new Date(time.setting_value);
|
||||
fetchData(parseDate);
|
||||
});
|
||||
fetchData(hoursAgo(24, now.current));
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(console.error);
|
||||
}, [fetchData, lastLogin]);
|
||||
|
||||
useEffect(() => {
|
||||
if (eose === false) {
|
||||
isNoteExist();
|
||||
} else {
|
||||
router.replace('/newsfeed/following');
|
||||
}
|
||||
|
||||
return () => {
|
||||
unsubscribe.current;
|
||||
clearTimeout(timer.current);
|
||||
};
|
||||
}, [activeAccount.id, done, pool, relays, router, fetchData]);
|
||||
}, [router, eose, isNoteExist]);
|
||||
|
||||
return (
|
||||
<div className="relative h-full overflow-hidden">
|
||||
|
||||
Reference in New Issue
Block a user