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;
|
||||
|
||||
Reference in New Issue
Block a user