@@ -35,6 +35,7 @@ import Composing from "./composing";
35
35
import Message from "./message" ;
36
36
import { ChatMessageTyped , ChatMessages , MessageHistory , Mode } from "./types" ;
37
37
import { getSelectedHashtagsSearch , newest_content } from "./utils" ;
38
+ import { DivTempHeight } from "@cocalc/frontend/jupyter/cell-list" ;
38
39
39
40
interface Props {
40
41
project_id : string ; // used to render links more effectively
@@ -54,6 +55,7 @@ export function ChatLog(props: Readonly<Props>) {
54
55
project_id ,
55
56
path ,
56
57
) ;
58
+ const virtuosoHeightsRef = useRef < { [ index : number ] : number } > ( { } ) ;
57
59
58
60
// see similar code in task list:
59
61
const selectedHashtags0 = useRedux ( [ "selectedHashtags" ] , project_id , path ) ;
@@ -160,7 +162,16 @@ export function ChatLog(props: Readonly<Props>) {
160
162
< Virtuoso
161
163
ref = { virtuosoRef }
162
164
totalCount = { sortedDates . length }
163
- overscan = { 10000 }
165
+ itemSize = { ( el ) => {
166
+ // see comment in jupyter/cell-list.tsx
167
+ const h = el . getBoundingClientRect ( ) . height ;
168
+ const data = el . getAttribute ( "data-item-index" ) ;
169
+ if ( data != null ) {
170
+ const index = parseInt ( data ) ;
171
+ virtuosoHeightsRef . current [ index ] = h ;
172
+ }
173
+ return h ;
174
+ } }
164
175
itemContent = { ( index ) => {
165
176
const date = sortedDates [ index ] ;
166
177
const message : ChatMessageTyped | undefined = messages . get ( date ) ;
@@ -175,50 +186,55 @@ export function ChatLog(props: Readonly<Props>) {
175
186
const is_folded =
176
187
! force_unfold && isFolded ( messages , message , account_id ) ;
177
188
const is_thread_body = message . get ( "reply_to" ) != null ;
189
+ const h = virtuosoHeightsRef . current [ index ] ;
178
190
179
191
return (
180
192
< div style = { { overflow : "hidden" } } >
181
- < Message
182
- key = { date }
183
- index = { index }
184
- account_id = { account_id }
185
- user_map = { user_map }
186
- message = { message }
187
- project_id = { project_id }
188
- path = { path }
189
- font_size = { fontSize }
190
- selectedHashtags = { selectedHashtags }
191
- actions = { actions }
192
- is_thread = { is_thread }
193
- is_folded = { is_folded }
194
- force_unfold = { force_unfold }
195
- is_thread_body = { is_thread_body }
196
- is_prev_sender = { isPrevMessageSender (
197
- index ,
198
- sortedDates ,
199
- messages ,
200
- ) }
201
- is_next_sender = { isNextMessageSender (
202
- index ,
203
- sortedDates ,
204
- messages ,
205
- ) }
206
- show_avatar = { ! isNextMessageSender ( index , sortedDates , messages ) }
207
- mode = { mode }
208
- get_user_name = { ( account_id : string | undefined ) =>
209
- // ATTN: this also works for LLM chat bot IDs, not just account UUIDs
210
- typeof account_id === "string"
211
- ? getUserName ( user_map , account_id )
212
- : "Unknown name"
213
- }
214
- scroll_into_view = { ( ) =>
215
- virtuosoRef . current ?. scrollIntoView ( { index } )
216
- }
217
- allowReply = {
218
- messages . getIn ( [ sortedDates [ index + 1 ] , "reply_to" ] ) == null
219
- }
220
- llm_cost_reply = { llm_cost_reply }
221
- />
193
+ < DivTempHeight height = { h ? `${ h } px` : undefined } >
194
+ < Message
195
+ key = { date }
196
+ index = { index }
197
+ account_id = { account_id }
198
+ user_map = { user_map }
199
+ message = { message }
200
+ project_id = { project_id }
201
+ path = { path }
202
+ font_size = { fontSize }
203
+ selectedHashtags = { selectedHashtags }
204
+ actions = { actions }
205
+ is_thread = { is_thread }
206
+ is_folded = { is_folded }
207
+ force_unfold = { force_unfold }
208
+ is_thread_body = { is_thread_body }
209
+ is_prev_sender = { isPrevMessageSender (
210
+ index ,
211
+ sortedDates ,
212
+ messages ,
213
+ ) }
214
+ is_next_sender = { isNextMessageSender (
215
+ index ,
216
+ sortedDates ,
217
+ messages ,
218
+ ) }
219
+ show_avatar = {
220
+ ! isNextMessageSender ( index , sortedDates , messages )
221
+ }
222
+ mode = { mode }
223
+ get_user_name = { ( account_id : string | undefined ) =>
224
+ // ATTN: this also works for LLM chat bot IDs, not just account UUIDs
225
+ typeof account_id === "string"
226
+ ? getUserName ( user_map , account_id )
227
+ : "Unknown name"
228
+ }
229
+ scroll_into_view = { ( ) =>
230
+ virtuosoRef . current ?. scrollIntoView ( { index } )
231
+ }
232
+ allowReply = {
233
+ messages . getIn ( [ sortedDates [ index + 1 ] , "reply_to" ] ) == null
234
+ }
235
+ llm_cost_reply = { llm_cost_reply }
236
+ />
237
+ </ DivTempHeight >
222
238
</ div >
223
239
) ;
224
240
} }
0 commit comments