|
| 1 | +import React, { useState, useRef } from 'react'; |
| 2 | +import { Canvas, useFrame } from '@react-three/fiber'; |
| 3 | +import { XR, ARButton, Controllers, Hands } from '@react-three/xr'; |
| 4 | +import { OrbitControls, Text, Sky, ContactShadows, Environment, Float, Box } from '@react-three/drei'; |
| 5 | +import * as THREE from 'three'; |
| 6 | +import { ethers } from 'ethers'; |
| 7 | + |
| 8 | +// --- Components --- |
| 9 | + |
| 10 | +// Digital Twin: Warehouse / Cargo Box |
| 11 | +function CargoBox({ position, color, speed = 0.01 }) { |
| 12 | + const meshRef = useRef(); |
| 13 | + const [hovered, setHovered] = useState(false); |
| 14 | + |
| 15 | + useFrame((state) => { |
| 16 | + if (meshRef.current) { |
| 17 | + // Move box back and forth to simulate movement in a warehouse |
| 18 | + meshRef.current.position.z += speed * Math.sin(state.clock.elapsedTime); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + return ( |
| 23 | + <Float speed={2} rotationIntensity={0.5} floatIntensity={0.5}> |
| 24 | + <Box |
| 25 | + ref={meshRef} |
| 26 | + position={position} |
| 27 | + args={[0.5, 0.5, 0.5]} |
| 28 | + onPointerOver={() => setHovered(true)} |
| 29 | + onPointerOut={() => setHovered(false)} |
| 30 | + > |
| 31 | + <meshStandardMaterial color={hovered ? 'orange' : color} /> |
| 32 | + </Box> |
| 33 | + </Float> |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +// Digital Twin: Warehouse Floor |
| 38 | +function Warehouse() { |
| 39 | + return ( |
| 40 | + <group> |
| 41 | + <mesh rotation={[-Math.PI / 2, 0, 0]} receiveShadow> |
| 42 | + <planeGeometry args={[20, 20]} /> |
| 43 | + <meshStandardMaterial color="#222" /> |
| 44 | + </mesh> |
| 45 | + <gridHelper args={[20, 20, 0xffffff, 0x444444]} rotation={[-Math.PI / 2, 0, 0]} /> |
| 46 | + |
| 47 | + {/* Some shelving units simulated by boxes */} |
| 48 | + <Box position={[-2, 0.5, -2]} args={[1, 1, 4]}> |
| 49 | + <meshStandardMaterial color="#555" /> |
| 50 | + </Box> |
| 51 | + <Box position={[2, 0.5, -2]} args={[1, 1, 4]}> |
| 52 | + <meshStandardMaterial color="#555" /> |
| 53 | + </Box> |
| 54 | + |
| 55 | + {/* Moving Cargo */} |
| 56 | + <CargoBox position={[-1, 0.5, 0]} color="#8B4513" speed={0.02} /> |
| 57 | + <CargoBox position={[1, 0.5, -1]} color="#CD853F" speed={0.015} /> |
| 58 | + <CargoBox position={[0, 0.5, -2]} color="#DEB887" speed={0.01} /> |
| 59 | + </group> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +// --- Main Component --- |
| 64 | + |
| 65 | +const INCOTERMS_DATA = { |
| 66 | + "EXW": "Ex Works: Buyer takes all risk from seller's door.", |
| 67 | + "FOB": "Free On Board: Seller covers costs until goods are on ship.", |
| 68 | + "CIF": "Cost, Insurance, and Freight: Seller pays to destination port.", |
| 69 | + "DDP": "Delivered Duty Paid: Seller covers all costs including taxes." |
| 70 | +}; |
| 71 | + |
| 72 | +export default function SupplyChainPlatform() { |
| 73 | + const [activeTab, setActiveTab] = useState('twin'); |
| 74 | + const [selectedTerm, setSelectedTerm] = useState('EXW'); |
| 75 | + const [trackingId, setTrackingId] = useState(''); |
| 76 | + const [aiStatus, setAiStatus] = useState('Idle'); |
| 77 | + const [blockchainLog, setBlockchainLog] = useState([]); |
| 78 | + |
| 79 | + const logToBlockchain = async (event) => { |
| 80 | + setAiStatus('Securing data on blockchain...'); |
| 81 | + // Simulate ethers interaction |
| 82 | + const hash = ethers.id(event + Date.now()); |
| 83 | + const newLog = { event, hash: hash.substring(0, 16) + '...', time: new Date().toLocaleTimeString() }; |
| 84 | + setBlockchainLog([newLog, ...blockchainLog].slice(0, 5)); |
| 85 | + await new Promise(r => setTimeout(r, 800)); |
| 86 | + setAiStatus('Data Verified.'); |
| 87 | + }; |
| 88 | + |
| 89 | + const runAIPrediction = () => { |
| 90 | + setAiStatus('AI Analyzing supply chain routes...'); |
| 91 | + setTimeout(() => { |
| 92 | + setAiStatus('Optimal route found: Route A-7 via Singapore. Delay Probability: 5%.'); |
| 93 | + logToBlockchain('Route Optimization Run'); |
| 94 | + }, 1500); |
| 95 | + }; |
| 96 | + |
| 97 | + return ( |
| 98 | + <div className="supply-chain-container" style={{ color: 'white', textAlign: 'left', padding: '20px' }}> |
| 99 | + <h2>Supply Chain & Digital Twin Platform</h2> |
| 100 | + |
| 101 | + <div className="tabs" style={{ marginBottom: '20px', display: 'flex', gap: '10px' }}> |
| 102 | + <button onClick={() => setActiveTab('twin')}>Digital Twin (3D)</button> |
| 103 | + <button onClick={() => setActiveTab('incoterms')}>Incoterms</button> |
| 104 | + <button onClick={() => setActiveTab('logistics')}>Logistics AI</button> |
| 105 | + </div> |
| 106 | + |
| 107 | + {activeTab === 'twin' && ( |
| 108 | + <div style={{ width: '100%', height: '500px', background: '#000', borderRadius: '10px', overflow: 'hidden', position: 'relative' }}> |
| 109 | + <ARButton /> |
| 110 | + <Canvas shadows camera={{ position: [5, 5, 5], fov: 50 }}> |
| 111 | + <XR> |
| 112 | + <Sky sunPosition={[100, 20, 100]} /> |
| 113 | + <Environment preset="warehouse" /> |
| 114 | + <ambientLight intensity={0.5} /> |
| 115 | + <pointLight position={[10, 10, 10]} castShadow /> |
| 116 | + |
| 117 | + <Warehouse /> |
| 118 | + |
| 119 | + <Text position={[0, 4, -5]} fontSize={0.5} color="white"> |
| 120 | + Warehouse Digital Twin |
| 121 | + </Text> |
| 122 | + |
| 123 | + <Controllers /> |
| 124 | + <Hands /> |
| 125 | + <OrbitControls /> |
| 126 | + <ContactShadows opacity={0.4} scale={20} blur={2} far={4.5} /> |
| 127 | + </XR> |
| 128 | + </Canvas> |
| 129 | + <div style={{ position: 'absolute', bottom: 10, left: 10, background: 'rgba(0,0,0,0.6)', padding: '10px', fontSize: '12px' }}> |
| 130 | + Interactive 3D Simulation. Use mouse to rotate. Box movement simulates AGVs (Automated Guided Vehicles). |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + )} |
| 134 | + |
| 135 | + {activeTab === 'incoterms' && ( |
| 136 | + <div style={{ background: '#222', padding: '20px', borderRadius: '10px' }}> |
| 137 | + <h3>Incoterms 2020 Navigator</h3> |
| 138 | + <select |
| 139 | + value={selectedTerm} |
| 140 | + onChange={(e) => setSelectedTerm(e.target.value)} |
| 141 | + style={{ padding: '10px', borderRadius: '5px', width: '100%', marginBottom: '20px' }} |
| 142 | + > |
| 143 | + {Object.keys(INCOTERMS_DATA).map(term => ( |
| 144 | + <option key={term} value={term}>{term}</option> |
| 145 | + ))} |
| 146 | + </select> |
| 147 | + <div style={{ padding: '20px', border: '1px solid #444', borderRadius: '5px' }}> |
| 148 | + <strong>{selectedTerm}:</strong> {INCOTERMS_DATA[selectedTerm]} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + )} |
| 152 | + |
| 153 | + {activeTab === 'logistics' && ( |
| 154 | + <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '20px' }}> |
| 155 | + <div style={{ background: '#222', padding: '20px', borderRadius: '10px' }}> |
| 156 | + <h3>Logistics Optimizer</h3> |
| 157 | + <input |
| 158 | + type="text" |
| 159 | + placeholder="Enter Tracking ID (e.g. SHIP-123)" |
| 160 | + value={trackingId} |
| 161 | + onChange={(e) => setTrackingId(e.target.value)} |
| 162 | + style={{ padding: '10px', width: '80%', marginBottom: '10px' }} |
| 163 | + /> |
| 164 | + <button onClick={runAIPrediction} style={{ display: 'block' }}>Run AI Analysis</button> |
| 165 | + <p style={{ marginTop: '20px', color: '#aaa' }}>Status: <span style={{ color: '#00ff00' }}>{aiStatus}</span></p> |
| 166 | + </div> |
| 167 | + |
| 168 | + <div style={{ background: '#222', padding: '20px', borderRadius: '10px' }}> |
| 169 | + <h3>Blockchain Audit Trail</h3> |
| 170 | + <ul style={{ listStyle: 'none', padding: 0 }}> |
| 171 | + {blockchainLog.length === 0 && <li>No entries yet.</li>} |
| 172 | + {blockchainLog.map((log, i) => ( |
| 173 | + <li key={i} style={{ fontSize: '12px', borderBottom: '1px solid #333', padding: '5px 0' }}> |
| 174 | + [{log.time}] {log.event} <br/> |
| 175 | + <small style={{ color: '#888' }}>Hash: {log.hash}</small> |
| 176 | + </li> |
| 177 | + ))} |
| 178 | + </ul> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + )} |
| 182 | + |
| 183 | + <footer style={{ marginTop: '30px', fontSize: '12px', color: '#666' }}> |
| 184 | + Supply Chain AI Services Software v1.0.0 | Integrated with Digital Twin & Web3 Ledger. |
| 185 | + </footer> |
| 186 | + </div> |
| 187 | + ); |
| 188 | +} |
0 commit comments