|
| 1 | +import React, {FC, useCallback, useContext, useEffect} from 'react' |
| 2 | +import {useDispatch, useSelector} from 'react-redux' |
| 3 | +import {Button, ComponentColor} from '@influxdata/clockface' |
| 4 | +import {PROJECT_NAME} from 'src/flows' |
| 5 | +import {AppState, AutoRefreshStatus} from 'src/types' |
| 6 | +import {resetAutoRefresh} from 'src/shared/actions/autoRefresh' |
| 7 | +import {dismissOverlay, showOverlay} from 'src/overlays/actions/overlays' |
| 8 | +import {GlobalAutoRefresher} from 'src/utils/AutoRefresher' |
| 9 | +import {notify} from 'src/shared/actions/notifications' |
| 10 | +import {autoRefreshTimeoutSuccess} from 'src/shared/copy/notifications' |
| 11 | +import {event} from 'src/cloud/utils/reporting' |
| 12 | +import {AUTOREFRESH_DEFAULT} from 'src/shared/constants' |
| 13 | +import {FlowContext} from 'src/flows/context/flow.current' |
| 14 | +import {FlowQueryContext} from 'src/flows/context/flow.query' |
| 15 | +import {isFlagEnabled} from 'src/shared/utils/featureFlag' |
| 16 | + |
| 17 | +const AutoRefreshButton: FC = () => { |
| 18 | + const {flow} = useContext(FlowContext) |
| 19 | + const {queryAll} = useContext(FlowQueryContext) |
| 20 | + const dispatch = useDispatch() |
| 21 | + |
| 22 | + const autoRefresh = useSelector( |
| 23 | + (state: AppState) => |
| 24 | + state.autoRefresh?.[`${PROJECT_NAME}-${flow?.id}`] ?? AUTOREFRESH_DEFAULT |
| 25 | + ) |
| 26 | + |
| 27 | + const timeoutFunction = useCallback(() => { |
| 28 | + if (isFlagEnabled('flowAutoRefresh')) { |
| 29 | + dispatch(resetAutoRefresh(`${PROJECT_NAME}-${flow?.id}`)) |
| 30 | + dispatch(notify(autoRefreshTimeoutSuccess())) |
| 31 | + GlobalAutoRefresher.onDisconnect() |
| 32 | + event('flow inactivitytimeout', { |
| 33 | + timeout: autoRefresh.inactivityTimeout, |
| 34 | + }) |
| 35 | + } |
| 36 | + }, [autoRefresh.inactivityTimeout, flow?.id, dispatch]) |
| 37 | + |
| 38 | + const startTimeout = useCallback(() => { |
| 39 | + if (isFlagEnabled('flowAutoRefresh')) { |
| 40 | + GlobalAutoRefresher.startTimeout( |
| 41 | + timeoutFunction, |
| 42 | + autoRefresh.inactivityTimeout |
| 43 | + ) |
| 44 | + } |
| 45 | + }, [autoRefresh.inactivityTimeout, timeoutFunction]) |
| 46 | + |
| 47 | + const stopFunc = useCallback(() => { |
| 48 | + if (isFlagEnabled('flowAutoRefresh')) { |
| 49 | + if ( |
| 50 | + !autoRefresh.infiniteDuration && |
| 51 | + new Date(autoRefresh?.duration?.upper).getTime() <= new Date().getTime() |
| 52 | + ) { |
| 53 | + GlobalAutoRefresher.stopPolling() |
| 54 | + dispatch(resetAutoRefresh(`${PROJECT_NAME}-${flow?.id}`)) |
| 55 | + } |
| 56 | + } |
| 57 | + }, [ |
| 58 | + flow?.id, |
| 59 | + dispatch, |
| 60 | + autoRefresh?.duration?.upper, |
| 61 | + autoRefresh.infiniteDuration, |
| 62 | + ]) |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + if (isFlagEnabled('flowAutoRefresh')) { |
| 66 | + if (autoRefresh?.status === AutoRefreshStatus.Active) { |
| 67 | + GlobalAutoRefresher.poll(autoRefresh, stopFunc) |
| 68 | + if (autoRefresh.inactivityTimeout > 0) { |
| 69 | + GlobalAutoRefresher.onDisconnect() |
| 70 | + startTimeout() |
| 71 | + GlobalAutoRefresher.onConnect() |
| 72 | + } |
| 73 | + } else { |
| 74 | + GlobalAutoRefresher.onDisconnect() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return () => { |
| 79 | + if (isFlagEnabled('flowAutoRefresh')) { |
| 80 | + GlobalAutoRefresher.onDisconnect() |
| 81 | + GlobalAutoRefresher.stopPolling() |
| 82 | + } |
| 83 | + } |
| 84 | + }, [ |
| 85 | + autoRefresh.interval, |
| 86 | + autoRefresh?.status, |
| 87 | + autoRefresh.inactivityTimeout, |
| 88 | + stopFunc, |
| 89 | + startTimeout, |
| 90 | + ]) |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + GlobalAutoRefresher.subscribe(queryAll) |
| 94 | + |
| 95 | + return () => GlobalAutoRefresher.unsubscribe(queryAll) |
| 96 | + }, [queryAll]) |
| 97 | + |
| 98 | + const isActive = autoRefresh?.status === AutoRefreshStatus.Active |
| 99 | + |
| 100 | + return ( |
| 101 | + <Button |
| 102 | + text={ |
| 103 | + isActive |
| 104 | + ? `Refreshing Every ${autoRefresh?.label}` |
| 105 | + : 'Enable Auto Refresh' |
| 106 | + } |
| 107 | + color={isActive ? ComponentColor.Secondary : ComponentColor.Default} |
| 108 | + onClick={ |
| 109 | + isActive |
| 110 | + ? () => dispatch(resetAutoRefresh(`${PROJECT_NAME}-${flow?.id}`)) |
| 111 | + : () => |
| 112 | + dispatch( |
| 113 | + showOverlay( |
| 114 | + 'toggle-auto-refresh', |
| 115 | + {id: `${PROJECT_NAME}-${flow?.id}`}, |
| 116 | + () => dispatch(dismissOverlay()) |
| 117 | + ) |
| 118 | + ) |
| 119 | + } |
| 120 | + testID="enable-auto-refresh-button" |
| 121 | + /> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export default AutoRefreshButton |
0 commit comments