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