// Description: This component combines PubChem lookup with 2D and 3D visualization
import { useState } from "react";
import SMILESDisplay from "../common/SMILESDisplay";
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 { useAppContext } from "../../context/AppContext"; // For adding to recent molecules
import { lookupPubChem } from "../../services/chemService";
// Import visualization components
import MoleculeDepiction2D from "./MoleculeDepiction2D";
import MoleculeDepiction3D from "./MoleculeDepiction3D";
import {
AlertCircle,
Atom,
BarChart3,
Box,
Check,
Circle,
Copy,
Info,
Loader2,
Search,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Input } from "@/components/ui/input";
// Animated Atom Component
const AnimatedAtom = () => {
return (
{/* Main nucleus */}
{/* Orbiting electrons */}
{/* First orbit */}
{/* Second orbit - rotated */}
{/* Third orbit - tilted */}
{/* Pulse ring animation */}
);
};
const StructureVisualizerView = () => {
// Search state
const [identifier, setIdentifier] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [result, setResult] = useState(null);
const [copySuccess, setCopySuccess] = useState(false);
const { addRecentMolecule } = useAppContext();
// Visualization settings
const [depictToolkit, setDepictToolkit] = useState("rdkit");
const [vis3DToolkit, setVis3DToolkit] = useState("openbabel");
// Examples for each identifier type
const examples = [
{ name: "Caffeine", value: "caffeine", description: "Common stimulant" },
{ name: "Aspirin", value: "aspirin", description: "Common pain reliever" },
{
name: "Ibuprofen",
value: "ibuprofen",
description: "NSAID pain reliever",
},
{
name: "Paracetamol",
value: "paracetamol",
description: "Also known as acetaminophen",
},
{ name: "CAS: 50-78-2", value: "50-78-2", description: "CAS for aspirin" },
{
name: "Formula: C8H10N4O2",
value: "C8H10N4O2",
description: "Caffeine formula",
},
];
const handleSubmit = async (e) => {
e.preventDefault();
// Trim the identifier and check if it's empty
const trimmedIdentifier = identifier.trim();
if (!trimmedIdentifier) {
setError("Please enter a chemical identifier");
return;
}
setLoading(true);
setError(null);
setResult(null);
try {
const data = await lookupPubChem(trimmedIdentifier);
setResult(data);
// Add to recent molecules if lookup was successful
if (data.success && data.canonical_smiles) {
addRecentMolecule({
smiles: data.canonical_smiles,
name: data.name || trimmedIdentifier,
timestamp: new Date().toISOString(),
});
}
} catch (err) {
console.error("PubChem lookup error:", err);
setError(getErrorMessage("depict", err));
} finally {
setLoading(false);
}
};
const handleUseExample = (exampleValue) => {
setIdentifier(exampleValue);
};
const copyToClipboard = (text) => {
navigator.clipboard.writeText(text).then(
() => {
setCopySuccess(true);
setTimeout(() => setCopySuccess(false), 2000);
},
(err) => {
console.error("Failed to copy text:", err);
}
);
};
return (
{/* Header with animated background */}
{/* Main content area */}
{/* Search panel - left side on larger screens */}
{/* Search Card */}
{/* Information Box */}
About This Tool
This visualizer helps you explore chemical structures using data from PubChem, one
of the world's largest collections of freely accessible chemical information.
Features:
Search by name, CAS number, formula, InChI, or SMILES
View precise 2D structural diagrams
Explore interactive 3D molecular models
Switch between rendering engines for different views
{/* Main content area - right side / bottom */}
{/* Loading State */}
{loading && !result &&
}
{/* Error Display */}
{error && !loading && (
{
setError(null);
document.getElementById("identifier-input")?.focus();
}}
/>
)}
{/* Results Display */}
{result && !loading && (
{/* Status Card */}
{result.success
? `Found: ${result.name || result.input}`
: "No structure found in PubChem"}
Input identifier:{" "}
{result.input}
{result.input_type}
{/* SMILES Result & Visualizations */}
{result.success && result.canonical_smiles && (
{/* SMILES Header */}
Canonical SMILES
copyToClipboard(result.canonical_smiles)}
className="flex items-center text-sm bg-white dark:bg-gray-700 px-3 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors shadow-xs"
>
{copySuccess ? (
<>
Copied
>
) : (
<>
Copy SMILES
>
)}
{/* Content */}
{/* SMILES Display */}
{/* Visualizations Grid */}
)}
)}
{/* Initial State or No Results */}
{!result && !loading && !error && (
Begin Your Search
Enter a chemical name, formula, CAS number, or SMILES to search PubChem and
visualize molecular structures
handleUseExample("caffeine")}
className="px-4 py-2 bg-indigo-50 dark:bg-indigo-900/30 text-indigo-700 dark:text-indigo-300 rounded-lg hover:bg-indigo-100 dark:hover:bg-indigo-900/50 transition-colors"
>
Try Caffeine
handleUseExample("aspirin")}
className="px-4 py-2 bg-indigo-50 dark:bg-indigo-900/30 text-indigo-700 dark:text-indigo-300 rounded-lg hover:bg-indigo-100 dark:hover:bg-indigo-900/50 transition-colors"
>
Try Aspirin
)}
);
};
// Add the custom atom animation CSS
const styles = `
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.animate-fadeIn {
animation: fadeIn 0.5s ease-out forwards;
}
@keyframes spin-slow {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes spin-reverse {
0% { transform: rotate(0deg); }
100% { transform: rotate(-360deg); }
}
@keyframes spin-medium {
0% { transform: rotate(45deg); }
100% { transform: rotate(405deg); }
}
.animate-spin-slow {
animation: spin-slow 8s linear infinite;
}
.animate-spin-reverse {
animation: spin-reverse 10s linear infinite;
}
.animate-spin-medium {
animation: spin-medium 12s linear infinite;
}
`;
// Add the styles to the document
if (typeof document !== "undefined") {
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
}
export default StructureVisualizerView;