Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PrintPreview from "@/CAREUI/misc/PrintPreview";
import PrintFooter from "@/components/Common/PrintFooter";
import "@/lib/pdfWorker";
import useCurrentFacility from "@/pages/Facility/utils/useCurrentFacility";
import { useCurrentFacilitySilently } from "@/pages/Facility/utils/useCurrentFacility";
import diagnosticReportApi from "@/types/emr/diagnosticReport/diagnosticReportApi";
import { PrintTemplateType } from "@/types/facility/printTemplate";
import { FileReadMinimal } from "@/types/files/file";
Expand Down Expand Up @@ -94,7 +94,7 @@ export default function DiagnosticReportPrint({
diagnosticReportId: string;
}) {
const { t } = useTranslation();
const { facility } = useCurrentFacility();
const { facility } = useCurrentFacilitySilently();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Hooks called inside try-catch in the underlying utility

useCurrentFacilitySilently wraps useCurrentFacility in a try-catch. Inside useCurrentFacility, useFullPath() runs first, then extractFacilityId throws synchronously before useQuery is reached. This means on a non-facility path only one hook fires, while on a facility path two fire — a conditional hook-count difference that violates React's Rules of Hooks. In practice this is stable here because a print page stays on one path type for its lifetime, but if the component were ever remounted on a different path type React would throw a "Rendered more hooks than during the previous render" error. A safer long-term fix would be to make extractFacilityId return undefined instead of throwing, then pass enabled: !!facilityId to useQuery, avoiding the try-catch entirely.


const { data: report, isLoading } = useQuery({
queryKey: ["diagnosticReport", diagnosticReportId],
Expand Down
Loading