useRedirectAfterSignup

This commit is contained in:
mantikoros 2022-08-23 12:37:02 -05:00
parent 2e95ac449d
commit 50279bd864
3 changed files with 35 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import { createUser } from 'web/lib/firebase/api'
import { randomString } from 'common/util/random' import { randomString } from 'common/util/random'
import { identifyUser, setUserProperty } from 'web/lib/service/analytics' import { identifyUser, setUserProperty } from 'web/lib/service/analytics'
import { useStateCheckEquality } from 'web/hooks/use-state-check-equality' import { useStateCheckEquality } from 'web/hooks/use-state-check-equality'
import { handleRedirectAfterSignup } from 'web/hooks/use-redirect-after-signup'
// Either we haven't looked up the logged in user yet (undefined), or we know // Either we haven't looked up the logged in user yet (undefined), or we know
// the user is not logged in (null), or we know the user is logged in. // the user is not logged in (null), or we know the user is logged in.
@ -62,11 +63,13 @@ export function AuthProvider(props: {
// Note: Cap on localStorage size is ~5mb // Note: Cap on localStorage size is ~5mb
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(current)) localStorage.setItem(CACHED_USER_KEY, JSON.stringify(current))
setCachedReferralInfoForUser(current.user) setCachedReferralInfoForUser(current.user)
handleRedirectAfterSignup(current.user)
} else { } else {
// User logged out; reset to null // User logged out; reset to null
deleteTokenCookies() deleteTokenCookies()
setAuthUser(null) setAuthUser(null)
localStorage.removeItem(CACHED_USER_KEY) localStorage.removeItem(CACHED_USER_KEY)
handleRedirectAfterSignup(null)
} }
}) })
}, [setAuthUser]) }, [setAuthUser])

View File

@ -0,0 +1,30 @@
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
}
}

View File

@ -9,8 +9,10 @@ import { withTracking } from 'web/lib/service/analytics'
import { Row } from 'web/components/layout/row' import { Row } from 'web/components/layout/row'
import { Button } from 'web/components/button' import { Button } from 'web/components/button'
import { useTracking } from 'web/hooks/use-tracking' import { useTracking } from 'web/hooks/use-tracking'
import { useRedirectAfterSignup } from 'web/hooks/use-redirect-after-signup'
export default function TwitchLandingPage() { export default function TwitchLandingPage() {
useRedirectAfterSignup('twitch')
useSaveReferral() useSaveReferral()
useTracking('view twitch landing page') useTracking('view twitch landing page')