const cards = [...document.querySelectorAll(".card")];
const count = document.getElementById("count")!;
const search = document.getElementById("search") as HTMLInputElement;
const filters = [...document.querySelectorAll(".filter")];
const dialogs = new Map(
[...document.querySelectorAll("dialog.details")].map((d) => [d.dataset.game!, d]),
);
let platform = "";
let game = "";
const apply = () => {
const query = search.value.toLowerCase().trim();
let visible = 0;
for (const card of cards) {
const match =
card.dataset.search!.includes(query) && (!platform || card.dataset.platform === platform);
card.hidden = !match;
if (match) visible++;
}
count.textContent = String(visible);
for (const f of filters) f.setAttribute("aria-pressed", String(f.dataset.platform === platform));
};
const writeUrl = (push: boolean, state: { dialog?: true } | null = null) => {
const url = new URL(location.href);
const set = (key: string, value: string) =>
value ? url.searchParams.set(key, value) : url.searchParams.delete(key);
set("q", search.value.trim());
set("platform", platform);
set("game", game);
if (url.href === location.href) return;
history[push ? "pushState" : "replaceState"](state, "", url);
};
const showGame = (name: string) => {
game = dialogs.has(name) ? name : "";
for (const [key, dialog] of dialogs) {
if (key === game && !dialog.open) {
dialog.showModal();
dialog.focus();
}
else if (key !== game && dialog.open) dialog.close();
}
};
const readUrl = () => {
const params = new URLSearchParams(location.search);
search.value = params.get("q") ?? "";
// Ignore unknown platforms/games (e.g. an old link) instead of showing nothing.
const wanted = params.get("platform") ?? "";
platform = filters.some((f) => f.dataset.platform === wanted) ? wanted : "";
apply();
showGame(params.get("game") ?? "");
};
search.addEventListener("input", () => {
apply();
writeUrl(false);
});
for (const filter of filters) {
filter.addEventListener("click", () => {
// Clicking the active platform clears it
const chosen = filter.dataset.platform!;
platform = chosen === platform ? "" : chosen;
apply();
writeUrl(true);
});
}
for (const button of document.querySelectorAll(".card .open")) {
button.addEventListener("click", () => {
showGame(button.dataset.game!);
writeUrl(true, { dialog: true });
});
}
for (const [name, dialog] of dialogs) {
dialog.addEventListener("click", (event) => {
if (event.target === dialog) dialog.close();
});
dialog.addEventListener("close", () => {
if (game !== name) return;
game = "";
if (history.state?.dialog) history.back();
else writeUrl(false);
});
}
addEventListener("popstate", readUrl);
readUrl();
const resetTimers = new WeakMap();
document.addEventListener("click", async (event) => {
const button = (event.target as Element).closest("button[data-command]");
if (!button) return;
await navigator.clipboard.writeText(button.dataset.command!);
button.toggleAttribute("data-copied", true);
clearTimeout(resetTimers.get(button));
resetTimers.set(
button,
window.setTimeout(() => button.removeAttribute("data-copied"), 1500),
);
});