-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Cody Chat UI rendering reconciliation #5850
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import { clsx } from 'clsx' | |
import debounce from 'lodash/debounce' | ||
import isEqual from 'lodash/isEqual' | ||
import { Search } from 'lucide-react' | ||
import { type FC, memo, useCallback, useMemo, useRef, useState } from 'react' | ||
import { type FC, type MutableRefObject, memo, useCallback, useMemo, useRef } from 'react' | ||
import type { UserAccountInfo } from '../Chat' | ||
import type { ApiPostMessage } from '../Chat' | ||
import { Button } from '../components/shadcn/ui/button' | ||
|
@@ -180,7 +180,7 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => { | |
smartApplyEnabled, | ||
} = props | ||
|
||
const [intentResults, setIntentResults] = useState< | ||
const [intentResults, setIntentResults] = useMutatedValue< | ||
| { intent: ChatMessage['intent']; allScores?: { intent: string; score: number }[] } | ||
| undefined | ||
| null | ||
|
@@ -193,28 +193,27 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => { | |
editHumanMessage({ | ||
messageIndexInTranscript: humanMessage.index, | ||
editorValue, | ||
intent: intentFromSubmit || intentResults?.intent, | ||
intentScores: intentFromSubmit ? undefined : intentResults?.allScores, | ||
intent: intentFromSubmit || intentResults.current?.intent, | ||
intentScores: intentFromSubmit ? undefined : intentResults.current?.allScores, | ||
manuallySelectedIntent: !!intentFromSubmit, | ||
}) | ||
}, | ||
[humanMessage, intentResults] | ||
[humanMessage.index, intentResults] | ||
) | ||
|
||
const onFollowupSubmit = useCallback( | ||
(editorValue: SerializedPromptEditorValue, intentFromSubmit?: ChatMessage['intent']): void => { | ||
submitHumanMessage({ | ||
editorValue, | ||
intent: intentFromSubmit || intentResults?.intent, | ||
intentScores: intentFromSubmit ? undefined : intentResults?.allScores, | ||
intent: intentFromSubmit || intentResults.current?.intent, | ||
intentScores: intentFromSubmit ? undefined : intentResults.current?.allScores, | ||
manuallySelectedIntent: !!intentFromSubmit, | ||
}) | ||
}, | ||
[intentResults] | ||
) | ||
|
||
const extensionAPI = useExtensionAPI() | ||
|
||
const experimentalOneBoxEnabled = useExperimentalOneBox() | ||
|
||
const onChange = useMemo(() => { | ||
|
@@ -240,7 +239,7 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => { | |
}) | ||
} | ||
}, 300) | ||
}, [experimentalOneBoxEnabled, extensionAPI]) | ||
}, [experimentalOneBoxEnabled, extensionAPI, setIntentResults]) | ||
|
||
const onStop = useCallback(() => { | ||
getVSCodeAPI().postMessage({ | ||
|
@@ -290,7 +289,7 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => { | |
[reSubmitWithIntent] | ||
) | ||
|
||
const resetIntent = useCallback(() => setIntentResults(undefined), []) | ||
const resetIntent = useCallback(() => setIntentResults(undefined), [setIntentResults]) | ||
|
||
return ( | ||
<> | ||
|
@@ -312,6 +311,7 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => { | |
className={!isFirstInteraction && isLastInteraction ? 'tw-mt-auto' : ''} | ||
onEditorFocusChange={resetIntent} | ||
/> | ||
|
||
{experimentalOneBoxEnabled && humanMessage.intent && ( | ||
<InfoMessage> | ||
{humanMessage.intent === 'search' ? ( | ||
|
@@ -389,6 +389,17 @@ const TranscriptInteraction: FC<TranscriptInteractionProps> = memo(props => { | |
) | ||
}, isEqual) | ||
|
||
function useMutatedValue<T>(value?: T): [MutableRefObject<T | undefined>, setValue: (value: T) => void] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should document that this should not be used for rendering and won't cause re-rendering. |
||
const valueRef = useRef<T | undefined>(value) | ||
|
||
return [ | ||
valueRef, | ||
useCallback(value => { | ||
valueRef.current = value | ||
}, []), | ||
] | ||
} | ||
|
||
// TODO(sqs): Do this the React-y way. | ||
export function focusLastHumanMessageEditor(): void { | ||
const elements = document.querySelectorAll<HTMLElement>('[data-lexical-editor]') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export function useTracePropsUpdate(props: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment here that this is for debugging with advice about how to use it. From time to team people do mass-deletions based on use. You probably intend for this to stick around. |
||
const prev = useRef(props) | ||
|
||
useEffect(() => { | ||
const changedProps = Object.entries(props).reduce( | ||
(prop, [k, v]) => { | ||
if (prev.current[k] !== v) { | ||
prop[k] = [prev.current[k], v] | ||
} | ||
return prop | ||
}, | ||
{} as Record<any, any> | ||
) | ||
|
||
if (Object.keys(changedProps).length > 0) { | ||
console.log('Changed props:', changedProps) | ||
} | ||
|
||
prev.current = props | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't simply
useRef
enough here?