diff --git a/apps/Frontend/src/pages/claims-page.tsx b/apps/Frontend/src/pages/claims-page.tsx index fca13944..f17b7197 100755 --- a/apps/Frontend/src/pages/claims-page.tsx +++ b/apps/Frontend/src/pages/claims-page.tsx @@ -44,6 +44,10 @@ export default function ClaimsPage() { const [isClaimFormOpen, setIsClaimFormOpen] = useState(false); const [selectedPatientId, setSelectedPatientId] = useState(null); const [chatbotAutoSubmitSiteKey, setChatbotAutoSubmitSiteKey] = useState(undefined); + // Mirrors chatbotAutoSubmitSiteKey but survives closeClaim(), which resets the state + // synchronously right after the selenium job is kicked off — well before the job's async + // completion handlers (which gate the PDF preview popup) actually run. + const chatbotAutoSubmitSiteKeyRef = useRef(undefined); const [chatbotInitialTab, setChatbotInitialTab] = useState(undefined); const [chatbotAutoSubmitPreauthSiteKey, setChatbotAutoSubmitPreauthSiteKey] = useState(undefined); // for redirect from appointment page directly, then passing to claimform @@ -156,11 +160,12 @@ export default function ClaimsPage() { if (jobResult.pdfFileId) { advanceAiClaimQueue(); const advancedToNext = advanceChatbotClaimQueue(); - if (!advancedToNext && !chatbotAutoSubmitSiteKey) { + if (!advancedToNext && !chatbotAutoSubmitSiteKeyRef.current) { setPreviewPdfId(jobResult.pdfFileId); setPreviewFallbackFilename(jobResult.pdfFilename ?? `cca_claim_${jobResult.claimNumber ?? "unknown"}.pdf`); setPreviewOpen(true); } + chatbotAutoSubmitSiteKeyRef.current = undefined; dispatch(setTaskStatus({ key: "claimSubmit", status: "success", @@ -240,6 +245,7 @@ export default function ClaimsPage() { const parsed = JSON.parse(raw); if (parsed?.autoSubmit && parsed?.siteKey) { setChatbotAutoSubmitSiteKey(parsed.siteKey); + chatbotAutoSubmitSiteKeyRef.current = parsed.siteKey; } } } catch {} @@ -308,6 +314,7 @@ export default function ClaimsPage() { const parsed = JSON.parse(raw); if (parsed?.autoSubmit && parsed?.siteKey) { setChatbotAutoSubmitSiteKey(parsed.siteKey); + chatbotAutoSubmitSiteKeyRef.current = parsed.siteKey; } } } catch {} @@ -994,11 +1001,12 @@ export default function ClaimsPage() { // Pop up the final PDF so the user doesn't need to navigate to Documents // (skip if we're about to navigate to the next queued patient's claim, // or if this claim was submitted via the AI chat column) - if (result.pdfFileId && !advancedToNext && !chatbotAutoSubmitSiteKey) { + if (result.pdfFileId && !advancedToNext && !chatbotAutoSubmitSiteKeyRef.current) { setPreviewPdfId(result.pdfFileId); setPreviewFallbackFilename(result.fileName ?? null); setPreviewOpen(true); } + chatbotAutoSubmitSiteKeyRef.current = undefined; return result; } catch (error: any) {