@devries-cameron20 / OCIU-npo Mirror · read-only
Fork 0

Mirrored from https://github.com/Camo67/OCIU-npo.git

Synced Nov 21, 06:00 UTC

1 import React, { useState, useEffect } from 'react';
2 import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
3 import { Progress } from "@/components/ui/progress";
4 import { Badge } from "@/components/ui/badge";
5 import { Button } from "@/components/ui/button";
6 import { Input } from "@/components/ui/input";
7 import { Textarea } from "@/components/ui/textarea";
8 import { Cpu, Database, Activity, Code, PenTool, Mic, Layout, BookOpen, Briefcase, Heart } from "lucide-react";
9 import { CommunityBuddy } from "@/components/buddy/CommunityBuddy";
10 import { StudyBuddy } from "@/components/study/StudyBuddy";
11 import { JobBoard } from "@/components/jobs/JobBoard";
12 import { CommunityGiving } from "@/components/giving/CommunityGiving";
13 import { ImpactReport } from "@/components/dashboard/ImpactReport";
14 import { MemberProfile } from "@/components/dashboard/MemberProfile";
15 import { WebsiteBuilder } from "@/components/builder/WebsiteBuilder";
16 import { AIBuilder } from "@/components/builder/AIBuilder";
17 import { CommunityFeed } from "@/components/dashboard/CommunityFeed";
18
19 export const Dashboard = () => {
20 const [ramUsage, setRamUsage] = useState(45);
21 const [agents, setAgents] = useState([
22 { name: 'Code Generator', status: 'idle', model: 'Qwen3-8B', icon: Code },
23 { name: 'Content Gen', status: 'idle', model: 'Qwen3-4B', icon: PenTool },
24 { name: 'Voice Studio', status: 'idle', model: 'Qwen3-1.7B', icon: Mic },
25 { name: 'Web Designer', status: 'idle', model: 'Qwen3-8B', icon: Layout },
26 ]);
27
28 const [prompt, setPrompt] = useState("");
29 const [output, setOutput] = useState("");
30 const [isPhoneRender, setIsPhoneRender] = useState(false);
31 const [taskUpdates, setTaskUpdates] = useState<string[]>([]);
32
33 // Use environment variable or relative URL for API endpoint
34 const API_BASE_URL = import.meta.env.VITE_API_URL || window.location.origin;
35
36 useEffect(() => {
37 const eventSource = new EventSource(`${API_BASE_URL}/api/events`);
38
39 eventSource.onmessage = (event) => {
40 setTaskUpdates((prev) => [...prev, event.data]);
41 };
42
43 eventSource.onerror = () => {
44 console.error("SSE Connection failed. Reconnecting...");
45 eventSource.close();
46 // Reconnect after 3s
47 setTimeout(() => {
48 // Re-run effect
49 }, 3000);
50 };
51
52 return () => {
53 eventSource.close();
54 };
55 }, []);
56
57 const handleRunAgent = async (agentName: string) => {
58 const userId = "test@example.com"; // Simulated
59 const endpoint = agentName === 'Code Generator' ? '/api/agent/code' : '/api/agent/content';
60 const body = agentName === 'Code Generator' ? { prompt } : { topic: prompt };
61
62 try {
63 const response = await fetch(`${API_BASE_URL}${endpoint}`, {
64 method: 'POST',
65 headers: {
66 'Content-Type': 'application/json',
67 'X-User-Id': userId
68 },
69 body: JSON.stringify(body),
70 });
71 if (response.status === 402) {
72 setOutput("Usage limit reached. Please Upgrade to Pro!");
73 return;
74 }
75 const data = await response.json();
76 setOutput(data.output);
77 } catch (error) {
78 console.error(error);
79 setOutput("Error connecting to backend");
80 }
81 };
82
83 return (
84 <div className="p-8 bg-background min-h-screen text-foreground">
85 <div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-8">
86 <div>
87 <h1 className="text-4xl font-bold tracking-tight">OCIU Community Platform</h1>
88 <p className="text-sm text-muted-foreground">Our Community in Unity: Learn, Build, Earn.</p>
89 </div>
90 <div className="flex flex-wrap items-center gap-4">
91 <Badge variant={ramUsage > 80 ? "destructive" : "secondary"} className="flex items-center gap-1">
92 <Cpu size={14} />
93 RAM: {ramUsage}%
94 </Badge>
95 <Badge variant="outline" className="flex items-center gap-1">
96 <Database size={14} />
97 Vault Synced
98 </Badge>
99 <div className="flex items-center gap-2 bg-secondary/50 px-3 py-1 rounded-full text-xs">
100 <span>Render on Phone</span>
101 <input
102 type="checkbox"
103 checked={isPhoneRender}
104 onChange={() => setIsPhoneRender(!isPhoneRender)}
105 className="accent-primary"
106 />
107 </div>
108 <Button variant="default" className="bg-yellow-500 hover:bg-yellow-600 text-black font-bold h-8 text-xs">
109 Upgrade to Pro
110 </Button>
111 </div>
112 </div>
113
114 <div className="mb-8 p-4 bg-yellow-500/10 border border-yellow-500/50 rounded-lg flex items-center justify-between">
115 <div className="flex items-center gap-3">
116 <div className="p-2 bg-yellow-500 rounded-full text-black">
117 <Activity size={16} />
118 </div>
119 <div>
120 <h3 className="font-semibold text-sm">Join the Pro Waitlist</h3>
121 <p className="text-xs text-muted-foreground">Unlock priority rendering and custom voice slots.</p>
122 </div>
123 </div>
124 <Button size="sm" variant="outline" className="text-xs border-yellow-500 hover:bg-yellow-500 hover:text-black">
125 Apply Now
126 </Button>
127 </div>
128
129 <div className="mb-8">
130 <ImpactReport />
131 </div>
132
133 <div className="mb-8">
134 <CommunityBuddy />
135 </div>
136
137 <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
138 {agents.map((agent) => (
139 <Card key={agent.name}>
140 <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
141 <CardTitle className="text-sm font-medium">
142 {agent.name}
143 </CardTitle>
144 <agent.icon className="h-4 w-4 text-muted-foreground" />
145 </CardHeader>
146 <CardContent>
147 <div className="text-2xl font-bold">{agent.model}</div>
148 <p className="text-xs text-muted-foreground capitalize">
149 Status: {agent.status}
150 </p>
151 </CardContent>
152 </Card>
153 ))}
154 </div>
155
156 <div className="grid grid-cols-1 lg:grid-cols-3 gap-8 mb-8">
157 <div className="lg:col-span-2 space-y-8">
158 <MemberProfile />
159 <AIBuilder />
160 <StudyBuddy />
161 <JobBoard />
162 </div>
163 <div className="space-y-8">
164 <CommunityGiving />
165 <WebsiteBuilder />
166 <CommunityFeed />
167 <Card>
168 <CardHeader>
169 <CardTitle className="text-sm font-medium">Donor Buddy Insights</CardTitle>
170 </CardHeader>
171 <CardContent>
172 <div className="p-3 bg-red-500/5 rounded border border-red-500/20 text-xs italic">
173 "3 local businesses in your area have CSR programs that match our current learning needs."
174 </div>
175 </CardContent>
176 </Card>
177 </div>
178 </div>
179
180 <div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
181 <Card className="lg:col-span-2">
182 <CardHeader>
183 <CardTitle>Task Control</CardTitle>
184 </CardHeader>
185 <CardContent className="space-y-4">
186 <div className="space-y-2">
187 <label className="text-sm font-medium">Prompt</label>
188 <Textarea
189 placeholder="Enter instructions for the agent..."
190 value={prompt}
191 onChange={(e) => setPrompt(e.target.value)}
192 />
193 </div>
194 <div className="flex flex-wrap gap-2">
195 <Button onClick={() => handleRunAgent('Code Generator')}>
196 <Code className="mr-2 h-4 w-4" /> AI Builder
197 </Button>
198 <Button variant="outline" onClick={() => handleRunAgent('StudyBuddy')}>
199 <BookOpen className="mr-2 h-4 w-4" /> StudyBuddy
200 </Button>
201 <Button variant="outline" onClick={() => handleRunAgent('Real Jobs')}>
202 <Briefcase className="mr-2 h-4 w-4" /> Real Jobs
203 </Button>
204 <Button variant="outline" className="border-red-500/50 hover:bg-red-500/10 text-red-500" onClick={() => handleRunAgent('Donor Buddy')}>
205 <Heart className="mr-2 h-4 w-4" /> Donor Buddy
206 </Button>
207 </div>
208 {output && (
209 <div className="mt-4 p-4 bg-muted rounded-md font-mono text-sm">
210 <pre>{output}</pre>
211 </div>
212 )}
213 </CardContent>
214 </Card>
215
216 <Card>
217 <CardHeader>
218 <CardTitle>System Activity</CardTitle>
219 </CardHeader>
220 <CardContent>
221 <div className="space-y-4 mb-4">
222 <h3 className="text-xs font-semibold uppercase text-muted-foreground">Live Updates</h3>
223 <div className="max-h-32 overflow-y-auto space-y-1 text-xs font-mono bg-muted p-2 rounded">
224 {taskUpdates.length === 0 && <span>Waiting for tasks...</span>}
225 {taskUpdates.map((update, i) => (
226 <div key={i} className="border-l-2 border-primary pl-2">{update}</div>
227 ))}
228 </div>
229 </div>
230 <div className="space-y-4">
231 <div className="space-y-2">
232 <div className="flex justify-between text-sm">
233 <span>CPU Load</span>
234 <span>12%</span>
235 </div>
236 <Progress value={12} />
237 </div>
238 <div className="space-y-2">
239 <div className="flex justify-between text-sm">
240 <span>Task Queue</span>
241 <span>0 active</span>
242 </div>
243 <Progress value={0} />
244 </div>
245 </div>
246 </CardContent>
247 </Card>
248 </div>
249 </div>
250 );
251 };