Elffuss BraidHub

kikocis/continue public

68dbf00e2c2f · 5.8 KiBRawPermalink
1import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline";
2import { ContextItemWithId } from "core";
3import { ctxItemToRifWithContents } from "core/commands/util";
4import { getUriPathBasename } from "core/util/uri";
5import { ComponentType, useContext, useMemo } from "react";
6import { AnimatedEllipsis } from "../..";
7import { IdeMessengerContext } from "../../../context/IdeMessenger";
8import { useAppSelector } from "../../../redux/hooks";
9import { selectIsGatheringContext } from "../../../redux/slices/sessionSlice";
10import FileIcon from "../../FileIcon";
11import SafeImg from "../../SafeImg";
12import ToggleDiv from "../../ToggleDiv";
13import { getIconFromDropdownItem } from "../AtMentionDropdown";
14import { NAMED_ICONS } from "../icons";
15
16interface ContextItemsPeekProps {
17 contextItems?: ContextItemWithId[];
18 isCurrentContextPeek: boolean;
19 icon?: ComponentType<React.SVGProps<SVGSVGElement>>;
20 title?: JSX.Element | string;
21}
22
23interface ContextItemsPeekItemProps {
24 contextItem: ContextItemWithId;
25}
26
27export function ContextItemsPeekItem({
28 contextItem,
29}: ContextItemsPeekItemProps) {
30 const ideMessenger = useContext(IdeMessengerContext);
31 const isUrl = contextItem.uri?.type === "url";
32
33 function openContextItem() {
34 const { uri, name, content } = contextItem;
35
36 if (uri && uri?.type === "file") {
37 const isRangeInFile = name.includes(" (") && name.endsWith(")");
38
39 if (isRangeInFile) {
40 const rif = ctxItemToRifWithContents(contextItem, true);
41 void ideMessenger.ide.showLines(
42 rif.filepath,
43 rif.range.start.line,
44 rif.range.end.line,
45 );
46 } else {
47 void ideMessenger.ide.openFile(uri.value);
48 }
49 } else {
50 void ideMessenger.ide.showVirtualFile(name, content);
51 }
52 }
53
54 function getContextItemIcon() {
55 const dimensions = "18px";
56
57 if (contextItem.icon) {
58 const NamedIcon = contextItem.icon
59 ? NAMED_ICONS[contextItem.icon]
60 : undefined;
61 return (
62 <SafeImg
63 className="mr-2 flex-shrink-0 rounded-md p-1"
64 src={contextItem.icon}
65 height={dimensions}
66 width={dimensions}
67 fallback={
68 NamedIcon ? (
69 <div
70 className="mr-2 flex-shrink-0"
71 style={{
72 height: dimensions,
73 width: dimensions,
74 }}
75 >
76 <NamedIcon />
77 </div>
78 ) : null
79 }
80 />
81 );
82 }
83
84 // Heuristic to check if it's a file
85 const shouldShowFileIcon =
86 contextItem.content.includes("```") || contextItem.uri?.type === "file";
87
88 if (shouldShowFileIcon) {
89 return (
90 <div className="mr-2 flex-shrink-0">
91 <FileIcon
92 filename={
93 contextItem.description.split(" ").shift()?.split("#").shift() ||
94 ""
95 }
96 height={dimensions}
97 width={dimensions}
98 />
99 </div>
100 );
101 }
102
103 const ProviderIcon = getIconFromDropdownItem(
104 contextItem.id.providerTitle,
105 "contextProvider",
106 );
107
108 return (
109 <ProviderIcon
110 className="mr-2 flex-shrink-0"
111 height={dimensions}
112 width={dimensions}
113 />
114 );
115 }
116
117 return (
118 <div
119 onClick={openContextItem}
120 className="group mr-2 flex cursor-pointer items-center overflow-hidden text-ellipsis whitespace-nowrap rounded px-1.5 py-1 text-xs hover:bg-white/10"
121 data-testid="context-items-peek-item"
122 >
123 <div className="flex w-full items-center">
124 {getContextItemIcon()}
125 <div className="flex min-w-0 flex-1 gap-2 text-xs">
126 <div
127 className={`max-w-[50%] flex-shrink-0 truncate ${isUrl ? "hover:underline" : ""}`}
128 >
129 {contextItem.name}
130 </div>
131 <div
132 className={`min-w-0 flex-1 overflow-hidden truncate whitespace-nowrap text-xs text-gray-400 ${isUrl ? "hover:underline" : ""}`}
133 onClick={
134 isUrl
135 ? (e) => {
136 if (contextItem.uri?.value) {
137 e.stopPropagation();
138 ideMessenger.post("openUrl", contextItem.uri.value);
139 } else {
140 console.error("Couldn't open url", contextItem.uri);
141 }
142 }
143 : undefined
144 }
145 >
146 {contextItem.uri?.type === "file"
147 ? getUriPathBasename(contextItem.description)
148 : contextItem.description}
149 </div>
150 </div>
151
152 {isUrl && (
153 <ArrowTopRightOnSquareIcon className="mx-2 h-4 w-4 flex-shrink-0 text-gray-400 opacity-0 group-hover:opacity-100" />
154 )}
155 </div>
156 </div>
157 );
158}
159
160export function ContextItemsPeek({
161 contextItems,
162 isCurrentContextPeek,
163 icon,
164 title,
165}: ContextItemsPeekProps) {
166 const ctxItems = useMemo(() => {
167 return contextItems?.filter((ctxItem) => !ctxItem.hidden) ?? [];
168 }, [contextItems]);
169
170 const isGatheringContext = useAppSelector(selectIsGatheringContext);
171
172 const indicateIsGathering = isCurrentContextPeek && isGatheringContext;
173
174 if ((!ctxItems || ctxItems.length === 0) && !indicateIsGathering) {
175 return null;
176 }
177
178 return (
179 <ToggleDiv
180 icon={icon}
181 title={
182 title ??
183 (isGatheringContext ? (
184 <>
185 Gathering context
186 <AnimatedEllipsis />
187 </>
188 ) : (
189 `${ctxItems.length} context ${ctxItems.length > 1 ? "items" : "item"}`
190 ))
191 }
192 >
193 {ctxItems.length ? (
194 ctxItems.map((contextItem, idx) => (
195 <ContextItemsPeekItem key={idx} contextItem={contextItem} />
196 ))
197 ) : (
198 <div className="pl-2 text-xs italic text-gray-400">No results</div>
199 )}
200 </ToggleDiv>
201 );
202}