From 1cb8e8fd1ebfb106f798db6ef444f12b2d301286 Mon Sep 17 00:00:00 2001 From: Alma Date: Wed, 9 Apr 2025 22:17:37 +0200 Subject: [PATCH] update widget --- app/page.tsx | 21 +--------- components/parole.tsx | 93 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 components/parole.tsx diff --git a/app/page.tsx b/app/page.tsx index 088f3935..62bc8e55 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,9 +1,5 @@ import { QuoteCard } from "@/components/quote-card"; -import { Messages } from "@/components/messages"; -import { Podcast } from "@/components/podcast"; -import { CalendarWidget } from "@/components/calendar-widget"; -import { News } from "@/components/news"; -import { Todo } from "@/components/todo"; +import { Parole } from "@/components/parole"; export const metadata = { title: "Enkun - Dashboard", @@ -12,25 +8,12 @@ export const metadata = { export default function DashboardPage() { return (
- {/* Empty welcome section with reduced height */} -
-
-
-
-
- -
- -
-
- - - +
diff --git a/components/parole.tsx b/components/parole.tsx new file mode 100644 index 00000000..8d870918 --- /dev/null +++ b/components/parole.tsx @@ -0,0 +1,93 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; + +interface Message { + _id: string; + msg: string; + ts: string; + u: { + _id: string; + username: string; + name?: string; + }; +} + +export function Parole() { + const [messages, setMessages] = useState([]); + const [error, setError] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + const fetchMessages = async () => { + try { + // First, login to get auth token + const loginResponse = await fetch('https://parole.slm-lab.net/api/v1/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + user: process.env.NEXT_PUBLIC_ROCKET_CHAT_USER, + password: process.env.NEXT_PUBLIC_ROCKET_CHAT_PASSWORD, + }), + }); + + if (!loginResponse.ok) { + throw new Error('Failed to authenticate with RocketChat'); + } + + const { data: authData } = await loginResponse.json(); + + // Then fetch messages using the auth token + const messagesResponse = await fetch('https://parole.slm-lab.net/api/v1/channels.messages?roomName=general', { + headers: { + 'X-Auth-Token': authData.authToken, + 'X-User-Id': authData.userId, + }, + }); + + if (!messagesResponse.ok) { + throw new Error('Failed to fetch messages'); + } + + const { messages: chatMessages } = await messagesResponse.json(); + setMessages(chatMessages); + setError(null); + } catch (err) { + console.error('Error fetching messages:', err); + setError(err instanceof Error ? err.message : 'Failed to fetch messages'); + } finally { + setLoading(false); + } + }; + + fetchMessages(); + }, []); + + return ( + + + Parole Messages + + + {loading &&

Loading messages...

} + {error &&

Error: {error}

} + {!loading && !error && ( +
+ {messages.map((message) => ( +
+

{message.u.name || message.u.username}

+

{message.msg}

+

+ {new Date(message.ts).toLocaleString()} +

+
+ ))} +
+ )} +
+
+ ); +} \ No newline at end of file