forked from andyburke/autonomous.contact
		
	
		
			
				
	
	
		
			275 lines
		
	
	
	
		
			7.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			275 lines
		
	
	
	
		
			7.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <style>
 | |
| 	#essays-container {
 | |
| 		padding: 1rem;
 | |
| 		max-width: 60rem;
 | |
| 		margin: 0 auto;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container {
 | |
| 		position: relative;
 | |
| 		display: grid;
 | |
| 		grid-template-rows: auto auto auto auto 1fr;
 | |
| 		grid-template-columns: 1fr;
 | |
| 		grid-template-areas:
 | |
| 			"preview"
 | |
| 			"title"
 | |
| 			"info"
 | |
| 			"content"
 | |
| 			"newessay"
 | |
| 			"replies";
 | |
| 		padding: 1rem;
 | |
| 		border: 1px solid var(--border-subtle);
 | |
| 		overflow-y: hidden;
 | |
| 		transition: all ease-in-out 0.33s;
 | |
| 		margin-bottom: 2rem;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .media-preview-container {
 | |
| 		grid-area: preview;
 | |
| 		max-height: 600px;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .media-preview-container img {
 | |
| 		max-width: 100%;
 | |
| 		max-height: 100%;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container {
 | |
| 		grid-area: info;
 | |
| 		display: flex;
 | |
| 		margin-left: 1.8rem;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container .avatar-container {
 | |
| 		display: block;
 | |
| 		margin: 0.5rem;
 | |
| 		width: 3rem;
 | |
| 		height: 3rem;
 | |
| 		border-radius: 20%;
 | |
| 		overflow: hidden;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container .avatar-container img {
 | |
| 		min-width: 100%;
 | |
| 		min-height: 100%;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container .username-container {
 | |
| 		display: block;
 | |
| 		margin: 0 0.5rem;
 | |
| 		font-weight: bold;
 | |
| 		align-content: center;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container .datetime-container {
 | |
| 		display: block;
 | |
| 		margin: 0 0.5rem;
 | |
| 		align-content: center;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container .datetime-container .long {
 | |
| 		font-size: x-small;
 | |
| 		text-transform: uppercase;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .info-container .datetime-container .short {
 | |
| 		font-size: xx-small;
 | |
| 		visibility: hidden;
 | |
| 		display: none;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .title-container {
 | |
| 		grid-area: title;
 | |
| 		padding-left: 1rem;
 | |
| 		margin: 1rem 0;
 | |
| 		font-size: xx-large;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .content-container {
 | |
| 		grid-area: content;
 | |
| 		padding-left: 0.25rem;
 | |
| 		margin: 1rem 0;
 | |
| 		font-size: large;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .new-essay-container {
 | |
| 		grid-area: newessay;
 | |
| 		display: none;
 | |
| 	}
 | |
| 
 | |
| 	.essay-container .replies-container {
 | |
| 		grid-area: replies;
 | |
| 	}
 | |
| </style>
 | |
| 
 | |
| <div id="essays" class="tab">
 | |
| 	<input
 | |
| 		type="radio"
 | |
| 		name="top-level-tabs"
 | |
| 		id="essay-tab-input"
 | |
| 		class="tab-switch"
 | |
| 		data-view="essay"
 | |
| 	/>
 | |
| 	<label for="essay-tab-input" class="tab-label"
 | |
| 		><div class="icon essay"></div>
 | |
| 		<div class="label">Essays</div>
 | |
| 	</label>
 | |
| 	<div class="tab-content">
 | |
| 		<div id="essays-container" class="container">
 | |
| 			<!-- #include file="./README.md" -->
 | |
| 			<!-- #include file="./new_essay.html" -->
 | |
| 
 | |
| 			<div id="essays-list"></div>
 | |
| 			<script>
 | |
| 				const essays_list = document.getElementById("essays-list");
 | |
| 
 | |
| 				async function render_essay(essay, position = "beforeend") {
 | |
| 					const creator = await USERS.get(essay.creator_id);
 | |
| 					const existing_element =
 | |
| 						document.getElementById(essay.id) ??
 | |
| 						document.getElementById(essay.meta?.temp_id ?? "") ??
 | |
| 						document.querySelector(`[data-temp_id="${essay.meta?.temp_id ?? ""}"]`);
 | |
| 					const essay_datetime = datetime_to_local(essay.timestamps.created);
 | |
| 
 | |
| 					const html_content = `<div class="essay-container" data-creator_id="${creator.id}" data-essay_id="${essay.id}" data-temp_id="${essay.meta?.temp_id ?? ""}">
 | |
| 						<div class="media-preview-container">
 | |
| 							${essay.data?.media?.length ? essay.data.media.map((url) => `<img src="${url}" />`).join("\n") : ""}
 | |
| 						</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">${essay_datetime.long}</span>
 | |
| 								<span class="short">${essay_datetime.short}</span>
 | |
| 							</div>
 | |
| 						</div>
 | |
| 						<div class="title-container">${essay.data.title}</div>
 | |
| 						<div class="content-container">${htmlify(md_to_html(essay.data.essay))}</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(
 | |
| 								`.essay-container[data-essay_id='${essay.parent_id}'] > .replies-container`,
 | |
| 							) ?? essays_list;
 | |
| 						target_container.insertAdjacentHTML(position, html_content);
 | |
| 					}
 | |
| 				}
 | |
| 
 | |
| 				async function append_essays(essays) {
 | |
| 					let last_essay_id = essays_list.dataset.last_essay_id ?? "";
 | |
| 					for await (const essay of essays) {
 | |
| 						// if the last essay is undefined, it becomes this event, otherwise, if this event's id is newer,
 | |
| 						// it becomes the latest essay
 | |
| 						last_essay_id =
 | |
| 							essay.id > last_essay_id && essay.id.indexOf("TEMP") !== 0
 | |
| 								? essay.id
 | |
| 								: last_essay_id;
 | |
| 
 | |
| 						// if the last essay has been updated, update the content's dataset to reflect that
 | |
| 						if (last_essay_id !== essays_list.dataset.last_essay_id) {
 | |
| 							essays_list.dataset.last_essay_id = last_essay_id;
 | |
| 						}
 | |
| 
 | |
| 						await render_essay(essay);
 | |
| 					}
 | |
| 
 | |
| 					const new_essay_forms = document.querySelectorAll(".essay-creation-form");
 | |
| 					for (const new_essay_form of new_essay_forms) {
 | |
| 						new_essay_form.action =
 | |
| 							"/api/topics/" + document.body.dataset?.topic + "/events";
 | |
| 					}
 | |
| 
 | |
| 					essays_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 essay_polling_abort_controller = null;
 | |
| 				async function poll_for_new_essays() {
 | |
| 					const essays_list = document.getElementById("essays-list");
 | |
| 					const topic_id = document.body.dataset.topic;
 | |
| 					const last_essay_id = essays_list.dataset.last_essay_id;
 | |
| 
 | |
| 					if (!topic_id) {
 | |
| 						return;
 | |
| 					}
 | |
| 
 | |
| 					const message_polling_url = `/api/topics/${topic_id}/events?type=essay&limit=100&sort=newest&wait=true${last_essay_id ? `&after_id=${last_essay_id}` : ""}`;
 | |
| 
 | |
| 					essay_polling_abort_controller =
 | |
| 						essay_polling_abort_controller || new AbortController();
 | |
| 
 | |
| 					api.fetch(message_polling_url, {
 | |
| 						signal: essay_polling_abort_controller.signal,
 | |
| 					})
 | |
| 						.then(async (new_events_response) => {
 | |
| 							const new_events = (await new_events_response.json()) ?? [];
 | |
| 							await append_essays(new_events.reverse(), "afterbegin");
 | |
| 							poll_for_new_essays(topic_id);
 | |
| 						})
 | |
| 						.catch((error) => {
 | |
| 							// TODO: poll again? back off?
 | |
| 							console.error(error);
 | |
| 						});
 | |
| 				}
 | |
| 
 | |
| 				async function load_active_topic_for_essays() {
 | |
| 					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 essays_list = document.getElementById("essays-list");
 | |
| 
 | |
| 					if (essay_polling_abort_controller) {
 | |
| 						essay_polling_abort_controller.abort();
 | |
| 						essay_polling_abort_controller = null;
 | |
| 						delete essays_list.dataset.last_essay_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();
 | |
| 
 | |
| 					essays_list.innerHTML = "";
 | |
| 
 | |
| 					const events_response = await api.fetch(
 | |
| 						`/api/topics/${topic_id}/events?type=essay&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_essays(events);
 | |
| 					poll_for_new_essays();
 | |
| 				}
 | |
| 				document.addEventListener("topic_changed", load_active_topic_for_essays);
 | |
| 				document.addEventListener("user_logged_in", load_active_topic_for_essays);
 | |
| 			</script>
 | |
| 		</div>
 | |
| 	</div>
 | |
| </div>
 |