160 lines
6.2 KiB
TypeScript
160 lines
6.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
function selectTab(button: HTMLElement, selector: string) {
|
|
document.querySelectorAll<HTMLElement>(selector).forEach((tab) => {
|
|
const active = tab === button;
|
|
tab.setAttribute("aria-selected", String(active));
|
|
tab.classList.toggle("text-light-on-surface", active);
|
|
tab.classList.toggle("text-light-on-surface-variant", !active);
|
|
tab.querySelector(".tab-indicator")?.classList.toggle("hidden", !active);
|
|
});
|
|
}
|
|
|
|
export function HomeInteractions() {
|
|
useEffect(() => {
|
|
const byId = (id: string) => document.getElementById(id);
|
|
const home = byId("view-home");
|
|
const profile = byId("view-profile");
|
|
const menu = byId("profile-more-menu");
|
|
const menuButton = byId("profile-more-btn");
|
|
const dialog = byId("about-modal");
|
|
let feed = "for-you";
|
|
let category = "all";
|
|
|
|
function switchView(view: string) {
|
|
const homeActive = view === "home";
|
|
home?.classList.toggle("hidden", !homeActive);
|
|
home?.classList.toggle("flex", homeActive);
|
|
profile?.classList.toggle("hidden", homeActive);
|
|
profile?.classList.toggle("flex", !homeActive);
|
|
|
|
document.querySelectorAll<HTMLElement>(".nav-item").forEach((item) => {
|
|
const active = item.dataset.nav === view;
|
|
if (active) item.setAttribute("aria-current", "page");
|
|
else item.removeAttribute("aria-current");
|
|
item.classList.toggle("font-title", active);
|
|
item.classList.toggle("font-body", !active);
|
|
const icon = item.querySelector<HTMLElement>(".nav-icon");
|
|
if (icon) {
|
|
icon.textContent = item.dataset.nav === "home" ? "home" : active ? "person" : "person_outline";
|
|
icon.style.fontVariationSettings = active ? "'FILL' 1" : "'FILL' 0";
|
|
}
|
|
});
|
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
}
|
|
|
|
function updateCards() {
|
|
document.querySelectorAll<HTMLElement>("#home-note-feed .note-card").forEach((card) => {
|
|
const categoryMatches = category === "all" || card.dataset.category === category;
|
|
const feedMatches = feed === "for-you" || card.dataset.following === "true";
|
|
card.style.display = categoryMatches && feedMatches ? "" : "none";
|
|
});
|
|
}
|
|
|
|
function changeChannel(button: HTMLElement) {
|
|
category = button.dataset.category ?? "all";
|
|
document.querySelectorAll(".channel-pill").forEach((pill) => {
|
|
const active = pill === button;
|
|
pill.setAttribute("aria-selected", String(active));
|
|
pill.classList.toggle("bg-light-primary", active);
|
|
pill.classList.toggle("text-light-on-primary", active);
|
|
pill.classList.toggle("bg-light-surface-container", !active);
|
|
pill.classList.toggle("text-light-on-surface-variant", !active);
|
|
});
|
|
updateCards();
|
|
}
|
|
|
|
function toggleFollow(button: HTMLElement) {
|
|
const following = button.dataset.following !== "true";
|
|
button.dataset.following = String(following);
|
|
for (const name of ["border", "border-light-outline", "bg-light-surface", "text-light-on-surface"]) {
|
|
button.classList.toggle(name, following);
|
|
}
|
|
for (const name of ["bg-light-primary", "text-light-on-primary"]) {
|
|
button.classList.toggle(name, !following);
|
|
}
|
|
const label = button.querySelector("span");
|
|
if (label) label.textContent = following ? "Following" : "Follow";
|
|
}
|
|
|
|
function toggleLike(button: HTMLElement) {
|
|
const liked = button.dataset.liked !== "true";
|
|
const count = Math.max(0, Number(button.dataset.count ?? "0") + (liked ? 1 : -1));
|
|
button.dataset.liked = String(liked);
|
|
button.dataset.count = String(count);
|
|
button.setAttribute("aria-pressed", String(liked));
|
|
button.classList.toggle("text-light-error", liked);
|
|
button.classList.toggle("text-light-on-surface-variant", !liked);
|
|
const icon = button.querySelector<HTMLElement>(".like-icon");
|
|
if (icon) {
|
|
icon.textContent = liked ? "favorite" : "favorite_border";
|
|
icon.style.fontVariationSettings = liked ? "'FILL' 1" : "'FILL' 0";
|
|
}
|
|
const countLabel = button.querySelector(".like-count");
|
|
if (countLabel) countLabel.textContent = String(count);
|
|
}
|
|
|
|
function setMenu(open: boolean) {
|
|
menu?.classList.toggle("hidden", !open);
|
|
menuButton?.setAttribute("aria-expanded", String(open));
|
|
}
|
|
|
|
function handleClick(event: MouseEvent) {
|
|
const target = event.target;
|
|
if (!(target instanceof Element)) return;
|
|
const button = target.closest<HTMLElement>("button");
|
|
|
|
if (button === menuButton && menu) {
|
|
setMenu(menu.classList.contains("hidden"));
|
|
return;
|
|
}
|
|
if (!menu?.contains(target)) setMenu(false);
|
|
if (target === dialog || button?.id === "close-about-btn") {
|
|
dialog?.classList.add("hidden");
|
|
return;
|
|
}
|
|
if (!button) return;
|
|
|
|
if (button.matches(".nav-switch") && button.dataset.nav) {
|
|
switchView(button.dataset.nav);
|
|
} else if (button.matches(".feed-tab")) {
|
|
feed = button.dataset.feed ?? "for-you";
|
|
selectTab(button, ".feed-tab");
|
|
updateCards();
|
|
} else if (button.matches(".channel-pill")) {
|
|
changeChannel(button);
|
|
} else if (button.matches(".profile-tab")) {
|
|
selectTab(button, ".profile-tab");
|
|
for (const panel of ["notes", "saved", "likes", "history"]) {
|
|
byId("panel-" + panel)?.classList.toggle("hidden", panel !== button.dataset.tab);
|
|
}
|
|
} else if (button.id === "profile-follow-btn") {
|
|
toggleFollow(button);
|
|
} else if (button.matches(".like-btn")) {
|
|
toggleLike(button);
|
|
} else if (button.id === "open-about-btn" || button.id === "joined-date-btn") {
|
|
setMenu(false);
|
|
dialog?.classList.remove("hidden");
|
|
}
|
|
}
|
|
|
|
function handleKey(event: KeyboardEvent) {
|
|
if (event.key === "Escape") {
|
|
setMenu(false);
|
|
dialog?.classList.add("hidden");
|
|
}
|
|
}
|
|
|
|
document.addEventListener("click", handleClick);
|
|
document.addEventListener("keydown", handleKey);
|
|
return () => {
|
|
document.removeEventListener("click", handleClick);
|
|
document.removeEventListener("keydown", handleKey);
|
|
};
|
|
}, []);
|
|
|
|
return null;
|
|
}
|