Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerCommandPopover.tsx

225 lines6.2 KB

1import { isLiquidGlassSupported, LiquidGlassView } from "@callstack/liquid-glass";
2import type { ComposerTriggerKind } from "@t3tools/shared/composerTrigger";
3import type { ServerProviderSkill, ServerProviderSlashCommand } from "@t3tools/contracts";
4import { SymbolView } from "expo-symbols";
5import { memo } from "react";
6import { Pressable, ScrollView, useColorScheme, View, type ViewStyle } from "react-native";
7​
8import { AppText as Text } from "../../components/AppText";
9import { PierreEntryIcon } from "../../components/PierreEntryIcon";
10import { MOBILE_TYPOGRAPHY } from "../../lib/typography";
11​
12export type ComposerCommandItem =
13 | {
14 readonly id: string;
15 readonly type: "path";
16 readonly path: string;
17 readonly kind: "file" | "directory";
18 readonly label: string;
19 readonly description: string;
20 }
21 | {
22 readonly id: string;
23 readonly type: "slash-command";
24 readonly command: string;
25 readonly label: string;
26 readonly description: string;
27 }
28 | {
29 readonly id: string;
30 readonly type: "provider-slash-command";
31 readonly command: ServerProviderSlashCommand;
32 readonly label: string;
33 readonly description: string;
34 }
35 | {
36 readonly id: string;
37 readonly type: "skill";
38 readonly skill: ServerProviderSkill;
39 readonly label: string;
40 readonly description: string;
41 };
42​
43interface ComposerCommandPopoverProps {
44 readonly items: ReadonlyArray<ComposerCommandItem>;
45 readonly triggerKind: ComposerTriggerKind | null;
46 readonly isLoading: boolean;
47 readonly onSelect: (item: ComposerCommandItem) => void;
49​
50function PopoverSurface(props: {
51 readonly children: React.ReactNode;
52 readonly isDarkMode: boolean;
53 readonly style?: ViewStyle;
54}) {
55 const baseStyle: ViewStyle = {
56 borderRadius: 16,
57 overflow: "hidden",
58 ...props.style,
59 };
60​
61 if (isLiquidGlassSupported) {
62 return (
63 <LiquidGlassView
64 effect="clear"
65 interactive={false}
66 tintColor={props.isDarkMode ? "rgba(30,30,32,0.95)" : "rgba(255,255,255,0.92)"}
67 colorScheme={props.isDarkMode ? "dark" : "light"}
68 style={baseStyle}
69 >
70 {props.children}
71 </LiquidGlassView>
72 );
73 }
74​
75 return (
76 <View
77 style={[
78 baseStyle,
79 {
80 backgroundColor: props.isDarkMode ? "rgba(44,44,46,0.96)" : "rgba(255,255,255,0.96)",
81 borderWidth: 1,
82 borderColor: props.isDarkMode ? "rgba(255,255,255,0.08)" : "rgba(0,0,0,0.06)",
83 },
84 ]}
85 >
86 {props.children}
87 </View>
88 );
90​
91function itemIcon(item: ComposerCommandItem) {
92 switch (item.type) {
93 case "slash-command":
94 case "provider-slash-command":
95 return "terminal" as const;
96 case "skill":
97 return "cube" as const;
98 case "path":
99 return null;
100 }
102​
103function groupLabel(triggerKind: ComposerTriggerKind | null): string | null {
104 switch (triggerKind) {
105 case "slash-command":
106 return "Commands";
107 case "skill":
108 return "Skills";
109 case "path":
110 return "Files";
111 default:
112 return null;
113 }
115​
116function emptyText(triggerKind: ComposerTriggerKind | null, isLoading: boolean): string {
117 if (isLoading) {
118 return triggerKind === "path" ? "Searching files…" : "Loading…";
119 }
120 switch (triggerKind) {
121 case "path":
122 return "No matching files or folders.";
123 case "skill":
124 return "No skills found.";
125 case "slash-command":
126 return "No matching commands.";
127 default:
128 return "No results.";
129 }
131​
132const CommandRow = memo(function CommandRow(props: {
133 readonly item: ComposerCommandItem;
134 readonly onPress: () => void;
135 readonly isLast: boolean;
136}) {
137 const iconName = itemIcon(props.item);
138 const iconColor = "#a1a1aa";
139​
140 return (
141 <Pressable
142 onPress={props.onPress}
143 style={({ pressed }) => ({
144 flexDirection: "row",
145 alignItems: "center",
146 paddingHorizontal: 14,
147 paddingVertical: 10,
148 gap: 10,
149 opacity: pressed ? 0.6 : 1,
150 borderBottomWidth: props.isLast ? 0 : 0.5,
151 borderBottomColor: "rgba(255,255,255,0.1)",
152 })}
153 >
154 {props.item.type === "path" ? (
155 <PierreEntryIcon path={props.item.path} kind={props.item.kind} size={16} />
156 ) : iconName ? (
157 <SymbolView name={iconName} size={14} tintColor={iconColor} type="monochrome" />
158 ) : null}
159 <Text
160 className="text-base font-t3-medium text-foreground"
161 numberOfLines={1}
162 style={{ flexShrink: 0 }}
163 >
164 {props.item.label}
165 </Text>
166 {props.item.description ? (
167 <Text
168 numberOfLines={1}
169 style={{
170 flex: 1,
171 minWidth: 0,
172 fontSize: MOBILE_TYPOGRAPHY.label.fontSize,
173 color: "#a1a1aa",
174 }}
175 >
176 {props.item.description}
177 </Text>
178 ) : null}
179 </Pressable>
180 );
181});
182​
183export const ComposerCommandPopover = memo(function ComposerCommandPopover(
184 props: ComposerCommandPopoverProps,
185) {
186 const isDarkMode = useColorScheme() === "dark";
187 const label = groupLabel(props.triggerKind);
188​
189 return (
190 <PopoverSurface isDarkMode={isDarkMode}>
191 {label ? (
192 <View style={{ paddingHorizontal: 14, paddingTop: 10, paddingBottom: 4 }}>
193 <Text
194 className="text-3xs font-t3-bold text-foreground-muted"
195 style={{ letterSpacing: 0.8, textTransform: "uppercase" }}
196 >
197 {label}
198 </Text>
199 </View>
200 ) : null}
201 {props.items.length > 0 ? (
202 <ScrollView
203 style={{ maxHeight: 180 }}
204 keyboardShouldPersistTaps="always"
205 showsVerticalScrollIndicator={false}
206 >
207 {props.items.map((item, index) => (
208 <CommandRow
209 key={item.id}
210 item={item}
211 onPress={() => props.onSelect(item)}
212 isLast={index === props.items.length - 1}
213 />
214 ))}
215 </ScrollView>
216 ) : (
217 <View style={{ paddingHorizontal: 14, paddingVertical: 10 }}>
218 <Text className="text-xs text-foreground-tertiary">
219 {emptyText(props.triggerKind, props.isLoading)}
220 </Text>
221 </View>
222 )}
223 </PopoverSurface>
224 );
225});