| 1 | import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline"; |
| 2 | import { ContextItemWithId } from "core"; |
| 3 | import { ctxItemToRifWithContents } from "core/commands/util"; |
| 4 | import { getUriPathBasename } from "core/util/uri"; |
| 5 | import { ComponentType, useContext, useMemo } from "react"; |
| 6 | import { AnimatedEllipsis } from "../.."; |
| 7 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 8 | import { useAppSelector } from "../../../redux/hooks"; |
| 9 | import { selectIsGatheringContext } from "../../../redux/slices/sessionSlice"; |
| 10 | import FileIcon from "../../FileIcon"; |
| 11 | import SafeImg from "../../SafeImg"; |
| 12 | import ToggleDiv from "../../ToggleDiv"; |
| 13 | import { getIconFromDropdownItem } from "../AtMentionDropdown"; |
| 14 | import { NAMED_ICONS } from "../icons"; |
| 15 | |
| 16 | interface ContextItemsPeekProps { |
| 17 | contextItems?: ContextItemWithId[]; |
| 18 | isCurrentContextPeek: boolean; |
| 19 | icon?: ComponentType<React.SVGProps<SVGSVGElement>>; |
| 20 | title?: JSX.Element | string; |
| 21 | } |
| 22 | |
| 23 | interface ContextItemsPeekItemProps { |
| 24 | contextItem: ContextItemWithId; |
| 25 | } |
| 26 | |
| 27 | export 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 (isUrl) { |
| 37 | if (uri?.value) { |
| 38 | ideMessenger.post("openUrl", uri.value); |
| 39 | } else { |
| 40 | console.error("Couldn't open url", uri); |
| 41 | } |
| 42 | } else if (uri) { |
| 43 | const isRangeInFile = name.includes(" (") && name.endsWith(")"); |
| 44 | |
| 45 | if (isRangeInFile) { |
| 46 | const rif = ctxItemToRifWithContents(contextItem, true); |
| 47 | ideMessenger.ide.showLines( |
| 48 | rif.filepath, |
| 49 | rif.range.start.line, |
| 50 | rif.range.end.line, |
| 51 | ); |
| 52 | } else { |
| 53 | ideMessenger.ide.openFile(uri.value); |
| 54 | } |
| 55 | } else { |
| 56 | ideMessenger.ide.showVirtualFile(name, content); |
| 57 | } |
| 58 | } |
| 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} |
| 126 | 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" |
| 127 | data-testid="context-items-peek-item" |
| 128 | > |
| 129 | <div className="flex w-full items-center"> |
| 130 | {getContextItemIcon()} |
| 131 | <div className="flex min-w-0 flex-1 gap-2 text-xs"> |
| 132 | <div |
| 133 | className={`max-w-[50%] flex-shrink-0 truncate ${isUrl ? "hover:underline" : ""}`} |
| 134 | > |
| 135 | {contextItem.name} |
| 136 | </div> |
| 137 | <div |
| 138 | className={`min-w-0 flex-1 overflow-hidden truncate whitespace-nowrap text-xs text-gray-400 ${isUrl ? "hover:underline" : ""}`} |
| 139 | > |
| 140 | {contextItem.uri?.type === "file" |
| 141 | ? getUriPathBasename(contextItem.description) |
| 142 | : contextItem.description} |
| 143 | </div> |
| 144 | </div> |
| 145 | |
| 146 | {isUrl && ( |
| 147 | <ArrowTopRightOnSquareIcon className="mx-2 h-4 w-4 flex-shrink-0 text-gray-400 opacity-0 group-hover:opacity-100" /> |
| 148 | )} |
| 149 | </div> |
| 150 | </div> |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | export function ContextItemsPeek({ |
| 155 | contextItems, |
| 156 | isCurrentContextPeek, |
| 157 | icon, |
| 158 | title, |
| 159 | }: ContextItemsPeekProps) { |
| 160 | const ctxItems = useMemo(() => { |
| 161 | return contextItems?.filter((ctxItem) => !ctxItem.hidden) ?? []; |
| 162 | }, [contextItems]); |
| 163 | |
| 164 | const isGatheringContext = useAppSelector(selectIsGatheringContext); |
| 165 | |
| 166 | const indicateIsGathering = isCurrentContextPeek && isGatheringContext; |
| 167 | |
| 168 | if ((!ctxItems || ctxItems.length === 0) && !indicateIsGathering) { |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | return ( |
| 173 | <ToggleDiv |
| 174 | icon={icon} |
| 175 | title={ |
| 176 | title ?? |
| 177 | (isGatheringContext ? ( |
| 178 | <> |
| 179 | Gathering context |
| 180 | <AnimatedEllipsis /> |
| 181 | </> |
| 182 | ) : ( |
| 183 | `${ctxItems.length} context ${ctxItems.length > 1 ? "items" : "item"}` |
| 184 | )) |
| 185 | } |
| 186 | > |
| 187 | {ctxItems.length ? ( |
| 188 | ctxItems.map((contextItem, idx) => ( |
| 189 | <ContextItemsPeekItem key={idx} contextItem={contextItem} /> |
| 190 | )) |
| 191 | ) : ( |
| 192 | <div className="pl-2 text-xs italic text-gray-400">No results</div> |
| 193 | )} |
| 194 | </ToggleDiv> |
| 195 | ); |
| 196 | } |