diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-03 01:47:07 -0800 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-03 01:47:07 -0800 |
commit | f163a242792cd325c9414587d52f3d8584f28df1 (patch) | |
tree | b57ad121cc3f4ffc2bc55f4d63bfaaf6026dd239 /static/js/components/chat.js | |
download | phoneof-f163a242792cd325c9414587d52f3d8584f28df1.tar.gz phoneof-f163a242792cd325c9414587d52f3d8584f28df1.zip |
initial commit
Diffstat (limited to 'static/js/components/chat.js')
-rw-r--r-- | static/js/components/chat.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/static/js/components/chat.js b/static/js/components/chat.js new file mode 100644 index 0000000..8286a37 --- /dev/null +++ b/static/js/components/chat.js @@ -0,0 +1,27 @@ +const runChat = async () => { + const frenId = + new URLSearchParams(document.location.search).get("fren_id") ?? + document.getElementById("fren_id").value; + const html = await fetch(`/chat/messages?fren_id=${frenId}`).then((r) => + r.text(), + ); + + const { scrollTop, scrollHeight } = document.getElementById( + "chat-container", + ) ?? { scrollTop: 0 }; + const isAtEdge = scrollTop === scrollHeight || scrollTop === 0; + document.getElementById("messages").innerHTML = html; + if (isAtEdge) { + document.getElementById("chat-container").scrollTop = + document.getElementById("chat-container").scrollHeight; + } else { + // save the position. + document.getElementById("chat-container").scrollTop = scrollTop; + } +}; + +setTimeout(() => { + if (document.location.pathname === "/chat") { + runChat().then(() => setInterval(runChat, 5_000)); + } +}, 200); |