import {useEffect, useState} from "react"; import {SendHorizonal} from 'lucide-react' import {useStore} from "../store/store.ts"; import * as Switch from '@radix-ui/react-switch'; import {ShoutType} from "../components/ShoutType.ts"; export const ShoutPage = ()=>{ const [totalUsers, setTotalUsers] = useState(0); const [message, setMessage] = useState(""); const [sticky, setSticky] = useState(false); const socket = useStore(state => state.settingsSocket); const [shouts, setShouts] = useState([]); useEffect(() => { fetch('/stats') .then(response => response.json()) .then(data => setTotalUsers(data.totalUsers)); }, []); useEffect(() => { if(socket) { socket.on('shout', (shout) => { setShouts([...shouts, shout]) }) } }, [socket, shouts]) const sendMessage = () => { socket?.emit('shout', { message, sticky }); setMessage('') } return (

Communication

{totalUsers > 0 &&

There {totalUsers>1?"are":"is"} currently {totalUsers} user{totalUsers>1?"s":""} online

}
{ shouts.map((shout) => { return (
{shout.data.payload.message.message}
{new Date(shout.data.payload.timestamp).toLocaleTimeString() + " " + new Date(shout.data.payload.timestamp).toLocaleDateString()}
) }) }
{ e.preventDefault() sendMessage() }} className="send-message search-field" style={{display: 'flex', gap: '10px'}}> { setSticky(!sticky); }}> setMessage(v.target.value)} style={{width: '100%', paddingRight: '55px', backgroundColor: '#e0e0e0', flexGrow: 1}}/> sendMessage()}/>
) }