Skip to content

Commit 12b84d3

Browse files
committed
Change character count to word count max for native context
Signed-off-by: Brent Salisbury <[email protected]>
1 parent f9df911 commit 12b84d3

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/components/Contribute/Knowledge/Native/KnowledgeQuestionAnswerPairsNative/KnowledgeQuestionAnswerPairs.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const KnowledgeQuestionAnswerPairsNative: React.FC<Props> = ({
6060
const [expandedFiles, setExpandedFiles] = useState<Record<string, boolean>>({});
6161
const [selectedWordCount, setSelectedWordCount] = useState<number>(0);
6262
const [showAllCommits, setShowAllCommits] = useState<boolean>(false);
63+
const [contextWordCount, setContextWordCount] = useState(0);
64+
const MAX_WORDS = 500;
6365

6466
// Ref for the <pre> elements to track selections TODO: figure out how to make text expansions taller in PF without a custom-pre
6567
const preRefs = useRef<Record<string, HTMLPreElement | null>>({});
@@ -217,6 +219,27 @@ const KnowledgeQuestionAnswerPairsNative: React.FC<Props> = ({
217219
[]
218220
);
219221

222+
// TODO: replace with a tokenizer library
223+
const countWords = (text: string) => {
224+
return text.trim().split(/\s+/).filter(Boolean).length;
225+
};
226+
227+
// Update word count whenever context changes
228+
useEffect(() => {
229+
setContextWordCount(countWords(seedExample.context));
230+
}, [seedExample.context]);
231+
232+
// Handle context input change with word count validation
233+
const onContextChange = (_event: React.FormEvent<HTMLTextAreaElement>, contextValue: string) => {
234+
const wordCount = countWords(contextValue);
235+
if (wordCount <= MAX_WORDS) {
236+
handleContextInputChange(seedExampleIndex, contextValue);
237+
} else {
238+
// allow the overage and show validation error
239+
handleContextInputChange(seedExampleIndex, contextValue);
240+
}
241+
};
242+
220243
return (
221244
<FormGroup style={{ padding: '20px' }}>
222245
<Tooltip content={<div>Select context from your knowledge files</div>} position="top">
@@ -232,11 +255,18 @@ const KnowledgeQuestionAnswerPairsNative: React.FC<Props> = ({
232255
placeholder="Enter the context from which the Q&A pairs are derived. (500 words max)"
233256
value={seedExample.context}
234257
validated={seedExample.isContextValid}
235-
maxLength={500}
258+
onChange={onContextChange}
236259
style={{ marginBottom: '20px' }}
237-
onChange={(_event, contextValue: string) => handleContextInputChange(seedExampleIndex, contextValue)}
238260
onBlur={() => handleContextBlur(seedExampleIndex)}
239261
/>
262+
{/* Display word count */}
263+
<FormHelperText>
264+
<HelperText>
265+
<HelperTextItem>
266+
{contextWordCount} / {MAX_WORDS} words
267+
</HelperTextItem>
268+
</HelperText>
269+
</FormHelperText>
240270
{seedExample.isContextValid === ValidatedOptions.error && (
241271
<FormHelperText>
242272
<HelperText>

0 commit comments

Comments
 (0)