diff --git a/src/app/chat/[id]/page.tsx b/src/app/chat/[id]/page.tsx index db8929c..aaaf2b7 100644 --- a/src/app/chat/[id]/page.tsx +++ b/src/app/chat/[id]/page.tsx @@ -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(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 ( (m.channel_id === id && m.from === app.profile?.id) || diff --git a/src/components/MessageBox.tsx b/src/components/MessageBox.tsx index 50d3bb6..010e271 100644 --- a/src/components/MessageBox.tsx +++ b/src/components/MessageBox.tsx @@ -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({
app.setSidebarOpen(true)} - className="cursor-pointer flex" + className="cursor-pointer flex gap-2" > - - {channelName} + + + {channelIcon && ( + + )} + {channelName} +
)} diff --git a/src/components/ProfilePicture.tsx b/src/components/ProfilePicture.tsx index 7a17ac9..7228443 100644 --- a/src/components/ProfilePicture.tsx +++ b/src/components/ProfilePicture.tsx @@ -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 ( - + {name[0]} diff --git a/src/hooks/use-async.ts b/src/hooks/use-async.ts new file mode 100644 index 0000000..7742870 --- /dev/null +++ b/src/hooks/use-async.ts @@ -0,0 +1,16 @@ +import { useEffect, useState } from "react"; + +export default function useAsync(func: () => Promise) { + const [loading, setLoading] = useState(true); + const [value, setValue] = useState(); + const [error, setError] = useState(); + + useEffect(() => { + func() + .then(setValue) + .catch(setError) + .finally(() => setLoading(false)); + }, []); + + return { value, loading, error }; +} diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 9e330bc..5b7bafa 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -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);