Chat bug fix and top name implementation

This commit is contained in:
2025-10-18 11:09:43 +02:00
parent 10d7b80c13
commit d878721098
5 changed files with 46 additions and 17 deletions
+14 -13
View File
@@ -6,34 +6,35 @@ import { useParams } from "next/navigation";
import useApp from "@/hooks/use-app";
import { useEffect, useRef } from "react";
import auth from "@/lib/auth";
import useAsync from "@/hooks/use-async";
export default function DMs() {
const { id } = useParams<{ id: string }>();
const app = useApp();
const targetNode = useRef<WebSocket | null>(null);
const { value: target } = useAsync(() => app.getUserById(id));
useEffect(() => {
async function connectTargetNode() {
const target = await app.getUserById(id);
if (target) {
auth(
target.node_address,
targetNode,
() => {},
app.addMessage,
() => {}
);
}
if (!target) return;
auth(
target.node_address,
targetNode,
() => {},
app.addMessage,
() => {}
);
}
connectTargetNode().then(() => {});
}, [id]);
connectTargetNode();
}, [target]);
return (
<AppSidebar app={app}>
<MessageBox
app={app}
channelName={app.profiles[id]?.display_name || id}
channelName={target?.display_name || id}
channelIcon={target?.avatar_url || "_"}
messages={app.messages.filter(
(m) =>
(m.channel_id === id && m.from === app.profile?.id) ||
+10 -3
View File
@@ -120,11 +120,13 @@ export default function MessageBox({
app,
sendMessage,
channelName,
channelIcon,
messages,
}: {
app: App;
sendMessage: (m: string) => void;
channelName?: string;
channelIcon?: string;
messages: Message[];
}) {
const [text, setText] = useState("");
@@ -136,10 +138,15 @@ export default function MessageBox({
<header className="h-12 py-4 flex items-center border-b text-sm font-semibold">
<span
onClick={() => app.setSidebarOpen(true)}
className="cursor-pointer flex"
className="cursor-pointer flex gap-2"
>
<ChevronLeftIcon className="w-5 h-5" />
{channelName}
<ChevronLeftIcon className="w-5 h-5 my-auto" />
<span className="flex items-center gap-1.5">
{channelIcon && (
<ProfilePicture name={channelName} url={channelIcon} className="size-7" />
)}
{channelName}
</span>
</span>
</header>
)}
+4 -1
View File
@@ -1,14 +1,17 @@
import { cn } from "@/lib/utils";
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
export default function ProfilePicture({
name,
url,
className,
}: {
name: string;
url: string | undefined;
className?: string;
}) {
return (
<Avatar className="h-8 w-8">
<Avatar className={cn("h-8 w-8", className)}>
<AvatarImage src={url} alt={name} />
<AvatarFallback>{name[0]}</AvatarFallback>
</Avatar>
+16
View File
@@ -0,0 +1,16 @@
import { useEffect, useState } from "react";
export default function useAsync<T, E = any>(func: () => Promise<T>) {
const [loading, setLoading] = useState(true);
const [value, setValue] = useState<T>();
const [error, setError] = useState<E>();
useEffect(() => {
func()
.then(setValue)
.catch(setError)
.finally(() => setLoading(false));
}, []);
return { value, loading, error };
}
+2
View File
@@ -13,6 +13,8 @@ export default async function auth(
) {
console.log("Authenticating with server id:", id);
if (!id) return;
const ip = ((await get(`/api/server/${id}`)).data as any).address as string;
console.log("Authenticating with server at:", ip);