grungnet/src/contexts/UserIdContext.tsx
Mitchell McCaffrey ecfab87aa0 More typescript
2021-07-09 16:22:35 +10:00

37 lines
931 B
TypeScript

import React, { useEffect, useState, useContext } from "react";
import { useDatabase } from "./DatabaseContext";
const UserIdContext = React.createContext<string | undefined>(undefined);
export function UserIdProvider({ children }: { children: React.ReactNode }) {
const { database, databaseStatus } = useDatabase();
const [userId, setUserId] = useState();
useEffect(() => {
if (!database || databaseStatus === "loading") {
return;
}
async function loadUserId() {
if (database) {
const storedUserId = await database.table("user").get("userId");
if (storedUserId) {
setUserId(storedUserId.value);
}
}
}
loadUserId();
}, [database, databaseStatus]);
return (
<UserIdContext.Provider value={userId}>{children}</UserIdContext.Provider>
);
}
export function useUserId() {
return useContext(UserIdContext);
}
export default UserIdContext;