1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useMemo , useEffect } from 'react' ;
2
2
import { Link } from 'react-router-dom' ;
3
3
import { useTheme } from '../contexts/ThemeContext' ;
4
4
import { getTimeAgo } from '../utils/lmpUtils' ;
5
5
import { DependencyGraph } from '../components/depgraph/DependencyGraph' ;
6
6
import { useLatestLMPs , useTraces } from '../hooks/useBackend' ;
7
7
import VersionBadge from '../components/VersionBadge' ;
8
+ const MemoizedDependencyGraph = React . memo ( DependencyGraph ) ;
8
9
function Home ( ) {
9
10
const [ expandedLMP , setExpandedLMP ] = useState ( null ) ;
10
11
const { darkMode } = useTheme ( ) ;
11
12
const { data : lmps , isLoading : isLoadingLMPs } = useLatestLMPs ( ) ;
12
13
const { data : traces , isLoading : isLoadingTraces } = useTraces ( lmps ) ;
14
+
13
15
14
16
const toggleExpand = ( lmpName , event ) => {
15
17
if ( event . target . tagName . toLowerCase ( ) !== 'a' ) {
@@ -21,7 +23,22 @@ function Home() {
21
23
return id . length > 8 ? `${ id . substring ( 0 , 8 ) } ...` : id ;
22
24
} ;
23
25
24
- if ( isLoadingLMPs || isLoadingTraces ) {
26
+ const [ firstTraces , setFirstTraces ] = useState ( traces ) ;
27
+ const [ firstLMPs , setFirstLMPs ] = useState ( lmps ) ;
28
+
29
+ useEffect ( ( ) => {
30
+ if ( ( ! firstTraces || ! firstLMPs ) && traces && lmps ) {
31
+ console . log ( "Setting first traces and lmps" ) ;
32
+ setFirstTraces ( traces ) ;
33
+ setFirstLMPs ( lmps ) ;
34
+ }
35
+ } , [ traces , firstTraces , lmps , firstLMPs ] ) ;
36
+
37
+ // TODO: Make graph dynamically update.
38
+ const memoizedTraces = useMemo ( ( ) => firstTraces , [ firstTraces ] ) ;
39
+ const memoizedLMPs = useMemo ( ( ) => firstLMPs , [ firstLMPs ] ) ;
40
+
41
+ if ( ! memoizedLMPs || ! memoizedTraces ) {
25
42
return < div className = { `bg-${ darkMode ? 'gray-900' : 'gray-100' } min-h-screen flex items-center justify-center` } >
26
43
< p className = { `text-${ darkMode ? 'white' : 'black' } ` } > Loading...</ p >
27
44
</ div > ;
@@ -31,7 +48,7 @@ function Home() {
31
48
< div className = { `bg-${ darkMode ? 'gray-900' : 'gray-100' } min-h-screen` } >
32
49
< div className = "container mx-auto px-4 py-8" >
33
50
< h1 className = { `text-3xl font-bold mb-6 ${ darkMode ? 'text-gray-100' : 'text-gray-800' } ` } > Language Model Programs</ h1 >
34
- { lmps && traces && < DependencyGraph lmps = { lmps } traces = { traces } /> }
51
+ < MemoizedDependencyGraph lmps = { memoizedLMPs } traces = { memoizedTraces } key = { memoizedLMPs . length + memoizedTraces . length } />
35
52
< div className = "space-y-4" >
36
53
{ lmps . map ( ( lmp ) => (
37
54
< div
0 commit comments