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

ComposerCommandPopover.tsx

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