Elffuss BraidHub

kikocis/continue public

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