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