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

ComposerCommandPopover.tsx

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