import { useState } from 'react'; import { Icon } from '@/components/ui/icon'; import { cn } from '@/lib/utils'; import type { CheatOption } from '../protocol'; import { resolveOption } from '../protocol'; import type { ControlInternalProps } from './shared'; export const SelectionControl = ({ cheat, value, disabled, onChange }: ControlInternalProps) => { const options = (cheat.args.options ?? []).map(resolveOption); const [open, setOpen] = useState(false); if (options.length === 0) { return No options; } const selectedOption = findOption(options, String(value ?? options[0].value)) ?? options[0]; const handleToggle = () => { if (disabled) { return; } setOpen((current) => !current); }; const handleSelect = (option: CheatOption) => { onChange(option.value); setOpen(false); }; return (
{open ? (
{options.map((option) => { const active = isSameOption(selectedOption.value, option.value); return ( ); })}
) : null}
); }; function optionKey(option: CheatOption): string { return String(option.value); } function findOption(options: CheatOption[], value: string): CheatOption | undefined { return options.find((option) => String(option.value) === value); } function isSameOption(left: unknown, right: unknown): boolean { return String(left) === String(right); }