jitsi-roomsv2/frontend/src/hooks/useLocalUser.ts
open-schnick 1aa7914289
All checks were successful
continuous-integration/drone/push Build is passing
add prettier and reformat project
2023-04-10 23:36:24 +02:00

33 lines
929 B
TypeScript

import { useState } from 'react'
import { USER_COOKIE_NAME } from '../background/constants'
import { getCookie, setCookie } from '../background/cookies'
import { UserInfo } from '../components/jitsi/types'
function useLocalUser() {
const [userInfo, setUserInfoLocal] = useState<UserInfo>(() => getUserInfoFromCookie())
const setUserInfo = (newData: UserInfo) => {
storeUserInfoInCookie(newData)
setUserInfoLocal(newData)
}
return { userInfo, setUserInfo }
}
function getUserInfoFromCookie(): UserInfo {
let cookie = getCookie(USER_COOKIE_NAME)
console.log('[Rooms] getUserNameFromCookie', cookie)
if (cookie) return JSON.parse(cookie)
return {
displayName: 'Unknown traveller',
email: '',
}
}
function storeUserInfoInCookie(userInfo: UserInfo) {
console.log('[Rooms] storeUserInfoInCookie', userInfo)
setCookie(USER_COOKIE_NAME, JSON.stringify(userInfo))
}
export default useLocalUser