diff --git a/src/app/server/[ip]/page.tsx b/src/app/server/[ip]/page.tsx index e847af2..135e204 100644 --- a/src/app/server/[ip]/page.tsx +++ b/src/app/server/[ip]/page.tsx @@ -1,7 +1,7 @@ "use client"; -import { useRef, useState } from "react"; -import { useParams } from "next/navigation"; +import { useEffect, useRef, useState } from "react"; +import { useParams, useSearchParams } from "next/navigation"; import MessageBox from "@/components/MessageBox"; import AppLayout from "@/components/app/AppLayout"; import { Server as ServerType } from "@/types/types"; @@ -12,6 +12,7 @@ import { useEffectOnceWhenReady } from "@/hooks/use-once"; export default function Server() { const { ip } = useParams<{ ip: string }>(); + const searchParams = useSearchParams(); const serverRef = useRef(null); const [server, setServer] = useState(); const app = useApp(); @@ -31,27 +32,48 @@ export default function Server() { ); }, [ip, messages], - [undefined, (v) => v[ip]] + [undefined, (v) => v] ); + useEffect(() => { + const channelId = searchParams.get("ch"); + if (channelId) { + app.setCurrentChannel( + server?.channels.find((v) => v.id === channelId) || + server?.channels[0] || + null + ); + } else { + app.setCurrentChannel(server?.channels[0] || null); + } + }, [server]); + return ( - m.channel_id === "general") || []} - sendMessage={(message) => { - serverRef.current?.send( - JSON.stringify({ - type: "send_message", - params: { - channel_id: "general", - contents: message, - }, - }) - ); - }} - /> + {app.currentChannel ? ( + m.channel_id === app.currentChannel?.id + ) || [] + } + sendMessage={(message) => { + serverRef.current?.send( + JSON.stringify({ + type: "send_message", + params: { + channel_id: app.currentChannel?.id, + contents: message, + }, + }) + ); + }} + /> + ) : ( +
+ )}
); } diff --git a/src/components/app/AppLayout.tsx b/src/components/app/AppLayout.tsx index abed679..19fb9b6 100644 --- a/src/components/app/AppLayout.tsx +++ b/src/components/app/AppLayout.tsx @@ -78,7 +78,7 @@ export default function AppLayout({
{server ? ( - + ) : ( )} diff --git a/src/components/app/SidebarChannels.tsx b/src/components/app/SidebarChannels.tsx index 15cdc4b..d04f9f2 100644 --- a/src/components/app/SidebarChannels.tsx +++ b/src/components/app/SidebarChannels.tsx @@ -3,6 +3,7 @@ import { ScrollArea } from "../ui/scroll-area"; import { Server, Channel } from "@/types/types"; import { Card } from "../ui/card"; import { HashIcon, Volume2Icon } from "lucide-react"; +import App from "@/types/app"; export function ChannelIcon({ kind }: { kind: "text" | "voice" }) { switch (kind) { @@ -15,16 +16,28 @@ export function ChannelIcon({ kind }: { kind: "text" | "voice" }) { } } -export function ChannelItem({ channel }: { channel: Channel }) { +export function ChannelItem({ channel, app }: { channel: Channel; app: App }) { return ( - + { + app.setCurrentChannel(channel); + app.setSidebarOpen(false); + }} + > {channel.name} ); } -export default function SidebarChannels({ server }: { server: Server }) { +export default function SidebarChannels({ + server, + app, +}: { + server: Server; + app: App; +}) { return (
@@ -33,7 +46,7 @@ export default function SidebarChannels({ server }: { server: Server }) {
{server.channels.map((channel: Channel) => ( - + ))}
diff --git a/src/hooks/use-app.ts b/src/hooks/use-app.ts index b90180d..d65f47f 100644 --- a/src/hooks/use-app.ts +++ b/src/hooks/use-app.ts @@ -5,7 +5,7 @@ import { StringMap } from "@/types/typeUtils"; import useUser, { UserProfile } from "./get-user"; import { useClientSettings } from "./use-settings"; import { get, Response } from "@/lib/request"; -import { Server } from "@/types/types"; +import { Channel, Server } from "@/types/types"; import Cookies from "js-cookie"; import { toast } from "sonner"; @@ -18,6 +18,7 @@ export default function useApp(): App { const [clientSettings, setClientSettings] = useClientSettings(); const [dms, setDms] = useState([]); const [servers, setServers] = useState>({}); + const [currentChannel, setCurrentChannel] = useState(null); useEffect(() => { const stored = Cookies.get("servers"); @@ -36,6 +37,8 @@ export default function useApp(): App { }, []); return { + currentChannel, + setCurrentChannel, servers, setServers, dms, diff --git a/src/types/app.ts b/src/types/app.ts index 45f7ceb..4353fcc 100644 --- a/src/types/app.ts +++ b/src/types/app.ts @@ -1,7 +1,7 @@ import { Dispatch, RefObject, SetStateAction } from "react"; import { StringMap } from "./typeUtils"; import { UserProfile } from "@/hooks/get-user"; -import { Message, Server } from "./types"; +import { Channel, Message, Server } from "./types"; import { ClientSettings } from "./settings"; export type SetState = Dispatch>; @@ -29,4 +29,7 @@ export default interface App { dms: UserProfile[]; setDms: SetState; + + currentChannel: Channel | null; + setCurrentChannel: SetState; }