|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { useApp } from '@/context/AppContext'; |
| 3 | + |
| 4 | +// Fake system stats generator |
| 5 | +function getFakeStats() { |
| 6 | + return { |
| 7 | + cpu: (Math.random() * 100).toFixed(1), |
| 8 | + ram: (Math.random() * 16).toFixed(1), |
| 9 | + storage: (Math.random() * 512).toFixed(1), |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +export default function Monitor() { |
| 14 | + const [stats, setStats] = useState(getFakeStats()); |
| 15 | + const [dateTime, setDateTime] = useState(new Date()); |
| 16 | + const { state, dispatch } = useApp(); |
| 17 | + const { openApps } = state; |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const statsInterval = setInterval(() => { |
| 21 | + setStats(getFakeStats()); |
| 22 | + }, 1500); |
| 23 | + |
| 24 | + const clockInterval = setInterval(() => { |
| 25 | + setDateTime(new Date()); |
| 26 | + }, 1000); |
| 27 | + |
| 28 | + return () => { |
| 29 | + clearInterval(statsInterval); |
| 30 | + clearInterval(clockInterval); |
| 31 | + }; |
| 32 | + }, []); |
| 33 | + |
| 34 | + return ( |
| 35 | + <div> |
| 36 | + <div className="flex flex-col gap-8 p-4 relative"> |
| 37 | + <div className="my-2"> |
| 38 | + <h4 className="text-lg font-semibold mb-3">Running Processes</h4> |
| 39 | + <div className="overflow-hidden rounded-lg border border-gray-300 shadow-sm"> |
| 40 | + <table className="w-full text-left text-sm"> |
| 41 | + <thead className="bg-gray-200"> |
| 42 | + <tr> |
| 43 | + <th className="px-3 py-2 font-medium">App</th> |
| 44 | + <th className="px-3 py-2 font-medium">PID</th> |
| 45 | + <th className="px-3 py-2 font-medium">Actions</th> |
| 46 | + </tr> |
| 47 | + </thead> |
| 48 | + <tbody className="divide-y divide-gray-200 bg-white"> |
| 49 | + {openApps.map((app) => ( |
| 50 | + <tr key={app.id} className="hover:bg-gray-50"> |
| 51 | + <td className="px-3 py-2">{app.name}</td> |
| 52 | + <td className="px-3 py-2">{app.processId}</td> |
| 53 | + <td |
| 54 | + className="px-3 py-2 text-red-500 cursor-pointer" |
| 55 | + onClick={() => |
| 56 | + dispatch({ type: 'CLOSE_APP', payload: app.id }) |
| 57 | + } |
| 58 | + > |
| 59 | + close app |
| 60 | + </td> |
| 61 | + </tr> |
| 62 | + ))} |
| 63 | + {openApps.length === 0 && ( |
| 64 | + <tr> |
| 65 | + <td |
| 66 | + colSpan={3} |
| 67 | + className="px-3 py-4 text-center text-gray-500 italic" |
| 68 | + > |
| 69 | + No apps running |
| 70 | + </td> |
| 71 | + </tr> |
| 72 | + )} |
| 73 | + </tbody> |
| 74 | + </table> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + {/* System Stats */} |
| 78 | + <div className="absolute top-0 grid grid-cols-3 gap-4"> |
| 79 | + <div className="flex items-center gap-2 rounded-lg text-center"> |
| 80 | + <div className="text-sm text-gray-500">CPU Usage</div> |
| 81 | + <div className="text-sm">{stats.cpu}%</div> |
| 82 | + </div> |
| 83 | + <div className="flex items-center gap-2 rounded-lg text-center"> |
| 84 | + <div className="text-sm text-gray-500">RAM Usage</div> |
| 85 | + <div className="text-sm">{stats.ram} GB</div> |
| 86 | + </div> |
| 87 | + <div className="flex items-center gap-2 rounded-lg text-center"> |
| 88 | + <div className="text-sm text-gray-500">Storage Used</div> |
| 89 | + <div className="text-sm">{stats.storage} GB</div> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + {/* Clock (fixed at bottom right) */} |
| 94 | + <div className="absolute bottom-0 right-0 m-4 flex items-center gap-2 text-sm bg-white px-3 py-1 rounded-lg shadow"> |
| 95 | + <span>{dateTime.toLocaleTimeString()}</span> |
| 96 | + <span className="text-gray-400">|</span> |
| 97 | + <span>{dateTime.toLocaleDateString()}</span> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments