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

ComposerCommandPopover.tsx

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