|
| 1 | +import type { ReactElement, ReactNode } from 'react'; |
| 2 | +import { SPEEDS, type Scale } from '../types'; |
| 3 | +import { isValidHex, stripHash } from '../utils'; |
| 4 | +import { SectionLabel } from './SectionLabel'; |
| 5 | +import { StyledSelect, ThemeSelector } from './ThemeSelector'; |
| 6 | + |
| 7 | +function ControlRow({ label, children }: { label: string; children: ReactNode }): ReactElement { |
| 8 | + return ( |
| 9 | + <div className="flex flex-col gap-1.5"> |
| 10 | + <SectionLabel>{label}</SectionLabel> |
| 11 | + {children} |
| 12 | + </div> |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +function HexInput({ |
| 17 | + id, |
| 18 | + label, |
| 19 | + value, |
| 20 | + onChange, |
| 21 | + placeholder, |
| 22 | +}: { |
| 23 | + id: string; |
| 24 | + label: string; |
| 25 | + value: string; |
| 26 | + onChange: (v: string) => void; |
| 27 | + placeholder: string; |
| 28 | +}): ReactElement { |
| 29 | + const pickerValue = isValidHex(value) ? `#${stripHash(value)}` : '#000000'; |
| 30 | + const swatchColor = isValidHex(value) ? pickerValue : null; |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="flex flex-col gap-1.5"> |
| 34 | + <SectionLabel>{label}</SectionLabel> |
| 35 | + <div className="relative flex items-center gap-2"> |
| 36 | + <label |
| 37 | + htmlFor={`${id}-picker`} |
| 38 | + title="Open color picker" |
| 39 | + className="relative shrink-0 w-9 h-9 rounded-xl border border-white/10 overflow-hidden cursor-pointer hover:border-emerald-500/50 transition-colors" |
| 40 | + style={{ backgroundColor: swatchColor ?? '#1a1a1a' }} |
| 41 | + > |
| 42 | + {!swatchColor && ( |
| 43 | + <span |
| 44 | + className="absolute inset-0" |
| 45 | + style={{ |
| 46 | + backgroundImage: 'repeating-conic-gradient(#333 0% 25%, #1a1a1a 0% 50%)', |
| 47 | + backgroundSize: '8px 8px', |
| 48 | + }} |
| 49 | + /> |
| 50 | + )} |
| 51 | + <input |
| 52 | + id={`${id}-picker`} |
| 53 | + type="color" |
| 54 | + value={pickerValue} |
| 55 | + onChange={(e) => onChange(stripHash(e.target.value))} |
| 56 | + className="absolute inset-0 w-full h-full opacity-0 cursor-pointer" |
| 57 | + aria-label={`Color picker for ${label}`} |
| 58 | + /> |
| 59 | + </label> |
| 60 | + |
| 61 | + <div className="relative flex-1 flex items-center"> |
| 62 | + <span className="absolute left-3 text-white/30 text-sm select-none pointer-events-none"> |
| 63 | + # |
| 64 | + </span> |
| 65 | + <input |
| 66 | + id={id} |
| 67 | + type="text" |
| 68 | + value={value} |
| 69 | + onChange={(e) => onChange(e.target.value.replace(/^#/, ''))} |
| 70 | + placeholder={placeholder.replace(/^#/, '')} |
| 71 | + maxLength={6} |
| 72 | + className="w-full bg-black border border-white/10 rounded-xl pl-7 pr-4 py-2.5 text-sm font-mono text-emerald-300 placeholder:text-white/20 outline-none focus:border-emerald-500/50 transition-colors" |
| 73 | + /> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +export function ControlsPanel({ |
| 81 | + username, |
| 82 | + theme, |
| 83 | + bgHex, |
| 84 | + accentHex, |
| 85 | + textHex, |
| 86 | + scale, |
| 87 | + speed, |
| 88 | + onUsernameChange, |
| 89 | + onThemeChange, |
| 90 | + onBgHexChange, |
| 91 | + onAccentHexChange, |
| 92 | + onTextHexChange, |
| 93 | + onScaleChange, |
| 94 | + onSpeedChange, |
| 95 | + onClearOverrides, |
| 96 | +}: { |
| 97 | + username: string; |
| 98 | + theme: string; |
| 99 | + bgHex: string; |
| 100 | + accentHex: string; |
| 101 | + textHex: string; |
| 102 | + scale: Scale; |
| 103 | + speed: string; |
| 104 | + onUsernameChange: (value: string) => void; |
| 105 | + onThemeChange: (value: string) => void; |
| 106 | + onBgHexChange: (value: string) => void; |
| 107 | + onAccentHexChange: (value: string) => void; |
| 108 | + onTextHexChange: (value: string) => void; |
| 109 | + onScaleChange: (value: Scale) => void; |
| 110 | + onSpeedChange: (value: string) => void; |
| 111 | + onClearOverrides: () => void; |
| 112 | +}): ReactElement { |
| 113 | + const hasOverrides = Boolean(bgHex || accentHex || textHex); |
| 114 | + |
| 115 | + return ( |
| 116 | + <div> |
| 117 | + <p className="text-xs font-bold uppercase tracking-[0.22em] text-emerald-400 mb-4"> |
| 118 | + Controls |
| 119 | + </p> |
| 120 | + |
| 121 | + <div className="flex flex-col gap-5"> |
| 122 | + <ControlRow label="GitHub Username"> |
| 123 | + <input |
| 124 | + id="username-input" |
| 125 | + type="text" |
| 126 | + value={username} |
| 127 | + onChange={(e) => onUsernameChange(e.target.value)} |
| 128 | + placeholder="jhasourav07" |
| 129 | + className="w-full bg-black border border-white/10 rounded-xl px-4 py-2.5 text-sm font-mono text-emerald-300 placeholder:text-white/20 outline-none focus:border-emerald-500/50 transition-colors" |
| 130 | + /> |
| 131 | + </ControlRow> |
| 132 | + |
| 133 | + <div className="h-px bg-white/5" /> |
| 134 | + |
| 135 | + <ThemeSelector theme={theme} onThemeChange={onThemeChange} /> |
| 136 | + |
| 137 | + <div className="h-px bg-white/5" /> |
| 138 | + |
| 139 | + <div> |
| 140 | + <SectionLabel>Custom Color Overrides</SectionLabel> |
| 141 | + <p className="text-[11px] text-white/25 mb-3 leading-relaxed"> |
| 142 | + These override the theme preset above. Enter HEX values without |
| 143 | + <code className="text-white/40">#</code>. |
| 144 | + </p> |
| 145 | + <div className="flex flex-col gap-3"> |
| 146 | + <HexInput |
| 147 | + id="bg-hex-input" |
| 148 | + label="Background" |
| 149 | + value={bgHex} |
| 150 | + onChange={onBgHexChange} |
| 151 | + placeholder="e.g. 0a0a0a" |
| 152 | + /> |
| 153 | + <HexInput |
| 154 | + id="accent-hex-input" |
| 155 | + label="Accent / Tower Color" |
| 156 | + value={accentHex} |
| 157 | + onChange={onAccentHexChange} |
| 158 | + placeholder="e.g. 00ffaa" |
| 159 | + /> |
| 160 | + <HexInput |
| 161 | + id="text-hex-input" |
| 162 | + label="Text / Label Color" |
| 163 | + value={textHex} |
| 164 | + onChange={onTextHexChange} |
| 165 | + placeholder="e.g. ffffff" |
| 166 | + /> |
| 167 | + </div> |
| 168 | + {hasOverrides && ( |
| 169 | + <button |
| 170 | + id="clear-overrides-btn" |
| 171 | + onClick={onClearOverrides} |
| 172 | + className="mt-3 text-[11px] text-red-400/60 hover:text-red-400 transition-colors" |
| 173 | + > |
| 174 | + Clear overrides |
| 175 | + </button> |
| 176 | + )} |
| 177 | + </div> |
| 178 | + |
| 179 | + <div className="h-px bg-white/5" /> |
| 180 | + |
| 181 | + <ControlRow label="Tower Height Scaling"> |
| 182 | + <div className="grid grid-cols-2 gap-2"> |
| 183 | + {(['linear', 'log'] as Scale[]).map((currentScale) => ( |
| 184 | + <button |
| 185 | + key={currentScale} |
| 186 | + id={`scale-${currentScale}-btn`} |
| 187 | + onClick={() => onScaleChange(currentScale)} |
| 188 | + className={`py-2.5 rounded-xl text-sm font-bold transition-all ${ |
| 189 | + scale === currentScale |
| 190 | + ? 'bg-emerald-500/15 border border-emerald-500/30 text-emerald-400' |
| 191 | + : 'bg-black border border-white/8 text-white/30 hover:text-white/60 hover:border-white/20' |
| 192 | + }`} |
| 193 | + > |
| 194 | + {currentScale === 'linear' ? 'Linear' : 'Logarithmic'} |
| 195 | + </button> |
| 196 | + ))} |
| 197 | + </div> |
| 198 | + <p className="text-[11px] text-white/25 mt-1.5 leading-relaxed"> |
| 199 | + {scale === 'log' |
| 200 | + ? 'Log mode compresses extreme outliers. Great for power committers.' |
| 201 | + : 'Linear mode shows raw commit counts as tower heights.'} |
| 202 | + </p> |
| 203 | + </ControlRow> |
| 204 | + |
| 205 | + <ControlRow label="Radar Scan Speed"> |
| 206 | + <div className="relative"> |
| 207 | + <StyledSelect id="speed-select" value={speed} onChange={onSpeedChange}> |
| 208 | + {SPEEDS.map((speedOption) => ( |
| 209 | + <option key={speedOption.value} value={speedOption.value}> |
| 210 | + {speedOption.label} |
| 211 | + </option> |
| 212 | + ))} |
| 213 | + </StyledSelect> |
| 214 | + </div> |
| 215 | + </ControlRow> |
| 216 | + </div> |
| 217 | + </div> |
| 218 | + ); |
| 219 | +} |
0 commit comments