import React, { createContext, Dispatch, SetStateAction, useState, useEffect, useCallback, ReactNode } from 'react' import axios from 'axios' export enum ModeType { Server = 'server', Desktop = 'desktop' } export enum RunTimeType { SAS = 'sas', JS = 'js', PY = 'py', R = 'r' } interface AppContextProps { checkingSession: boolean loggedIn: boolean setLoggedIn: Dispatch> | null needsToUpdatePassword: boolean setNeedsToUpdatePassword: Dispatch> | null userId: number setUserId: Dispatch> | null username: string setUsername: Dispatch> | null displayName: string setDisplayName: Dispatch> | null isAdmin: boolean setIsAdmin: Dispatch> | null mode: ModeType runTimes: RunTimeType[] logout: (() => void) | null } export const AppContext = createContext({ checkingSession: false, loggedIn: false, setLoggedIn: null, needsToUpdatePassword: false, setNeedsToUpdatePassword: null, userId: 0, setUserId: null, username: '', setUsername: null, displayName: '', setDisplayName: null, isAdmin: false, setIsAdmin: null, mode: ModeType.Server, runTimes: [], logout: null }) const AppContextProvider = (props: { children: ReactNode }) => { const { children } = props const [checkingSession, setCheckingSession] = useState(false) const [loggedIn, setLoggedIn] = useState(false) const [needsToUpdatePassword, setNeedsToUpdatePassword] = useState(false) const [userId, setUserId] = useState(0) const [username, setUsername] = useState('') const [displayName, setDisplayName] = useState('') const [isAdmin, setIsAdmin] = useState(false) const [mode, setMode] = useState(ModeType.Server) const [runTimes, setRunTimes] = useState([]) useEffect(() => { setCheckingSession(true) axios .get('/SASjsApi/session') .then((res) => res.data) .then((data: any) => { setCheckingSession(false) setUserId(data.id) setUsername(data.username) setDisplayName(data.displayName) setIsAdmin(data.isAdmin) setLoggedIn(true) setNeedsToUpdatePassword(data.needsToUpdatePassword) }) .catch(() => { setLoggedIn(false) // get CSRF TOKEN and set cookie axios .get('/') .then((res) => res.data) .then((data: string) => { const result = /