// Description: This component provides a user interface for validating and standardizing chemical structures using SMILES notation. It includes input handling, error checking, and displays results with appropriate messaging and styling for both light and dark themes. import { useState } from "react"; // Ensure all used icons are imported // Assuming these components are correctly implemented and styled for dark/light mode import SMILESInput from "../common/SMILESInput"; import MoleculeCard from "../common/MoleculeCard"; import { checkStructureErrors } from "../../services/chemService"; // Assuming this service exists import { AlertCircle, Check, Info, PencilLine, ShieldCheck, Loader2 } from "lucide-react"; import { ToolSkeleton } from "@/components/feedback/ToolSkeleton"; import { GlassErrorCard } from "@/components/feedback/GlassErrorCard"; import { EmptyState } from "@/components/feedback/EmptyState"; import { getErrorMessage } from "@/lib/error-messages"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; const StructureErrorView = () => { const [smiles, setSmiles] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [result, setResult] = useState(null); // Stores { original: { smi, messages }, standardized: { smi, messages } } const [fix, setFix] = useState(true); // Option to attempt fixing issues // Handle form submission const handleSubmit = async (e) => { e.preventDefault(); const trimmedSmiles = smiles.trim(); if (!trimmedSmiles) { setError("Please enter a SMILES string."); setResult(null); // Clear previous results return; } setLoading(true); setError(null); setResult(null); // Clear previous results before fetching try { // Call the service function const validationResult = await checkStructureErrors(trimmedSmiles, fix); setResult(validationResult); // Optional: Check if standardized structure is same as original even if messages exist // if (fix && validationResult?.standardized?.smi === validationResult?.original?.smi) { // // Could set an info message here if desired // } } catch (err) { console.error("Structure check error:", err); // Log the error setError(getErrorMessage("chem", err)); setResult(null); // Ensure result is null on error } finally { setLoading(false); } }; // Render validation messages with appropriate styling const renderMessages = (messages) => { // Handle null or empty messages array if (!messages || messages.length === 0) { // Optionally return a default "No messages" state or null return (
No validation messages reported.
); } // Handle the specific "No Errors Found" message if (messages.length === 1 && messages[0] === "No Errors Found") { return ( // Success message styling
); } // Handle actual error/warning messages return ( // Warning/Error message styling (using amber for visibility)

); }; return ( // Main container
{/* Input Card */}

Structure Validation & Standardization

{/* Form */}
{/* SMILES Input */} {/* Fix Option Checkbox */}
setFix(e.target.checked)} // Checkbox styling for light/dark mode className="h-4 w-4 rounded-sm bg-gray-50 dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-blue-600 dark:text-blue-500 focus:ring-blue-500 dark:focus:ring-offset-gray-800 shadow-xs" />
{/* Submit Button */}
{/* Loading State */} {loading && !result && } {/* Error Display */} {error && !loading && ( { setError(null); document.getElementById("smiles-input")?.focus(); }} /> )} {/* Results Display Section */} {/* Show only if result object exists and not loading */} {result && !loading && (
{" "} {/* Increased spacing */} {/* Original Structure Section */}

Original Structure

{/* Original SMILES Display */}
{/* Original Structure Messages */} {renderMessages(result.original?.messages)}
{/* Standardized Structure Section (if fix was true and data exists) */} {fix && result.standardized && (

Standardized Structure

{/* Standardized SMILES Display */}
{/* Use check icon if standardized messages indicate no errors */} {result.standardized.messages?.includes("No Errors Found") ? (
{/* Standardized Structure Messages */} {renderMessages(result.standardized.messages)} {/* Molecule Card Comparison (only if standardization occurred) */} {/* Ensure MoleculeCard is theme-aware */}
)}
)} {/* Initial State / About Box */} {/* Show only if no result, not loading, and no error */} {!result && !loading && !error && (
)}
); }; export default StructureErrorView;