import React, { createContext, useEffect } from "react" import { AT, getUser, userObjIsEqual, } from "@lingdocs/lingdocs-main"; import { useStickyState, } from "@lingdocs/ps-react"; import { CronJob } from "cron"; import { postSavedResults } from "./lib/game-results"; const UserContext = createContext< { user: AT.LingdocsUser | undefined, setUser: React.Dispatch>, pullUser: () => void, } | undefined >(undefined); // TODO: persisting user in local state function UserProvider({ children }: any) { const [value, setValue] = useStickyState( undefined, "saved-user", ); function pullUser() { getUser().then((user) => { if (user === "offline") return; // don't update if there's nothing new - to avoid re-renders erasing game input etc if (!userObjIsEqual(user, value)) { setValue(user); } }).catch(console.error); } const checkUserCronJob = new CronJob("10 * * * *", () => { pullUser(); if (value) { postSavedResults(value.userId); } }); useEffect(() => { pullUser(); checkUserCronJob.start(); return () => { checkUserCronJob.stop(); } // eslint-disable-next-line }, []); return {children} ; } function useUser() { const context = React.useContext(UserContext) if (context === undefined) { throw new Error('useUser must be used within a UserProvider') } return context; } export { UserProvider, useUser };