diff --git a/packages/cli/src/ui/components/AuthDialog.tsx b/packages/cli/src/ui/components/AuthDialog.tsx index 1a7c73a33c9..c0874f315b5 100644 --- a/packages/cli/src/ui/components/AuthDialog.tsx +++ b/packages/cli/src/ui/components/AuthDialog.tsx @@ -65,6 +65,7 @@ export function AuthDialog({ return item.value === AuthType.LOGIN_WITH_GOOGLE; }); + const validInitialAuthIndex = initialAuthIndex >= 0 ? initialAuthIndex : 0; const handleAuthSelect = (authMethod: AuthType) => { const error = validateAuthMethod(authMethod); @@ -145,7 +146,7 @@ export function AuthDialog({ diff --git a/packages/cli/src/ui/components/ThemeDialog.tsx b/packages/cli/src/ui/components/ThemeDialog.tsx index 0ca176cba1f..7aac1eb7e3d 100644 --- a/packages/cli/src/ui/components/ThemeDialog.tsx +++ b/packages/cli/src/ui/components/ThemeDialog.tsx @@ -53,6 +53,7 @@ export function ThemeDialog({ const initialThemeIndex = themeItems.findIndex( (item) => item.value === (settings.merged.theme || DEFAULT_THEME.name), ); + const validInitialThemeIndex = initialThemeIndex >= 0 ? initialThemeIndex : 0; const scopeItems = [ { label: 'User Settings', value: SettingScope.User }, @@ -198,7 +199,7 @@ export function ThemeDialog({ ({ showScrollArrows = false, maxItemsToShow = 10, }: RadioButtonSelectProps): React.JSX.Element { - const [activeIndex, setActiveIndex] = useState(initialIndex); + const validInitialIndex = items.length > 0 && initialIndex >= 0 && initialIndex < items.length ? initialIndex : 0; + const [activeIndex, setActiveIndex] = useState(validInitialIndex); const [scrollOffset, setScrollOffset] = useState(0); useEffect(() => { @@ -76,15 +77,21 @@ export function RadioButtonSelect({ if (input === 'k' || key.upArrow) { const newIndex = activeIndex > 0 ? activeIndex - 1 : items.length - 1; setActiveIndex(newIndex); - onHighlight?.(items[newIndex]!.value); + if (items[newIndex]) { + onHighlight?.(items[newIndex].value); + } } if (input === 'j' || key.downArrow) { const newIndex = activeIndex < items.length - 1 ? activeIndex + 1 : 0; setActiveIndex(newIndex); - onHighlight?.(items[newIndex]!.value); + if (items[newIndex]) { + onHighlight?.(items[newIndex].value); + } } if (key.return) { - onSelect(items[activeIndex]!.value); + if (items[activeIndex]) { + onSelect(items[activeIndex].value); + } } // Enable selection directly from number keys.