|
| 1 | +import { TextAttributes } from '@opentui/core' |
| 2 | +import { useKeyboard } from '@opentui/react' |
| 3 | +import React, { useCallback, useMemo, useState } from 'react' |
| 4 | + |
| 5 | +import { Button } from './button' |
| 6 | +import { FREEBUFF_MODELS } from '@codebuff/common/constants/freebuff-models' |
| 7 | + |
| 8 | +import { switchFreebuffModel } from '../hooks/use-freebuff-session' |
| 9 | +import { useFreebuffModelStore } from '../state/freebuff-model-store' |
| 10 | +import { useFreebuffSessionStore } from '../state/freebuff-session-store' |
| 11 | +import { useTheme } from '../hooks/use-theme' |
| 12 | + |
| 13 | +import type { KeyEvent } from '@opentui/core' |
| 14 | + |
| 15 | +/** |
| 16 | + * Lets the user pick which model's queue they're in. Tapping (or pressing the |
| 17 | + * row's number key) on a different model triggers a re-POST: the server moves |
| 18 | + * them to the back of the new model's queue. |
| 19 | + * |
| 20 | + * Each row shows a live "N ahead" count sourced from the server's |
| 21 | + * `queueDepthByModel` snapshot so the choice is informed (e.g. "3 ahead" vs |
| 22 | + * "12 ahead") rather than a blind preference toggle. |
| 23 | + */ |
| 24 | +export const FreebuffModelSelector: React.FC = () => { |
| 25 | + const theme = useTheme() |
| 26 | + const selectedModel = useFreebuffModelStore((s) => s.selectedModel) |
| 27 | + const session = useFreebuffSessionStore((s) => s.session) |
| 28 | + const [pending, setPending] = useState<string | null>(null) |
| 29 | + const [hoveredId, setHoveredId] = useState<string | null>(null) |
| 30 | + |
| 31 | + // For the user's current queue, "ahead" is `position - 1` (themselves don't |
| 32 | + // count). For every other queue, switching would land them at the back, so |
| 33 | + // it's that queue's full depth. Null before the first queued snapshot so |
| 34 | + // the UI doesn't flash misleading zeros. |
| 35 | + const aheadByModel = useMemo<Record<string, number> | null>(() => { |
| 36 | + if (session?.status !== 'queued') return null |
| 37 | + const depths = session.queueDepthByModel ?? {} |
| 38 | + const out: Record<string, number> = {} |
| 39 | + for (const { id } of FREEBUFF_MODELS) { |
| 40 | + out[id] = |
| 41 | + id === session.model ? Math.max(0, session.position - 1) : depths[id] ?? 0 |
| 42 | + } |
| 43 | + return out |
| 44 | + }, [session]) |
| 45 | + |
| 46 | + const pick = useCallback( |
| 47 | + (modelId: string) => { |
| 48 | + if (pending) return |
| 49 | + if (modelId === selectedModel) return |
| 50 | + setPending(modelId) |
| 51 | + switchFreebuffModel(modelId).finally(() => setPending(null)) |
| 52 | + }, |
| 53 | + [pending, selectedModel], |
| 54 | + ) |
| 55 | + |
| 56 | + // Number-key shortcuts (1-9) so keyboard-only users can switch without |
| 57 | + // hunting for a clickable region. |
| 58 | + useKeyboard( |
| 59 | + useCallback( |
| 60 | + (key: KeyEvent) => { |
| 61 | + if (pending) return |
| 62 | + const name = key.name ?? '' |
| 63 | + if (!/^[1-9]$/.test(name)) return |
| 64 | + const digit = Number(name) |
| 65 | + if (digit > FREEBUFF_MODELS.length) return |
| 66 | + const target = FREEBUFF_MODELS[digit - 1] |
| 67 | + if (target && target.id !== selectedModel) { |
| 68 | + key.preventDefault?.() |
| 69 | + pick(target.id) |
| 70 | + } |
| 71 | + }, |
| 72 | + [pending, pick, selectedModel], |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + return ( |
| 77 | + <box |
| 78 | + style={{ |
| 79 | + flexDirection: 'column', |
| 80 | + alignItems: 'flex-start', |
| 81 | + gap: 0, |
| 82 | + }} |
| 83 | + > |
| 84 | + <text style={{ fg: theme.muted, marginBottom: 1 }}> |
| 85 | + Model — tap or press 1-{FREEBUFF_MODELS.length} to switch |
| 86 | + </text> |
| 87 | + {FREEBUFF_MODELS.map((model, idx) => { |
| 88 | + const isSelected = model.id === selectedModel |
| 89 | + const isPending = pending === model.id |
| 90 | + const isHovered = hoveredId === model.id |
| 91 | + const indicator = isSelected ? '●' : '○' |
| 92 | + const indicatorColor = isSelected ? theme.primary : theme.muted |
| 93 | + const labelColor = isSelected ? theme.foreground : theme.muted |
| 94 | + const interactable = !pending && !isSelected |
| 95 | + const ahead = aheadByModel?.[model.id] |
| 96 | + const hint = |
| 97 | + ahead === undefined |
| 98 | + ? model.tagline |
| 99 | + : ahead === 0 |
| 100 | + ? 'No wait' |
| 101 | + : `${ahead} ahead` |
| 102 | + return ( |
| 103 | + <Button |
| 104 | + key={model.id} |
| 105 | + onClick={() => pick(model.id)} |
| 106 | + onMouseOver={() => interactable && setHoveredId(model.id)} |
| 107 | + onMouseOut={() => setHoveredId((curr) => (curr === model.id ? null : curr))} |
| 108 | + style={{ paddingLeft: 0, paddingRight: 1 }} |
| 109 | + > |
| 110 | + <text> |
| 111 | + <span fg={indicatorColor}>{indicator} </span> |
| 112 | + <span fg={theme.muted}>{idx + 1}. </span> |
| 113 | + <span |
| 114 | + fg={labelColor} |
| 115 | + attributes={isSelected ? TextAttributes.BOLD : TextAttributes.NONE} |
| 116 | + > |
| 117 | + {model.displayName} |
| 118 | + </span> |
| 119 | + <span fg={theme.muted}> {hint}</span> |
| 120 | + {isPending && <span fg={theme.muted}> switching…</span>} |
| 121 | + {isHovered && interactable && !isPending && ( |
| 122 | + <span fg={theme.muted}> ↵</span> |
| 123 | + )} |
| 124 | + </text> |
| 125 | + </Button> |
| 126 | + ) |
| 127 | + })} |
| 128 | + </box> |
| 129 | + ) |
| 130 | +} |
0 commit comments