11import type { IFlow } from '@plumber/types'
22
3- import { useEffect } from 'react'
3+ import { useEffect , useMemo } from 'react'
44import { useNavigate } from 'react-router-dom'
5- import { Box , Center , Container , Flex } from '@chakra-ui/react'
5+ import { useQuery } from '@apollo/client'
6+ import { Box , Center , Flex } from '@chakra-ui/react'
67import { Pagination } from '@opengovsg/design-system-react'
78
9+ import Container from '@/components/Container'
810import DebouncedSearchInput from '@/components/DebouncedSearchInput'
911import FlowRow from '@/components/FlowRow'
1012import NoResultFound from '@/components/NoResultFound'
1113import PageTitle from '@/components/PageTitle'
1214import PrimarySpinner from '@/components/PrimarySpinner'
1315import * as URLS from '@/config/urls'
14- import { PageInfo } from '@/graphql/__generated__/graphql '
16+ import { GET_FLOWS } from '@/graphql/queries/get-flows '
1517import { usePaginationAndFilter } from '@/hooks/usePaginationAndFilter'
1618
1719const RESULTS_PER_PAGE = 10
1820const EXECUTIONS_TITLE = 'Select a pipe to view executions'
1921
22+ const getLimitAndOffset = ( page : number ) => ( {
23+ limit : RESULTS_PER_PAGE ,
24+ offset : ( page - 1 ) * RESULTS_PER_PAGE ,
25+ } )
26+
2027interface FlowsInternalProps {
2128 isLoading : boolean
2229 isSearching : boolean
@@ -37,7 +44,7 @@ function FlowsList({ isLoading, isSearching, flows }: FlowsInternalProps) {
3744 }
3845
3946 if ( hasNoUserFlows ) {
40- return < > No Executions </ >
47+ return < > Create a pipe to see executions </ >
4148 }
4249
4350 if ( isEmptySearchResults ) {
@@ -64,26 +71,38 @@ function FlowsList({ isLoading, isSearching, flows }: FlowsInternalProps) {
6471}
6572
6673interface FlowsListPageProps {
67- loading : boolean
68- flows : IFlow [ ]
69- pageInfo : PageInfo
74+ setFlows : React . Dispatch < React . SetStateAction < IFlow [ ] > >
7075}
7176
72- export default function FlowListPage ( {
73- loading,
74- flows,
75- pageInfo,
76- } : FlowsListPageProps ) {
77+ export default function FlowListPage ( { setFlows } : FlowsListPageProps ) {
7778 const { input, page, status, setSearchParams, isSearching } =
7879 usePaginationAndFilter ( )
7980 const navigate = useNavigate ( )
8081
82+ const { data, loading } = useQuery ( GET_FLOWS , {
83+ variables : {
84+ ...getLimitAndOffset ( page ) ,
85+ name : input ,
86+ } ,
87+ } )
88+
89+ const { pageInfo, edges } = data ?. getFlows || { }
90+ const flows : IFlow [ ] = useMemo (
91+ ( ) => edges ?. map ( ( { node } : { node : IFlow } ) => node ) ?? [ ] ,
92+ [ edges ] ,
93+ )
94+
95+ useEffect ( ( ) => {
96+ setFlows ( flows )
97+ } , [ flows , setFlows ] )
98+
8199 const totalCount : number = pageInfo ?. totalCount ?? 0
82100 const hasPagination = ! loading && totalCount > RESULTS_PER_PAGE
83101 const hasNoUserFlows = flows . length === 0 && ! isSearching
84102
85103 // ensure invalid pages won't be accessed even after deleting flows
86104 const lastPage = Math . ceil ( totalCount / RESULTS_PER_PAGE )
105+
87106 useEffect ( ( ) => {
88107 // Defer the search params update till after the initial render
89108 if ( lastPage !== 0 && page > lastPage ) {
@@ -92,10 +111,12 @@ export default function FlowListPage({
92111 } , [ lastPage , page , setSearchParams ] )
93112
94113 useEffect ( ( ) => {
95- if ( ! loading && input && flows . length > 0 && status === 'failure' ) {
96- const pipeId = flows . filter ( ( f ) => f . name === input ) ?. [ 0 ] ?. id
97- if ( pipeId ) {
98- navigate ( `${ URLS . EXECUTION_FLOW ( pipeId ) } &status=failure` , {
114+ if ( ! loading && input && flows && status === 'failure' ) {
115+ const matchingPipeIds = flows
116+ . filter ( ( f ) => f . name === input )
117+ . map ( ( f ) => f . id )
118+ if ( matchingPipeIds . length === 1 ) {
119+ navigate ( `${ URLS . EXECUTION_FLOW ( matchingPipeIds [ 0 ] ) } &status=failure` , {
99120 replace : true ,
100121 } )
101122 } else {
0 commit comments