1
- import { useEffect , useState } from 'react' ;
2
-
3
- import { Flex } from 'sentry/components/container/flex' ;
4
1
import { Button } from 'sentry/components/core/button' ;
5
- import { ButtonBar } from 'sentry/components/core/button/buttonBar' ;
6
2
import LoadingError from 'sentry/components/loadingError' ;
7
3
import LoadingIndicator from 'sentry/components/loadingIndicator' ;
4
+ import Pagination from 'sentry/components/pagination' ;
8
5
import { ActionCell } from 'sentry/components/workflowEngine/gridCell/actionCell' ;
9
6
import AutomationTitleCell from 'sentry/components/workflowEngine/gridCell/automationTitleCell' ;
10
7
import { TimeAgoCell } from 'sentry/components/workflowEngine/gridCell/timeAgoCell' ;
11
8
import { defineColumns , SimpleTable } from 'sentry/components/workflowEngine/simpleTable' ;
12
- import { IconChevron } from 'sentry/icons' ;
13
9
import { t } from 'sentry/locale' ;
14
10
import type { Automation } from 'sentry/types/workflowEngine/automations' ;
15
11
import type { Detector } from 'sentry/types/workflowEngine/detectors' ;
16
12
import { defined } from 'sentry/utils' ;
13
+ import { useLocation } from 'sentry/utils/useLocation' ;
14
+ import { useNavigate } from 'sentry/utils/useNavigate' ;
17
15
import useOrganization from 'sentry/utils/useOrganization' ;
18
- import { useDetectorQueriesByIds } from 'sentry/views/automations/hooks' ;
16
+ import { useAutomationsQuery } from 'sentry/views/automations/hooks' ;
19
17
import { getAutomationActions } from 'sentry/views/automations/hooks/utils' ;
20
18
import { makeAutomationDetailsPathname } from 'sentry/views/automations/pathnames' ;
21
19
@@ -34,36 +32,23 @@ export function ConnectedAutomationsList({
34
32
} : Props ) {
35
33
const organization = useOrganization ( ) ;
36
34
const canEdit = connectedAutomationIds && ! ! toggleConnected ;
37
- // TODO: There will eventually be a single api call to fetch a page of automations
38
- const queries = useDetectorQueriesByIds ( automationIds ) ;
39
- const [ currentPage , setCurrentPage ] = useState ( 0 ) ;
40
- const totalPages = Math . ceil ( queries . length / AUTOMATIONS_PER_PAGE ) ;
41
-
42
- // Reset the page when the automationIds change
43
- useEffect ( ( ) => {
44
- setCurrentPage ( 0 ) ;
45
- } , [ automationIds ] ) ;
46
-
47
- const data = queries
48
- . map ( ( query ) : ConnectedAutomationsData | undefined => {
49
- if ( ! query . data ) {
50
- return undefined ;
51
- }
52
- return {
53
- ...query . data ,
54
- link : makeAutomationDetailsPathname ( organization . slug , query . data . id ) ,
55
- connected : canEdit
56
- ? {
57
- isConnected : connectedAutomationIds ?. has ( query . data . id ) ,
58
- toggleConnected : ( ) => toggleConnected ?.( query . data . id ) ,
59
- }
60
- : undefined ,
61
- } ;
62
- } )
63
- . filter ( defined ) ;
64
-
65
- const isLoading = queries . some ( query => query . isPending ) ;
66
- const isError = queries . some ( query => query . isError ) ;
35
+ const navigate = useNavigate ( ) ;
36
+ const location = useLocation ( ) ;
37
+
38
+ const {
39
+ data : automations ,
40
+ isLoading,
41
+ isError,
42
+ getResponseHeader,
43
+ } = useAutomationsQuery (
44
+ {
45
+ ids : automationIds ,
46
+ limit : AUTOMATIONS_PER_PAGE ,
47
+ cursor :
48
+ typeof location . query . cursor === 'string' ? location . query . cursor : undefined ,
49
+ } ,
50
+ { enabled : automationIds . length > 0 }
51
+ ) ;
67
52
68
53
if ( isError ) {
69
54
return < LoadingError /> ;
@@ -73,52 +58,42 @@ export function ConnectedAutomationsList({
73
58
return < LoadingIndicator /> ;
74
59
}
75
60
76
- const handlePreviousPage = ( ) => {
77
- setCurrentPage ( prev => Math . max ( 0 , prev - 1 ) ) ;
78
- } ;
79
- const handleNextPage = ( ) => {
80
- setCurrentPage ( prev => Math . min ( totalPages - 1 , prev + 1 ) ) ;
81
- } ;
82
-
83
- const pagination = (
84
- < Flex justify = "flex-end" >
85
- < ButtonBar merged >
86
- < Button
87
- onClick = { handlePreviousPage }
88
- disabled = { currentPage === 0 }
89
- aria-label = { t ( 'Previous page' ) }
90
- icon = { < IconChevron direction = "left" /> }
91
- size = "sm"
92
- />
93
- < Button
94
- onClick = { handleNextPage }
95
- disabled = { currentPage === totalPages - 1 }
96
- aria-label = { t ( 'Next page' ) }
97
- icon = { < IconChevron direction = "right" /> }
98
- size = "sm"
99
- />
100
- </ ButtonBar >
101
- </ Flex >
102
- ) ;
103
-
104
- if ( canEdit ) {
105
- return (
106
- < Flex column >
107
- < SimpleTable columns = { connectedColumns } data = { data } />
108
- { pagination }
109
- </ Flex >
110
- ) ;
111
- }
61
+ const tableData : ConnectedAutomationsData [ ] =
62
+ automations
63
+ ?. map ( automation => {
64
+ return {
65
+ ...automation ,
66
+ link : makeAutomationDetailsPathname ( organization . slug , automation . id ) ,
67
+ connected : canEdit
68
+ ? {
69
+ isConnected : connectedAutomationIds ?. has ( automation . id ) ,
70
+ toggleConnected : ( ) => toggleConnected ?.( automation . id ) ,
71
+ }
72
+ : undefined ,
73
+ } ;
74
+ } )
75
+ . filter ( defined ) ?? [ ] ;
112
76
113
77
return (
114
- < Flex column >
78
+ < div >
115
79
< SimpleTable
116
- columns = { baseColumns }
117
- data = { data }
80
+ columns = { canEdit ? connectedColumns : baseColumns }
81
+ data = { tableData }
118
82
fallback = { t ( 'No automations connected' ) }
119
83
/>
120
- { pagination }
121
- </ Flex >
84
+ < Pagination
85
+ onCursor = { cursor => {
86
+ navigate ( {
87
+ pathname : location . pathname ,
88
+ query : {
89
+ ...location . query ,
90
+ cursor,
91
+ } ,
92
+ } ) ;
93
+ } }
94
+ pageLinks = { getResponseHeader ?.( 'Link' ) }
95
+ />
96
+ </ div >
122
97
) ;
123
98
}
124
99
0 commit comments