@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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