diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx index 2c4e286c7..e645e68c9 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx @@ -1,7 +1,6 @@ import { CheckIcon, PlayIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { ApplyState } from "core"; import { useEffect, useState } from "react"; -import { lightGray, vscForeground } from "../.."; import { getMetaKeyLabel } from "../../../util"; import Spinner from "../../gui/Spinner"; import { ToolbarButtonWithTooltip } from "./ToolbarButtonWithTooltip"; @@ -14,7 +13,7 @@ interface ApplyActionsProps { onClickApply: () => void; } -export default function ApplyActions(props: ApplyActionsProps) { +export function ApplyActions(props: ApplyActionsProps) { const [hasRejected, setHasRejected] = useState(false); const [showApplied, setShowApplied] = useState(false); const isClosed = props.applyState?.status === "closed"; @@ -37,11 +36,10 @@ export default function ApplyActions(props: ApplyActionsProps) { const applyButton = (text: string) => ( + + ); +} diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/FileInfo.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/FileInfo.tsx index 7b204190b..550d1fd28 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/FileInfo.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/FileInfo.tsx @@ -1,25 +1,42 @@ import { getLastNPathParts } from "core/util/uri"; import FileIcon from "../../FileIcon"; +import { ToolTip } from "../../gui/Tooltip"; export interface FileInfoProps { filepath: string; - onClick: () => void; + onClick?: () => void; range?: string; } +const TOOLTIP_ID = "file-info-tooltip"; + export const FileInfo = ({ filepath, range, onClick }: FileInfoProps) => { return ( -
-
- + <> +
+
+ +
+ + {getLastNPathParts(filepath, 1)} + {range && ` ${range}`} +
- - {getLastNPathParts(filepath, 1)} - {range && ` ${range}`} - -
+ + ); }; diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx index c430c8d62..b730b30e5 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx @@ -5,7 +5,7 @@ export interface GeneratingCodeLoaderProps { codeBlockContent: string; } -export default function GeneratingCodeLoader({ +export function GeneratingCodeLoader({ showLineCount, codeBlockContent, }: GeneratingCodeLoaderProps) { @@ -16,7 +16,7 @@ export default function GeneratingCodeLoader({ : `${numLinesCodeBlock} lines generated`; return ( - + {showLineCount ? linesGeneratedText : "Generating"} diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/InsertButton.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/InsertButton.tsx index 7d56e012b..b8784dd07 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/InsertButton.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/InsertButton.tsx @@ -1,5 +1,4 @@ import { ArrowLeftEndOnRectangleIcon } from "@heroicons/react/24/outline"; -import { lightGray } from "../.."; /** * Button that inserts code at the current cursor position @@ -8,17 +7,14 @@ interface InsertButtonProps { onInsert: () => void; } -export default function InsertButton({ onInsert }: InsertButtonProps) { +export function InsertButton({ onInsert }: InsertButtonProps) { return (
-
- +
+
); diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/RunInTerminalButton.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/RunInTerminalButton.tsx index 35bc54349..07da9e7da 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/RunInTerminalButton.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/RunInTerminalButton.tsx @@ -7,9 +7,7 @@ interface RunInTerminalButtonProps { command: string; } -export default function RunInTerminalButton({ - command, -}: RunInTerminalButtonProps) { +export function RunInTerminalButton({ command }: RunInTerminalButtonProps) { const ideMessenger = useContext(IdeMessengerContext); function runInTerminal() { @@ -18,7 +16,7 @@ export default function RunInTerminalButton({ return (
<> - Run + Run
diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/StepContainerPreToolbar.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/StepContainerPreToolbar.tsx index 6da63b36b..9c0dcad82 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/StepContainerPreToolbar.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/StepContainerPreToolbar.tsx @@ -16,12 +16,13 @@ import { } from "../../../redux/slices/sessionSlice"; import { getFontSize } from "../../../util"; import { isTerminalCodeBlock } from "../utils"; -import ApplyActions from "./ApplyActions"; -import CopyButton from "./CopyButton"; +import { ApplyActions } from "./ApplyActions"; +import { CopyButton } from "./CopyButton"; +import { CreateFileButton } from "./CreateFileButton"; import { FileInfo } from "./FileInfo"; -import GeneratingCodeLoader from "./GeneratingCodeLoader"; -import InsertButton from "./InsertButton"; -import RunInTerminalButton from "./RunInTerminalButton"; +import { GeneratingCodeLoader } from "./GeneratingCodeLoader"; +import { InsertButton } from "./InsertButton"; +import { RunInTerminalButton } from "./RunInTerminalButton"; const TopDiv = styled.div` display: flex; @@ -59,7 +60,7 @@ export interface StepContainerPreToolbarProps { disableManualApply?: boolean; } -export function StepContainerPreToolbar({ +export default function StepContainerPreToolbar({ codeBlockContent, language, relativeFilepath, @@ -77,11 +78,8 @@ export function StepContainerPreToolbar({ expanded ?? (isInEditMode ? false : true), ); - // We store the resolved file URI to ensure we use the same file when - // accepting/rejecting diffs, even if no `relativeFilepath` is provided - const [resolvedFileUri, setResolvedFileUri] = useState( - undefined, - ); + const { fileExists, refreshFileExists } = useFileExists(relativeFilepath); + const nextCodeBlockIndex = useAppSelector( (state) => state.session.codeBlockApplyStates.curIndex, ); @@ -90,16 +88,27 @@ export function StepContainerPreToolbar({ selectApplyStateByStreamId(state, codeBlockStreamId), ); + /** + * In the case where `relativeFilepath` is defined, this will just be the resolved URI + * of `relativeFilepath`. However, if no `relativeFilepath` is defined, then this will + * be the URI of the currently open file at the time the user clicks "Apply". + */ + const [appliedFileUri, setAppliedFileUri] = useState( + undefined, + ); + const isNextCodeBlock = nextCodeBlockIndex === codeBlockIndex; const hasFileExtension = relativeFilepath && /\.[0-9a-z]+$/i.test(relativeFilepath); - const displayFilepath = relativeFilepath ?? resolvedFileUri; + // If we are creating a file, we already render that in the button + // so we don't want to dispaly it twice here + const displayFilepath = relativeFilepath ?? appliedFileUri; - async function getFileUri() { + async function getFileUriToApplyTo() { // If we've already resolved a file URI (from clicking apply), use that - if (resolvedFileUri) { - return resolvedFileUri; + if (appliedFileUri) { + return appliedFileUri; } // If a relative filepath was provided, try to resolve it @@ -120,23 +129,24 @@ export function StepContainerPreToolbar({ } async function onClickApply() { - const fileUri = await getFileUri(); + const fileUri = await getFileUriToApplyTo(); if (!fileUri) { ideMessenger.ide.showToast( "error", "Could not resolve filepath to apply changes", ); - return; } - setResolvedFileUri(fileUri); - + // applyToFile will create the file if it doesn't exist ideMessenger.post("applyToFile", { streamId: codeBlockStreamId, filepath: fileUri, text: codeBlockContent, }); + + setAppliedFileUri(fileUri); + refreshFileExists(); } function onClickInsertAtCursor() { @@ -153,7 +163,7 @@ export function StepContainerPreToolbar({ ); async function handleDiffAction(action: "accept" | "reject") { - const filepath = await getFileUri(); + const filepath = await getFileUriToApplyTo(); if (!filepath) { ideMessenger.ide.showToast( "error", @@ -167,13 +177,13 @@ export function StepContainerPreToolbar({ streamId: codeBlockStreamId, }); - setResolvedFileUri(undefined); + setAppliedFileUri(undefined); } function onClickFilename() { - if (resolvedFileUri) { + if (appliedFileUri) { ideMessenger.post("showFile", { - filepath: resolvedFileUri, + filepath: appliedFileUri, }); } @@ -184,8 +194,8 @@ export function StepContainerPreToolbar({ } } - // We want until there is an extension in the filepath to avoid rendering - // an incomplete filepath + // We wait until there is an extension in the filepath to avoid rendering + // an incomplete filepath if (relativeFilepath && !hasFileExtension) { return children; } @@ -196,7 +206,7 @@ export function StepContainerPreToolbar({
setIsExpanded(!isExpanded)} - className={`h-3.5 w-3.5 flex-shrink-0 cursor-pointer text-gray-400 hover:brightness-125 ${ + className={`text-lightgray h-3.5 w-3.5 flex-shrink-0 cursor-pointer hover:brightness-125 ${ isExpanded ? "rotate-0" : "-rotate-90" }`} /> @@ -204,10 +214,12 @@ export function StepContainerPreToolbar({ ) : ( - {language} + + {language} + )}
@@ -219,11 +231,15 @@ export function StepContainerPreToolbar({ /> ) : ( <> - - +
+ + +
{isTerminalCodeBlock(language, codeBlockContent) ? ( + ) : !fileExists ? ( + ) : ( (null); + + const { result: fileExists, refresh: refreshFileExists } = + useIdeMessengerRequest( + "fileExists", + resolvedUri ? { filepath: resolvedUri } : null, + ); + + useEffect(() => { + async function resolveUri() { + if (!filepath) { + setResolvedUri(null); + return; + } + + try { + const uri = await inferResolvedUriFromRelativePath( + filepath, + ideMessenger.ide, + ); + setResolvedUri(uri); + } catch (err) { + console.error("Failed to resolve URI:", err); + } + } + + resolveUri(); + }, [filepath, ideMessenger.ide]); + + console.log({ fileExists, resolvedUri }); + + return { fileExists, refreshFileExists }; +} diff --git a/gui/src/hooks/useIdeMessengerRequest.ts b/gui/src/hooks/useIdeMessengerRequest.ts new file mode 100644 index 000000000..af2dfb5ad --- /dev/null +++ b/gui/src/hooks/useIdeMessengerRequest.ts @@ -0,0 +1,37 @@ +import type { FromWebviewProtocol } from "core/protocol"; +import type { SuccessWebviewSingleMessage } from "core/protocol/util"; +import { useContext, useEffect, useState } from "react"; +import { IdeMessengerContext } from "../context/IdeMessenger"; + +/** + * Generic hook for making IDE messenger requests with automatic error handling and refresh capability + */ +export function useIdeMessengerRequest( + messageType: T, + data: FromWebviewProtocol[T][0] | null, +) { + const ideMessenger = useContext(IdeMessengerContext); + const [result, setResult] = useState< + SuccessWebviewSingleMessage["content"] | null + >(null); + + async function makeRequest() { + if (!data) { + setResult(null); + return; + } + + const response = await ideMessenger.request(messageType, data); + if (response.status === "success") { + setResult(response.content); + } else { + console.error(`Error in ${messageType} request:`, response.error); + } + } + + useEffect(() => { + makeRequest(); + }, [data, messageType]); + + return { result, refresh: makeRequest }; +}