import { useQueryClient } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { useStorage } from '@libs/storage/provider'; import { useStronghold } from '@stores/stronghold'; import { useNostr } from '@utils/hooks/useNostr'; export function EventLoader({ firstTime }: { firstTime: boolean }) { const { db } = useStorage(); const { getAllEventsSinceLastLogin } = useNostr(); const setIsFetched = useStronghold((state) => state.setIsFetched); const queryClient = useQueryClient(); const [progress, setProgress] = useState(0); useEffect(() => { async function getEvents() { const events = await getAllEventsSinceLastLogin(); console.log('total new events has found: ', events.data.length); const promises = await Promise.all( events.data.map(async (event) => await db.createEvent(event)) ); if (promises) { setProgress(100); setIsFetched(); // invalidate queries queryClient.invalidateQueries(['local-network-widget']); } } // only start download if progress === 0 if (progress === 0) getEvents(); // auto increase progress after 2 secs setInterval(() => setProgress((prev) => (prev += 5)), 2000); }, []); return (
{firstTime ? (
👋

Hello, this is the first time you're using Lume

Lume is downloading all events since the last 24 hours. It will auto refresh when it done, please be patient

) : (

Downloading all events while you're away...

)}
); }