Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/components/ui/accordion/Accordion.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
import "node:crypto";

import AccordionItem from "./AccordionItem.astro";

export interface AccordionItemType {
Expand All @@ -15,7 +13,15 @@ export interface Props {
}

const { items, multiExpand = false, variant = "default" } = Astro.props;
const accordionId = crypto.randomUUID();
if (items?.some((i) => typeof i.title !== "string" || !i.title.trim())) {
throw new Error("<Accordion> item has an empty title.");
}
const accordionId = items?.length
? `${variant}-${[...items.map((i) => i.title).join("")]
.slice(0, 7)
.map((c) => c.codePointAt(0))
.join("-")}`
: `${variant}-slot`;
Comment on lines +16 to +24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a stableId helper, smth like this that works with both strings & lists:

import { createHash } from "node:crypto";

export const stableId = (input: string | string[], length = 12): string => {
  const data = typeof input === "string" ? input : JSON.stringify(input);
  return createHash("sha256").update(data).digest("hex").slice(0, length);
};

then can collapse to smth like this, and apply in NavItem, SectionTabs the same:

import { stableId } from "@/utils/stableId";
// ...
const titles = items?.map((i) => i.title) ?? [];
if (titles.some((t) => !t.trim())) {
  throw new Error("<Accordion> item has an empty title.");
}
const accordionId = `${variant}-${titles.length ? stableId(titles) : "slot"}`

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor , (although adding the import back), could PR it after this as i'd hate to back into diff hell / confirming what works

const accordionName = multiExpand ? undefined : `accordion-${accordionId}`;
---

Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/header/NavItem.astro
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export interface Props {

const { label, href, dropdown = [] } = Astro.props;
const isDropdown = dropdown.length > 0;
const id = "dd-" + Math.random().toString(36).slice(2, 11);
const id = `dd-${[...label]
.slice(0, 7)
.map((c) => c.codePointAt(0))
.join("-")}`;
const closeId = id + "-close";
---

Expand Down
9 changes: 5 additions & 4 deletions src/components/ui/tabs/PageTabs.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
---
import crypto from "node:crypto";

interface Props {
labels: string[]; // One label per panel slot (panel-0 ... panel-9)
links?: string[]; // Optional array of URLs, if provided, renders as navigation links instead of same-page tabs
id?: string; // Optional stable id if multiple components on a page
id: string; // Required id
initial?: number; // 0-based default tab
activeIndex?: number; // 0-based active tab for navigation mode
}
Expand All @@ -23,7 +21,10 @@ if (links && links.length !== labels.length) {
if (links && labels.some((_, i) => Astro.slots.has(`panel-${i}`))) {
throw new Error("<PageTabs> cannot have both links and slot content.");
}
const uid = id ?? `tabs-${crypto.randomUUID()}`;
if (typeof id !== "string" || id.trim() === "") {
throw new Error("<PageTabs> requires a non-empty id.");
}
const uid = id.trim();
const I = (n: number) =>
Math.min(Math.max(0, Number(n) || 0), labels.length - 1);
const active = I(initial);
Expand Down
7 changes: 4 additions & 3 deletions src/components/ui/tabs/SectionTabs.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
import crypto from "node:crypto";

interface Tab {
label: string;
count?: number;
Expand All @@ -17,7 +15,10 @@ if (!tabs || tabs.length === 0) {
throw new Error("<SectionTabs> requires at least one tab");
}

const uid = `stabs-${crypto.randomUUID().slice(0, 8)}`;
const uid = `stabs-${[...tabs.map((t) => t.label).join("")]
.slice(0, 7)
.map((c) => c.codePointAt(0))
.join("-")}`;
---

<div class:list={["section-tabs", className]} id={uid}>
Expand Down
1 change: 1 addition & 0 deletions src/pages/blog/[...page].astro
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const tabs = [
<PageContainer>
<Row justify="between" align="center" wrap={false} class="tabs-bar">
<PageTabs
id="tabs-blog"
labels={tabs.map((tab) => tab.label)}
links={tabs.map((tab) => localizeHref(tab.link))}
activeIndex={0}
Expand Down
1 change: 1 addition & 0 deletions src/pages/blog/tags/[tag]/[...page].astro
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const titleKey = isMainTab ? "blog:index.hero.title" : "blog:tags.hero.title";
<PageContainer>
<Row justify="between" align="center" wrap={false} class="tabs-bar">
<PageTabs
id={`tabs-blog-tag-${tag}`}
labels={tabs.map((tab) => tab.label)}
links={tabs.map((tab) => localizeHref(tab.link))}
activeIndex={activeTab}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/blog/tags/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import TitleCard from "@/components/ui/TitleCard.astro";
import Layout from "@/layouts/Layout.astro";
import { getCollection } from "astro:content";

const allPosts = await getCollection("blog");
const allPosts = (await getCollection("blog")).sort((a, b) =>
a.id.localeCompare(b.id),
);
const tags = [...new Set(allPosts.flatMap((post) => post.data.tags))].filter(
(tag) => tag !== undefined,
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/downloads/community.astro
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const setBuiltinId = (wallet: Wallet) => {
/>
<PageContainer>
<PageTabs
id="tabs-downloads-community"
labels={tabs.map((tab) => tab.label)}
links={tabs.map((tab) => tab.link)}
activeIndex={1}
Expand Down
1 change: 1 addition & 0 deletions src/pages/downloads/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const tabs = [
/>
<PageContainer>
<PageTabs
id="tabs-downloads-core"
labels={tabs.map((tab) => tab.label)}
links={tabs.map((tab) => tab.link)}
activeIndex={0}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/feed.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { createSafeMarkdown } from "@/utils/safeMarkdown";
const safeMarkdown = createSafeMarkdown();

export async function GET(context) {
const blog = await getCollection("blog");
const blog = (await getCollection("blog")).sort((a, b) =>
b.id.localeCompare(a.id),
);
return rss({
title: "Monero",
description: "Monero Blog RSS Feed",
Expand Down
Loading