import { useState, useEffect } from "react" import { Link } from "react-router-dom" import { Channels, RecentChannels, User, Messages } from "../types" import TopBar from "../components/TopBar" import MessageComponent from "../components/MessageComponent" import axios from "axios" import "../styles/Home.css" export default function Home({socket}: {socket: WebSocket}) { const [user, setUser] = useState(); const [channels, setChannels] = useState(); const [messages , setMessages] = useState(); const [searchedChannels, setSearchedChannels] = useState([]); const [newChannels, setNewChannels] = useState([]); const [search, setSearch] = useState(""); useEffect(() => { const token = localStorage.getItem("token") if (token) { axios .post("/api/auth/me", { token: token }) .then((res) => { setUser(res.data) }) .catch((err) => { console.error(err.response) }) } axios .get("/api/activechannels") .then((res) => {channels setChannels(res.data) }) .catch((err) => { console.error(err.response) }) axios .get("/api/lastmessages") .then((res) => { setMessages(res.data) }) .catch((err) => { console.error(err.response) }) axios .get("/api/newchannels") .then((res) => { setNewChannels(res.data) }) .catch((err) => { console.error(err.response) }) }, []) useEffect(() => { socket.addEventListener('message', function (event) { const data = JSON.parse(event.data); if (data.type === "new_message" || data.type === "delete_message" || data.type === "purge_channel") { axios .get("/api/lastmessages") .then((res) => { setMessages(res.data) }) .catch((err) => { console.error(err.response) }) axios .get("/api/activechannels") .then((res) => { setChannels(res.data) }) .catch((err) => { console.error(err.response) }) } else if (data.type === "new_channel") { axios .get("/api/activechannels") .then((res) => { setChannels(res.data) }) .catch((err) => { console.error(err.response) }) axios .get("/api/newchannels") .then((res) => { setNewChannels(res.data) }) .catch((err) => { console.error(err.response) }) } }); }, []) useEffect(() => { if (!search) { setSearchedChannels([]) return } axios .get(`/api/searchchannels?search=${search}`).then((res) => { setSearchedChannels(res.data) } ).catch((err) => { console.error(err.response) }) }, [search]) return (

Welcome to Tanuki's forum !

I don't know what to say, but I hope you will enjoy your stay here.

osaka

Last messages

{messages?.map((message) => ( ))}

Channels

All channels {user && ( Create channel )}

Recent active channels

    {channels?.map((channel) => (
  • {channel.name}
  • ))}

Last created channels

    {newChannels?.map((channel) => (
  • {channel.name}
  • ))}

Search channels

setSearch(e.target.value)} />
    {searchedChannels?.map((channel) => (
  • {channel.name}
  • ))}
) }