| 1 | import { Box, Text, useInput } from "ink"; |
| 2 | import React, { ReactNode } from "react"; |
| 3 | |
| 4 | import { defaultBoxStyles } from "./styles.js"; |
| 5 | |
| 6 | export interface SelectorOption { |
| 7 | id: string; |
| 8 | name: string; |
| 9 | displaySuffix?: string; |
| 10 | } |
| 11 | |
| 12 | interface SelectorProps<T extends SelectorOption> { |
| 13 | title: string; |
| 14 | options: T[]; |
| 15 | selectedIndex: number; |
| 16 | loading: boolean; |
| 17 | error: string | null; |
| 18 | loadingMessage?: string; |
| 19 | currentId?: string | null; |
| 20 | onSelect: (option: T) => void; |
| 21 | onCancel: () => void; |
| 22 | onNavigate: (newIndex: number) => void; |
| 23 | renderOption?: ( |
| 24 | option: T, |
| 25 | isSelected: boolean, |
| 26 | isCurrent: boolean, |
| 27 | ) => ReactNode; |
| 28 | } |
| 29 | |
| 30 | export function Selector<T extends SelectorOption>({ |
| 31 | title, |
| 32 | options, |
| 33 | selectedIndex, |
| 34 | loading, |
| 35 | error, |
| 36 | loadingMessage = "Loading...", |
| 37 | currentId, |
| 38 | onSelect, |
| 39 | onCancel, |
| 40 | onNavigate, |
| 41 | renderOption, |
| 42 | }: SelectorProps<T>) { |
| 43 | // Handle input for loading and error states |
| 44 | useInput((input, key) => { |
| 45 | if (loading || error) { |
| 46 | if (key.escape || (key.ctrl && input === "c")) { |
| 47 | onCancel(); |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | useInput((input, key) => { |
| 54 | if (loading || error) return; // Skip normal input handling during loading/error |
| 55 | |
| 56 | if (key.escape || (key.ctrl && input === "c")) { |
| 57 | onCancel(); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if (key.return) { |
| 62 | const selectedOption = options[selectedIndex]; |
| 63 | if (selectedOption) { |
| 64 | onSelect(selectedOption); |
| 65 | } |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // loop back to the first option after the last option, and vice versa |
| 70 | if (key.upArrow) { |
| 71 | onNavigate(selectedIndex === 0 ? options.length - 1 : selectedIndex - 1); |
| 72 | } else if (key.downArrow) { |
| 73 | onNavigate(selectedIndex === options.length - 1 ? 0 : selectedIndex + 1); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | if (loading) { |
| 78 | return ( |
| 79 | <Box {...defaultBoxStyles("blue")}> |
| 80 | <Text color="blue" bold> |
| 81 | {title} |
| 82 | </Text> |
| 83 | <Text> </Text> |
| 84 | <Text italic color="gray"> |
| 85 | {loadingMessage} |
| 86 | </Text> |
| 87 | </Box> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | if (error) { |
| 92 | return ( |
| 93 | <Box {...defaultBoxStyles("blue")}> |
| 94 | <Text color="red" bold> |
| 95 | Error |
| 96 | </Text> |
| 97 | <Text color="red">{error}</Text> |
| 98 | <Text color="gray" dimColor> |
| 99 | Press Esc to cancel |
| 100 | </Text> |
| 101 | </Box> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | const defaultRenderOption = ( |
| 106 | option: T, |
| 107 | isSelected: boolean, |
| 108 | isCurrent: boolean, |
| 109 | ) => ( |
| 110 | <> |
| 111 | <Text |
| 112 | color={isSelected ? "blue" : isCurrent ? "green" : "white"} |
| 113 | bold={isSelected} |
| 114 | > |
| 115 | {isSelected ? "➤ " : " "} |
| 116 | {option.name} |
| 117 | {option.displaySuffix || ""} |
| 118 | </Text> |
| 119 | {isCurrent && ( |
| 120 | <Text bold color="green"> |
| 121 | {" ✔"} |
| 122 | </Text> |
| 123 | )} |
| 124 | </> |
| 125 | ); |
| 126 | |
| 127 | return ( |
| 128 | <Box {...defaultBoxStyles("blue")}> |
| 129 | <Text color="blue" bold> |
| 130 | {title} |
| 131 | </Text> |
| 132 | <Box flexDirection="column" marginTop={1}> |
| 133 | {options.map((option, index) => { |
| 134 | const isSelected = index === selectedIndex; |
| 135 | const isCurrent = currentId === option.id; |
| 136 | const render = renderOption || defaultRenderOption; |
| 137 | |
| 138 | return ( |
| 139 | <Box key={option.id}>{render(option, isSelected, isCurrent)}</Box> |
| 140 | ); |
| 141 | })} |
| 142 | </Box> |
| 143 | <Box marginTop={1}> |
| 144 | <Text color="gray" dimColor> |
| 145 | ↑/↓ to navigate, Enter to select, Esc to cancel |
| 146 | </Text> |
| 147 | </Box> |
| 148 | </Box> |
| 149 | ); |
| 150 | } |