TaskHeader.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { VSCodeButton } from "@vscode/webview-ui-toolkit/react"
  2. import React, { memo, 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. return (
  81. <div style={{ padding: "10px 13px 10px 13px" }}>
  82. <div
  83. style={{
  84. backgroundColor: "var(--vscode-badge-background)",
  85. color: "var(--vscode-badge-foreground)",
  86. borderRadius: "3px",
  87. padding: "12px",
  88. display: "flex",
  89. flexDirection: "column",
  90. gap: "8px",
  91. position: "relative",
  92. zIndex: 1,
  93. }}>
  94. <div
  95. style={{
  96. display: "flex",
  97. justifyContent: "space-between",
  98. alignItems: "center",
  99. }}>
  100. <span style={{ fontWeight: "bold" }}>Task</span>
  101. <VSCodeButton
  102. appearance="icon"
  103. onClick={onClose}
  104. style={{ marginTop: "-6px", marginRight: "-4px" }}>
  105. <span className="codicon codicon-close"></span>
  106. </VSCodeButton>
  107. </div>
  108. <div
  109. ref={textContainerRef}
  110. style={{
  111. fontSize: "var(--vscode-font-size)",
  112. overflowY: isExpanded ? "auto" : "hidden",
  113. wordBreak: "break-word",
  114. overflowWrap: "anywhere",
  115. position: "relative",
  116. }}>
  117. <div
  118. ref={textRef}
  119. style={{
  120. display: "-webkit-box",
  121. WebkitLineClamp: isExpanded ? "unset" : 3,
  122. WebkitBoxOrient: "vertical",
  123. overflow: "hidden",
  124. whiteSpace: "pre-wrap",
  125. wordBreak: "break-word",
  126. overflowWrap: "anywhere",
  127. }}>
  128. {task.text}
  129. </div>
  130. {!isExpanded && showSeeMore && (
  131. <div
  132. style={{
  133. position: "absolute",
  134. right: 0,
  135. bottom: 0,
  136. display: "flex",
  137. alignItems: "center",
  138. }}>
  139. <div
  140. style={{
  141. width: 30,
  142. height: "1.2em",
  143. background:
  144. "linear-gradient(to right, transparent, var(--vscode-badge-background))",
  145. }}
  146. />
  147. <div
  148. style={{
  149. cursor: "pointer",
  150. color: "var(--vscode-textLink-foreground)",
  151. paddingRight: 0,
  152. paddingLeft: 3,
  153. backgroundColor: "var(--vscode-badge-background)",
  154. }}
  155. onClick={() => setIsExpanded(!isExpanded)}>
  156. See more
  157. </div>
  158. </div>
  159. )}
  160. </div>
  161. {isExpanded && showSeeMore && (
  162. <div
  163. style={{
  164. cursor: "pointer",
  165. color: "var(--vscode-textLink-foreground)",
  166. marginLeft: "auto",
  167. textAlign: "right",
  168. paddingRight: 2,
  169. }}
  170. onClick={() => setIsExpanded(!isExpanded)}>
  171. See less
  172. </div>
  173. )}
  174. {task.images && task.images.length > 0 && <Thumbnails images={task.images} />}
  175. <div style={{ display: "flex", flexDirection: "column", gap: "4px" }}>
  176. <div
  177. style={{
  178. display: "flex",
  179. justifyContent: "space-between",
  180. alignItems: "center",
  181. }}>
  182. <div style={{ display: "flex", alignItems: "center", gap: "4px", flexWrap: "wrap" }}>
  183. <span style={{ fontWeight: "bold" }}>Tokens:</span>
  184. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  185. <i
  186. className="codicon codicon-arrow-up"
  187. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-2px" }}
  188. />
  189. {tokensIn?.toLocaleString()}
  190. </span>
  191. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  192. <i
  193. className="codicon codicon-arrow-down"
  194. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-2px" }}
  195. />
  196. {tokensOut?.toLocaleString()}
  197. </span>
  198. </div>
  199. {(apiConfiguration?.apiProvider === "openai" || apiConfiguration?.apiProvider === "ollama") && (
  200. <ExportButton />
  201. )}
  202. </div>
  203. {(doesModelSupportPromptCache || cacheReads !== undefined || cacheWrites !== undefined) && (
  204. <div style={{ display: "flex", alignItems: "center", gap: "4px", flexWrap: "wrap" }}>
  205. <span style={{ fontWeight: "bold" }}>Cache:</span>
  206. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  207. <i
  208. className="codicon codicon-database"
  209. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: "-1px" }}
  210. />
  211. +{(cacheWrites || 0)?.toLocaleString()}
  212. </span>
  213. <span style={{ display: "flex", alignItems: "center", gap: "3px" }}>
  214. <i
  215. className="codicon codicon-arrow-right"
  216. style={{ fontSize: "12px", fontWeight: "bold", marginBottom: 0 }}
  217. />
  218. {(cacheReads || 0)?.toLocaleString()}
  219. </span>
  220. </div>
  221. )}
  222. {apiConfiguration?.apiProvider !== "openai" && apiConfiguration?.apiProvider !== "ollama" && (
  223. <div
  224. style={{
  225. display: "flex",
  226. justifyContent: "space-between",
  227. alignItems: "center",
  228. }}>
  229. <div style={{ display: "flex", alignItems: "center", gap: "4px" }}>
  230. <span style={{ fontWeight: "bold" }}>API Cost:</span>
  231. <span>${totalCost?.toFixed(4)}</span>
  232. </div>
  233. <ExportButton />
  234. </div>
  235. )}
  236. </div>
  237. </div>
  238. {/* {apiProvider === "kodu" && (
  239. <div
  240. style={{
  241. backgroundColor: "color-mix(in srgb, var(--vscode-badge-background) 50%, transparent)",
  242. color: "var(--vscode-badge-foreground)",
  243. borderRadius: "0 0 3px 3px",
  244. display: "flex",
  245. justifyContent: "space-between",
  246. alignItems: "center",
  247. padding: "4px 12px 6px 12px",
  248. fontSize: "0.9em",
  249. marginLeft: "10px",
  250. marginRight: "10px",
  251. }}>
  252. <div style={{ fontWeight: "500" }}>Credits Remaining:</div>
  253. <div>
  254. {formatPrice(koduCredits || 0)}
  255. {(koduCredits || 0) < 1 && (
  256. <>
  257. {" "}
  258. <VSCodeLink style={{ fontSize: "0.9em" }} href={getKoduAddCreditsUrl(vscodeUriScheme)}>
  259. (get more?)
  260. </VSCodeLink>
  261. </>
  262. )}
  263. </div>
  264. </div>
  265. )} */}
  266. </div>
  267. )
  268. }
  269. const ExportButton = () => (
  270. <VSCodeButton
  271. appearance="icon"
  272. onClick={() => vscode.postMessage({ type: "exportCurrentTask" })}
  273. style={{
  274. marginBottom: "-2px",
  275. marginRight: "-2.5px",
  276. }}>
  277. <div style={{ fontSize: "10.5px", fontWeight: "bold", opacity: 0.6 }}>EXPORT</div>
  278. </VSCodeButton>
  279. )
  280. export default memo(TaskHeader)