|
1 |
| -import { useCallback, useContext, useEffect, useRef } from 'react' |
| 1 | +import { useCallback, useContext, useEffect, useRef, useState } from 'react' |
2 | 2 | import { useTranslation } from 'react-i18next'
|
3 | 3 | import { useQueryClient } from 'react-query'
|
4 | 4 | import { useDispatch } from 'react-redux'
|
@@ -152,34 +152,49 @@ export function useGetNewModules(): AttachedModule[] {
|
152 | 152 | }
|
153 | 153 |
|
154 | 154 | export function useModuleAttachedToast(
|
155 |
| - launchModuleSetupCallback: () => void |
| 155 | + launchModuleSetupCallback: (open: boolean) => void |
156 | 156 | ): void {
|
157 | 157 | const newModules = useGetNewModules()
|
158 | 158 | const currentRunId = useCurrentRunId({ refetchInterval: CURRENT_RUN_POLL })
|
159 | 159 | const attachedPipettes = useAttachedPipettes(newModules.length > 0)
|
160 | 160 | const { t, i18n } = useTranslation(['module_wizard_flows', 'shared'])
|
161 |
| - const { makeToast } = useToaster() |
| 161 | + const { makeToast, eatToast } = useToaster() |
162 | 162 | const moduleSerials = newModules.map(m => m.serialNumber)
|
163 | 163 | const moduleSerialsRef = useRef(moduleSerials)
|
164 | 164 | const runInProgress = currentRunId != null
|
| 165 | + const [toastID, setToastID] = useState<string>('') |
165 | 166 |
|
166 | 167 | useEffect(() => {
|
167 | 168 | const newModuleSerials = difference(moduleSerials, moduleSerialsRef.current)
|
168 | 169 | const hasPipette =
|
169 | 170 | attachedPipettes.left != null || attachedPipettes.right != null
|
170 | 171 | if (!runInProgress && hasPipette && newModuleSerials.length > 0) {
|
171 |
| - makeToast(t('module_added') as string, 'info', { |
172 |
| - buttonText: i18n.format(t('shared:close'), 'capitalize'), |
173 |
| - linkText: t('module_added_link'), |
174 |
| - onLinkClick: launchModuleSetupCallback, |
175 |
| - disableTimeout: true, |
176 |
| - displayType: 'odd', |
177 |
| - }) |
| 172 | + setToastID( |
| 173 | + makeToast(t('module_added') as string, 'info', { |
| 174 | + buttonText: i18n.format(t('shared:close'), 'capitalize'), |
| 175 | + linkText: t('module_added_link'), |
| 176 | + onLinkClick: () => { |
| 177 | + launchModuleSetupCallback(true) |
| 178 | + }, |
| 179 | + disableTimeout: true, |
| 180 | + displayType: 'odd', |
| 181 | + }) |
| 182 | + ) |
178 | 183 | }
|
| 184 | + |
179 | 185 | moduleSerialsRef.current = moduleSerials
|
180 | 186 | // dont want this hook to rerun when other deps change
|
181 | 187 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
182 | 188 | }, [moduleSerials, runInProgress])
|
| 189 | + |
| 190 | + useEffect(() => { |
| 191 | + // Close toast if there are no new modules to setup |
| 192 | + if (toastID && newModules.length === 0) { |
| 193 | + launchModuleSetupCallback(false) |
| 194 | + eatToast(toastID) |
| 195 | + setToastID('') |
| 196 | + } |
| 197 | + }, [toastID, newModules]) |
183 | 198 | }
|
184 | 199 |
|
185 | 200 | export function useScrollRef(): {
|
|
0 commit comments