refactor(scan): reset upload form state after successful diagnosis
- Restore setFileName(null) and setPreviewUrl(null) in the mutation onSuccess callback. - Ensure the file upload area resets to its default state immediately after a successful scan, preparing the UI for the next input.
This commit is contained in:
@@ -18,12 +18,15 @@ import { Modal } from "@/components/ui/modal";
|
||||
import type { DiagnosisRecord } from "@zeavis/shared";
|
||||
import { apiClient } from "@/lib/api-client";
|
||||
import { trackScan, trackDiagnosisResult } from "@/lib/telemetry";
|
||||
import { DiagnosisResultView } from "../components/diagnose-result-view";
|
||||
import { DiagnosisResultView } from "../components/diagnose-result-view";
|
||||
|
||||
export function ScanPage() {
|
||||
const [fileName, setFileName] = useState<string | null>(null);
|
||||
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
||||
const [imageDimensions, setImageDimensions] = useState<{ width: number; height: number } | null>(null);
|
||||
const [imageDimensions, setImageDimensions] = useState<{
|
||||
width: number;
|
||||
height: number;
|
||||
} | null>(null);
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
const imageRef = useRef<HTMLImageElement | null>(null);
|
||||
const navigate = useNavigate();
|
||||
@@ -36,6 +39,8 @@ export function ScanPage() {
|
||||
queryClient.invalidateQueries({ queryKey: ["diagnoses"] });
|
||||
setDiagnosisPreview(diagnosis);
|
||||
setPreviewOpen(true);
|
||||
setFileName(null);
|
||||
setPreviewUrl(null);
|
||||
if (inputRef.current) inputRef.current.value = "";
|
||||
},
|
||||
onError: () => {
|
||||
@@ -68,7 +73,8 @@ export function ScanPage() {
|
||||
};
|
||||
|
||||
const [previewOpen, setPreviewOpen] = useState(false);
|
||||
const [diagnosisPreview, setDiagnosisPreview] = useState<DiagnosisRecord | null>(null);
|
||||
const [diagnosisPreview, setDiagnosisPreview] =
|
||||
useState<DiagnosisRecord | null>(null);
|
||||
|
||||
const diagnosesQuery = useQuery({
|
||||
queryKey: ["diagnoses"],
|
||||
@@ -83,7 +89,8 @@ export function ScanPage() {
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-emerald-800">Scan Tanaman</h1>
|
||||
<p className="text-gray-500 mt-1 text-md">
|
||||
Unggah foto daun jagung untuk dianalisis oleh sistem AI kami secara real-time.
|
||||
Unggah foto daun jagung untuk dianalisis oleh sistem AI kami secara
|
||||
real-time.
|
||||
</p>
|
||||
</div>
|
||||
<Button asChild variant="outline">
|
||||
@@ -109,8 +116,12 @@ export function ScanPage() {
|
||||
onClick={() => inputRef.current?.click()}
|
||||
>
|
||||
<Upload className="mx-auto text-green-500 mb-3" size={48} />
|
||||
<h3 className="font-semibold text-base text-gray-800 mb-1">Seret & Lepas Foto Daun</h3>
|
||||
<p className="text-xs text-gray-500 mb-3">atau klik untuk memilih file berkas dari perangkat Anda</p>
|
||||
<h3 className="font-semibold text-base text-gray-800 mb-1">
|
||||
Seret & Lepas Foto Daun
|
||||
</h3>
|
||||
<p className="text-xs text-gray-500 mb-3">
|
||||
atau klik untuk memilih file berkas dari perangkat Anda
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-2 justify-center">
|
||||
<div className="bg-green-100 text-green-700 px-3 py-1 rounded-full inline-flex items-center gap-1 text-xs font-medium">
|
||||
<Check size={14} /> PNG, JPG, JPEG, WEBP
|
||||
@@ -138,7 +149,10 @@ export function ScanPage() {
|
||||
className="w-full max-h-80 object-contain"
|
||||
onLoad={(e) => {
|
||||
const img = e.currentTarget;
|
||||
setImageDimensions({ width: img.naturalWidth, height: img.naturalHeight });
|
||||
setImageDimensions({
|
||||
width: img.naturalWidth,
|
||||
height: img.naturalHeight,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
{imageDimensions && (
|
||||
@@ -156,7 +170,9 @@ export function ScanPage() {
|
||||
<div className="absolute bottom-0 left-0 w-6 h-6 border-b-2 border-l-2 border-lime-300" />
|
||||
<div className="absolute bottom-0 right-0 w-6 h-6 border-b-2 border-r-2 border-lime-300" />
|
||||
<div className="text-white text-center flex flex-col gap-1">
|
||||
<span className="text-xs font-semibold tracking-widest">AREA SCAN</span>
|
||||
<span className="text-xs font-semibold tracking-widest">
|
||||
AREA SCAN
|
||||
</span>
|
||||
<span className="text-[10px] text-lime-200 font-medium">
|
||||
{imageDimensions.width} × {imageDimensions.height}
|
||||
</span>
|
||||
@@ -218,7 +234,9 @@ export function ScanPage() {
|
||||
<div className="mt-4 p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-600">
|
||||
<p className="font-medium">Upload gagal</p>
|
||||
<p className="mt-1">
|
||||
{mutation.error instanceof Error ? mutation.error.message : "Terjadi kesalahan saat memproses gambar"}
|
||||
{mutation.error instanceof Error
|
||||
? mutation.error.message
|
||||
: "Terjadi kesalahan saat memproses gambar"}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
@@ -234,32 +252,42 @@ export function ScanPage() {
|
||||
<div className="inline-flex h-9 w-9 items-center justify-center rounded-full">
|
||||
<CircleAlert className="text-emerald-600" size={20} />
|
||||
</div>
|
||||
<h2 className="font-bold text-emerald-800">Panduan Pengambilan Foto</h2>
|
||||
<h2 className="font-bold text-emerald-800">
|
||||
Panduan Pengambilan Foto
|
||||
</h2>
|
||||
</div>
|
||||
<ul className="space-y-3">
|
||||
<li className="flex items-center gap-3">
|
||||
<div className="h-9 w-9 rounded bg-[#ECF4E8] flex items-center justify-center shrink-0">
|
||||
<ZoomIn className="text-emerald-600" size={20} />
|
||||
</div>
|
||||
<div className="text-sm text-slate-700">Jarak 15–30 cm dari daun</div>
|
||||
<div className="text-sm text-slate-700">
|
||||
Jarak 15–30 cm dari daun
|
||||
</div>
|
||||
</li>
|
||||
<li className="flex items-center gap-3">
|
||||
<div className="h-9 w-9 rounded bg-[#ECF4E8] flex items-center justify-center shrink-0">
|
||||
<Sun className="text-emerald-600" size={20} />
|
||||
</div>
|
||||
<div className="text-sm text-slate-700">Pencahayaan cukup merata</div>
|
||||
<div className="text-sm text-slate-700">
|
||||
Pencahayaan cukup merata
|
||||
</div>
|
||||
</li>
|
||||
<li className="flex items-center gap-3">
|
||||
<div className="h-9 w-9 rounded bg-[#ECF4E8] flex items-center justify-center shrink-0">
|
||||
<AlignCenter className="text-emerald-600" size={20} />
|
||||
</div>
|
||||
<div className="text-sm text-slate-700">Posisi daun mengisi bingkai dengan jelas</div>
|
||||
<div className="text-sm text-slate-700">
|
||||
Posisi daun mengisi bingkai dengan jelas
|
||||
</div>
|
||||
</li>
|
||||
<li className="flex items-center gap-3">
|
||||
<div className="h-9 w-9 rounded bg-[#ECF4E8] flex items-center justify-center shrink-0">
|
||||
<Camera className="text-emerald-600" size={20} />
|
||||
</div>
|
||||
<div className="text-sm text-slate-700">Hindari bayangan, pantulan, & blur</div>
|
||||
<div className="text-sm text-slate-700">
|
||||
Hindari bayangan, pantulan, & blur
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</CardContent>
|
||||
@@ -267,7 +295,9 @@ export function ScanPage() {
|
||||
|
||||
<Card className="w-full h-max">
|
||||
<CardContent className="px-6 py-4">
|
||||
<h3 className="font-bold text-emerald-800 flex items-center gap-3 mb-4">Persyaratan Berkas</h3>
|
||||
<h3 className="font-bold text-emerald-800 flex items-center gap-3 mb-4">
|
||||
Persyaratan Berkas
|
||||
</h3>
|
||||
<div className="flex flex-col gap-2">
|
||||
{[
|
||||
{ label: "Format", value: "PNG, JPG, JPEG, WEBP" },
|
||||
@@ -275,9 +305,17 @@ export function ScanPage() {
|
||||
{ label: "Resolusi Min.", value: "512 × 512 px" },
|
||||
{ label: "Objek Foto", value: "Daun jagung tunggal" },
|
||||
].map((r, i) => (
|
||||
<div key={i} className="flex items-center justify-between py-1.5" style={{ borderBottom: i < 3 ? "1px solid #f0f0f0" : "none" }}>
|
||||
<div
|
||||
key={i}
|
||||
className="flex items-center justify-between py-1.5"
|
||||
style={{
|
||||
borderBottom: i < 3 ? "1px solid #f0f0f0" : "none",
|
||||
}}
|
||||
>
|
||||
<span className="text-gray-400 text-xs">{r.label}</span>
|
||||
<span className="text-sm font-medium text-gray-700">{r.value}</span>
|
||||
<span className="text-sm font-medium text-gray-700">
|
||||
{r.value}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
@@ -319,58 +357,78 @@ export function ScanPage() {
|
||||
>
|
||||
{diagnosisPreview ? (
|
||||
<div className="space-y-6 p-1">
|
||||
|
||||
{/* Warning if expert review is needed */}
|
||||
{diagnosisPreview.status === "needs_review" && (
|
||||
<div className="rounded-xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900 shadow-sm">
|
||||
<strong>Catatan:</strong> Hasil ini masih sementara karena tingkat keyakinan (confidence) di bawah standar minimum.
|
||||
<strong>Catatan:</strong> Hasil ini masih sementara karena
|
||||
tingkat keyakinan (confidence) di bawah standar minimum.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Message if AI fails to process */}
|
||||
{diagnosisPreview.failureReason && (
|
||||
<div className="rounded-xl border border-red-200 bg-red-50 p-4 text-sm text-red-900 shadow-sm">
|
||||
<strong>Gagal Memproses:</strong> {diagnosisPreview.failureReason}
|
||||
<strong>Gagal Memproses:</strong>{" "}
|
||||
{diagnosisPreview.failureReason}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<DiagnosisResultView
|
||||
imageUrl={previewUrl || diagnosisPreview.imageUrl || "https://placehold.co/600x400?text=Foto+Daun"}
|
||||
<DiagnosisResultView
|
||||
imageUrl={
|
||||
previewUrl ||
|
||||
diagnosisPreview.imageUrl ||
|
||||
"https://placehold.co/600x400?text=Foto+Daun"
|
||||
}
|
||||
confidence={diagnosisPreview.confidence ?? 0}
|
||||
diseaseName={diagnosisPreview.disease?.commonName ?? "Tidak Diketahui"}
|
||||
diseaseName={
|
||||
diagnosisPreview.disease?.commonName ?? "Tidak Diketahui"
|
||||
}
|
||||
scientificName={diagnosisPreview.disease?.label ?? ""}
|
||||
riskLevel={diagnosisPreview.disease?.riskLevel ?? "Sedang"}
|
||||
description={diagnosisPreview.disease?.description ?? diagnosisPreview.disease?.summary ?? "Deskripsi tidak tersedia."}
|
||||
description={
|
||||
diagnosisPreview.disease?.description ??
|
||||
diagnosisPreview.disease?.summary ??
|
||||
"Deskripsi tidak tersedia."
|
||||
}
|
||||
symptoms={diagnosisPreview.disease?.symptoms ?? []}
|
||||
preventions={diagnosisPreview.disease?.recommendations ?? []}
|
||||
medicines={(diagnosisPreview.disease as any)?.medicineRecommendations ?? []}
|
||||
medicines={
|
||||
(diagnosisPreview.disease as any)?.medicineRecommendations ?? []
|
||||
}
|
||||
/>
|
||||
|
||||
{/* All Model Predictions */}
|
||||
{diagnosisPreview.predictions && diagnosisPreview.predictions.length > 0 && (
|
||||
<div className="bg-white rounded-2xl border border-slate-200 shadow-sm p-4 md:p-6 mt-4">
|
||||
<h4 className="font-bold text-[#214B11] mb-1">Semua Prediksi Model</h4>
|
||||
<p className="text-xs text-slate-500 mb-4">Tebakan alternatif lain dari sistem AI ZeaVis</p>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
{diagnosisPreview.predictions
|
||||
.sort((a, b) => a.rank - b.rank)
|
||||
.map((pred) => (
|
||||
<div
|
||||
key={pred.id}
|
||||
className="flex items-center justify-between rounded-xl border border-slate-100 bg-slate-50 p-3 text-sm"
|
||||
>
|
||||
<span className="text-slate-600 font-medium">{pred.modelLabel}</span>
|
||||
<span className="font-bold text-slate-700">
|
||||
{(pred.confidence * 100).toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
{diagnosisPreview.predictions &&
|
||||
diagnosisPreview.predictions.length > 0 && (
|
||||
<div className="bg-white rounded-2xl border border-slate-200 shadow-sm p-4 md:p-6 mt-4">
|
||||
<h4 className="font-bold text-[#214B11] mb-1">
|
||||
Semua Prediksi Model
|
||||
</h4>
|
||||
<p className="text-xs text-slate-500 mb-4">
|
||||
Tebakan alternatif lain dari sistem AI ZeaVis
|
||||
</p>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
{diagnosisPreview.predictions
|
||||
.sort((a, b) => a.rank - b.rank)
|
||||
.map((pred) => (
|
||||
<div
|
||||
key={pred.id}
|
||||
className="flex items-center justify-between rounded-xl border border-slate-100 bg-slate-50 p-3 text-sm"
|
||||
>
|
||||
<span className="text-slate-600 font-medium">
|
||||
{pred.modelLabel}
|
||||
</span>
|
||||
<span className="font-bold text-slate-700">
|
||||
{(pred.confidence * 100).toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user