| 1 | import { CheckIcon, PlayIcon, XMarkIcon } from "@heroicons/react/24/outline"; |
| 2 | import { ApplyState } from "core"; |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { getMetaKeyLabel } from "../../../util"; |
| 5 | import Spinner from "../../gui/Spinner"; |
| 6 | import { ToolTip } from "../../gui/Tooltip"; |
| 7 | import HoverItem from "../../mainInput/InputToolbar/HoverItem"; |
| 8 | import { ToolbarButtonWithTooltip } from "./ToolbarButtonWithTooltip"; |
| 9 | |
| 10 | interface ApplyActionsProps { |
| 11 | disableManualApply?: boolean; |
| 12 | applyState?: ApplyState; |
| 13 | onClickAccept: () => void; |
| 14 | onClickReject: () => void; |
| 15 | onClickApply: () => void; |
| 16 | } |
| 17 | |
| 18 | export function ApplyActions(props: ApplyActionsProps) { |
| 19 | const [hasRejected, setHasRejected] = useState(false); |
| 20 | const [showApplied, setShowApplied] = useState(false); |
| 21 | const isClosed = props.applyState?.status === "closed"; |
| 22 | const isSuccessful = !hasRejected && props.applyState?.numDiffs === 0; |
| 23 | |
| 24 | useEffect(() => { |
| 25 | if (isClosed && isSuccessful) { |
| 26 | setShowApplied(true); |
| 27 | const timer = setTimeout(() => { |
| 28 | setShowApplied(false); |
| 29 | }, 3_000); |
| 30 | return () => clearTimeout(timer); |
| 31 | } |
| 32 | }, [isClosed, isSuccessful]); |
| 33 | |
| 34 | function onClickReject() { |
| 35 | props.onClickReject(); |
| 36 | setHasRejected(true); |
| 37 | } |
| 38 | |
| 39 | const applyButton = (text: string) => ( |
| 40 | <HoverItem |
| 41 | data-tooltip-id="codeblock-apply-code-button-tooltip" |
| 42 | className="!p-0" |
| 43 | > |
| 44 | <button |
| 45 | data-testid="codeblock-toolbar-apply" |
| 46 | className="text-lightgray text-[${vscForeground}] flex cursor-pointer items-center border-none bg-transparent pl-0 text-xs outline-none hover:brightness-125" |
| 47 | onClick={props.onClickApply} |
| 48 | > |
| 49 | <div className="text-lightgray flex items-center gap-1"> |
| 50 | <PlayIcon className="h-3.5 w-3.5" /> |
| 51 | <span className="xs:inline hidden">{text}</span> |
| 52 | </div> |
| 53 | </button> |
| 54 | <ToolTip id="codeblock-apply-code-button-tooltip" place="top"> |
| 55 | {text} Code |
| 56 | </ToolTip> |
| 57 | </HoverItem> |
| 58 | ); |
| 59 | |
| 60 | switch (props.applyState ? props.applyState.status : null) { |
| 61 | case "not-started": |
| 62 | return ( |
| 63 | <div className="flex select-none items-center rounded bg-zinc-700 pl-2 pr-1"> |
| 64 | <span className="text-lightgray inline-flex w-min items-center gap-2 text-center text-xs"> |
| 65 | Pending |
| 66 | </span> |
| 67 | </div> |
| 68 | ); |
| 69 | case "streaming": |
| 70 | return ( |
| 71 | <div className="flex select-none items-center rounded bg-zinc-700 pl-2 pr-1"> |
| 72 | <span className="text-lightgray inline-flex w-min items-center gap-2 text-center text-xs"> |
| 73 | Applying |
| 74 | <Spinner /> |
| 75 | </span> |
| 76 | </div> |
| 77 | ); |
| 78 | case "done": |
| 79 | return ( |
| 80 | <div className="flex select-none items-center rounded bg-zinc-700 px-1.5 sm:gap-1"> |
| 81 | <span className="max-xs:hidden text-lightgray text-center text-xs"> |
| 82 | {`${props.applyState?.numDiffs === 1 ? "1 diff" : `${props.applyState?.numDiffs} diffs`}`} |
| 83 | </span> |
| 84 | |
| 85 | <div className="flex"> |
| 86 | <ToolbarButtonWithTooltip |
| 87 | data-testid="codeblock-toolbar-reject" |
| 88 | onClick={onClickReject} |
| 89 | tooltipContent={`Reject all (${getMetaKeyLabel()}⇧⌫)`} |
| 90 | > |
| 91 | <XMarkIcon className="h-3.5 w-3.5 text-red-600 hover:brightness-125" /> |
| 92 | </ToolbarButtonWithTooltip> |
| 93 | |
| 94 | <ToolbarButtonWithTooltip |
| 95 | data-testid="codeblock-toolbar-accept" |
| 96 | onClick={props.onClickAccept} |
| 97 | tooltipContent={`Accept all (${getMetaKeyLabel()}⇧⏎)`} |
| 98 | > |
| 99 | <CheckIcon className="h-3.5 w-3.5 text-green-600 hover:brightness-125" /> |
| 100 | </ToolbarButtonWithTooltip> |
| 101 | </div> |
| 102 | </div> |
| 103 | ); |
| 104 | case "closed": |
| 105 | if (isSuccessful) { |
| 106 | if (showApplied || props.disableManualApply) { |
| 107 | return ( |
| 108 | <span className="flex select-none items-center rounded bg-zinc-700 text-slate-400 max-sm:px-0.5 sm:pl-2"> |
| 109 | <span className="max-sm:hidden">Applied</span> |
| 110 | <CheckIcon className="h-3.5 w-3.5 hover:brightness-125 sm:px-1" /> |
| 111 | </span> |
| 112 | ); |
| 113 | } |
| 114 | return applyButton("Reapply"); |
| 115 | } |
| 116 | default: |
| 117 | if (props.disableManualApply) { |
| 118 | return null; |
| 119 | } |
| 120 | return applyButton("Apply"); |
| 121 | } |
| 122 | } |