Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 14x 1x 1x 1x 13x 13x 13x 14x 1x 1x 1x 12x 14x 14x 14x 14x 14x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 10x 10x 4x 3x 14x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 14x 5x 5x 5x 5x 5x 5x 3x 3x 14x 24x 24x 24x 24x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 24x 24x 24x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 8x 8x 24x 24x 24x 24x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 24x 24x 24x 24x | 'use client'
import Link from 'next/link'
import { useCallback, useEffect, useState } from 'react'
import { useTheme } from '@/contexts/ThemeContext'
import { useTier } from '@/hooks/useTier'
import { css } from '../../styled-system/css'
const STORAGE_KEY = 'guest-session-count'
const DISMISSED_KEY = 'guest-banner-dismissed'
const LAST_VISIT_KEY = 'guest-last-visit'
interface GuestMessageOptions {
/** Current session accuracy (0-1) */
sessionAccuracy?: number | null
/** Previous session accuracy (0-1) */
previousAccuracy?: number | null
}
/**
* Determine which guest progress message to show.
*
* Returns null if the user is not a guest, the banner was dismissed,
* or there's no compelling reason to show it yet.
*
* Priority order: returning after 24h+ > accuracy gain > 3+ sessions.
*/
function useGuestMessage(options: GuestMessageOptions = {}): {
message: string
cta: string
} | null {
const { sessionAccuracy, previousAccuracy } = options
const { tier } = useTier()
const [result, setResult] = useState<{ message: string; cta: string } | null>(null)
useEffect(() => {
if (tier !== 'guest') {
setResult(null)
return
}
try {
const dismissed = localStorage.getItem(DISMISSED_KEY)
if (dismissed) {
setResult(null)
return
}
const count = parseInt(localStorage.getItem(STORAGE_KEY) ?? '0', 10)
const lastVisit = localStorage.getItem(LAST_VISIT_KEY)
const now = Date.now()
// Update last visit
localStorage.setItem(LAST_VISIT_KEY, now.toString())
// Returning after 24h+ with sessions
if (lastVisit && count > 0) {
const hoursSince = (now - parseInt(lastVisit, 10)) / (1000 * 60 * 60)
if (hoursSince >= 24) {
setResult({
message: `Welcome back! You have ${count} session${count === 1 ? '' : 's'} of progress stored locally.`,
cta: 'Create a free account to keep it safe',
})
return
}
}
// Accuracy improvement >= 5 percentage points
if (
sessionAccuracy != null &&
previousAccuracy != null &&
sessionAccuracy - previousAccuracy >= 0.05
) {
const prevPct = Math.round(previousAccuracy * 100)
const curPct = Math.round(sessionAccuracy * 100)
setResult({
message: `Great session! Your accuracy jumped from ${prevPct}% to ${curPct}%.`,
cta: 'Create a free account to track your progress',
})
return
}
// After 3+ sessions
if (count >= 3) {
setResult({
message: `You've completed ${count} sessions! Your progress is stored locally.`,
cta: 'Create a free account to keep your progress',
})
return
}
setResult(null)
} catch {
setResult(null)
}
}, [tier, sessionAccuracy, previousAccuracy])
return result
}
/** Increment the guest session counter (call after session completion). */
export function recordGuestSession(): void {
try {
const current = parseInt(localStorage.getItem(STORAGE_KEY) ?? '0', 10)
localStorage.setItem(STORAGE_KEY, (current + 1).toString())
} catch {
// localStorage unavailable
}
}
interface GuestProgressBannerProps {
/**
* When true, hides the dismiss button and uses subtler styling.
* Used on the dashboard where the banner should always be visible.
*/
persistent?: boolean
/** Current session accuracy (0-1) */
sessionAccuracy?: number | null
/** Previous session accuracy (0-1) */
previousAccuracy?: number | null
}
/**
* Subtle banner prompting guests to create an account.
* Shows after 3 sessions, after returning from 24h+ absence, or after accuracy improvement.
* Dismissible (unless `persistent` is set) — won't show again once closed.
*/
export function GuestProgressBanner({
persistent = false,
sessionAccuracy,
previousAccuracy,
}: GuestProgressBannerProps = {}) {
const { resolvedTheme } = useTheme()
const isDark = resolvedTheme === 'dark'
const message = useGuestMessage({ sessionAccuracy, previousAccuracy })
const [visible, setVisible] = useState(true)
const handleDismiss = useCallback(() => {
setVisible(false)
try {
localStorage.setItem(DISMISSED_KEY, '1')
} catch {
// ignore
}
}, [])
if (!message || !visible) return null
return (
<div
data-component="guest-progress-banner"
className={css({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
gap: '0.75rem',
padding: '0.625rem 1rem',
backgroundColor: persistent
? isDark
? 'blue.900/20'
: 'blue.50/70'
: isDark
? 'blue.900/40'
: 'blue.50',
borderBottom: persistent ? 'none' : '1px solid',
borderColor: isDark ? 'blue.800' : 'blue.100',
fontSize: '0.8125rem',
...(persistent && {
borderRadius: '8px',
border: '1px solid',
borderColor: isDark ? 'blue.800/50' : 'blue.100',
margin: '0.75rem',
}),
})}
>
<div
className={css({
display: 'flex',
alignItems: 'center',
gap: '0.5rem',
flex: 1,
flexWrap: 'wrap',
})}
>
<span className={css({ color: isDark ? 'blue.300' : 'blue.700' })}>{message.message}</span>
<Link
href="/auth/signin"
data-action="guest-save-progress"
className={css({
fontWeight: '600',
color: isDark ? 'blue.200' : 'blue.600',
textDecoration: 'underline',
textUnderlineOffset: '2px',
whiteSpace: 'nowrap',
_hover: { color: isDark ? 'blue.100' : 'blue.700' },
})}
>
{message.cta}
</Link>
</div>
{!persistent && (
<button
type="button"
onClick={handleDismiss}
data-action="dismiss-guest-banner"
className={css({
background: 'none',
border: 'none',
color: isDark ? 'gray.500' : 'gray.400',
cursor: 'pointer',
fontSize: '1rem',
lineHeight: 1,
padding: '0.25rem',
flexShrink: 0,
_hover: { color: isDark ? 'gray.300' : 'gray.600' },
})}
aria-label="Dismiss"
>
×
</button>
)}
</div>
)
}
|