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

ComposerCommandPopover.tsx

183 lines5.3 KB

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