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

ComposerCommandPopover.tsx

249 lines7.2 KB

1import {
2 resolveProviderSkillSourceKind,
3 type ProviderSkillSourceKind,
4} from "@t3tools/client-runtime/providerSkills";
5import type {
6 EnvironmentId,
7 EnvironmentMachineKind,
8 PullRequestContextMetadata,
9 ScopedThreadRef,
10 ServerProviderSkill,
11 ServerProviderSlashCommand,
12} from "@t3tools/contracts";
13import type { ComposerTriggerKind } from "@t3tools/shared/composerTrigger";
14import { memo } from "react";
15import { Pressable, ScrollView, StyleSheet, View, type ViewStyle } from "react-native";
16​
17import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
18import { AppText as Text } from "../../components/ui/AppText";
19import { ENVIRONMENT_MACHINE_SYMBOLS } from "../../components/EnvironmentMachineSymbol";
20import { GlassSurface } from "../../components/ui/GlassSurface";
21import { PierreEntryIcon } from "../../components/PierreEntryIcon";
22import { pullRequestStatus } from "../pull-requests/pullRequestStatus";
23export type ComposerCommandItem =
24 | {
25 readonly id: string;
26 readonly type: "pull-request";
27 readonly pullRequest: PullRequestContextMetadata;
28 readonly label: string;
29 readonly description: string;
30 }
31 | {
32 readonly id: string;
33 readonly type: "path";
34 readonly path: string;
35 readonly kind: "file" | "directory";
36 readonly label: string;
37 readonly description: string;
38 }
39 | {
40 readonly id: string;
41 readonly type: "thread";
42 readonly thread: ScopedThreadRef;
43 readonly label: string;
44 readonly description: string;
45 }
46 | {
47 readonly id: string;
48 readonly type: "environment";
49 readonly environmentId: EnvironmentId;
50 readonly machine: EnvironmentMachineKind;
51 readonly label: string;
52 readonly description: string;
53 }
54 | {
55 readonly id: string;
56 readonly type: "slash-command";
57 readonly command: string;
58 readonly label: string;
59 readonly description: string;
60 }
61 | {
62 readonly id: string;
63 readonly type: "provider-slash-command";
64 readonly command: ServerProviderSlashCommand;
65 readonly label: string;
66 readonly description: string;
67 }
68 | {
69 readonly id: string;
70 readonly type: "skill";
71 readonly skill: ServerProviderSkill;
72 readonly label: string;
73 readonly description: string;
74 };
75​
76interface ComposerCommandPopoverProps {
77 readonly items: ReadonlyArray<ComposerCommandItem>;
78 readonly triggerKind: ComposerTriggerKind | null;
79 readonly isLoading: boolean;
80 readonly error?: string | null;
81 readonly onSelect: (item: ComposerCommandItem) => void;
83​
84function PopoverSurface(props: { readonly children: React.ReactNode; readonly style?: ViewStyle }) {
85 const baseStyle: ViewStyle = {
86 borderRadius: 16,
87 overflow: "hidden",
88 ...props.style,
89 };
90​
91 return (
92 <GlassSurface
93 glassEffectStyle="clear"
94 tintColorClassName="accent-glass-surface"
95 style={baseStyle}
96 >
97 {props.children}
98 </GlassSurface>
99 );
101​
102const SKILL_SOURCE_SYMBOL_BY_KIND: Record<ProviderSkillSourceKind, AppSymbolName> = {
103 app: "square.grid.2x2",
104 repo: "folder",
105 project: "folder",
106 personal: "person.crop.circle",
107 system: "gearshape",
108 other: "cube",
109};
110​
111function itemIcon(item: ComposerCommandItem): AppSymbolName | null {
112 switch (item.type) {
113 case "pull-request":
114 return pullRequestStatus(item.pullRequest).icon;
115 case "slash-command":
116 case "provider-slash-command":
117 return "terminal";
118 case "skill":
119 return SKILL_SOURCE_SYMBOL_BY_KIND[resolveProviderSkillSourceKind(item.skill)];
120 case "path":
121 return null;
122 case "thread":
123 return "text.bubble";
124 case "environment":
125 return ENVIRONMENT_MACHINE_SYMBOLS[item.machine];
126 }
128​
129function groupLabel(triggerKind: ComposerTriggerKind | null): string | null {
130 switch (triggerKind) {
131 case "pull-request":
132 return "Pull requests";
133 case "slash-command":
134 return "Commands";
135 case "skill":
136 return "Skills";
137 case "path":
138 return "Files";
139 default:
140 return null;
141 }
143​
144function emptyText(triggerKind: ComposerTriggerKind | null, isLoading: boolean): string {
145 if (isLoading) {
146 return triggerKind === "path" ? "Searching files…" : "Loading…";
147 }
148 switch (triggerKind) {
149 case "pull-request":
150 return "No matching pull requests.";
151 case "path":
152 return "No matching files or folders.";
153 case "skill":
154 return "No skills found.";
155 case "slash-command":
156 return "No matching commands.";
157 default:
158 return "No results.";
159 }
161​
162const CommandRow = memo(function CommandRow(props: {
163 readonly item: ComposerCommandItem;
164 readonly onPress: () => void;
165 readonly isLast: boolean;
166 readonly isSlashSkill: boolean;
167}) {
168 const iconName = itemIcon(props.item);
169​
170 return (
171 <Pressable
172 accessibilityRole="button"
173 onPress={props.onPress}
174 className="flex-row items-center gap-2.5 border-border px-3.5 py-2.5 active:opacity-60"
175 style={{ borderBottomWidth: props.isLast ? 0 : StyleSheet.hairlineWidth }}
176 >
177 {props.item.type === "path" ? (
178 <PierreEntryIcon path={props.item.path} kind={props.item.kind} size={16} />
179 ) : iconName ? (
180 <SymbolView
181 name={iconName}
182 size={14}
183 tintColorClassName={
184 props.item.type === "pull-request"
185 ? pullRequestStatus(props.item.pullRequest).tint
186 : "accent-icon-subtle"
187 }
188 type="monochrome"
189 />
190 ) : null}
191 <Text className="shrink-0 text-base font-t3-medium text-foreground" numberOfLines={1}>
192 {props.isSlashSkill && props.item.type === "skill" ? (
193 <>
194 <Text className="text-foreground-muted">skill:</Text>
195 {props.item.skill.name}
196 </>
197 ) : (
198 props.item.label
199 )}
200 </Text>
201 {props.item.description ? (
202 <Text className="min-w-0 flex-1 text-xs text-foreground-muted" numberOfLines={1}>
203 {props.item.description}
204 </Text>
205 ) : null}
206 </Pressable>
207 );
208});
209​
210export const ComposerCommandPopover = memo(function ComposerCommandPopover(
211 props: ComposerCommandPopoverProps,
212) {
213 const label = groupLabel(props.triggerKind);
214​
215 return (
216 <PopoverSurface>
217 {label ? (
218 <View className="px-3.5 pt-2.5 pb-1">
219 <Text className="text-3xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
220 {label}
221 </Text>
222 </View>
223 ) : null}
224 {props.items.length > 0 ? (
225 <ScrollView
226 className="max-h-[180px]"
227 keyboardShouldPersistTaps="always"
228 showsVerticalScrollIndicator={false}
229 >
230 {props.items.map((item, index) => (
231 <CommandRow
232 key={item.id}
233 item={item}
234 onPress={() => props.onSelect(item)}
235 isLast={index === props.items.length - 1}
236 isSlashSkill={props.triggerKind === "slash-command" && item.type === "skill"}
237 />
238 ))}
239 </ScrollView>
240 ) : (
241 <View className="px-3.5 py-2.5">
242 <Text className="text-xs text-foreground-tertiary">
243 {props.error ?? emptyText(props.triggerKind, props.isLoading)}
244 </Text>
245 </View>
246 )}
247 </PopoverSurface>
248 );
249});