mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 05:37:20 +08:00
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
import React from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import GitService from "#/api/git-service/git-service.api";
|
|
import V1GitService from "#/api/git-service/v1-git-service.api";
|
|
import { useConversationId } from "#/hooks/use-conversation-id";
|
|
import { useActiveConversation } from "#/hooks/query/use-active-conversation";
|
|
import { useSettings } from "#/hooks/query/use-settings";
|
|
import { getGitPath } from "#/utils/get-git-path";
|
|
import type { GitChangeStatus } from "#/api/open-hands.types";
|
|
|
|
type UseUnifiedGitDiffConfig = {
|
|
filePath: string;
|
|
type: GitChangeStatus;
|
|
enabled: boolean;
|
|
};
|
|
|
|
/**
|
|
* Unified hook to get git diff for both legacy (V0) and V1 conversations
|
|
* - V0: Uses the legacy GitService.getGitChangeDiff API endpoint
|
|
* - V1: Uses the V1GitService.getGitChangeDiff API endpoint with runtime URL
|
|
*/
|
|
export const useUnifiedGitDiff = (config: UseUnifiedGitDiffConfig) => {
|
|
const { conversationId } = useConversationId();
|
|
const { data: conversation } = useActiveConversation();
|
|
const { data: settings } = useSettings();
|
|
|
|
const isV1Conversation = conversation?.conversation_version === "V1";
|
|
const conversationUrl = conversation?.url;
|
|
const sessionApiKey = conversation?.session_api_key;
|
|
const selectedRepository = conversation?.selected_repository;
|
|
|
|
// Sandbox grouping is enabled when strategy is not NO_GROUPING
|
|
const useSandboxGrouping =
|
|
settings?.sandbox_grouping_strategy !== "NO_GROUPING" &&
|
|
settings?.sandbox_grouping_strategy !== undefined;
|
|
|
|
// For V1, we need to convert the relative file path to an absolute path
|
|
// The diff endpoint expects: /workspace/project/RepoName/relative/path
|
|
const absoluteFilePath = React.useMemo(() => {
|
|
if (!isV1Conversation) return config.filePath;
|
|
|
|
const gitPath = getGitPath(
|
|
conversationId,
|
|
selectedRepository,
|
|
useSandboxGrouping,
|
|
);
|
|
return `${gitPath}/${config.filePath}`;
|
|
}, [
|
|
isV1Conversation,
|
|
conversationId,
|
|
selectedRepository,
|
|
useSandboxGrouping,
|
|
config.filePath,
|
|
]);
|
|
|
|
return useQuery({
|
|
queryKey: [
|
|
"file_diff",
|
|
conversationId,
|
|
config.filePath,
|
|
config.type,
|
|
isV1Conversation,
|
|
conversationUrl,
|
|
],
|
|
queryFn: async () => {
|
|
if (!conversationId) throw new Error("No conversation ID");
|
|
|
|
// V1: Use the V1 API endpoint with runtime URL and absolute path
|
|
if (isV1Conversation) {
|
|
return V1GitService.getGitChangeDiff(
|
|
conversationUrl,
|
|
sessionApiKey,
|
|
absoluteFilePath,
|
|
);
|
|
}
|
|
|
|
// V0 (Legacy): Use the legacy API endpoint with relative path
|
|
return GitService.getGitChangeDiff(conversationId, config.filePath);
|
|
},
|
|
enabled: config.enabled,
|
|
staleTime: 1000 * 60 * 5, // 5 minutes
|
|
gcTime: 1000 * 60 * 15, // 15 minutes
|
|
});
|
|
};
|