Working frontend
This commit is contained in:
@@ -1,10 +1,65 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { useParams } from "next/navigation";
|
||||||
import MessageBox from "@/components/MessageBox";
|
import MessageBox from "@/components/MessageBox";
|
||||||
import AppSidebar from "@/components/Sidebar";
|
import AppSidebar from "@/components/Sidebar";
|
||||||
|
import { getUser } from '@/test/user';
|
||||||
|
import { auth } from '@/test/auth';
|
||||||
|
import Message from "@/types/message";
|
||||||
|
|
||||||
export default async function Server(params: {
|
export default function Server() {
|
||||||
params: Promise<{ id: string }>;
|
const { id } = useParams<{ id: string }>();
|
||||||
}) {
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const { id } = await params.params;
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!id) return;
|
||||||
|
|
||||||
|
getUser().then(session_token => {
|
||||||
|
auth(session_token, id === 'localhost' ? '127.0.0.1' : id).then(server_auth => {
|
||||||
|
// Open the WebSocket connection
|
||||||
|
const ws = new WebSocket(`ws://${id}:7080`);
|
||||||
|
wsRef.current = ws;
|
||||||
|
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.log("Connected to WebSocket:", id);
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
version: '0.0.1',
|
||||||
|
auth_token: server_auth,
|
||||||
|
last_message: 0
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = (m) => {
|
||||||
|
const data = JSON.parse(m.data);
|
||||||
|
console.log("Message received:", data);
|
||||||
|
|
||||||
|
switch (data.type) {
|
||||||
|
case 'authenticated':
|
||||||
|
setMessages(data.params.messages);
|
||||||
|
|
||||||
|
case 'message_create':
|
||||||
|
// setMessages([...messages, data.params]);
|
||||||
|
setMessages((prev) => [...prev, data.params]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onerror = (err) => {
|
||||||
|
console.error("WebSocket error:", err);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
console.log("WebSocket closed:", id);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cleanup on unmount or id change
|
||||||
|
return () => {
|
||||||
|
ws.close();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}, [id]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppSidebar
|
<AppSidebar
|
||||||
@@ -20,16 +75,18 @@ export default async function Server(params: {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<MessageBox
|
<MessageBox
|
||||||
messages={[
|
messages={messages.filter(m => m.channel_id === 'general').toReversed()}
|
||||||
{
|
sendMessage={(message) => {
|
||||||
id: "1",
|
wsRef.current?.send(JSON.stringify(
|
||||||
authorId: "user1",
|
{
|
||||||
content:
|
type: 'send_message',
|
||||||
"# Test\n### Hello World\n\nThis is a **markdown** message with _various_ elements:\n\n- Item 1\n- Item 2\n- Item 3\n\n[Click here](https://example.com) for more info.\n\n1. First\n2. Second\n3. Third",
|
params: {
|
||||||
channelId: "1",
|
channel_id: 'general',
|
||||||
timestamp: new Date(),
|
contents: message
|
||||||
},
|
}
|
||||||
]}
|
}
|
||||||
|
));
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</AppSidebar>
|
</AppSidebar>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -23,19 +23,19 @@ function MessageContainer({ message }: { message: Message }) {
|
|||||||
<div className="w-8 h-8 rounded-full bg-gray-400" />
|
<div className="w-8 h-8 rounded-full bg-gray-400" />
|
||||||
<div className="flex flex-col flex-1">
|
<div className="flex flex-col flex-1">
|
||||||
<span className="font-bold flex gap-1 items-center">
|
<span className="font-bold flex gap-1 items-center">
|
||||||
{message.authorId}
|
{message.from}
|
||||||
|
|
||||||
<p className="text-neutral-500 text-xs">
|
<p className="text-neutral-500 text-xs">
|
||||||
{isToday(message.timestamp)
|
{isToday(message.timestamp * 1000)
|
||||||
? format(message.timestamp, "p")
|
? format(message.timestamp * 1000, "p")
|
||||||
: isYesterday(message.timestamp)
|
: isYesterday(message.timestamp * 1000)
|
||||||
? "Yesterday, " + format(message.timestamp, "p")
|
? "Yesterday, " + format(message.timestamp * 1000, "p")
|
||||||
: format(message.timestamp, "PP, p")}
|
: format(message.timestamp * 1000, "PP, p")}
|
||||||
</p>
|
</p>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<span className="text-foreground/85 flex flex-col">
|
<span className="text-foreground/85 flex flex-col">
|
||||||
{message.content.split("\n\n").map((line, index) => (
|
{message.contents.split("\n\n").map((line, index) => (
|
||||||
<div key={index} className="h-max">
|
<div key={index} className="h-max">
|
||||||
{index > 0 && <br />}
|
{index > 0 && <br />}
|
||||||
<ReactMarkdown
|
<ReactMarkdown
|
||||||
@@ -86,7 +86,7 @@ function MessageContainer({ message }: { message: Message }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MessageBox({ messages }: { messages: Message[] }) {
|
export default function MessageBox({ messages, sendMessage }: { messages: Message[], sendMessage: (m: string) => void }) {
|
||||||
const [text, setText] = useState("");
|
const [text, setText] = useState("");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -102,6 +102,12 @@ export default function MessageBox({ messages }: { messages: Message[] }) {
|
|||||||
placeholder="Type a message..."
|
placeholder="Type a message..."
|
||||||
value={text}
|
value={text}
|
||||||
onChange={(e) => setText(e.target.value)}
|
onChange={(e) => setText(e.target.value)}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === "Enter") {
|
||||||
|
sendMessage(text);
|
||||||
|
setText('');
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
|
|||||||
+12
-9
@@ -1,16 +1,19 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { getUser } from "./user.js";
|
import { getUser } from "./user.js";
|
||||||
|
|
||||||
const session_token = await getUser();
|
export async function auth(session_token, server_ip) {
|
||||||
|
const res = (await axios.post('http://localhost:3000/api/auth', {
|
||||||
|
server_ip,
|
||||||
|
session_token,
|
||||||
|
})).data;
|
||||||
|
|
||||||
const res = (await axios.post('http://localhost:3000/api/auth', {
|
return res.token;
|
||||||
intents: 'server',
|
}
|
||||||
server_ip: '127.0.0.1',
|
|
||||||
session_token,
|
|
||||||
})).data;
|
|
||||||
|
|
||||||
const token = res.token;
|
// const session_token = await getUser();
|
||||||
|
|
||||||
const output = (await axios.get('http://localhost:3000/api/auth?intents=server&token='+token)).data;
|
// const token = await auth(session_token);
|
||||||
|
|
||||||
console.log(output);
|
// const output = (await axios.get('http://localhost:3000/api/auth?intents=server&token='+token)).data;
|
||||||
|
|
||||||
|
// console.log(output);
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
export default interface Message {
|
export default interface Message {
|
||||||
id: string;
|
id: number;
|
||||||
content: string;
|
channel_id: string;
|
||||||
authorId: string;
|
from: number;
|
||||||
channelId: string;
|
contents: string;
|
||||||
timestamp: Date;
|
timestamp: number;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user