useMaybeTruncateInput.ts
components/PromptInput/useMaybeTruncateInput.ts
59
Lines
1468
Bytes
1
Exports
3
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with ui-flow. It contains 59 lines, 3 detected imports, and 1 detected exports.
Important relationships
- components/PromptInput/HistorySearchInput.tsx
- components/PromptInput/IssueFlagBanner.tsx
- components/PromptInput/Notifications.tsx
- components/PromptInput/PromptInput.tsx
- components/PromptInput/PromptInputFooter.tsx
- components/PromptInput/PromptInputFooterLeftSide.tsx
- components/PromptInput/PromptInputFooterSuggestions.tsx
- components/PromptInput/PromptInputHelpMenu.tsx
Detected exports
useMaybeTruncateInput
Keywords
inputpastedcontentsoninputchangesetcursoroffsetsetpastedcontentsuseeffectpastedcontentvoidhasappliedtruncationtoinputsethasappliedtruncationtoinput
Detected imports
reactsrc/utils/config.js./inputPaste.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { useEffect, useState } from 'react'
import type { PastedContent } from 'src/utils/config.js'
import { maybeTruncateInput } from './inputPaste.js'
type Props = {
input: string
pastedContents: Record<number, PastedContent>
onInputChange: (input: string) => void
setCursorOffset: (offset: number) => void
setPastedContents: (contents: Record<number, PastedContent>) => void
}
export function useMaybeTruncateInput({
input,
pastedContents,
onInputChange,
setCursorOffset,
setPastedContents,
}: Props) {
// Track if we've initialized this specific input value
const [hasAppliedTruncationToInput, setHasAppliedTruncationToInput] =
useState(false)
// Process input for truncation and pasted images from MessageSelector.
useEffect(() => {
if (hasAppliedTruncationToInput) {
return
}
if (input.length <= 10_000) {
return
}
const { newInput, newPastedContents } = maybeTruncateInput(
input,
pastedContents,
)
onInputChange(newInput)
setCursorOffset(newInput.length)
setPastedContents(newPastedContents)
setHasAppliedTruncationToInput(true)
}, [
input,
hasAppliedTruncationToInput,
pastedContents,
onInputChange,
setPastedContents,
setCursorOffset,
])
// Reset hasInitializedInput when input is cleared (e.g., after submission)
useEffect(() => {
if (input === '') {
setHasAppliedTruncationToInput(false)
}
}, [input])
}