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

ComposerCommandPopover.tsx

189 lines5.3 KB

1import type { ComposerTriggerKind } from "@t3tools/shared/composerTrigger";
2import type { ServerProviderSkill, ServerProviderSlashCommand } from "@t3tools/contracts";
3import { SymbolView } from "../../components/AppSymbol";
4import { memo } from "react";
5import { Pressable, ScrollView, useColorScheme, View, type ViewStyle } from "react-native";
6​
7import { AppText as Text } from "../../components/AppText";
8import { GlassSurface } from "../../components/GlassSurface";
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;
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 return (
60 <GlassSurface
61 glassEffectStyle="clear"
62 tintColor={props.isDarkMode ? "rgba(30,30,32,0.95)" : "rgba(255,255,255,0.92)"}
63 style={baseStyle}
64 >
65 {props.children}
66 </GlassSurface>
67 );
69​
70function itemIcon(item: ComposerCommandItem) {
71 switch (item.type) {
72 case "slash-command":
73 case "provider-slash-command":
74 return "terminal" as const;
75 case "skill":
76 return "cube" as const;
77 case "path":
78 return null;
79 }
81​
82function groupLabel(triggerKind: ComposerTriggerKind | null): string | null {
83 switch (triggerKind) {
84 case "slash-command":
85 return "Commands";
86 case "skill":
87 return "Skills";
88 case "path":
89 return "Files";
90 default:
91 return null;
92 }
94​
95function emptyText(triggerKind: ComposerTriggerKind | null, isLoading: boolean): string {
96 if (isLoading) {
97 return triggerKind === "path" ? "Searching files…" : "Loading…";
98 }
99 switch (triggerKind) {
100 case "path":
101 return "No matching files or folders.";
102 case "skill":
103 return "No skills found.";
104 case "slash-command":
105 return "No matching commands.";
106 default:
107 return "No results.";
108 }
110​
111const CommandRow = memo(function CommandRow(props: {
112 readonly item: ComposerCommandItem;
113 readonly onPress: () => void;
114 readonly isLast: boolean;
115}) {
116 const iconName = itemIcon(props.item);
117 const iconColor = "#a1a1aa";
118​
119 return (
120 <Pressable
121 onPress={props.onPress}
122 style={({ pressed }) => ({
123 flexDirection: "row",
124 alignItems: "center",
125 paddingHorizontal: 14,
126 paddingVertical: 10,
127 gap: 10,
128 opacity: pressed ? 0.6 : 1,
129 borderBottomWidth: props.isLast ? 0 : 0.5,
130 borderBottomColor: "rgba(255,255,255,0.1)",
131 })}
132 >
133 {props.item.type === "path" ? (
134 <PierreEntryIcon path={props.item.path} kind={props.item.kind} size={16} />
135 ) : iconName ? (
136 <SymbolView name={iconName} size={14} tintColor={iconColor} type="monochrome" />
137 ) : null}
138 <Text className="shrink-0 text-base font-t3-medium text-foreground" numberOfLines={1}>
139 {props.item.label}
140 </Text>
141 {props.item.description ? (
142 <Text className="min-w-0 flex-1 text-xs text-zinc-400" numberOfLines={1}>
143 {props.item.description}
144 </Text>
145 ) : null}
146 </Pressable>
147 );
148});
149​
150export const ComposerCommandPopover = memo(function ComposerCommandPopover(
151 props: ComposerCommandPopoverProps,
152) {
153 const isDarkMode = useColorScheme() === "dark";
154 const label = groupLabel(props.triggerKind);
155​
156 return (
157 <PopoverSurface isDarkMode={isDarkMode}>
158 {label ? (
159 <View className="px-3.5 pt-2.5 pb-1">
160 <Text className="text-3xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
161 {label}
162 </Text>
163 </View>
164 ) : null}
165 {props.items.length > 0 ? (
166 <ScrollView
167 className="max-h-[180px]"
168 keyboardShouldPersistTaps="always"
169 showsVerticalScrollIndicator={false}
170 >
171 {props.items.map((item, index) => (
172 <CommandRow
173 key={item.id}
174 item={item}
175 onPress={() => props.onSelect(item)}
176 isLast={index === props.items.length - 1}
177 />
178 ))}
179 </ScrollView>
180 ) : (
181 <View className="px-3.5 py-2.5">
182 <Text className="text-xs text-foreground-tertiary">
183 {emptyText(props.triggerKind, props.isLoading)}
184 </Text>
185 </View>
186 )}
187 </PopoverSurface>
188 );
189});