move to public repo

This commit is contained in:
Nathaniel Tampus
2023-01-03 15:47:30 +08:00
commit 17ce3cf4c4
58 changed files with 2430 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import { PropsWithChildren, useEffect, useState } from "react";
import type { User } from "@types";
import { SocketContext, socket } from "./socket";
import { SessionContext } from "./session";
import { fetchSession } from "../utils/auth";
const ContextProvider = (props: PropsWithChildren) => {
const [user, setUser] = useState<User>({});
async function getSession() {
const user = await fetchSession();
if (user) {
setUser(user);
}
}
useEffect(() => {
getSession();
}, []);
return (
<SocketContext.Provider value={socket}>
<SessionContext.Provider value={{ user, setUser }}>{props.children}</SessionContext.Provider>
</SocketContext.Provider>
);
};
export default ContextProvider;

View File

@@ -0,0 +1,7 @@
import { User } from "@types";
import { createContext, Dispatch, SetStateAction } from "react";
export const SessionContext = createContext<{
user: User;
setUser: Dispatch<SetStateAction<User>>;
} | null>(null);

View File

@@ -0,0 +1,18 @@
import { createContext } from "react";
import { io, Socket } from "socket.io-client";
import { apiUrl } from "../config/config";
export const socket: Socket = io(apiUrl, {
withCredentials: true,
autoConnect: false
});
socket.on("connect", () => {
console.log("socket connected");
});
socket.on("disconnect", () => {
console.log("socket disconnected");
});
export const SocketContext = createContext<Socket | null>(null);