import { VSCodeButton } from "@vscode/webview-ui-toolkit/react" import React, { useEffect, useRef, useState } from "react" import { useWindowSize } from "react-use" import { ClaudeMessage } from "../../../src/shared/ExtensionMessage" import { useExtensionState } from "../context/ExtensionStateContext" import { vscode } from "../utils/vscode" import Thumbnails from "./Thumbnails" interface TaskHeaderProps { task: ClaudeMessage tokensIn: number tokensOut: number doesModelSupportPromptCache: boolean cacheWrites?: number cacheReads?: number totalCost: number onClose: () => void } const TaskHeader: React.FC = ({ task, tokensIn, tokensOut, doesModelSupportPromptCache, cacheWrites, cacheReads, totalCost, onClose, }) => { const { apiConfiguration } = useExtensionState() const [isExpanded, setIsExpanded] = useState(false) const [showSeeMore, setShowSeeMore] = useState(false) const textContainerRef = useRef(null) const textRef = useRef(null) /* 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. Sources - https://usehooks-ts.com/react-hook/use-event-listener - https://streamich.github.io/react-use/?path=/story/sensors-useevent--docs - https://github.com/streamich/react-use/blob/master/src/useEvent.ts - https://stackoverflow.com/questions/55565444/how-to-register-event-with-useeffect-hooks Before: const updateMaxHeight = useCallback(() => { if (isExpanded && textContainerRef.current) { const maxHeight = window.innerHeight * (3 / 5) textContainerRef.current.style.maxHeight = `${maxHeight}px` } }, [isExpanded]) useEffect(() => { updateMaxHeight() }, [isExpanded, updateMaxHeight]) useEffect(() => { window.removeEventListener("resize", updateMaxHeight) window.addEventListener("resize", updateMaxHeight) return () => { window.removeEventListener("resize", updateMaxHeight) } }, [updateMaxHeight]) After: */ const { height: windowHeight, width: windowWidth } = useWindowSize() useEffect(() => { if (isExpanded && textContainerRef.current) { const maxHeight = windowHeight * (1 / 2) textContainerRef.current.style.maxHeight = `${maxHeight}px` } }, [isExpanded, windowHeight]) useEffect(() => { if (textRef.current && textContainerRef.current) { let textContainerHeight = textContainerRef.current.clientHeight if (!textContainerHeight) { textContainerHeight = textContainerRef.current.getBoundingClientRect().height } const isOverflowing = textRef.current.scrollHeight > textContainerHeight // necessary to show see more button again if user resizes window to expand and then back to collapse if (!isOverflowing) { setIsExpanded(false) } setShowSeeMore(isOverflowing) } }, [task.text, windowWidth]) const toggleExpand = () => setIsExpanded(!isExpanded) const handleDownload = () => { vscode.postMessage({ type: "exportCurrentTask" }) } const ExportButton = () => (
EXPORT .MD
) return (
Task
{task.text}
{!isExpanded && showSeeMore && (
See more
)}
{isExpanded && showSeeMore && (
See less
)} {task.images && task.images.length > 0 && }
Tokens: {tokensIn?.toLocaleString()} {tokensOut?.toLocaleString()}
{apiConfiguration?.apiProvider === "openai" && }
{(doesModelSupportPromptCache || cacheReads !== undefined || cacheWrites !== undefined) && (
Cache: +{(cacheWrites || 0)?.toLocaleString()} {(cacheReads || 0)?.toLocaleString()}
)} {apiConfiguration?.apiProvider !== "openai" && (
API Cost: ${totalCost?.toFixed(4)}
)}
{/* {apiProvider === "kodu" && (
Credits Remaining:
{formatPrice(koduCredits || 0)} {(koduCredits || 0) < 1 && ( <> {" "} (get more?) )}
)} */}
) } export default TaskHeader