TaskHeader.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { VSCodeButton } from "@vscode/webview-ui-toolkit/react"
  2. import React, { useEffect, useRef, useState } from "react"
  3. import { useWindowSize } from "react-use"
  4. import { ClaudeMessage } from "../../../src/shared/ExtensionMessage"
  5. import { useExtensionState } from "../context/ExtensionStateContext"
  6. import { vscode } from "../utils/vscode"
  7. import Thumbnails from "./Thumbnails"
  8. interface TaskHeaderProps {
  9. task: ClaudeMessage
  10. tokensIn: number
  11. tokensOut: number
  12. doesModelSupportPromptCache: boolean
  13. cacheWrites?: number
  14. cacheReads?: number
  15. totalCost: number
  16. onClose: () => void
  17. }
  18. const TaskHeader: React.FC<TaskHeaderProps> = ({
  19. task,
  20. tokensIn,
  21. tokensOut,
  22. doesModelSupportPromptCache,
  23. cacheWrites,
  24. cacheReads,
  25. totalCost,
  26. onClose,
  27. }) => {
  28. const { apiConfiguration } = useExtensionState()
  29. const [isExpanded, setIsExpanded] = useState(false)
  30. const [showSeeMore, setShowSeeMore] = useState(false)
  31. const textContainerRef = useRef<HTMLDivElement>(null)
  32. const textRef = useRef<HTMLDivElement>(null)
  33. /*
  34. When dealing with event listeners in React components that depend on state variables, we face a challenge. We want our listener to always use the most up-to-date version of a callback function that relies on current state, but we don't want to constantly add and remove event listeners as that function updates. This scenario often arises with resize listeners or other window events. Simply adding the listener in a useEffect with an empty dependency array risks using stale state, while including the callback in the dependencies can lead to unnecessary re-registrations of the listener. There are react hook libraries that provide a elegant solution to this problem by utilizing the useRef hook to maintain a reference to the latest callback function without triggering re-renders or effect re-runs. This approach ensures that our event listener always has access to the most current state while minimizing performance overhead and potential memory leaks from multiple listener registrations.
  35. Sources
  36. - https://usehooks-ts.com/react-hook/use-event-listener
  37. - https://streamich.github.io/react-use/?path=/story/sensors-useevent--docs
  38. - https://github.com/streamich/react-use/blob/master/src/useEvent.ts
  39. - https://stackoverflow.com/questions/55565444/how-to-register-event-with-useeffect-hooks
  40. Before:
  41. const updateMaxHeight = useCallback(() => {
  42. if (isExpanded && textContainerRef.current) {
  43. const maxHeight = window.innerHeight * (3 / 5)
  44. textContainerRef.current.style.maxHeight = `${maxHeight}px`
  45. }
  46. }, [isExpanded])
  47. useEffect(() => {
  48. updateMaxHeight()
  49. }, [isExpanded, updateMaxHeight])
  50. useEffect(() => {
  51. window.removeEventListener("resize", updateMaxHeight)
  52. window.addEventListener("resize", updateMaxHeight)
  53. return () => {
  54. window.removeEventListener("resize", updateMaxHeight)
  55. }
  56. }, [updateMaxHeight])
  57. After:
  58. */
  59. const { height: windowHeight, width: windowWidth } = useWindowSize()
  60. useEffect(() => {
  61. if (isExpanded && textContainerRef.current) {
  62. const maxHeight = windowHeight * (1 / 2)
  63. textContainerRef.current.style.maxHeight = `${maxHeight}px`
  64. }
  65. }, [isExpanded, windowHeight])
  66. useEffect(() => {
  67. if (textRef.current && textContainerRef.current) {
  68. let textContainerHeight = textContainerRef.current.clientHeight
  69. if (!textContainerHeight) {
  70. textContainerHeight = textContainerRef.current.getBoundingClientRect().height
  71. }
  72. const isOverflowing = textRef.current.scrollHeight > textContainerHeight
  73. // necessary to show see more button again if user resizes window to expand and then back to collapse
  74. if (!isOverflowing) {
  75. setIsExpanded(false)
  76. }
  77. setShowSeeMore(isOverflowing)
  78. }
  79. }, [task.text, windowWidth])
  80. const toggleExpand = () => setIsExpanded(!isExpanded)
  81. const handleDownload = () => {
  82. vscode.postMessage({ type: "exportCurrentTask" })
  83. }
  84. const ExportButton = () => (
  85. <VSCodeButton
  86. appearance="icon"
  87. onClick={handleDownload}
  88. style={{
  89. marginBottom: "-2px",
  90. marginRight: "-2.5px",
  91. }}>
  92. <div style={{ fontSize: "10.5px", fontWeight: "bold", opacity: 0.6 }}>EXPORT</div>
  93. </VSCodeButton>
  94. )
  95. return (
  96. <div style={{ padding: "10px 13px 10px 13px" }}>
  97. <div
  98. style={{
  99. backgroundColor: "var(--vscode-badge-background)",
  100. color: "var(--vscode-badge-foreground)",
  101. borderRadius: "3px",
  102. padding: "12px",
  103. display: "flex",
  104. flexDirection: "column",
  105. gap: "8px",
  106. position: "relative",
  107. zIndex: 1,
  108. }}>
  109. <div
  110. style={{
  111. display: "flex",
  112. justifyContent: "space-between",
  113. alignItems: "center",
  114. }}>
  115. <span style={{ fontWeight: "bold", fontSize: "16px" }}>Task</span>
  116. <VSCodeButton
  117. appearance="icon"
  118. onClick={onClose}
  119. style={{ marginTop: "-6px", marginRight: "-4px" }}>
  120. <span className="codicon codicon-close"></span>
  121. </VSCodeButton>
  122. </div>
  123. <div
  124. ref={textContainerRef}
  125. style={{
  126. fontSize: "var(--vscode-font-size)",
  127. overflowY: isExpanded ? "auto" : "hidden",
  128. wordBreak: "break-word",
  129. overflowWrap: "anywhere",
  130. position: "relative",
  131. }}>
  132. <div
  133. ref={textRef}
  134. style={{
  135. display: "-webkit-box",
  136. WebkitLineClamp: isExpanded ? "unset" : 3,
  137. WebkitBoxOrient: "vertical",
  138. overflow: "hidden",
  139. whiteSpace: "pre-wrap",
  140. wordBreak: "break-word",
  141. overflowWrap: "anywhere",
  142. }}>
  143. {task.text}
  144. </div>
  145. {!isExpanded && showSeeMore && (
  146. <div
  147. style={{
  148. position: "absolute",
  149. right: 0,
  150. bottom: 0,
  151. display: "flex",
  152. alignItems: "center",
  153. }}>
  154. <div
  155. style={{
  156. width: 30,
  157. height: "1.2em",
  158. background:
  159. "linear-gradient(to right, transparent, var(--vscode-badge-background))",
  160. }}
  161. />
  162. <div
  163. style={{
  164. cursor: "pointer",
  165. color: "var(--vscode-textLink-foreground)",
  166. paddingRight: 0,
  167. paddingLeft: 3,
  168. backgroundColor: "var(--vscode-badge-background)",
  169. }}
  170. onClick={toggleExpand}>
  171. See more
  172. </div>
  173. </div>
  174. )}
  175. </div>
  176. {isExpanded && showSeeMore && (
  177. <div
  178. style={{
  179. cursor: "pointer",
  180. color: "var(--vscode-textLink-foreground)",
  181. marginLeft: "auto",
  182. textAlign: "right",
  183. paddingRight: 0,
  184. }}
  185. onClick={toggleExpand}>
  186. See less
  187. </div>
  188. )}
  189. {task.images && task.images.length > 0 && <Thumbnails images={task.images} />}
  190. <div style={{ display: "flex", flexDirection: "column", gap: "4px" }}>
  191. <div
  192. style={{
  193. display: "flex",
  194. justifyContent: "space-between",
  195. alignItems: "center",
  196. }}>
  197. <div style={{ display: "flex", alignItems: "center", gap: "4px", flexWrap: "wrap" }}>
  198. <span style={{ fontWeight: "bold" }}>Tokens:</span>
  199. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  200. <i
  201. className="codicon codicon-arrow-up"
  202. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-2px" }}
  203. />
  204. {tokensIn?.toLocaleString()}
  205. </span>
  206. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  207. <i
  208. className="codicon codicon-arrow-down"
  209. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-2px" }}
  210. />
  211. {tokensOut?.toLocaleString()}
  212. </span>
  213. </div>
  214. {apiConfiguration?.apiProvider === "openai" && <ExportButton />}
  215. </div>
  216. {(doesModelSupportPromptCache || cacheReads !== undefined || cacheWrites !== undefined) && (
  217. <div style={{ display: "flex", alignItems: "center", gap: "4px", flexWrap: "wrap" }}>
  218. <span style={{ fontWeight: "bold" }}>Cache:</span>
  219. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  220. <i
  221. className="codicon codicon-database"
  222. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-1px" }}
  223. />
  224. +{(cacheWrites || 0)?.toLocaleString()}
  225. </span>
  226. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  227. <i
  228. className="codicon codicon-arrow-right"
  229. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: 0 }}
  230. />
  231. {(cacheReads || 0)?.toLocaleString()}
  232. </span>
  233. </div>
  234. )}
  235. {apiConfiguration?.apiProvider !== "openai" && (
  236. <div
  237. style={{
  238. display: "flex",
  239. justifyContent: "space-between",
  240. alignItems: "center",
  241. }}>
  242. <div style={{ display: "flex", alignItems: "center", gap: "4px" }}>
  243. <span style={{ fontWeight: "bold" }}>API Cost:</span>
  244. <span>${totalCost?.toFixed(4)}</span>
  245. </div>
  246. <ExportButton />
  247. </div>
  248. )}
  249. </div>
  250. </div>
  251. {/* {apiProvider === "kodu" && (
  252. <div
  253. style={{
  254. backgroundColor: "color-mix(in srgb, var(--vscode-badge-background) 50%, transparent)",
  255. color: "var(--vscode-badge-foreground)",
  256. borderRadius: "0 0 3px 3px",
  257. display: "flex",
  258. justifyContent: "space-between",
  259. alignItems: "center",
  260. padding: "4px 12px 6px 12px",
  261. fontSize: "0.9em",
  262. marginLeft: "10px",
  263. marginRight: "10px",
  264. }}>
  265. <div style={{ fontWeight: "500" }}>Credits Remaining:</div>
  266. <div>
  267. {formatPrice(koduCredits || 0)}
  268. {(koduCredits || 0) < 1 && (
  269. <>
  270. {" "}
  271. <VSCodeLink style={{ fontSize: "0.9em" }} href={getKoduAddCreditsUrl(vscodeUriScheme)}>
  272. (get more?)
  273. </VSCodeLink>
  274. </>
  275. )}
  276. </div>
  277. </div>
  278. )} */}
  279. </div>
  280. )
  281. }
  282. export default TaskHeader