|
| 1 | +import { TextAttributes } from '@opentui/core' |
| 2 | +import { useRenderer } from '@opentui/react' |
| 3 | +import React, { useEffect, useMemo, useState } from 'react' |
| 4 | + |
| 5 | +import { AdBanner } from './ad-banner' |
| 6 | +import { ChoiceAdBanner } from './choice-ad-banner' |
| 7 | +import { ShimmerText } from './shimmer-text' |
| 8 | +import { useGravityAd } from '../hooks/use-gravity-ad' |
| 9 | +import { useLogo } from '../hooks/use-logo' |
| 10 | +import { useSheenAnimation } from '../hooks/use-sheen-animation' |
| 11 | +import { useTerminalDimensions } from '../hooks/use-terminal-dimensions' |
| 12 | +import { useTheme } from '../hooks/use-theme' |
| 13 | +import { getLogoAccentColor, getLogoBlockColor } from '../utils/theme-system' |
| 14 | + |
| 15 | +import type { FreebuffSessionResponse } from '../types/freebuff-session' |
| 16 | + |
| 17 | +interface WaitingRoomScreenProps { |
| 18 | + session: FreebuffSessionResponse | null |
| 19 | + error: string | null |
| 20 | +} |
| 21 | + |
| 22 | +const formatWait = (ms: number): string => { |
| 23 | + if (!Number.isFinite(ms) || ms <= 0) return 'any moment now' |
| 24 | + const totalSeconds = Math.round(ms / 1000) |
| 25 | + if (totalSeconds < 60) return `~${totalSeconds}s` |
| 26 | + const minutes = Math.round(totalSeconds / 60) |
| 27 | + if (minutes < 60) return `~${minutes} min` |
| 28 | + const hours = Math.floor(minutes / 60) |
| 29 | + const rem = minutes % 60 |
| 30 | + return rem === 0 ? `~${hours}h` : `~${hours}h ${rem}m` |
| 31 | +} |
| 32 | + |
| 33 | +const formatElapsed = (ms: number): string => { |
| 34 | + if (!Number.isFinite(ms) || ms < 0) return '0s' |
| 35 | + const totalSeconds = Math.floor(ms / 1000) |
| 36 | + const minutes = Math.floor(totalSeconds / 60) |
| 37 | + const seconds = totalSeconds % 60 |
| 38 | + if (minutes === 0) return `${seconds}s` |
| 39 | + return `${minutes}m ${seconds.toString().padStart(2, '0')}s` |
| 40 | +} |
| 41 | + |
| 42 | +export const WaitingRoomScreen: React.FC<WaitingRoomScreenProps> = ({ |
| 43 | + session, |
| 44 | + error, |
| 45 | +}) => { |
| 46 | + const theme = useTheme() |
| 47 | + const renderer = useRenderer() |
| 48 | + const { terminalWidth, contentMaxWidth } = useTerminalDimensions() |
| 49 | + |
| 50 | + const [sheenPosition, setSheenPosition] = useState(0) |
| 51 | + const blockColor = getLogoBlockColor(theme.name) |
| 52 | + const accentColor = getLogoAccentColor(theme.name) |
| 53 | + const { applySheenToChar } = useSheenAnimation({ |
| 54 | + logoColor: theme.foreground, |
| 55 | + accentColor, |
| 56 | + blockColor, |
| 57 | + terminalWidth: renderer?.width ?? terminalWidth, |
| 58 | + sheenPosition, |
| 59 | + setSheenPosition, |
| 60 | + }) |
| 61 | + const { component: logoComponent } = useLogo({ |
| 62 | + availableWidth: contentMaxWidth, |
| 63 | + accentColor, |
| 64 | + blockColor, |
| 65 | + applySheenToChar, |
| 66 | + }) |
| 67 | + |
| 68 | + // Always enable ads in the waiting room — this is where monetization lives. |
| 69 | + const { ad, adData, recordImpression } = useGravityAd({ enabled: true }) |
| 70 | + |
| 71 | + // Elapsed-in-queue timer. Starts from `queuedAt` so it keeps ticking even if |
| 72 | + // the user wanders away and comes back. |
| 73 | + const queuedAtMs = useMemo(() => { |
| 74 | + if (session?.status === 'queued') return Date.parse(session.queuedAt) |
| 75 | + return null |
| 76 | + }, [session]) |
| 77 | + const [now, setNow] = useState(() => Date.now()) |
| 78 | + useEffect(() => { |
| 79 | + const id = setInterval(() => setNow(Date.now()), 1000) |
| 80 | + return () => clearInterval(id) |
| 81 | + }, []) |
| 82 | + const elapsedMs = queuedAtMs ? now - queuedAtMs : 0 |
| 83 | + |
| 84 | + const isQueued = session?.status === 'queued' |
| 85 | + |
| 86 | + return ( |
| 87 | + <box |
| 88 | + style={{ |
| 89 | + width: '100%', |
| 90 | + height: '100%', |
| 91 | + flexDirection: 'column', |
| 92 | + backgroundColor: theme.background, |
| 93 | + }} |
| 94 | + > |
| 95 | + <box |
| 96 | + style={{ |
| 97 | + flexGrow: 1, |
| 98 | + flexDirection: 'column', |
| 99 | + alignItems: 'center', |
| 100 | + justifyContent: 'center', |
| 101 | + paddingLeft: 2, |
| 102 | + paddingRight: 2, |
| 103 | + gap: 1, |
| 104 | + }} |
| 105 | + > |
| 106 | + <box style={{ marginBottom: 1 }}>{logoComponent}</box> |
| 107 | + |
| 108 | + <box |
| 109 | + style={{ |
| 110 | + flexDirection: 'column', |
| 111 | + alignItems: 'center', |
| 112 | + gap: 0, |
| 113 | + maxWidth: contentMaxWidth, |
| 114 | + }} |
| 115 | + > |
| 116 | + {error && !session && ( |
| 117 | + <text style={{ fg: theme.secondary, wrapMode: 'word' }}> |
| 118 | + ⚠ {error} |
| 119 | + </text> |
| 120 | + )} |
| 121 | + |
| 122 | + {((!session && !error) || session?.status === 'none') && ( |
| 123 | + <text style={{ fg: theme.muted }}> |
| 124 | + <ShimmerText text="Joining the waiting room…" /> |
| 125 | + </text> |
| 126 | + )} |
| 127 | + |
| 128 | + {isQueued && session && ( |
| 129 | + <> |
| 130 | + <text |
| 131 | + style={{ fg: theme.foreground, marginBottom: 1 }} |
| 132 | + > |
| 133 | + <ShimmerText text="You're in the waiting room" /> |
| 134 | + </text> |
| 135 | + |
| 136 | + <box |
| 137 | + style={{ |
| 138 | + flexDirection: 'column', |
| 139 | + alignItems: 'center', |
| 140 | + gap: 0, |
| 141 | + }} |
| 142 | + > |
| 143 | + <text style={{ fg: theme.foreground }}> |
| 144 | + Position{' '} |
| 145 | + <span fg={theme.primary} attributes={TextAttributes.BOLD}> |
| 146 | + {session.position} |
| 147 | + </span> |
| 148 | + <span fg={theme.muted}> of {session.queueDepth}</span> |
| 149 | + </text> |
| 150 | + <text style={{ fg: theme.foreground }}> |
| 151 | + Estimated wait:{' '} |
| 152 | + <span fg={theme.primary}> |
| 153 | + {formatWait(session.estimatedWaitMs)} |
| 154 | + </span> |
| 155 | + </text> |
| 156 | + <text style={{ fg: theme.muted }}> |
| 157 | + Waiting for {formatElapsed(elapsedMs)} |
| 158 | + </text> |
| 159 | + </box> |
| 160 | + |
| 161 | + <box style={{ marginTop: 1, alignItems: 'center' }}> |
| 162 | + <text style={{ fg: theme.muted, wrapMode: 'word' }}> |
| 163 | + Leave this window open — we'll ding when your session starts. |
| 164 | + </text> |
| 165 | + </box> |
| 166 | + </> |
| 167 | + )} |
| 168 | + |
| 169 | + {/* Server says the waiting room is disabled — this screen should not |
| 170 | + normally render in that case, but show a minimal message just in |
| 171 | + case App.tsx's guard is bypassed. */} |
| 172 | + {session?.status === 'disabled' && ( |
| 173 | + <text style={{ fg: theme.muted }}>Waiting room disabled.</text> |
| 174 | + )} |
| 175 | + </box> |
| 176 | + </box> |
| 177 | + |
| 178 | + {/* Ad banner pinned to the bottom, same look-and-feel as in chat. */} |
| 179 | + {ad && ( |
| 180 | + <box style={{ flexShrink: 0 }}> |
| 181 | + {adData?.variant === 'choice' ? ( |
| 182 | + <ChoiceAdBanner |
| 183 | + ads={adData.ads} |
| 184 | + onImpression={recordImpression} |
| 185 | + /> |
| 186 | + ) : ( |
| 187 | + <AdBanner ad={ad} onDisableAds={() => {}} isFreeMode /> |
| 188 | + )} |
| 189 | + </box> |
| 190 | + )} |
| 191 | + |
| 192 | + {/* Horizontal separator (mirrors chat input divider style) */} |
| 193 | + {!ad && ( |
| 194 | + <text style={{ fg: theme.muted, flexShrink: 0 }}> |
| 195 | + {'─'.repeat(terminalWidth)} |
| 196 | + </text> |
| 197 | + )} |
| 198 | + </box> |
| 199 | + ) |
| 200 | +} |
0 commit comments