| 1 | import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline"; |
| 2 | import { ApplyState } from "core"; |
| 3 | import { useContext } from "react"; |
| 4 | import { IdeMessengerContext } from "../context/IdeMessenger"; |
| 5 | import { useAppDispatch } from "../redux/hooks"; |
| 6 | import { cancelToolCall } from "../redux/slices/sessionSlice"; |
| 7 | import { getMetaKeyLabel } from "../util"; |
| 8 | import { ToolTip } from "./gui/Tooltip"; |
| 9 | |
| 10 | export interface AcceptRejectAllButtonsProps { |
| 11 | applyStates: ApplyState[]; |
| 12 | onAcceptOrReject?: (outcome: AcceptOrRejectOutcome) => void; |
| 13 | } |
| 14 | |
| 15 | export type AcceptOrRejectOutcome = "acceptDiff" | "rejectDiff"; |
| 16 | |
| 17 | export default function AcceptRejectAllButtons({ |
| 18 | applyStates, |
| 19 | onAcceptOrReject, |
| 20 | }: AcceptRejectAllButtonsProps) { |
| 21 | const pendingApplyStates = applyStates.filter( |
| 22 | (state) => state.status === "done", |
| 23 | ); |
| 24 | const ideMessenger = useContext(IdeMessengerContext); |
| 25 | const dispatch = useAppDispatch(); |
| 26 | async function handleAcceptOrReject(status: AcceptOrRejectOutcome) { |
| 27 | // For reject operations, cancel all tool calls associated with pending apply states |
| 28 | if (status === "rejectDiff") { |
| 29 | for (const applyState of pendingApplyStates) { |
| 30 | if (applyState.toolCallId && applyState.status === "done") { |
| 31 | dispatch( |
| 32 | cancelToolCall({ |
| 33 | toolCallId: applyState.toolCallId, |
| 34 | }), |
| 35 | ); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Process all pending apply states |
| 41 | for (const { filepath = "", streamId } of pendingApplyStates) { |
| 42 | ideMessenger.post(status, { |
| 43 | filepath, |
| 44 | streamId, |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | if (onAcceptOrReject) { |
| 49 | onAcceptOrReject(status); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | const rejectShortcut = `${getMetaKeyLabel()}⇧⌫`; |
| 54 | const acceptShortcut = `${getMetaKeyLabel()}⇧⏎`; |
| 55 | |
| 56 | return ( |
| 57 | <div |
| 58 | className="flex flex-row items-center justify-evenly gap-3 px-3" |
| 59 | data-testid="accept-reject-all-buttons" |
| 60 | > |
| 61 | <ToolTip content={`Reject All (${rejectShortcut})`}> |
| 62 | <button |
| 63 | className="text-foreground flex cursor-pointer flex-row flex-wrap justify-center gap-1 border-none bg-transparent p-0 text-xs opacity-80 hover:opacity-100 hover:brightness-125" |
| 64 | onClick={() => handleAcceptOrReject("rejectDiff")} |
| 65 | data-testid="edit-reject-button" |
| 66 | > |
| 67 | <div className="flex flex-row items-center gap-1"> |
| 68 | <XMarkIcon className="text-error h-4 w-4" /> |
| 69 | <span className="hidden sm:inline">Reject</span> |
| 70 | <span className="text-lightgray -ml-1.5 hidden scale-75 text-xs md:inline"> |
| 71 | {rejectShortcut} |
| 72 | </span> |
| 73 | </div> |
| 74 | </button> |
| 75 | </ToolTip> |
| 76 | |
| 77 | <ToolTip content={`Accept All (${acceptShortcut})`}> |
| 78 | <button |
| 79 | className="text-foreground flex cursor-pointer flex-row flex-wrap justify-center gap-1 border-none bg-transparent p-0 text-xs opacity-80 hover:opacity-100 hover:brightness-125" |
| 80 | onClick={() => handleAcceptOrReject("acceptDiff")} |
| 81 | data-testid="edit-accept-button" |
| 82 | > |
| 83 | <div className="flex flex-row items-center gap-1"> |
| 84 | <CheckIcon className="text-success h-4 w-4" /> |
| 85 | <span className="hidden sm:inline">Accept</span> |
| 86 | <span className="text-lightgray -ml-1.5 hidden scale-75 text-xs md:inline"> |
| 87 | {acceptShortcut} |
| 88 | </span> |
| 89 | </div> |
| 90 | </button> |
| 91 | </ToolTip> |
| 92 | </div> |
| 93 | ); |
| 94 | } |