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

ComposerCommandPopover.tsx

202 lines5.8 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 onPress={props.onPress}
131 className="flex-row items-center gap-2.5 border-border px-3.5 py-2.5 active:opacity-60"
132 style={{ borderBottomWidth: props.isLast ? 0 : StyleSheet.hairlineWidth }}
133 >
134 {props.item.type === "path" ? (
135 <PierreEntryIcon path={props.item.path} kind={props.item.kind} size={16} />
136 ) : iconName ? (
137 <SymbolView
138 name={iconName}
139 size={14}
140 tintColorClassName={"accent-icon-subtle"}
141 type="monochrome"
142 />
143 ) : null}
144 <Text className="shrink-0 text-base font-t3-medium text-foreground" numberOfLines={1}>
145 {props.isSlashSkill && props.item.type === "skill" ? (
146 <>
147 <Text className="text-foreground-muted">skill:</Text>
148 {props.item.skill.name}
149 </>
150 ) : (
151 props.item.label
152 )}
153 </Text>
154 {props.item.description ? (
155 <Text className="min-w-0 flex-1 text-xs text-foreground-muted" numberOfLines={1}>
156 {props.item.description}
157 </Text>
158 ) : null}
159 </Pressable>
160 );
161});
162​
163export const ComposerCommandPopover = memo(function ComposerCommandPopover(
164 props: ComposerCommandPopoverProps,
165) {
166 const label = groupLabel(props.triggerKind);
167​
168 return (
169 <PopoverSurface>
170 {label ? (
171 <View className="px-3.5 pt-2.5 pb-1">
172 <Text className="text-3xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
173 {label}
174 </Text>
175 </View>
176 ) : null}
177 {props.items.length > 0 ? (
178 <ScrollView
179 className="max-h-[180px]"
180 keyboardShouldPersistTaps="always"
181 showsVerticalScrollIndicator={false}
182 >
183 {props.items.map((item, index) => (
184 <CommandRow
185 key={item.id}
186 item={item}
187 onPress={() => props.onSelect(item)}
188 isLast={index === props.items.length - 1}
189 isSlashSkill={props.triggerKind === "slash-command" && item.type === "skill"}
190 />
191 ))}
192 </ScrollView>
193 ) : (
194 <View className="px-3.5 py-2.5">
195 <Text className="text-xs text-foreground-tertiary">
196 {emptyText(props.triggerKind, props.isLoading)}
197 </Text>
198 </View>
199 )}
200 </PopoverSurface>
201 );
202});