Add Mastodon comments and remove date-fns dependency
This commit is contained in:
parent
00fa1fb164
commit
3e8bcbcf43
19 changed files with 393 additions and 142 deletions
|
|
@ -1,10 +1,14 @@
|
|||
<Fragment>
|
||||
---
|
||||
|
||||
---
|
||||
|
||||
<template id="template-modal-age-restricted">
|
||||
<div
|
||||
id="modal-age-restricted"
|
||||
class="fixed inset-0 hidden bg-stone-100 dark:bg-stone-900"
|
||||
class="fixed inset-0 bg-stone-100 dark:bg-stone-900"
|
||||
tabindex="-1"
|
||||
role="dialog"
|
||||
aria-hidden="true"
|
||||
aria-hidden="false"
|
||||
>
|
||||
<div class="mx-auto flex min-h-screen max-w-3xl flex-col items-center justify-center text-center tracking-tight">
|
||||
<div class="h-14 w-14 text-bm-500 sm:h-16 sm:w-16 dark:text-bm-400">
|
||||
|
|
@ -44,32 +48,33 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script is:inline data-astro-rerun>
|
||||
(function () {
|
||||
if (localStorage.getItem("ageVerified") !== "true") {
|
||||
const modal = document.querySelector("#modal-age-restricted");
|
||||
const rejectButton = modal.querySelector("button[data-modal-reject]");
|
||||
const acceptButton = modal.querySelector("button[data-modal-accept]");
|
||||
function onRejectButtonClick(e) {
|
||||
e.preventDefault();
|
||||
location.href = "about:blank";
|
||||
}
|
||||
function onAcceptButtonClick(e) {
|
||||
e.preventDefault();
|
||||
rejectButton.removeEventListener("click", onRejectButtonClick);
|
||||
acceptButton.removeEventListener("click", onAcceptButtonClick);
|
||||
localStorage.setItem("ageVerified", "true");
|
||||
document.body.style = "overflow:auto;";
|
||||
modal.classList.add("hidden");
|
||||
modal.setAttribute("aria-hidden", "true");
|
||||
}
|
||||
rejectButton.addEventListener("click", onRejectButtonClick);
|
||||
acceptButton.addEventListener("click", onAcceptButtonClick);
|
||||
document.body.style = "overflow:hidden;";
|
||||
modal.setAttribute("aria-hidden", "false");
|
||||
modal.classList.remove("hidden");
|
||||
rejectButton.focus();
|
||||
</template>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
if (localStorage.getItem("ageVerified") !== "true") {
|
||||
document.body.appendChild(
|
||||
(document.getElementById("template-modal-age-restricted") as HTMLTemplateElement).content.cloneNode(true),
|
||||
);
|
||||
const modal = document.querySelector<HTMLDivElement>("body > #modal-age-restricted")!;
|
||||
const rejectButton = modal.querySelector<HTMLButtonElement>("button[data-modal-reject]")!;
|
||||
const acceptButton = modal.querySelector<HTMLButtonElement>("button[data-modal-accept]")!;
|
||||
function onRejectButtonClick(e: MouseEvent) {
|
||||
e.preventDefault();
|
||||
location.href = "about:blank";
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</Fragment>
|
||||
function onAcceptButtonClick(e: MouseEvent) {
|
||||
e.preventDefault();
|
||||
rejectButton.removeEventListener("click", onRejectButtonClick);
|
||||
acceptButton.removeEventListener("click", onAcceptButtonClick);
|
||||
localStorage.setItem("ageVerified", "true");
|
||||
document.body.style.overflow = "auto";
|
||||
modal.remove();
|
||||
}
|
||||
rejectButton.addEventListener("click", onRejectButtonClick);
|
||||
acceptButton.addEventListener("click", onAcceptButtonClick);
|
||||
document.body.style.overflow = "hidden";
|
||||
rejectButton.focus();
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -2,27 +2,23 @@
|
|||
|
||||
---
|
||||
|
||||
<script is:inline data-astro-rerun>
|
||||
<script is:inline>!function(){var a="dark",b="auto",c="colorScheme",d=document.body.classList,e=localStorage,f=e.getItem(c);f&&f!==b?f===a&&d.add(a):(e.setItem(c,b),matchMedia("(prefers-color-scheme: dark)").matches&&d.add(a))}();</script>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
var colorScheme = localStorage.getItem("colorScheme");
|
||||
if (colorScheme == null || colorScheme === "auto") {
|
||||
localStorage.setItem("colorScheme", "auto");
|
||||
colorScheme = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
||||
}
|
||||
const bodyClassList = document.body.classList;
|
||||
if (colorScheme === "dark") {
|
||||
bodyClassList.add("dark");
|
||||
}
|
||||
|
||||
document.querySelectorAll("button[data-dark-mode]").forEach(function (button) {
|
||||
button.addEventListener("click", function (e) {
|
||||
e.preventDefault();
|
||||
if (colorScheme === "dark") {
|
||||
colorScheme = "light";
|
||||
bodyClassList.remove("dark");
|
||||
document.body.classList.remove("dark");
|
||||
} else {
|
||||
colorScheme = "dark";
|
||||
bodyClassList.add("dark");
|
||||
document.body.classList.add("dark");
|
||||
}
|
||||
localStorage.setItem("colorScheme", colorScheme);
|
||||
});
|
||||
|
|
|
|||
220
src/components/MastodonComments.astro
Normal file
220
src/components/MastodonComments.astro
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
---
|
||||
type Props = {
|
||||
instance?: string;
|
||||
user?: string;
|
||||
postId?: string;
|
||||
};
|
||||
|
||||
const { instance, user, postId } = Astro.props;
|
||||
---
|
||||
|
||||
<section
|
||||
id="comment-section"
|
||||
class="hidden px-2 font-serif"
|
||||
aria-describedby="title-comment-section"
|
||||
data-instance={instance || ""}
|
||||
data-user={user || ""}
|
||||
data-post-id={postId || ""}
|
||||
>
|
||||
<h2 id="title-comment-section" class="py-2 font-serif text-xl font-semibold text-stone-800 dark:text-stone-100">
|
||||
Comments
|
||||
</h2>
|
||||
<div class="text-stone-800 dark:text-stone-100" id="comments">
|
||||
<button
|
||||
class="mx-auto w-64 rounded-lg bg-bm-300 px-4 py-1 underline disabled:bg-bm-400 disabled:no-underline dark:bg-green-800 dark:disabled:bg-green-900"
|
||||
id="load-comments-button"
|
||||
data-load-comments
|
||||
>
|
||||
<span>Click to load comments</span>
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<template id="template-comments-loading">
|
||||
<svg class="-mt-1 mr-1 inline h-5 w-5 animate-spin" fill="none" viewBox="0 0 24 24">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path
|
||||
class="opacity-100"
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||
></path>
|
||||
</svg>
|
||||
<span>Loading...</span>
|
||||
</template>
|
||||
|
||||
<template id="template-comment-box">
|
||||
<div class="my-2 rounded-md border-2 border-stone-400 bg-stone-200 p-2 dark:border-stone-600 dark:bg-stone-800">
|
||||
<div class="ml-1">
|
||||
<a data-author class="text-link flex items-center text-lg hover:underline focus:underline">
|
||||
<img data-avatar class="mr-2 w-10 rounded-full border border-stone-400 dark:border-stone-600" />
|
||||
<span data-display-name></span>
|
||||
</a>
|
||||
<a data-post-link class="text-link my-1 flex items-center text-sm font-light hover:underline focus:underline">
|
||||
<span class="mr-1" data-publish-date></span>
|
||||
</a>
|
||||
</div>
|
||||
<div data-content class="prose-a:text-link prose prose-story my-1 dark:prose-invert prose-img:my-0"></div>
|
||||
<div class="ml-1 flex flex-row pb-2 pt-1">
|
||||
<div class="flex" aria-label="Favorites">
|
||||
<span data-favorites></span>
|
||||
<svg class="ml-2 w-5 fill-current" viewBox="0 0 576 512" aria-hidden>
|
||||
<path
|
||||
d="M316.9 18C311.6 7 300.4 0 288.1 0s-23.4 7-28.8 18L195 150.3 51.4 171.5c-12 1.8-22 10.2-25.7 21.7s-.7 24.2 7.9 32.7L137.8 329 113.2 474.7c-2 12 3 24.2 12.9 31.3s23 8 33.8 2.3l128.3-68.5 128.3 68.5c10.8 5.7 23.9 4.9 33.8-2.3s14.9-19.3 12.9-31.3L438.5 329 542.7 225.9c8.6-8.5 11.7-21.2 7.9-32.7s-13.7-19.9-25.7-21.7L381.2 150.3 316.9 18z"
|
||||
></path>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="ml-4 flex" aria-label="Reblogs">
|
||||
<span data-reblogs></span>
|
||||
<svg class="ml-2 w-5 fill-current" viewBox="0 0 512 512" aria-hidden>
|
||||
<path
|
||||
d="M0 224c0 17.7 14.3 32 32 32s32-14.3 32-32c0-53 43-96 96-96H320v32c0 12.9 7.8 24.6 19.8 29.6s25.7 2.2 34.9-6.9l64-64c12.5-12.5 12.5-32.8 0-45.3l-64-64c-9.2-9.2-22.9-11.9-34.9-6.9S320 19.1 320 32V64H160C71.6 64 0 135.6 0 224zm512 64c0-17.7-14.3-32-32-32s-32 14.3-32 32c0 53-43 96-96 96H192V352c0-12.9-7.8-24.6-19.8-29.6s-25.7-2.2-34.9 6.9l-64 64c-12.5 12.5-12.5 32.8 0 45.3l64 64c9.2 9.2 22.9 11.9 34.9 6.9s19.8-16.6 19.8-29.6V448H352c88.4 0 160-71.6 160-160z"
|
||||
></path>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
<div data-comment-thread class="-mb-2"></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
interface Emoji {
|
||||
shortcode: string;
|
||||
url: string;
|
||||
static_url: string;
|
||||
}
|
||||
|
||||
interface Comment {
|
||||
id: string;
|
||||
in_reply_to_id: string;
|
||||
url: string;
|
||||
favourites_count: number;
|
||||
reblogs_count: number;
|
||||
created_at: string;
|
||||
edited_at: string | null;
|
||||
account: {
|
||||
username: string;
|
||||
acct: string;
|
||||
display_name: string;
|
||||
url: string;
|
||||
avatar: string;
|
||||
avatar_static: string;
|
||||
emojis: Emoji[];
|
||||
};
|
||||
content: string;
|
||||
emojis: Emoji[];
|
||||
}
|
||||
|
||||
(function () {
|
||||
const replaceEmojis = (text: string, emojis: Emoji[], imgClass: string) =>
|
||||
emojis.reduce(
|
||||
(acc, emoji) =>
|
||||
acc.replaceAll(
|
||||
`:${emoji.shortcode}:`,
|
||||
`<img class="${imgClass}" alt=":${emoji.shortcode}: emoji" src="${emoji.url}" />`,
|
||||
),
|
||||
text,
|
||||
);
|
||||
|
||||
const commentSection = document.querySelector<Element>("#comment-section")!;
|
||||
const instance = commentSection.getAttribute("data-instance");
|
||||
const user = commentSection.getAttribute("data-user");
|
||||
const postId = commentSection.getAttribute("data-post-id");
|
||||
if (instance && user && postId) {
|
||||
commentSection.classList.remove("hidden");
|
||||
commentSection.querySelector<HTMLButtonElement>("button[data-load-comments]")!.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
const loadCommentsButton = e.target as HTMLButtonElement;
|
||||
loadCommentsButton.setAttribute("disabled", "true");
|
||||
loadCommentsButton.replaceChildren(
|
||||
(document.getElementById("template-comments-loading") as HTMLTemplateElement).content.cloneNode(true),
|
||||
);
|
||||
const renderComments = async () => {
|
||||
try {
|
||||
if (!instance || !user || !postId) {
|
||||
throw new Error(
|
||||
`Cannot fetch comments without all fields (instance=${instance}, user=${user}, post-id=${postId})`,
|
||||
);
|
||||
}
|
||||
const response = await fetch(`https://${instance}/api/v1/statuses/${postId}/context`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Received error status ${response.status} - ${response.statusText}!`);
|
||||
}
|
||||
const data: { ancestors: Comment[]; descendants: Comment[] } = await response.json();
|
||||
// console.log(data);
|
||||
|
||||
const commentsList: HTMLElement[] = [];
|
||||
const commentMap: Record<string, number> = {};
|
||||
const commentTemplate = document.getElementById("template-comment-box") as HTMLTemplateElement;
|
||||
data.descendants.forEach((comment) => {
|
||||
const commentBox = commentTemplate.content.cloneNode(true) as HTMLDivElement;
|
||||
|
||||
const commentBoxAuthor = commentBox.querySelector<HTMLAnchorElement>("[data-author]")!;
|
||||
commentBoxAuthor.href = comment.account.url;
|
||||
commentBoxAuthor.target = "_blank";
|
||||
const avatar = commentBoxAuthor.querySelector<HTMLImageElement>("[data-avatar]")!;
|
||||
avatar.src = comment.account.avatar;
|
||||
avatar.alt = `Profile picture of ${comment.account.username}`;
|
||||
const displayName = commentBoxAuthor.querySelector<HTMLSpanElement>("[data-display-name]")!;
|
||||
displayName.innerHTML = replaceEmojis(
|
||||
comment.account.display_name,
|
||||
comment.account.emojis,
|
||||
"inline mx-[1px] w-5",
|
||||
);
|
||||
|
||||
const commentBoxPostLink = commentBox.querySelector<HTMLAnchorElement>("[data-post-link]")!;
|
||||
commentBoxPostLink.href = comment.url;
|
||||
commentBoxPostLink.target = "_blank";
|
||||
const publishDate = commentBoxPostLink.querySelector<HTMLSpanElement>("[data-publish-date]")!;
|
||||
// TO-DO Pretty format date
|
||||
publishDate.innerText = new Date(Date.parse(comment.created_at)).toLocaleString("en-US", {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
|
||||
if (comment.edited_at) {
|
||||
const edited = document.createElement("span");
|
||||
edited.className = "italic";
|
||||
edited.innerText = "(edited)";
|
||||
commentBoxPostLink.appendChild(edited);
|
||||
}
|
||||
|
||||
const commentBoxContent = commentBox.querySelector<HTMLDivElement>("[data-content]")!;
|
||||
commentBoxContent.innerHTML = replaceEmojis(comment.content, comment.emojis, "inline mx-[1px] w-5");
|
||||
|
||||
const commentBoxFavorites = commentBox.querySelector<HTMLSpanElement>("[data-favorites]")!;
|
||||
commentBoxFavorites.innerText = comment.favourites_count.toString();
|
||||
|
||||
const commentBoxReblogs = commentBox.querySelector<HTMLSpanElement>("[data-reblogs]")!;
|
||||
commentBoxReblogs.innerText = comment.reblogs_count.toString();
|
||||
|
||||
if (comment.in_reply_to_id === postId || !(comment.in_reply_to_id in commentMap)) {
|
||||
commentMap[comment.id] = commentsList.length;
|
||||
commentsList.push(commentBox);
|
||||
} else {
|
||||
const commentsIndex = commentMap[comment.in_reply_to_id];
|
||||
commentMap[comment.id] = commentsIndex;
|
||||
commentsList[commentsIndex]
|
||||
.querySelector<HTMLDivElement>("[data-comment-thread]")!
|
||||
.appendChild(commentBox);
|
||||
}
|
||||
});
|
||||
const commentsDiv = commentSection.querySelector<HTMLDivElement>("#comments")!;
|
||||
if (commentsList.length === 0) {
|
||||
commentsDiv.innerHTML = `<p class="my-1">No comments yet. <a class="text-link underline" href="https://${instance}/@${user}/${postId}" target="_noblank">Be the first to join the conversation on Mastodon</a>.</p>`;
|
||||
} else {
|
||||
commentsDiv.innerHTML = `<p class="my-1">Join the conversation <a class="text-link underline" href="https://${instance}/@${user}/${postId}" target="_noblank">by replying on Mastodon</a>.</p>`;
|
||||
commentsDiv.append(...commentsList);
|
||||
}
|
||||
} catch (e) {
|
||||
loadCommentsButton.innerHTML = `<span>Unable to load comments.</span>`;
|
||||
console.error("Fetch Mastodon comments error", e);
|
||||
}
|
||||
};
|
||||
renderComments();
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue