Fixes here and there
This commit is contained in:
@@ -16,25 +16,18 @@ export default function DMs() {
|
|||||||
const { id } = useParams<{ id: string }>();
|
const { id } = useParams<{ id: string }>();
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const [messages, setMessages] = useState<Message[]>([]);
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
const { user, loading } = useUser();
|
|
||||||
const [userList, setUserList] = useState<StringMap<User>>({});
|
const [userList, setUserList] = useState<StringMap<User>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
async function fetchInitialData() {
|
|
||||||
if (!user || loading) return;
|
|
||||||
await auth(makeAddress("localhost", 7090), wsRef, () => {}, setMessages);
|
|
||||||
setUserList((prev) => {
|
|
||||||
prev[user.id] = user;
|
|
||||||
return prev;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
fetchInitialData();
|
|
||||||
}, [id, user, loading]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppSidebar user={user}>
|
<AppSidebar
|
||||||
|
setMessages={setMessages}
|
||||||
|
wsRef={wsRef}
|
||||||
|
userList={userList}
|
||||||
|
setUserList={setUserList}
|
||||||
|
chatWith={id}
|
||||||
|
>
|
||||||
<MessageBox
|
<MessageBox
|
||||||
channelName="Alice"
|
channelName={userList[id]?.display_name || id}
|
||||||
userList={userList}
|
userList={userList}
|
||||||
setUserList={setUserList}
|
setUserList={setUserList}
|
||||||
messages={messages
|
messages={messages
|
||||||
|
|||||||
+8
-26
@@ -1,41 +1,23 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import AppSidebar from "@/components/Sidebar";
|
import AppSidebar from "@/components/Sidebar";
|
||||||
import useUser, { User } from "@/hooks/get-user";
|
import { User } from "@/hooks/get-user";
|
||||||
import { StringMap } from "@/types/typeUtils";
|
import { StringMap } from "@/types/typeUtils";
|
||||||
import auth, { makeAddress } from "@/lib/auth";
|
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
import { toast } from "sonner";
|
|
||||||
import ProfilePicture from "@/components/ProfilePicture";
|
import ProfilePicture from "@/components/ProfilePicture";
|
||||||
|
|
||||||
export default function Chat() {
|
export default function Chat() {
|
||||||
const ip = "localhost";
|
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const { user, loading } = useUser();
|
|
||||||
const [userList, setUserList] = useState<StringMap<User>>({});
|
const [userList, setUserList] = useState<StringMap<User>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!ip || !user || loading) return;
|
|
||||||
auth(
|
|
||||||
makeAddress(ip, 7090),
|
|
||||||
wsRef,
|
|
||||||
() => {},
|
|
||||||
() => {},
|
|
||||||
(m) => {
|
|
||||||
toast(`New message from ${m.from}`, {
|
|
||||||
description: m.contents.slice(0, 80),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
setUserList((prev) => {
|
|
||||||
prev[user.id] = user;
|
|
||||||
return prev;
|
|
||||||
});
|
|
||||||
}, [ip, user, loading]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppSidebar user={user}>
|
<AppSidebar
|
||||||
|
wsRef={wsRef}
|
||||||
|
userList={userList}
|
||||||
|
setUserList={setUserList}
|
||||||
|
setMessages={() => {}}
|
||||||
|
>
|
||||||
<Card className="bg-background w-full">
|
<Card className="bg-background w-full">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>Direct Messages</CardTitle>
|
<CardTitle>Direct Messages</CardTitle>
|
||||||
|
|||||||
+47
-10
@@ -8,8 +8,8 @@ import {
|
|||||||
SettingsIcon,
|
SettingsIcon,
|
||||||
Volume2Icon,
|
Volume2Icon,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { Server, Channel } from "@/types/types";
|
import { Server, Channel, Message } from "@/types/types";
|
||||||
import { useEffect, useState } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
@@ -25,32 +25,69 @@ import { Input } from "./ui/input";
|
|||||||
import { Label } from "./ui/label";
|
import { Label } from "./ui/label";
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
import { Card } from "./ui/card";
|
import { Card } from "./ui/card";
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
|
|
||||||
import { ScrollArea } from "./ui/scroll-area";
|
import { ScrollArea } from "./ui/scroll-area";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { NumberMap } from "@/types/typeUtils";
|
import useUser, { User } from "@/hooks/get-user";
|
||||||
import { User } from "@/hooks/get-user";
|
|
||||||
import ProfilePicture from "./ProfilePicture";
|
import ProfilePicture from "./ProfilePicture";
|
||||||
|
import auth, { makeAddress } from "@/lib/auth";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { StringMap } from "@/types/typeUtils";
|
||||||
|
|
||||||
export default function AppSidebar({
|
export default function AppSidebar({
|
||||||
user,
|
|
||||||
server,
|
|
||||||
children,
|
children,
|
||||||
|
chatWith,
|
||||||
|
wsRef,
|
||||||
|
setMessages,
|
||||||
|
onNewMessage,
|
||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
server?: Server;
|
wsRef: React.RefObject<WebSocket | null>;
|
||||||
user: User | null;
|
setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
|
||||||
|
onNewMessage?: (msg: Message) => void;
|
||||||
|
userList: StringMap<User>;
|
||||||
|
setUserList: React.Dispatch<React.SetStateAction<StringMap<User>>>;
|
||||||
|
chatWith?: string;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
}>) {
|
}>) {
|
||||||
const [servers, setServers] = useState<[string, string][]>([]);
|
const [servers, setServers] = useState<[string, string][]>([]);
|
||||||
const [newServer, setNewServer] = useState({ name: "", ip: "" });
|
const [newServer, setNewServer] = useState({ name: "", ip: "" });
|
||||||
|
const [server, setServer] = useState<Server | undefined>();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const user = useUser();
|
||||||
|
const [userList, setUserList] = useState<StringMap<User>>({});
|
||||||
|
const ip = "192.168.100.3";
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const s = Cookies.get("servers")?.split(",");
|
const s = Cookies.get("servers")?.split(",");
|
||||||
if (s) setServers(s.map((x) => x.trim().split("@") as [string, string]));
|
if (s) setServers(s.map((x) => x.trim().split("@") as [string, string]));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!ip || !user) return;
|
||||||
|
auth(
|
||||||
|
makeAddress(ip, 7090),
|
||||||
|
wsRef,
|
||||||
|
() => {},
|
||||||
|
setMessages,
|
||||||
|
(m) => {
|
||||||
|
if (m.from !== chatWith)
|
||||||
|
toast(`New message from ${m.from}`, {
|
||||||
|
description: m.contents.slice(0, 80),
|
||||||
|
action: {
|
||||||
|
label: "View",
|
||||||
|
onClick: () => router.push(`/chat/${m.from}`),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (onNewMessage) onNewMessage(m);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
setUserList((prev) => {
|
||||||
|
prev[user.id] = user;
|
||||||
|
return prev;
|
||||||
|
});
|
||||||
|
}, [chatWith, user]);
|
||||||
|
|
||||||
const handleAddServer = () => {
|
const handleAddServer = () => {
|
||||||
if (!newServer.ip) return;
|
if (!newServer.ip) return;
|
||||||
const updatedServers = [
|
const updatedServers = [
|
||||||
@@ -204,7 +241,7 @@ export default function AppSidebar({
|
|||||||
function DMItem({ name, status }: { name: string; status: string }) {
|
function DMItem({ name, status }: { name: string; status: string }) {
|
||||||
return (
|
return (
|
||||||
<Card className="p-2 flex flex-row items-center gap-2 cursor-pointer hover:bg-accent">
|
<Card className="p-2 flex flex-row items-center gap-2 cursor-pointer hover:bg-accent">
|
||||||
<ProfilePicture name={name} />
|
<ProfilePicture name={name} url="" />
|
||||||
<div className="flex flex-col">
|
<div className="flex flex-col">
|
||||||
<span className="text-sm font-medium">{name}</span>
|
<span className="text-sm font-medium">{name}</span>
|
||||||
<span className="text-xs text-muted-foreground">{status}</span>
|
<span className="text-xs text-muted-foreground">{status}</span>
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ export interface User {
|
|||||||
|
|
||||||
export default function useUser() {
|
export default function useUser() {
|
||||||
const [user, setUser] = useState<User | null>(null);
|
const [user, setUser] = useState<User | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -31,13 +30,11 @@ export default function useUser() {
|
|||||||
setUser(res);
|
setUser(res);
|
||||||
} catch {
|
} catch {
|
||||||
router.push("/login");
|
router.push("/login");
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchUser();
|
fetchUser();
|
||||||
}, [router]);
|
}, [router]);
|
||||||
|
|
||||||
return { user, loading };
|
return user;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ export default async function auth(
|
|||||||
console.log("Message received:", data);
|
console.log("Message received:", data);
|
||||||
|
|
||||||
if (data.version) {
|
if (data.version) {
|
||||||
setServer(data);
|
setServer({ channels: [], ...data });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user