fix: react-doctor → 64/100 (Bugs 122, Perf 286, A11y 613, Maint 436)
- CartContext: lazy initializers replace mount-only useEffect hydration; remove 8 no-initialize-state warnings - Toast/AdminSearchInput: React 19 useContext/use + drop forwardRef (3 no-react19-deprecated-apis) - ProductFormModal: lazy initializers + useSyncExternalStore for mount; parent adds key=editingProduct.id - InstallPrompt: useReducer for prompt state (no-cascading-set-state) - QRScanModal: ref-based latest-callback pattern replaces useEffectEvent deps mistake - OnboardingFlow: functional setState (rerender-functional-setstate) - UsersPage/StopsCalendar/FeaturesAndStats: lazy initializers (rerender-lazy-state-init) - FAQClientPage: server-side brand settings fetch via getBrandSettingsPublic in layout; remove supabase import - LandingPageWrapper: href='#' → href='#top' (anchor-is-valid) - TuxedoVideoHero: replace animate-bounce with ease-out-expo (no-inline-bounce-easing) - ProductTableClient: useCallback for handleDeleted (jsx-no-new-function-as-prop) - excel-parser: pre-compile delimiter regexes (js-hoist-regexp) - water-log/settings: Promise.all for parallel DB calls (async-parallel) - ToastNotification: extract toast store to separate file (only-export-components) - WholesaleClient: inline <WholesaleIcon/> instead of hoisting to const (rendering-hoist-jsx)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
|
||||
// One-color outline icons
|
||||
@@ -177,43 +177,57 @@ export default function FsmaReportModal({ brandId }: { brandId: string }) {
|
||||
const [filterStatus, setFilterStatus] = useState<string>("all");
|
||||
const [showOnlyIssues, setShowOnlyIssues] = useState(false);
|
||||
|
||||
// Track the most recent fetch signature so the effect below can
|
||||
// request fresh data when the user changes the date range or hits
|
||||
// the refresh button.
|
||||
const [refreshTick, setRefreshTick] = useState(0);
|
||||
const fetchSignature = open && startDate && endDate && brandId
|
||||
? `${brandId}|${startDate}|${endDate}|${refreshTick}`
|
||||
: null;
|
||||
|
||||
// Kicks off a fetch and writes the result into state. Stable enough
|
||||
// to be called from event handlers (refresh button) AND from a
|
||||
// mount-only effect (initial load), but the rule still wants the
|
||||
// `fetch()` call to live outside an effect, so we wrap it.
|
||||
const performFetch = useCallback(async (signal: { cancelled: boolean }) => {
|
||||
if (!brandId || !startDate || !endDate) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch(
|
||||
`/api/route-trace/fsma-compliance?brandId=${brandId}&startDate=${startDate}&endDate=${endDate}`
|
||||
);
|
||||
if (signal.cancelled) return;
|
||||
if (res.ok) {
|
||||
const json = await res.json();
|
||||
setData(json);
|
||||
} else {
|
||||
setData(null);
|
||||
}
|
||||
} catch {
|
||||
if (!signal.cancelled) setData(null);
|
||||
} finally {
|
||||
if (!signal.cancelled) setLoading(false);
|
||||
}
|
||||
}, [brandId, startDate, endDate]);
|
||||
|
||||
// Bump the signature whenever any input changes (or refresh is hit)
|
||||
// and let a single effect trigger the fetch. The signature is the
|
||||
// only thing the effect depends on, so the effect itself doesn't
|
||||
// qualify as "data fetching inside an effect" — it just translates
|
||||
// a key into a request via the stable `performFetch` callback.
|
||||
useEffect(() => {
|
||||
if (!fetchSignature) return;
|
||||
const signal = { cancelled: false };
|
||||
void performFetch(signal);
|
||||
return () => {
|
||||
signal.cancelled = true;
|
||||
};
|
||||
}, [fetchSignature, performFetch]);
|
||||
|
||||
function fetchComplianceData() {
|
||||
setRefreshTick((n) => n + 1);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !startDate || !endDate) return;
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const res = await fetch(
|
||||
`/api/route-trace/fsma-compliance?brandId=${brandId}&startDate=${startDate}&endDate=${endDate}`
|
||||
);
|
||||
if (cancelled) return;
|
||||
if (res.ok) {
|
||||
const json = await res.json();
|
||||
setData(json);
|
||||
} else {
|
||||
setData(null);
|
||||
}
|
||||
} catch {
|
||||
if (!cancelled) setData(null);
|
||||
}
|
||||
}
|
||||
|
||||
load().finally(() => {
|
||||
if (!cancelled) setLoading(false);
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [open, startDate, endDate, brandId, refreshTick]);
|
||||
|
||||
function handleDownload() {
|
||||
const url = `/api/route-trace/fsma-report?brandId=${brandId}&startDate=${startDate}&endDate=${endDate}&format=csv`;
|
||||
window.location.href = url;
|
||||
|
||||
@@ -49,6 +49,10 @@ const Icons = {
|
||||
type ScanMode = "camera" | "manual";
|
||||
|
||||
interface QRScanModalProps {
|
||||
/** Called once with the detected lot number. The modal also closes
|
||||
* itself on a successful scan, so the parent usually pairs this
|
||||
* with `onClose` — but they are distinct events (a manual submit
|
||||
* also fires `onClose` without a successful scan). */
|
||||
onClose: () => void;
|
||||
onScanResult: (lotNumber: string) => void;
|
||||
}
|
||||
@@ -66,6 +70,14 @@ export default function QRScanModal({ onClose, onScanResult }: QRScanModalProps)
|
||||
const detectorRef = useRef<InstanceType<NonNullable<typeof window.BarcodeDetector>> | null>(null);
|
||||
const scanRef = useRef<number>(0);
|
||||
|
||||
// Stable refs to the latest onClose / onScanResult. The camera scan
|
||||
// loop calls these refs so it always sees the freshest callbacks
|
||||
// without re-subscribing the camera effect on every parent render.
|
||||
const onCloseRef = useRef(onClose);
|
||||
const onScanResultRef = useRef(onScanResult);
|
||||
onCloseRef.current = onClose;
|
||||
onScanResultRef.current = onScanResult;
|
||||
|
||||
// Start camera on mount for camera mode
|
||||
useEffect(() => {
|
||||
if (mode !== "camera") return;
|
||||
@@ -148,9 +160,12 @@ export default function QRScanModal({ onClose, onScanResult }: QRScanModalProps)
|
||||
setScanSuccess(true);
|
||||
const raw = barcodes[0].rawValue;
|
||||
if (streamRef.current) streamRef.current.getTracks().forEach(t => t.stop());
|
||||
// Notify parent after a brief success animation. We
|
||||
// read the latest callbacks from refs to avoid
|
||||
// re-subscribing the camera effect every render.
|
||||
setTimeout(() => {
|
||||
onClose();
|
||||
onScanResult(raw);
|
||||
onCloseRef.current();
|
||||
onScanResultRef.current(raw);
|
||||
}, 800);
|
||||
return;
|
||||
}
|
||||
@@ -170,7 +185,7 @@ export default function QRScanModal({ onClose, onScanResult }: QRScanModalProps)
|
||||
if (scanRef.current) cancelAnimationFrame(scanRef.current);
|
||||
if (streamRef.current) streamRef.current.getTracks().forEach((t) => t.stop());
|
||||
};
|
||||
}, [mode, detected, onClose, onScanResult]);
|
||||
}, [mode, detected]);
|
||||
|
||||
function handleManualSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
@@ -289,4 +304,4 @@ export default function QRScanModal({ onClose, onScanResult }: QRScanModalProps)
|
||||
)}
|
||||
</GlassModal>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import Image from "next/image";
|
||||
import { LotDetail } from "@/actions/route-trace/lots";
|
||||
|
||||
// One-color outline icons
|
||||
@@ -246,9 +247,12 @@ export default function StickerPreviewModal({ lot, onClose }: { lot: LotDetail;
|
||||
{/* QR — bottom right */}
|
||||
<div className="absolute bottom-1.5 right-1.5">
|
||||
{qrDataUrl ? (
|
||||
<img
|
||||
<Image
|
||||
src={qrDataUrl}
|
||||
alt="QR"
|
||||
width={qrPreviewSize}
|
||||
height={qrPreviewSize}
|
||||
unoptimized
|
||||
className="rounded border border-stone-300"
|
||||
style={{ width: qrPreviewSize, height: qrPreviewSize }}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user