import React, { useEffect, useState } from "react"; import { Divider, Icon, IconButton, Input, InputGroup, InputLeftElement, useColorMode, VStack, Flex, } from "@chakra-ui/react"; import { RiSearch2Line, RiSunLine, RiMoonLine } from "react-icons/ri"; import { ProfileButton } from "./ProfileButton"; import { ChatSidebarPreview } from "./rooms/ChatSidebarPreview"; import { useLocalStorage } from "@strata-foundation/react"; import { VISIBLE_CHATS } from "../constants/globals"; import { useRouter } from "next/router"; import { useChat } from "../hooks/useChat"; import { useChatKeyFromIdentifier } from "../hooks/useChatKeyFromIdentifier"; import { CreateChatButton } from "./CreateChat/CreateChatButton"; const DARK_BG = { bg: "gray.900", }; export const Sidebar = (props: any) => { const [input, setInput] = useState(""); const { colorMode, toggleColorMode } = useColorMode(); const [chats, setChats] = useLocalStorage("chats", VISIBLE_CHATS); const router = useRouter(); const { id } = router.query; const { chatKey, loading: loadingId } = useChatKeyFromIdentifier( id as string ); const { info: chat } = useChat(chatKey); useEffect(() => { if (chat && id && chats.indexOf(id as string) === -1) { setChats([...new Set([...chats, id as string])]); } }, [setChats, chats, chat, id]); const handleSearch = (e: React.FormEvent) => { const content = e.currentTarget.value; setInput(content); }; return ( {chats .filter((identifier) => identifier.includes(input)) .map((identifier) => ( { setChats([...chats.filter((c) => c !== identifier)]); }} key={identifier} identifier={identifier} onClick={() => { setInput(""); props.onClose && props.onClose(); }} /> ))} ) : ( ) } onClick={toggleColorMode} /> ); };