manifold/web/hooks/use-redirect-after-signup.ts

31 lines
762 B
TypeScript
Raw Normal View History

2022-08-23 17:37:02 +00:00
import { User } from 'common/user'
import dayjs from 'dayjs'
import { useEffect } from 'react'
import { safeLocalStorage } from 'web/lib/util/local'
type page_redirects = 'twitch'
const key = 'redirect-after-signup'
export const useRedirectAfterSignup = (page: page_redirects) => {
useEffect(() => {
safeLocalStorage()?.setItem(key, page)
}, [page])
}
export const handleRedirectAfterSignup = (user: User | null) => {
const redirect = safeLocalStorage()?.getItem(key)
safeLocalStorage()?.removeItem(key)
if (!user || !redirect) return
const now = dayjs().utc()
const userCreatedTime = dayjs(user.createdTime)
if (now.diff(userCreatedTime, 'minute') > 5) return
if (redirect === 'twitch') {
// TODO: actual Twitch redirect
}
}