Elffuss BraidHub

kikocis/continue public

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