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

ComposerCommandPopover.tsx

203 lines5.9 KB

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