<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useStore } from "vuex";

import { formatDistance } from "date-fns";
import { marked } from "marked";
import DOMPurify from "dompurify";

import ws from "@/ws";

const store = useStore();

const { socket } = store.state.websockets;

const news = ref([]);

const init = () => {
	socket.dispatch("news.getPublished", res => {
		if (res.status === "success") news.value = res.data.news;
	});

	socket.dispatch("apis.joinRoom", "news");
};

const { sanitize } = DOMPurify;

onMounted(() => {
	marked.use({
		renderer: {
			table(header, body) {
				return `<table class="table">
					<thead>${header}</thead>
					<tbody>${body}</tbody>
					</table>`;
			}
		}
	});

	socket.on("event:news.created", res => news.value.unshift(res.data.news));

	socket.on("event:news.updated", res => {
		if (res.data.news.status === "draft") {
			news.value = news.value.filter(
				item => item._id !== res.data.news._id
			);
			return;
		}

		for (let n = 0; n < news.value.length; n += 1) {
			if (news.value[n]._id === res.data.news._id)
				news.value[n] = {
					...news.value[n],
					...res.data.news
				};
		}
	});

	socket.on("event:news.deleted", res => {
		news.value = news.value.filter(item => item._id !== res.data.newsId);
	});

	ws.onConnect(init);
});
</script>

<template>
	<div class="app">
		<page-metadata title="News" />
		<main-header />
		<div class="container">
			<div class="content-wrapper">
				<h1 class="has-text-centered page-title">News</h1>
				<div
					v-for="item in news"
					:key="item._id"
					class="section news-item"
				>
					<div v-html="sanitize(marked(item.markdown))"></div>
					<div class="info">
						<hr />
						By
						<user-link
							:user-id="item.createdBy"
							:alt="item.createdBy"
						/>&nbsp;<span :title="new Date(item.createdAt)">
							{{
								formatDistance(item.createdAt, new Date(), {
									addSuffix: true
								})
							}}
						</span>
					</div>
				</div>
				<h3 v-if="news.length === 0" class="has-text-centered">
					No news items were found.
				</h3>
			</div>
		</div>
		<main-footer />
	</div>
</template>

<style lang="less" scoped>
.night-mode {
	p {
		color: var(--light-grey-2);
	}
}

.container {
	width: calc(100% - 32px);
}

.section {
	border: 1px solid var(--light-grey-3);
	max-width: 100%;
	margin-top: 50px;

	&:last-of-type {
		margin-bottom: 50px;
	}
}
</style>
