forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(frontend): Prevent VSCode from opening when remounting (All-Hands…
- Loading branch information
Showing
2 changed files
with
30 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,13 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import React from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { useDispatch } from "react-redux"; | ||
import toast from "#/utils/toast"; | ||
import { addAssistantMessage } from "#/state/chat-slice"; | ||
import { I18nKey } from "#/i18n/declaration"; | ||
import OpenHands from "#/api/open-hands"; | ||
|
||
export const useVSCodeUrl = () => { | ||
const { t } = useTranslation(); | ||
const dispatch = useDispatch(); | ||
|
||
export const useVSCodeUrl = (config: { enabled: boolean }) => { | ||
const data = useQuery({ | ||
queryKey: ["vscode_url"], | ||
queryFn: OpenHands.getVSCodeUrl, | ||
enabled: false, | ||
enabled: config.enabled, | ||
refetchOnMount: false, | ||
}); | ||
|
||
const { data: vscodeUrlObject, isFetching } = data; | ||
|
||
React.useEffect(() => { | ||
if (isFetching) return; | ||
|
||
if (vscodeUrlObject?.vscode_url) { | ||
dispatch( | ||
addAssistantMessage( | ||
"You opened VS Code. Please inform the agent of any changes you made to the workspace or environment. To avoid conflicts, it's best to pause the agent before making any changes.", | ||
), | ||
); | ||
window.open(vscodeUrlObject.vscode_url, "_blank"); | ||
} else if (vscodeUrlObject?.error) { | ||
toast.error( | ||
`open-vscode-error-${new Date().getTime()}`, | ||
t(I18nKey.EXPLORER$VSCODE_SWITCHING_ERROR_MESSAGE, { | ||
error: vscodeUrlObject.error, | ||
}), | ||
); | ||
} | ||
}, [vscodeUrlObject, isFetching]); | ||
|
||
return data; | ||
}; |