-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathTerminalRunBannerContainer.tsx
161 lines (140 loc) · 4.21 KB
/
TerminalRunBannerContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { useTranslation } from 'react-i18next'
import {
SPACING,
TYPOGRAPHY,
Banner,
JUSTIFY_SPACE_BETWEEN,
Flex,
StyledText,
Link,
ALIGN_CENTER,
} from '@opentrons/components'
import { RUN_STATUS_STOPPED, RUN_STATUS_SUCCEEDED } from '@opentrons/api-client'
import {
useCloseCurrentRun,
useIsRunCurrent,
useMostRecentRunId,
} from '/app/resources/runs'
import type { RunHeaderBannerContainerProps } from '.'
type TerminalBannerType = 'success' | 'error' | null
// Determine which terminal banner to render, if any.
export function useTerminalRunBannerContainer({
runId,
runStatus,
isResetRunLoading,
runErrors,
enteredER,
}: RunHeaderBannerContainerProps): TerminalBannerType {
const { highestPriorityError, commandErrorList } = runErrors
const isRunCurrent = useIsRunCurrent(runId)
const mostRecentRunId = useMostRecentRunId()
const isMostRecentRun = mostRecentRunId === runId
const cancelledWithoutRecovery =
!enteredER && runStatus === RUN_STATUS_STOPPED
const completedWithErrors =
(commandErrorList != null && commandErrorList.length > 0) ||
highestPriorityError != null
const showSuccessBanner =
runStatus === RUN_STATUS_SUCCEEDED &&
isRunCurrent &&
!isResetRunLoading &&
!completedWithErrors
// TODO(jh, 08-14-24): Ideally, the backend never returns the "user cancelled a run" error and
// cancelledWithoutRecovery becomes unnecessary.
const showErrorBanner =
isMostRecentRun &&
!cancelledWithoutRecovery &&
!isResetRunLoading &&
completedWithErrors
if (showSuccessBanner) {
return 'success'
} else if (showErrorBanner) {
return 'error'
} else {
return null
}
}
interface TerminalRunBannerContainerProps
extends RunHeaderBannerContainerProps {
bannerType: TerminalBannerType
}
// Contains all possible banners that render after the run reaches a terminal run status.
export function TerminalRunBannerContainer(
props: TerminalRunBannerContainerProps
): JSX.Element {
const { bannerType } = props
switch (bannerType) {
case 'success':
return <ProtocolRunSuccessBanner />
case 'error':
return <ProtocolRunErrorBanner {...props} />
default:
console.error('Handle banner cases explicitly.')
return <div />
}
}
function ProtocolRunSuccessBanner(): JSX.Element {
const { t } = useTranslation('run_details')
const { closeCurrentRun, isClosingCurrentRun } = useCloseCurrentRun()
const handleRunSuccessClick = (): void => {
closeCurrentRun()
}
return (
<Banner
type="success"
onCloseClick={handleRunSuccessClick}
isCloseActionLoading={isClosingCurrentRun}
iconMarginLeft={SPACING.spacing4}
>
<Flex justifyContent={JUSTIFY_SPACE_BETWEEN} width="100%">
{t('run_completed')}
</Flex>
</Banner>
)
}
function ProtocolRunErrorBanner({
runErrors,
runStatus,
runHeaderModalContainerUtils,
}: RunHeaderBannerContainerProps): JSX.Element {
const { t } = useTranslation('run_details')
const { closeCurrentRun } = useCloseCurrentRun()
const { highestPriorityError } = runErrors
const handleFailedRunClick = (): void => {
closeCurrentRun()
runHeaderModalContainerUtils.runFailedModalUtils.toggleModal()
}
return (
<Banner
type={runStatus === RUN_STATUS_SUCCEEDED ? 'warning' : 'error'}
iconMarginLeft={SPACING.spacing4}
>
<Flex
justifyContent={JUSTIFY_SPACE_BETWEEN}
alignItems={ALIGN_CENTER}
width="100%"
>
<StyledText desktopStyle="bodyDefaultRegular">
{highestPriorityError != null
? t('error_info', {
errorType: highestPriorityError?.errorType,
errorCode: highestPriorityError?.errorCode,
})
: `${
runStatus === RUN_STATUS_SUCCEEDED
? t('run_completed_with_warnings')
: t('run_canceled_with_errors')
}`}
</StyledText>
<Link
onClick={handleFailedRunClick}
textDecoration={TYPOGRAPHY.textDecorationUnderline}
>
{runStatus === RUN_STATUS_SUCCEEDED
? t('view_warning_details')
: t('view_error_details')}
</Link>
</Flex>
</Banner>
)
}