1
- import { Box , Breadcrumbs , Typography , Stack , Chip , Button , CircularProgress } from '@mui/material'
1
+ import {
2
+ Box ,
3
+ Breadcrumbs ,
4
+ Typography ,
5
+ Stack ,
6
+ Chip ,
7
+ Button ,
8
+ CircularProgress ,
9
+ } from '@mui/material'
2
10
import { TableDocument } from 'iconsax-react'
3
11
import NavigateNextIcon from '@mui/icons-material/NavigateNext'
4
12
import NavigateBeforeIcon from '@mui/icons-material/NavigateBefore'
@@ -14,13 +22,18 @@ import axios from 'axios'
14
22
import { getIndexifyServiceURL } from '../../utils/helpers'
15
23
16
24
const IndividualComputeGraphPage = ( ) => {
17
- const { invocationsList, computeGraph, namespace, cursor } = useLoaderData ( ) as IndividualComputeGraphLoaderData
25
+ const {
26
+ invocationsList,
27
+ computeGraph,
28
+ namespace,
29
+ prevCursor : prevCursorLoader ,
30
+ nextCursor : nextCursorLoader ,
31
+ } = useLoaderData ( ) as IndividualComputeGraphLoaderData
18
32
19
33
const [ invocations , setInvocations ] = useState < Invocation [ ] > ( invocationsList )
20
34
const [ isLoading , setIsLoading ] = useState ( false )
21
- const [ currentCursor , setCurrentCursor ] = useState < string | null > ( null )
22
- const [ nextCursor , setNextCursor ] = useState < string | null > ( cursor )
23
- const [ cursorHistory , setCursorHistory ] = useState < string [ ] > ( [ ] )
35
+ const [ prevCursor , setPrevCursor ] = useState < string | null > ( prevCursorLoader )
36
+ const [ nextCursor , setNextCursor ] = useState < string | null > ( nextCursorLoader )
24
37
25
38
const handleDelete = useCallback ( ( updatedList : Invocation [ ] ) => {
26
39
const sortedList = [ ...updatedList ] . sort (
@@ -29,56 +42,49 @@ const IndividualComputeGraphPage = () => {
29
42
setInvocations ( sortedList )
30
43
} , [ ] )
31
44
32
- const fetchInvocations = useCallback ( async ( cursor : string | null , direction : 'forward' | 'backward' ) => {
33
- setIsLoading ( true )
34
- try {
35
- const serviceURL = getIndexifyServiceURL ( )
36
- const limit = 20
37
- const url = `${ serviceURL } /namespaces/${ namespace } /compute_graphs/${ computeGraph . name } /invocations?limit=${ limit } ${
38
- cursor ? `&cursor=${ cursor } ` : ''
39
- } &direction=${ direction } `
40
-
41
- const response = await axios . get ( url )
42
- const data = response . data
43
-
44
- setInvocations ( [ ...data . invocations ] . sort (
45
- ( a , b ) => ( b . created_at ?? 0 ) - ( a . created_at ?? 0 )
46
- ) )
47
-
48
- if ( direction === 'forward' ) {
49
- if ( cursor ) {
50
- setCursorHistory ( prev => [ ...prev , cursor ] )
51
- }
52
- setCurrentCursor ( cursor )
53
- setNextCursor ( data . cursor || null )
54
- } else {
55
- if ( cursorHistory . length > 0 ) {
56
- setCursorHistory ( prev => prev . slice ( 0 , - 1 ) )
57
- }
58
- setCurrentCursor ( cursorHistory . length > 1 ? cursorHistory [ cursorHistory . length - 2 ] : null )
59
- setNextCursor ( data . cursor || null )
45
+ const fetchInvocations = useCallback (
46
+ async ( cursor : string | null , direction : 'forward' | 'backward' ) => {
47
+ setIsLoading ( true )
48
+ try {
49
+ const serviceURL = getIndexifyServiceURL ( )
50
+ const limit = 20
51
+ const url = `${ serviceURL } /namespaces/${ namespace } /compute_graphs/${
52
+ computeGraph . name
53
+ } /invocations?limit=${ limit } ${
54
+ cursor ? `&cursor=${ cursor } ` : ''
55
+ } &direction=${ direction } `
56
+
57
+ const response = await axios . get ( url )
58
+ const data = response . data
59
+
60
+ setInvocations ( [ ...data . invocations ] )
61
+
62
+ setPrevCursor ( data . prev_cursor )
63
+ setNextCursor ( data . next_cursor )
64
+ console . log ( direction , {
65
+ prevCursor : data . prev_cursor ,
66
+ nextCursor : data . next_cursor ,
67
+ } )
68
+ } catch ( error ) {
69
+ console . error ( 'Error fetching invocations:' , error )
70
+ } finally {
71
+ setIsLoading ( false )
60
72
}
61
- } catch ( error ) {
62
- console . error ( "Error fetching invocations:" , error )
63
- } finally {
64
- setIsLoading ( false )
65
- }
66
- } , [ namespace , computeGraph . name , cursorHistory ] )
73
+ } ,
74
+ [ namespace , computeGraph . name ]
75
+ )
67
76
68
77
const handleNextPage = useCallback ( ( ) => {
69
- const cursor = nextCursor || currentCursor
70
- if ( cursor ) {
71
- fetchInvocations ( cursor , 'forward' )
78
+ if ( nextCursor ) {
79
+ fetchInvocations ( nextCursor , 'forward' )
72
80
}
73
- } , [ nextCursor , currentCursor , fetchInvocations ] )
81
+ } , [ nextCursor , fetchInvocations ] )
74
82
75
83
const handlePreviousPage = useCallback ( ( ) => {
76
- if ( cursorHistory . length > 0 ) {
77
- const prevCursor = cursorHistory [ cursorHistory . length - 1 ]
84
+ if ( prevCursor ) {
78
85
fetchInvocations ( prevCursor , 'backward' )
79
86
}
80
- } , [ cursorHistory , fetchInvocations ] )
81
-
87
+ } , [ prevCursor , fetchInvocations ] )
82
88
return (
83
89
< Stack direction = "column" spacing = { 3 } >
84
90
< Breadcrumbs
@@ -132,29 +138,29 @@ const IndividualComputeGraphPage = () => {
132
138
onDelete = { handleDelete }
133
139
/>
134
140
135
- < Box
136
- sx = { {
137
- display : 'flex' ,
138
- justifyContent : 'space-between' ,
141
+ < Box
142
+ sx = { {
143
+ display : 'flex' ,
144
+ justifyContent : 'space-between' ,
139
145
mt : 2 ,
140
- alignItems : 'center'
146
+ alignItems : 'center' ,
141
147
} }
142
148
>
143
149
< Button
144
150
startIcon = { < NavigateBeforeIcon /> }
145
151
onClick = { handlePreviousPage }
146
- disabled = { cursorHistory . length === 0 || isLoading }
152
+ disabled = { ! prevCursor || isLoading }
147
153
variant = "outlined"
148
154
>
149
155
Previous
150
156
</ Button >
151
-
157
+
152
158
{ isLoading && < CircularProgress size = { 24 } /> }
153
-
159
+
154
160
< Button
155
161
endIcon = { < NavigateNextIcon /> }
156
162
onClick = { handleNextPage }
157
- disabled = { isLoading }
163
+ disabled = { ! nextCursor || isLoading }
158
164
variant = "outlined"
159
165
>
160
166
Next
0 commit comments