From e5ca48b03bd8330dec958a89c3aea87a2fbadb07 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 29 Nov 2023 15:31:23 -0700 Subject: working trongle and rought draft of emacs --- trongleposting-client/src/App.js | 126 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 trongleposting-client/src/App.js (limited to 'trongleposting-client/src/App.js') diff --git a/trongleposting-client/src/App.js b/trongleposting-client/src/App.js new file mode 100644 index 0000000..37643c5 --- /dev/null +++ b/trongleposting-client/src/App.js @@ -0,0 +1,126 @@ +import { useRef, useState, useEffect } from "react"; +import { generateGruvboxFromString } from "./utils/generate_gruvbox"; +import "./App.css"; + +function App() { + const [socket, setSocket] = useState(null); + const [posts, setPosts] = useState([]); + const postsRef = useRef([]); + const [content, setContent] = useState(""); + const [username, setUsername] = useState(""); + const [error, setError] = useState(""); + const [color, setColor] = useState(""); + + const scrollToBottomOfChat = () => { + const objDiv = document.getElementById("chat"); + objDiv.scrollTop = objDiv.scrollHeight; + }; + + useEffect(() => { + const protocol = document.location.protocol === "https:" ? "wss:" : "ws:"; + let socket; + + fetch("/posts") + .then((r) => r.json()) + .then((msgs) => { + postsRef.current = msgs; + setPosts(postsRef.current); + scrollToBottomOfChat(); + }) + .then(() => { + socket = new WebSocket(`${protocol}//${document.location.host}/`); + socket.addEventListener("open", () => { + socket.send("ping"); + }); + socket.addEventListener("message", (msg) => { + const { data } = msg; + if (data === "pong") return; + + const chat = JSON.parse(msg.data); + if (chat.author) { + postsRef.current = [...postsRef.current, chat]; + setPosts(postsRef.current); + scrollToBottomOfChat(); + } + }); + + setSocket(socket); + }); + + return () => { + socket.close(); + }; + }, []); + + useEffect(() => { + setColor(generateGruvboxFromString(username)); + }, [username]); + + const addPost = () => { + setError(""); + if (socket) { + fetch("/posts", { + method: "POST", + headers: { Content: "application/json" }, + body: JSON.stringify({ author: username, message: content }), + }).then(() => setContent("")); + } + }; + + return ( +
+
+

TronglePosting in ELisp

+
+
+

Welcome!

+ {posts.map((post) => ( +
+
+ + {post.author}:{" "} + + {post.message} +
+
+ [{new Date(post.date).toLocaleString()}] +
+
+ ))} +
+
+ { + setUsername(e.target.value); + }} + value={username} + > + +
+ Post +
+ {error ?

{error}

: null} +
+
+ ); +} + +export default App; -- cgit v1.3