import { useState } from 'react' ; import { Api } from '../services/Api' ; import { useInterval } from './useInterval' ; import { useSelectedStatuses } from './useSelectedStatuses' ; import { QueueActions , SelectedStatuses } from '../../typings/app' ; import { AppJob } from '@bull-board/api/typings/app' ; import { GetQueuesResponse } from '@bull-board/api/typings/responses' ; import { useQuery } from './useQuery' ; import { ConfirmApi , useConfirm } from './useConfirm' ; const interval = 5000 ; type State = { data : null | GetQueuesResponse ; loading : boolean ; } ; export interface Store { state : State ; actions : QueueActions ; selectedStatuses : SelectedStatuses ; confirmProps : ConfirmApi [ 'confirmProps' ] ; } export const useStore = ( api ) : Store => { const query = useQuery ( ) ; const [ state , setState ] = useState < State > ( { data : null , loading : true , } ) ; const { confirmProps , openConfirm } = useConfirm ( ) ; const selectedStatuses = useSelectedStatuses ( ) ; const update = ( ) => api . getQueues ( { status : selectedStatuses , page : query . get ( 'page' ) || '1' } ) . then ( ( data ) => { setState ( { data , loading : false } ) ; } ) . catch ( ( error ) => console . error ( 'Failed to poll' , error ) ) ; function withConfirmAndUpdate ( action , description ) { return async ( ) => { try { await openConfirm ( { description } ) ; await action ( ) ; await update ( ) ; } catch ( e ) { if ( e ) { console . error ( e ) ; } } } ; } useInterval ( update , interval , [ selectedStatuses ] ) ; const promoteJob = ( queueName ) => ( job ) => withConfirmAndUpdate ( ( ) => api . promoteJob ( queueName , job . id ) , 'Are you sure that you want to promote this job?' ) ; const retryJob = ( queueName ) => ( job ) => withConfirmAndUpdate ( ( ) => api . retryJob ( queueName , job . id ) , 'Are you sure that you want to retry this job?' ) ; const cleanJob = ( queueName ) => ( job ) => withConfirmAndUpdate ( ( ) => api . cleanJob ( queueName , job . id ) , 'Are you sure that you want to clean this job?' ) ; const retryAll = ( queueName ) => withConfirmAndUpdate ( ( ) => api . retryAll ( queueName ) , 'Are you sure that you want to retry all jobs?' ) ; const cleanAllDelayed = ( queueName ) => withConfirmAndUpdate ( ( ) => api . cleanAllDelayed ( queueName ) , 'Are you sure that you want to clean all delayed jobs?' ) ; const cleanAllFailed = ( queueName ) => withConfirmAndUpdate ( ( ) => api . cleanAllFailed ( queueName ) , 'Are you sure that you want to clean all failed jobs?' ) ; const cleanAllCompleted = ( queueName ) => withConfirmAndUpdate ( ( ) => api . cleanAllCompleted ( queueName ) , 'Are you sure that you want to clean all completed jobs?' ) ; const pauseQueue = ( queueName ) => withConfirmAndUpdate ( ( ) => api . pauseQueue ( queueName ) , 'Are you sure that you want to pause queue processing?' ) ; const resumeQueue = ( queueName ) => withConfirmAndUpdate ( ( ) => api . resumeQueue ( queueName ) , 'Are you sure that you want to resume queue processing?' ) ; const getJobLogs = ( queueName ) => ( job ) => ( ) => api . getJobLogs ( queueName , job . id ) ; return { state , actions : { promoteJob , retryJob , retryAll , cleanJob , cleanAllDelayed , cleanAllFailed , cleanAllCompleted , getJobLogs , pauseQueue , resumeQueue } , confirmProps , selectedStatuses , } ; } ;