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

ComposerCommandPopover.tsx

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