import { useEffect } from 'react' ; import { BehaviorSubject } from 'rxjs' ; import { filter , distinctUntilChanged } from 'rxjs/operators' ; import { Subscription } from 'rxjs' ; import { TransformId } from '../../../common/types/transform' ; export function isTransformIdValid ( transformId ) { return /^[a-z0-9](?:[a-z0-9_\-\.]*[a-z0-9])?$/g . test ( transformId ) ; } export enum REFRESH_TRANSFORM_LIST_STATE { ERROR = 'error' , IDLE = 'idle' , LOADING = 'loading' , REFRESH = 'refresh' } export const refreshTransformList$ = new BehaviorSubject < REFRESH_TRANSFORM_LIST_STATE > ( REFRESH_TRANSFORM_LIST_STATE . IDLE ) ; export const useRefreshTransformList = ( callback = { } ) => { useEffect ( ( ) => { const distinct$ = refreshTransformList$ . pipe ( distinctUntilChanged ( ) ) ; const subscriptions = [ ] ; if ( typeof callback . onRefresh === 'function' ) { callback . onRefresh ( ) ; subscriptions . push ( distinct$ . pipe ( filter ( ( state ) => state === REFRESH_TRANSFORM_LIST_STATE . REFRESH ) ) . subscribe ( ( ) => typeof callback . onRefresh === 'function' && callback . onRefresh ( ) ) ) ; } if ( typeof callback . isLoading === 'function' ) { subscriptions . push ( distinct$ . subscribe ( ( state ) => typeof callback . isLoading === 'function' && callback . isLoading ( state === REFRESH_TRANSFORM_LIST_STATE . LOADING ) ) ) ; } return ( ) => { subscriptions . map ( ( sub ) => sub . unsubscribe ( ) ) ; } ; } , [ ] ) ; return { refresh : ( ) => { refreshTransformList$ . next ( REFRESH_TRANSFORM_LIST_STATE . REFRESH ) ; refreshTransformList$ . next ( REFRESH_TRANSFORM_LIST_STATE . LOADING ) ; } , } ; } ;