autonomous.contact/public/tabs/forum/forum.html
Andy Burke cbd58071b8 feature: blurbs
refactor: style updates and fixes
2025-09-23 17:13:51 -07:00

315 lines
8.7 KiB
HTML

<style>
.forum-container {
padding: 1rem;
}
.post-container {
position: relative;
display: grid;
grid-template-rows: auto auto auto auto 1fr;
grid-template-columns: auto auto 1fr;
grid-template-areas:
"expander preview info"
"expander preview subject"
". . content"
". . newpost"
". . replies";
max-height: 6rem;
padding: 1rem;
border: 1px solid var(--border-subtle);
overflow-y: hidden;
transition: all ease-in-out 0.33s;
margin-bottom: 2rem;
}
.post-container:has(> div > label > input[name="expanded"]:checked) {
max-height: unset;
}
.expand-toggle-container input[name="expanded"] {
display: none;
visibility: hidden;
opacity: 0;
}
.post-container:has(> div > label > input[name="expanded"]) .icon.minus,
.post-container:has(> div > label > input[name="expanded"]:checked) .icon.plus {
display: block;
opacity: 1;
visibility: visible;
}
.post-container:has(> div > label > input[name="expanded"]) .icon.plus,
.post-container:has(> div > label > input[name="expanded"]:checked) .icon.minus {
display: none;
opacity: 0;
visibility: hidden;
}
.post-container .expand-toggle-container {
grid-area: expander;
margin-right: 0.5rem;
}
.post-container .expand-toggle-container label {
display: flex;
align-items: center;
width: 22px;
height: 22px;
}
.post-container .media-preview-container {
grid-area: preview;
width: 6rem;
height: 4rem;
margin-right: 1rem;
}
.post-container .media-preview-container img {
max-width: 100%;
max-height: 100%;
}
.post-container .info-container {
grid-area: info;
display: flex;
}
.post-container .info-container .avatar-container {
display: block;
margin: 0.5rem;
width: 3rem;
height: 3rem;
border-radius: 16%;
overflow: hidden;
}
.post-container .info-container .avatar-container img {
min-width: 100%;
min-height: 100%;
}
.post-container .info-container .username-container {
display: block;
margin: 0 0.5rem;
font-weight: bold;
}
.post-container .info-container .datetime-container {
display: block;
margin: 0 0.5rem;
}
.post-container .info-container .datetime-container .long {
font-size: x-small;
text-transform: uppercase;
visibility: hidden;
display: none;
}
.post-container .info-container .datetime-container .short {
font-size: xx-small;
}
.post-container .subject-container {
grid-area: subject;
padding-left: 5rem;
margin-top: -2.5rem;
font-size: larger;
font-weight: bold;
}
.post-container .content-container {
grid-area: content;
padding-left: 5rem;
margin-top: 2rem;
}
.post-container .new-post-container {
grid-area: newpost;
}
.post-container .replies-container {
grid-area: replies;
}
</style>
<div id="forum" class="tab">
<input
type="radio"
name="top-level-tabs"
id="forum-tab-input"
class="tab-switch"
data-view="forum"
/>
<label for="forum-tab-input" class="tab-label"
><div class="icon forum"></div>
<div class="label">Forum</div></label
>
<div class="tab-content forum-container">
<!-- #include file="./README.md" -->
<div id="forum-posts" class="container">
<div id="forum-posts-list"></div>
<script>
const forum_posts_list = document.getElementById("forum-posts-list");
async function render_post(post) {
const creator = await USERS.get(post.creator_id);
const existing_element =
document.getElementById(`post-${post.id.substring(0, 49)}`) ??
document.getElementById(`post-${post.meta?.temp_id}`);
const post_datetime = datetime_to_local(post.timestamps.created);
const html_content = `<div class="post-container" data-creator_id="${creator.id}" data-post_id="${post.id}">
<div class="expand-toggle-container">
<label>
<input type="checkbox" name="expanded"/><i class="icon plus"></i><i class="icon minus"></i>
</label>
</div>
<div class="media-preview-container">
<img src="/images/placeholders/${String(post_datetime.ms % 10).padStart(2, "0")}.svg" />
</div>
<div class="info-container">
<div class="avatar-container">
<img src="${creator.meta?.avatar ?? "/images/default_avatar.gif"}" alt="user avatar" />
</div>
<div class="username-container">
<span class="username">${creator.username}</span>
</div>
<div class="datetime-container">
<span class="long">${post_datetime.long}</span>
<span class="short">${post_datetime.short}</span>
</div>
</div>
<div class="subject-container">${post.data.subject}</div>
<div class="content-container">${htmlify(post.data.content)}</div>
<!-- #include file="./new_post.html" -->
<div class="replies-container"></div>
</div>`;
if (existing_element) {
const template = document.createElement("template");
template.innerHTML = html_content;
existing_element.replaceWith(template.content.firstChild);
existing_element.classList.remove("sending");
} else {
const target_container =
document.querySelector(
`.post-container[data-post_id='${post.parent_id}'] > .replies-container`,
) ?? forum_posts_list;
target_container.insertAdjacentHTML("beforeend", html_content);
}
}
async function append_posts(posts) {
let last_post_id = forum_posts_list.dataset.last_post_id ?? "";
for await (const post of posts) {
// if the last post is undefined, it becomes this event, otherwise, if this event's id is newer,
// it becomes the latest post
last_post_id =
post.id > last_post_id && post.id.indexOf("TEMP") !== 0
? post.id
: last_post_id;
// if the last post has been updated, update the content's dataset to reflect that
if (last_post_id !== forum_posts_list.dataset.last_post_id) {
forum_posts_list.dataset.last_post_id = last_post_id;
}
await render_post(post);
}
const new_post_forms = document.querySelectorAll(".post-creation-form");
for (const new_post_form of new_post_forms) {
new_post_form.action =
"/api/topics/" + document.body.dataset?.topic + "/events";
}
forum_posts_list.scrollTop = 0;
}
// TODO: we need some abortcontroller handling here or something
// similar for when we change topics, this is the most basic
// first pass outline
let post_polling_abort_controller = null;
async function poll_for_new_posts() {
const forum_posts_list = document.getElementById("forum-posts-list");
const topic_id = document.body.dataset.topic;
const last_post_id = forum_posts_list.dataset.last_post_id;
if (!topic_id) {
return;
}
const message_polling_url = `/api/topics/${topic_id}/events?type=post&limit=100&sort=newest&wait=true${last_post_id ? `&after_id=${last_post_id}` : ""}`;
post_polling_abort_controller =
post_polling_abort_controller || new AbortController();
api.fetch(message_polling_url, {
signal: post_polling_abort_controller.signal,
})
.then(async (new_events_response) => {
const new_events = (await new_events_response.json()) ?? [];
await append_posts(new_events.reverse());
poll_for_new_posts(topic_id);
})
.catch((error) => {
// TODO: poll again? back off?
console.error(error);
});
}
async function load_active_topic_for_forum() {
const topic_id = document.body.dataset.topic;
if (!topic_id) return;
const user = document.body.dataset.user
? JSON.parse(document.body.dataset.user)
: null;
if (!user) return;
const forum_posts_list = document.getElementById("forum-posts-list");
if (post_polling_abort_controller) {
post_polling_abort_controller.abort();
post_polling_abort_controller = null;
delete forum_posts_list.dataset.last_post_id;
}
const topic_response = await api.fetch(`/api/topics/${topic_id}`);
if (!topic_response.ok) {
const error = await topic_response.json();
alert(error.message ?? JSON.stringify(error));
return;
}
const topic = await topic_response.json();
forum_posts_list.innerHTML = "";
const events_response = await api.fetch(
`/api/topics/${topic_id}/events?type=post&limit=100&sort=newest`,
);
if (!events_response.ok) {
const error = await events_response.json();
alert(error.message ?? JSON.stringify(error));
return;
}
const events = await events_response.json();
await append_posts(events.reverse());
poll_for_new_posts();
}
document.addEventListener("topic_changed", load_active_topic_for_forum);
document.addEventListener("user_logged_in", load_active_topic_for_forum);
</script>
</div>
<!-- #include file="./new_post.html" -->
</div>
</div>