import SeamlyGeneralError from 'api/errors/seamly-general-error' import { useInterrupt } from 'domains/interrupt/hooks' import { setInterrupt } from 'domains/interrupt/slice' import { useAppDispatch } from 'domains/store' import { useEffect, useRef } from 'preact/hooks' import useSeamlyCommands from './use-seamly-commands' export default function useSessionExpiredCommand() { const { meta: { originalError, action }, } = useInterrupt() const dispatch = useAppDispatch() const { reset } = useSeamlyCommands() const isExpiredError = originalError?.name === 'SeamlySessionExpiredError' const limit = useRef(0) const limitTimer = useRef | null>(null) useEffect(() => { if (isExpiredError && action === 'reset') { if (limit.current >= 10) { limitTimer.current = setTimeout(() => { limit.current = 0 }, 10000) const error = new SeamlyGeneralError() dispatch( setInterrupt({ name: error.name, message: error.message, langKey: error.langKey, originalEvent: error.originalEvent, originalError: error.originalError, action: error.action, }), ) return () => {} } limit.current += 1 reset() } return () => { if (limitTimer.current) clearTimeout(limitTimer.current) } }, [action, reset, isExpiredError, dispatch]) }