|
| 1 | +import React, { useMemo, useRef, useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { useApolloClient } from '@apollo/client'; |
| 4 | +import { Button } from 'react-bootstrap'; |
| 5 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 6 | +import { faFileImport } from '@fortawesome/free-solid-svg-icons'; |
| 7 | +import useConfirmationModal from '../../../hooks/useConfirmationModal'; |
| 8 | +import { useTriggerLoad } from '../../common/LoadingStatus'; |
| 9 | +import BasicModal from '../../common/BasicModal'; |
| 10 | +import { SCHEDULE_COLLECTION_JOB_MUTATION } from '../../AddTestToQueueWithConfirmation/queries'; |
| 11 | +import { TEST_QUEUE_PAGE_QUERY } from '../../TestQueue/queries'; |
| 12 | +import { TEST_PLAN_REPORT_STATUS_DIALOG_QUERY } from '../../TestPlanReportStatusDialog/queries'; |
| 13 | + |
| 14 | +const StartBotRunButton = ({ testPlanReport, onChange }) => { |
| 15 | + const client = useApolloClient(); |
| 16 | + const { triggerLoad } = useTriggerLoad(); |
| 17 | + const { showConfirmationModal, hideConfirmationModal } = |
| 18 | + useConfirmationModal(); |
| 19 | + // Synchronize the state of the action with the modal UI |
| 20 | + const [isActionPending, setIsActionPending] = useState(false); |
| 21 | + // Ref guard to prevent concurrent actions |
| 22 | + const isConfirmingRef = useRef(false); |
| 23 | + |
| 24 | + const atLatestAutomationSupportedVersion = useMemo(() => { |
| 25 | + const versions = testPlanReport?.at?.atVersions || []; |
| 26 | + const supported = versions.filter(v => v.supportedByAutomation); |
| 27 | + if (!supported.length) return null; |
| 28 | + return supported.reduce((latest, v) => |
| 29 | + new Date(v.releasedAt) > new Date(latest.releasedAt) ? v : latest |
| 30 | + ); |
| 31 | + }, [testPlanReport]); |
| 32 | + |
| 33 | + // Shorten the AT name to the first word |
| 34 | + // specifically needed for "VoiceOver for macOS" |
| 35 | + const shortenedAtName = useMemo(() => { |
| 36 | + return testPlanReport.at.name.split(' ')[0]; |
| 37 | + }, [testPlanReport]); |
| 38 | + |
| 39 | + const onStart = () => { |
| 40 | + const title = `Start ${testPlanReport.at.name} Bot Run`; |
| 41 | + const content = ( |
| 42 | + <> |
| 43 | + <p> |
| 44 | + This will run the bot using {testPlanReport.at.name}{' '} |
| 45 | + {atLatestAutomationSupportedVersion?.name ?? |
| 46 | + '(no automation-supported version found)'} |
| 47 | + . |
| 48 | + </p> |
| 49 | + </> |
| 50 | + ); |
| 51 | + |
| 52 | + const onConfirm = async () => { |
| 53 | + if (isConfirmingRef.current || isActionPending) return; |
| 54 | + isConfirmingRef.current = true; |
| 55 | + setIsActionPending(true); |
| 56 | + // Immediately reflect disabled/label in the modal UI |
| 57 | + showConfirmationModal(renderModal()); |
| 58 | + try { |
| 59 | + await triggerLoad(async () => { |
| 60 | + await client.mutate({ |
| 61 | + mutation: SCHEDULE_COLLECTION_JOB_MUTATION, |
| 62 | + variables: { testPlanReportId: testPlanReport.id }, |
| 63 | + refetchQueries: [ |
| 64 | + TEST_QUEUE_PAGE_QUERY, |
| 65 | + TEST_PLAN_REPORT_STATUS_DIALOG_QUERY |
| 66 | + ], |
| 67 | + awaitRefetchQueries: true |
| 68 | + }); |
| 69 | + }, 'Scheduling Collection Job'); |
| 70 | + hideConfirmationModal(); |
| 71 | + if (onChange) await onChange(); |
| 72 | + } finally { |
| 73 | + setIsActionPending(false); |
| 74 | + isConfirmingRef.current = false; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + const renderModal = () => ( |
| 79 | + <BasicModal |
| 80 | + show |
| 81 | + title={title} |
| 82 | + content={content} |
| 83 | + staticBackdrop={true} |
| 84 | + handleClose={() => hideConfirmationModal()} |
| 85 | + useOnHide={true} |
| 86 | + actions={[ |
| 87 | + { |
| 88 | + label: |
| 89 | + isConfirmingRef.current || isActionPending |
| 90 | + ? 'Starting...' |
| 91 | + : 'Start', |
| 92 | + onClick: onConfirm, |
| 93 | + testId: 'confirm-start-bot-run', |
| 94 | + disabled: isConfirmingRef.current || isActionPending |
| 95 | + } |
| 96 | + ]} |
| 97 | + /> |
| 98 | + ); |
| 99 | + |
| 100 | + showConfirmationModal(renderModal()); |
| 101 | + }; |
| 102 | + |
| 103 | + return ( |
| 104 | + <Button variant="secondary" onClick={onStart}> |
| 105 | + <FontAwesomeIcon icon={faFileImport} /> |
| 106 | + {`Start ${shortenedAtName} Bot Run`} |
| 107 | + </Button> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +StartBotRunButton.propTypes = { |
| 112 | + testPlanReport: PropTypes.shape({ |
| 113 | + id: PropTypes.string.isRequired, |
| 114 | + at: PropTypes.shape({ |
| 115 | + name: PropTypes.string.isRequired, |
| 116 | + atVersions: PropTypes.arrayOf( |
| 117 | + PropTypes.shape({ |
| 118 | + name: PropTypes.string.isRequired, |
| 119 | + releasedAt: PropTypes.string.isRequired, |
| 120 | + supportedByAutomation: PropTypes.bool |
| 121 | + }) |
| 122 | + ) |
| 123 | + }).isRequired |
| 124 | + }).isRequired, |
| 125 | + onChange: PropTypes.func |
| 126 | +}; |
| 127 | + |
| 128 | +export default StartBotRunButton; |
0 commit comments