Skip to content

Commit

Permalink
Ensure that the DOM updates for the scroll to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Kateryna Kodonenko committed Jun 13, 2024
1 parent b5511e3 commit f756e2a
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions src/components/content-tab-assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,42 @@ const AuthenticatedView = memo(
cliTime: string
) => void;
path: string;
} ) => (
<>
{ messages.map( ( message, index ) => (
<Message
key={ index }
isUser={ message.role === 'user' }
projectPath={ path }
updateMessage={ updateMessage }
id={ message.id }
blocks={ message.blocks }
>
{ message.content }
</Message>
) ) }
{ isAssistantThinking && (
<Message isUser={ false }>
<MessageThinking />
</Message>
) }
</>
)
} ) => {
const endOfMessagesRef = useRef< HTMLDivElement >( null );

useEffect( () => {
const timer = setTimeout( () => {
if ( endOfMessagesRef.current ) {
endOfMessagesRef.current.scrollIntoView( { behavior: 'smooth' } );
}
}, 100 ); // Slight delay to ensure DOM updates

return () => clearTimeout( timer );
}, [ messages ] );

return (
<>
{ messages.map( ( message, index ) => (
<Message
key={ index }
isUser={ message.role === 'user' }
projectPath={ path }
updateMessage={ updateMessage }
id={ message.id }
blocks={ message.blocks }
>
{ message.content }
</Message>
) ) }
{ isAssistantThinking && (
<Message isUser={ false }>
<MessageThinking />
</Message>
) }
<div ref={ endOfMessagesRef } />
</>
);
}
);
const UnauthenticatedView = ( { onAuthenticate }: { onAuthenticate: () => void } ) => (
<Message className="w-full" isUser={ false }>
Expand Down Expand Up @@ -94,7 +109,6 @@ export function ContentTabAssistant( { selectedSite }: ContentTabAssistantProps
const { messages, addMessage, clearMessages, updateMessage } = useAssistant( selectedSite.name );
const { fetchAssistant, isLoading: isAssistantThinking } = useAssistantApi();
const [ input, setInput ] = useState< string >( '' );
const endOfMessagesRef = useRef< HTMLDivElement >( null );
const { isAuthenticated, authenticate } = useAuth();
const isOffline = useOffline();
const { __ } = useI18n();
Expand Down Expand Up @@ -137,12 +151,6 @@ export function ContentTabAssistant( { selectedSite }: ContentTabAssistantProps
clearMessages();
};

useEffect( () => {
if ( endOfMessagesRef.current ) {
endOfMessagesRef.current.scrollIntoView( { behavior: 'smooth' } );
}
}, [ messages ] );

const disabled = isOffline || ! isAuthenticated;

return (
Expand All @@ -159,7 +167,6 @@ export function ContentTabAssistant( { selectedSite }: ContentTabAssistantProps
updateMessage={ updateMessage }
path={ selectedSite.path }
/>
<div ref={ endOfMessagesRef } />
</>
) : (
<UnauthenticatedView onAuthenticate={ authenticate } />
Expand Down

0 comments on commit f756e2a

Please sign in to comment.