"use client"; import { HashIcon, MessageCircle, Plus, Search, Volume2Icon, } from "lucide-react"; import { Server, Channel, Message } from "@/types/types"; import React, { useEffect, useRef, useState } from "react"; import Cookies from "js-cookie"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "./ui/dialog"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { Button } from "./ui/button"; import { Card } from "./ui/card"; import { ScrollArea } from "./ui/scroll-area"; import { useRouter } from "next/navigation"; import { UserProfile } from "@/hooks/get-user"; import ProfilePicture from "./ProfilePicture"; import auth from "@/lib/auth"; import { toast } from "sonner"; import { StringMap } from "@/types/typeUtils"; import SettingsDialog from "./settings/SettingsDialog"; import Link from "next/link"; import { get, Response } from "@/lib/request"; import { cn } from "@/lib/utils"; import App from "@/types/app"; export default function AppSidebar({ children, chatWith, onNewMessage, server, app, }: Readonly<{ onNewMessage?: (msg: Message) => void; chatWith?: string; children?: React.ReactNode; server?: Server | undefined; app: App; }>) { const [servers, setServers] = useState<[string, string][]>([]); const [newServer, setNewServer] = useState({ name: "", ip: "" }); const router = useRouter(); const dms = Array.from( new Set( app.messages ?.flatMap((item) => [item.from, item.channel_id]) .filter((item) => item !== app.profile?.id) || [] ) ); useEffect(() => { const s = Cookies.get("servers")?.split(","); if (s) setServers(s.map((x) => x.trim().split("@") as [string, string])); }, []); useEffect(() => { if (!app.profile?.node_address) return; auth( app.profile?.node_address, app.node, () => {}, app.addMessage, (m) => { if (m.from !== chatWith && m.from !== app.profile?.id) toast( `New message from ${app.profiles[m.from]?.display_name || m.from}`, { description: m.contents.slice(0, 80), action: { label: "View", onClick: () => router.push(`/chat/${m.from}`), }, position: "top-right", } ); if (onNewMessage) onNewMessage(m); }, app.messages ? app.messages[app.messages.length - 1]?.id : undefined ); app.setProfiles((prev) => { if (app.profile) prev[app.profile.id] = app.profile; return prev; }); }, [chatWith, app.profile]); const handleAddServer = () => { if (!newServer.ip) return; const updatedServers = [ ...servers, [newServer.ip, newServer.name] as [string, string], ]; setServers(updatedServers); Cookies.set("servers", updatedServers.map((x) => x.join("@")).join(","), { expires: 7, }); setNewServer({ name: "", ip: "" }); }; return (