source dump of claude code
at main 58 lines 1.5 kB view raw
1import { useEffect, useState } from 'react' 2import type { PastedContent } from 'src/utils/config.js' 3import { maybeTruncateInput } from './inputPaste.js' 4 5type Props = { 6 input: string 7 pastedContents: Record<number, PastedContent> 8 onInputChange: (input: string) => void 9 setCursorOffset: (offset: number) => void 10 setPastedContents: (contents: Record<number, PastedContent>) => void 11} 12 13export function useMaybeTruncateInput({ 14 input, 15 pastedContents, 16 onInputChange, 17 setCursorOffset, 18 setPastedContents, 19}: Props) { 20 // Track if we've initialized this specific input value 21 const [hasAppliedTruncationToInput, setHasAppliedTruncationToInput] = 22 useState(false) 23 24 // Process input for truncation and pasted images from MessageSelector. 25 useEffect(() => { 26 if (hasAppliedTruncationToInput) { 27 return 28 } 29 30 if (input.length <= 10_000) { 31 return 32 } 33 34 const { newInput, newPastedContents } = maybeTruncateInput( 35 input, 36 pastedContents, 37 ) 38 39 onInputChange(newInput) 40 setCursorOffset(newInput.length) 41 setPastedContents(newPastedContents) 42 setHasAppliedTruncationToInput(true) 43 }, [ 44 input, 45 hasAppliedTruncationToInput, 46 pastedContents, 47 onInputChange, 48 setPastedContents, 49 setCursorOffset, 50 ]) 51 52 // Reset hasInitializedInput when input is cleared (e.g., after submission) 53 useEffect(() => { 54 if (input === '') { 55 setHasAppliedTruncationToInput(false) 56 } 57 }, [input]) 58}