refactor: rework the frontend to move topics to the top

This commit is contained in:
Andy Burke 2025-09-10 19:38:38 -07:00
parent fac8f409f4
commit 11ecd86bb9
14 changed files with 362 additions and 292 deletions

View file

@ -311,23 +311,6 @@ function render_text_event(topic_chat_content, event, creator, existing_element)
}
}
async function get_new_topic_element() {
const existing_new_topic_element = document.getElementById("new-topic");
if (existing_new_topic_element) {
return existing_new_topic_element;
}
const topic_list = document.getElementById("topic-list");
topic_list.insertAdjacentHTML(
"beforeend",
`<li id="new-topic" class="topic"><a href="" contenteditable="true">new topic</a></li>`,
);
await new Promise((resolve) => setTimeout(resolve, 1));
const new_topic_element = document.getElementById("new-topic");
return new_topic_element;
}
const users = {};
async function append_topic_events(events) {
const topic_chat_content = document.getElementById("topic-chat-content");
@ -393,6 +376,8 @@ async function poll_for_new_events() {
}
async function load_topic(topic_id) {
if (!topic_id) return;
const topic_chat_content = document.getElementById("topic-chat-content");
if (topic_polling_request_abort_controller) {
@ -435,66 +420,6 @@ async function load_topic(topic_id) {
await append_topic_events(events);
poll_for_new_events(topic_id);
}
let last_topic_update = undefined;
async function update_chat_topics() {
const now = new Date();
const time_since_last_update = now - (last_topic_update ?? 0);
if (time_since_last_update < 5_000) {
return;
}
const topics_response = await api.fetch("/api/topics");
if (topics_response.ok) {
const topic_list = document.getElementById("topic-list");
topic_list.innerHTML = "";
const topics = await topics_response.json();
for (const topic of topics) {
topic_list.insertAdjacentHTML(
"beforeend",
`<li id="topic-selector-${topic.id}" class="topic"><a href="#/chat/topic/${topic.id}">${topic.name}</a></li>`,
);
}
last_topic_update = now;
}
}
window.addEventListener("locationchange", update_chat_topics);
function check_for_topic_in_url() {
const user_json = document.body.dataset.user;
if (!user_json) {
return;
}
const hash = window.location.hash;
const chat_in_url = hash.indexOf("#/chat") === 0;
if (!chat_in_url) {
return;
}
const first_topic_id = document.querySelector("li.topic")?.id.substring(14);
// #/chat/topic/{topic_id}
// ^ 12
const topic_id = hash.substring(12) || first_topic_id;
if (!topic_id) {
setTimeout(check_for_topic_in_url, 100);
return;
}
const topic_chat_container = document.getElementById("topic-chat-container");
if (topic_chat_container.dataset.topic_id !== topic_id) {
window.location.hash = `/chat/topic/${topic_id}`;
topic_chat_container.dataset.topic_id = topic_id;
load_topic(topic_id);
}
}
window.addEventListener("locationchange", check_for_topic_in_url);
document.addEventListener("DOMContentLoaded", async () => {
await update_chat_topics();
check_for_topic_in_url();
document.addEventListener("topic_changed", ({ detail: { topic } }) => {
load_topic(topic);
});