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
+7 -6
View File
@@ -6,16 +6,17 @@ import { useParams } from "next/navigation";
import useApp from "@/hooks/use-app"; import useApp from "@/hooks/use-app";
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
import auth from "@/lib/auth"; import auth from "@/lib/auth";
import useAsync from "@/hooks/use-async";
export default function DMs() { export default function DMs() {
const { id } = useParams<{ id: string }>(); const { id } = useParams<{ id: string }>();
const app = useApp(); const app = useApp();
const targetNode = useRef<WebSocket | null>(null); const targetNode = useRef<WebSocket | null>(null);
const { value: target } = useAsync(() => app.getUserById(id));
useEffect(() => { useEffect(() => {
async function connectTargetNode() { async function connectTargetNode() {
const target = await app.getUserById(id); if (!target) return;
if (target) {
auth( auth(
target.node_address, target.node_address,
targetNode, targetNode,
@@ -24,16 +25,16 @@ export default function DMs() {
() => {} () => {}
); );
} }
}
connectTargetNode().then(() => {}); connectTargetNode();
}, [id]); }, [target]);
return ( return (
<AppSidebar app={app}> <AppSidebar app={app}>
<MessageBox <MessageBox
app={app} app={app}
channelName={app.profiles[id]?.display_name || id} channelName={target?.display_name || id}
channelIcon={target?.avatar_url || "_"}
messages={app.messages.filter( messages={app.messages.filter(
(m) => (m) =>
(m.channel_id === id && m.from === app.profile?.id) || (m.channel_id === id && m.from === app.profile?.id) ||
+9 -2
View File
@@ -120,11 +120,13 @@ export default function MessageBox({
app, app,
sendMessage, sendMessage,
channelName, channelName,
channelIcon,
messages, messages,
}: { }: {
app: App; app: App;
sendMessage: (m: string) => void; sendMessage: (m: string) => void;
channelName?: string; channelName?: string;
channelIcon?: string;
messages: Message[]; messages: Message[];
}) { }) {
const [text, setText] = useState(""); const [text, setText] = useState("");
@@ -136,11 +138,16 @@ export default function MessageBox({
<header className="h-12 py-4 flex items-center border-b text-sm font-semibold"> <header className="h-12 py-4 flex items-center border-b text-sm font-semibold">
<span <span
onClick={() => app.setSidebarOpen(true)} onClick={() => app.setSidebarOpen(true)}
className="cursor-pointer flex" className="cursor-pointer flex gap-2"
> >
<ChevronLeftIcon className="w-5 h-5" /> <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} {channelName}
</span> </span>
</span>
</header> </header>
)} )}
<div className="mt-auto flex flex-col-reverse overflow-y-scroll"> <div className="mt-auto flex flex-col-reverse overflow-y-scroll">
+4 -1
View File
@@ -1,14 +1,17 @@
import { cn } from "@/lib/utils";
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar"; import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
export default function ProfilePicture({ export default function ProfilePicture({
name, name,
url, url,
className,
}: { }: {
name: string; name: string;
url: string | undefined; url: string | undefined;
className?: string;
}) { }) {
return ( return (
<Avatar className="h-8 w-8"> <Avatar className={cn("h-8 w-8", className)}>
<AvatarImage src={url} alt={name} /> <AvatarImage src={url} alt={name} />
<AvatarFallback>{name[0]}</AvatarFallback> <AvatarFallback>{name[0]}</AvatarFallback>
</Avatar> </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); console.log("Authenticating with server id:", id);
if (!id) return;
const ip = ((await get(`/api/server/${id}`)).data as any).address as string; const ip = ((await get(`/api/server/${id}`)).data as any).address as string;
console.log("Authenticating with server at:", ip); console.log("Authenticating with server at:", ip);