Skip to content

Commit 95917ba

Browse files
committed
Removed excessive loading screens
1 parent 63bded4 commit 95917ba

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

frontend/app/inspections/[id]/page.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useCallback, useEffect, useState } from "react";
3+
import { useCallback, useEffect, useRef, useState } from "react";
44
import { useParams, useRouter } from "next/navigation";
55
import Image from "next/image";
66
import InspectionDetailsPanel from "@/components/InspectionDetailsPanel";
@@ -18,6 +18,7 @@ const InspectionDetailPage = () => {
1818
const { inspections, fetchInspectionById } = useInspections();
1919

2020
const [inspection, setInspection] = useState<Inspection | null>(null);
21+
const inspectionRef = useRef<Inspection | null>(null);
2122
const [username, setUsername] = useState<string | null>(null);
2223
const [profileSrc, setProfileSrc] = useState<string>("/avatar.png");
2324
const [isLoading, setIsLoading] = useState(true);
@@ -32,6 +33,10 @@ const InspectionDetailPage = () => {
3233
});
3334
}, []);
3435

36+
useEffect(() => {
37+
inspectionRef.current = inspection;
38+
}, [inspection]);
39+
3540
useEffect(() => {
3641
try {
3742
const loggedIn =
@@ -63,15 +68,33 @@ const InspectionDetailPage = () => {
6368
}
6469
return;
6570
}
66-
setIsLoading(true);
67-
setLoadingMessage("Loading inspection...");
71+
const currentInspection = inspectionRef.current;
6872
setNotFound(false);
6973
const existing = inspections.find((candidate) => {
7074
if (candidate.id && candidate.id === rawId) return true;
7175
return candidate.inspectionNumber === rawId;
7276
});
73-
let resolved: Inspection | null = existing ?? null;
7477
const targetId = existing?.id ?? rawId;
78+
const targetKey = existing?.id ?? existing?.inspectionNumber ?? rawId;
79+
const currentIdentifiers = new Set<string>();
80+
if (typeof currentInspection?.id === "string") {
81+
currentIdentifiers.add(currentInspection.id);
82+
}
83+
if (typeof currentInspection?.inspectionNumber === "string") {
84+
currentIdentifiers.add(currentInspection.inspectionNumber);
85+
}
86+
const candidateKeys = new Set<string>();
87+
if (typeof rawId === "string") candidateKeys.add(rawId);
88+
if (typeof targetId === "string") candidateKeys.add(targetId);
89+
if (typeof targetKey === "string") candidateKeys.add(targetKey);
90+
const matchesCurrent =
91+
currentIdentifiers.size > 0 &&
92+
Array.from(candidateKeys).some((key) => currentIdentifiers.has(key));
93+
if (!matchesCurrent) {
94+
setIsLoading(true);
95+
setLoadingMessage("Loading inspection...");
96+
}
97+
let resolved: Inspection | null = existing ?? currentInspection ?? null;
7598
try {
7699
const fetched = await fetchInspectionById(targetId);
77100
if (fetched) {
@@ -80,17 +103,17 @@ const InspectionDetailPage = () => {
80103
} catch {
81104
// ignore fetch failures
82105
}
106+
if (!active) return;
83107
if (!resolved) {
84-
if (active) {
108+
if (!currentInspection) {
85109
setInspection(null);
86110
setNotFound(true);
87111
}
88-
} else if (active) {
112+
} else {
89113
setInspection(resolved);
114+
setNotFound(false);
90115
}
91-
if (active) {
92-
setIsLoading(false);
93-
}
116+
setIsLoading(false);
94117
};
95118
void load();
96119
return () => {

0 commit comments

Comments
 (0)