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