| 1 | import { useEffect, useRef, useState } from "react"; |
| 2 | import { LLMLog } from "../../hooks/useLLMLog"; |
| 3 | import ListItem from "./ListItem"; |
| 4 | |
| 5 | export interface ListProps { |
| 6 | llmLog: LLMLog; |
| 7 | onClickInteraction: (interactionId: string) => void; |
| 8 | } |
| 9 | |
| 10 | export default function List({ llmLog, onClickInteraction }: ListProps) { |
| 11 | const topRef = useRef<HTMLUListElement>(null); |
| 12 | const [selectedId, setSelectedId] = useState<string | undefined>(undefined); |
| 13 | |
| 14 | const lastSize = useRef(0); |
| 15 | |
| 16 | useEffect(() => { |
| 17 | if (llmLog.order.length != lastSize.current) { |
| 18 | setSelectedId(llmLog.order[llmLog.order.length - 1]); |
| 19 | onClickInteraction(llmLog.order[llmLog.order.length - 1]); |
| 20 | |
| 21 | lastSize.current = llmLog.order.length; |
| 22 | const lastChild = topRef.current?.lastChild; |
| 23 | if (lastChild) { |
| 24 | (lastChild as HTMLElement).scrollIntoView({ |
| 25 | behavior: "smooth", |
| 26 | block: "end", |
| 27 | }); |
| 28 | } |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | return ( |
| 33 | <ul |
| 34 | tabIndex={1} |
| 35 | ref={topRef} |
| 36 | className="group m-0 w-[150px] flex-none list-none overflow-auto border-0 border-r-2 border-solid border-[color:var(--vscode-panel-border)] p-0" |
| 37 | > |
| 38 | {llmLog.order.map((id) => ( |
| 39 | <ListItem |
| 40 | key={id} |
| 41 | interactionId={id} |
| 42 | interaction={llmLog.interactions.get(id)!} |
| 43 | onClickInteraction={(interactionId) => { |
| 44 | topRef.current?.focus(); |
| 45 | setSelectedId(interactionId); |
| 46 | onClickInteraction(interactionId); |
| 47 | }} |
| 48 | selected={id == selectedId} |
| 49 | ></ListItem> |
| 50 | ))} |
| 51 | </ul> |
| 52 | ); |
| 53 | } |