@@ -39,8 +39,8 @@ export default function ChatHub() {
|
|||||||
{/* Greeting */}
|
{/* Greeting */}
|
||||||
<div onClick={() => app.setSidebarOpen(true)}>
|
<div onClick={() => app.setSidebarOpen(true)}>
|
||||||
<h1 className="text-3xl font-bold flex cursor-pointer gap-2">
|
<h1 className="text-3xl font-bold flex cursor-pointer gap-2">
|
||||||
<ChevronLeft className="my-auto w-5" />
|
<ChevronLeft className="my-auto w-5 md:hidden" />
|
||||||
Hey {app.profile?.display_name || "there"}
|
Hey {app.profile?.display_name || "there"}!
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-muted-foreground">
|
<p className="text-muted-foreground">
|
||||||
Here's what's happening in your network today.
|
Here's what's happening in your network today.
|
||||||
|
|||||||
@@ -140,10 +140,14 @@ export default function MessageBox({
|
|||||||
onClick={() => app.setSidebarOpen(true)}
|
onClick={() => app.setSidebarOpen(true)}
|
||||||
className="cursor-pointer flex gap-2"
|
className="cursor-pointer flex gap-2"
|
||||||
>
|
>
|
||||||
<ChevronLeftIcon className="w-5 h-5 my-auto" />
|
<ChevronLeftIcon className="w-5 h-5 my-auto md:hidden" />
|
||||||
<span className="flex items-center gap-1.5">
|
<span className="flex items-center gap-1.5">
|
||||||
{channelIcon && (
|
{channelIcon && (
|
||||||
<ProfilePicture name={channelName} url={channelIcon} className="size-7" />
|
<ProfilePicture
|
||||||
|
name={channelName}
|
||||||
|
url={channelIcon}
|
||||||
|
className="size-7"
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
{channelName}
|
{channelName}
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { useEffect, useMemo, useState } from "react";
|
import React, { useEffect } from "react";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { get } from "@/lib/request";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import auth from "@/lib/auth";
|
import auth from "@/lib/auth";
|
||||||
import { useIsMobile } from "@/hooks/is-mobile";
|
import { useIsMobile } from "@/hooks/is-mobile";
|
||||||
@@ -28,7 +26,6 @@ export default function AppLayout({
|
|||||||
server?: Server;
|
server?: Server;
|
||||||
app: App;
|
app: App;
|
||||||
}) {
|
}) {
|
||||||
const [query, setQuery] = useState("");
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const isMobile = useIsMobile();
|
const isMobile = useIsMobile();
|
||||||
|
|
||||||
@@ -80,7 +77,7 @@ export default function AppLayout({
|
|||||||
{server ? (
|
{server ? (
|
||||||
<SidebarChannels server={server} />
|
<SidebarChannels server={server} />
|
||||||
) : (
|
) : (
|
||||||
<SidebarDMs app={app} query={query} setQuery={setQuery} />
|
<SidebarDMs app={app} />
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -9,38 +9,41 @@ import axios from "axios";
|
|||||||
import DMItem from "./DMItem";
|
import DMItem from "./DMItem";
|
||||||
import App from "@/types/app";
|
import App from "@/types/app";
|
||||||
import { UserProfile } from "@/hooks/get-user";
|
import { UserProfile } from "@/hooks/get-user";
|
||||||
import { useEffect } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
export default function SidebarDMs({
|
export default function SidebarDMs({ app }: { app: App }) {
|
||||||
app,
|
|
||||||
query,
|
|
||||||
setQuery,
|
|
||||||
}: {
|
|
||||||
app: App;
|
|
||||||
query: string;
|
|
||||||
setQuery: (v: string) => void;
|
|
||||||
}) {
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const [query, setQuery] = useState("");
|
||||||
|
const [dms, setDms] = useState<UserProfile[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!app.profile) return;
|
||||||
|
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
const users = await Promise.all(
|
const users = [
|
||||||
|
...new Set(
|
||||||
|
await Promise.all(
|
||||||
app.messages
|
app.messages
|
||||||
|
.sort((a, b) => a.timestamp - b.timestamp)
|
||||||
.flatMap((m) => [m.from, m.channel_id])
|
.flatMap((m) => [m.from, m.channel_id])
|
||||||
.filter((id) => id !== app.profile?.id)
|
.filter((id) => id !== app.profile?.id)
|
||||||
.map((id) => app.getUserById(id))
|
.map((id) => app.getUserById(id))
|
||||||
);
|
)
|
||||||
app.setDms(
|
),
|
||||||
|
].filter((v) => !!v);
|
||||||
|
|
||||||
|
app.setDms(users);
|
||||||
|
|
||||||
|
setDms(
|
||||||
users.filter(
|
users.filter(
|
||||||
(user) =>
|
(user) =>
|
||||||
user &&
|
user?.username.toLowerCase().includes(query.toLowerCase()) ||
|
||||||
(user.username.toLowerCase().includes(query.toLowerCase()) ||
|
user?.display_name.toLowerCase().includes(query.toLowerCase())
|
||||||
user.id.toLowerCase().includes(query.toLowerCase()))
|
)
|
||||||
) as UserProfile[]
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
load();
|
load();
|
||||||
}, [query]);
|
}, [query, app.messages, app.profile]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full flex flex-col bg-card/50">
|
<div className="w-full flex flex-col bg-card/50">
|
||||||
@@ -58,7 +61,7 @@ export default function SidebarDMs({
|
|||||||
|
|
||||||
<ScrollArea className="flex-1">
|
<ScrollArea className="flex-1">
|
||||||
<div className="p-2 flex flex-col gap-2">
|
<div className="p-2 flex flex-col gap-2">
|
||||||
{app.dms.map((user) => (
|
{dms.map((user) => (
|
||||||
<DMItem
|
<DMItem
|
||||||
key={user.id}
|
key={user.id}
|
||||||
name={user.display_name}
|
name={user.display_name}
|
||||||
@@ -69,7 +72,7 @@ export default function SidebarDMs({
|
|||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
{app.dms.length === 0 && query.trim() !== "" && (
|
{dms.length === 0 && query.trim() !== "" && (
|
||||||
<div className="px-3 py-2">
|
<div className="px-3 py-2">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export default function useUser() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchUser() {
|
async function fetchUser() {
|
||||||
try {
|
try {
|
||||||
setUser((await get("/api/profile")).data);
|
setUser((await get("/api/profile", (r) => setUser(r.data))).data);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
router.push("/login");
|
router.push("/login");
|
||||||
toast.error(`Error: ${e}`);
|
toast.error(`Error: ${e}`);
|
||||||
@@ -26,7 +26,7 @@ export default function useUser() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchUser();
|
fetchUser();
|
||||||
}, [router]);
|
}, []);
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-1
@@ -2,6 +2,7 @@ import { Message, Server } from "@/types/types";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { Dispatch, RefObject, SetStateAction } from "react";
|
import { Dispatch, RefObject, SetStateAction } from "react";
|
||||||
import { get } from "./request";
|
import { get } from "./request";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
export default async function auth(
|
export default async function auth(
|
||||||
id: string,
|
id: string,
|
||||||
@@ -22,6 +23,8 @@ export default async function auth(
|
|||||||
(await axios.post("/api/auth", { server_id: id })).data as any
|
(await axios.post("/api/auth", { server_id: id })).data as any
|
||||||
).token;
|
).token;
|
||||||
|
|
||||||
|
var name = id;
|
||||||
|
|
||||||
// Open the WebSocket connection
|
// Open the WebSocket connection
|
||||||
const ws = new WebSocket(`wss://${ip}`);
|
const ws = new WebSocket(`wss://${ip}`);
|
||||||
wsRef.current = ws;
|
wsRef.current = ws;
|
||||||
@@ -34,6 +37,13 @@ export default async function auth(
|
|||||||
const data = JSON.parse(m.data);
|
const data = JSON.parse(m.data);
|
||||||
console.log("Message received:", data);
|
console.log("Message received:", data);
|
||||||
|
|
||||||
|
if (data.error) {
|
||||||
|
toast.error(`Error from ${name} (${data.error})`, {
|
||||||
|
description: data.message,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (data.version) {
|
if (data.version) {
|
||||||
setServer({ channels: [], ...data });
|
setServer({ channels: [], ...data });
|
||||||
ws.send(
|
ws.send(
|
||||||
@@ -43,6 +53,7 @@ export default async function auth(
|
|||||||
last_message: lastMessage || 0,
|
last_message: lastMessage || 0,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
name = data.name;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,10 +71,28 @@ export default async function auth(
|
|||||||
|
|
||||||
ws.onerror = (err) => {
|
ws.onerror = (err) => {
|
||||||
console.error("WebSocket error:", err);
|
console.error("WebSocket error:", err);
|
||||||
|
|
||||||
|
let description = "An unknown WebSocket error occurred.";
|
||||||
|
|
||||||
|
if (err instanceof ErrorEvent) {
|
||||||
|
// Browser WebSocket errors show up here
|
||||||
|
description =
|
||||||
|
err.message || err.error?.message || "WebSocket connection failed.";
|
||||||
|
} else if (err instanceof Event) {
|
||||||
|
// Generic Event (no info)
|
||||||
|
description = "WebSocket encountered a network issue.";
|
||||||
|
} else if (err && typeof err === "object") {
|
||||||
|
// Custom data if something special got passed
|
||||||
|
description = JSON.stringify(err, null, 2);
|
||||||
|
} else {
|
||||||
|
description = String(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.error(`WebSocket error (${name})`, { description });
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onclose = () => {
|
ws.onclose = () => {
|
||||||
console.log("WebSocket closed:", ip);
|
console.log("WebSocket closed: ", ip);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cleanup on unmount or id change
|
// Cleanup on unmount or id change
|
||||||
|
|||||||
Reference in New Issue
Block a user