site/quartz/components/Btn8831.tsx

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-10-14 06:49:37 +00:00
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
// Import the JSON data
2024-10-14 07:10:49 +00:00
import buttonsData from "./buttons.json"
2024-10-14 07:09:28 +00:00
export default (() => {
// Define the component
const Btn8831: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
2024-10-14 10:32:16 +00:00
// Group buttons by type
const groupedButtons = groupButtonsByType(buttonsData)
2024-10-14 07:09:28 +00:00
2024-10-14 10:32:16 +00:00
// Define the order of types
2024-10-14 10:50:43 +00:00
const typeOrder = ["friend", "standard", "misc"]
2024-10-14 07:09:28 +00:00
return (
<div class={`btn8831-container ${displayClass ?? ""}`}>
2024-10-14 10:32:16 +00:00
{typeOrder.map((type, index) => {
const buttons = groupedButtons[type]
if (buttons && buttons.length > 0) {
return (
<div key={type}>
{/* Render buttons of the current type */}
<div class="button-group">
{buttons.map((button, idx) => (
2024-10-14 10:39:34 +00:00
<div key={idx} class="button-item">
{renderButtonContent(button)}
</div>
2024-10-14 10:32:16 +00:00
))}
</div>
{/* Add a horizontal line after the group except for the last one */}
{index < typeOrder.length - 1 && <hr />}
</div>
)
}
return null
})}
2024-10-14 07:09:28 +00:00
</div>
)
2024-10-14 07:08:15 +00:00
}
2024-10-14 06:49:37 +00:00
2024-10-14 10:32:16 +00:00
// Function to group buttons by type
2024-10-14 16:40:38 +00:00
function groupButtonsByType(buttons: any[]) {
const groups: { [key: string]: any[] } = {
2024-10-14 10:32:16 +00:00
misc: [],
friend: [],
standard: [],
}
2024-10-14 16:40:38 +00:00
buttons.forEach((button: any) => {
2024-10-14 10:32:16 +00:00
let type = button.type?.toLowerCase() || "standard"
if (type === "fren") type = "friend"
if (!groups[type]) type = "standard" // Default to 'standard' if type is unrecognized
groups[type].push(button)
})
return groups
}
2024-10-14 10:39:34 +00:00
// Function to render button content based on contentType
2024-10-14 16:40:38 +00:00
function renderButtonContent(button: any): preact.VNode | null {
2024-10-14 10:39:34 +00:00
const contentType = button.contentType || "image" // Default to 'image'
if (contentType === "image") {
return (
<a href={button.url}>
<img
src={button.image}
alt={button.alt}
title={button.title}
loading="lazy"
decoding="async"
/>
2024-10-14 10:39:34 +00:00
</a>
)
} else if (contentType === "iframe") {
const iframeAttrs = button.iframeAttributes || {}
return <iframe src={button.url} title={button.title} {...iframeAttrs}></iframe>
} else {
// Handle other content types if necessary
return null
}
}
2024-10-14 07:09:28 +00:00
return Btn8831
2024-10-14 16:40:38 +00:00
}) satisfies QuartzComponentConstructor