@lilybird/jsx
Version:
JSX support & builders for lilybird
60 lines (59 loc) • 1.67 kB
JavaScript
function parseEmbedChildren(children) {
if (children == null)
return;
if (!Array.isArray(children))
return { [children.type]: children.data };
let obj = { fields: [] };
for (let i = 0, { length } = children; i < length; i++) {
const child = children[i];
if (Array.isArray(child)) {
obj = { ...obj, ...parseEmbedChildren(child) };
continue;
}
if (child.type === "field") {
obj.fields.push(child.data);
continue;
}
obj[child.type] = child.data;
}
return obj;
}
export function Embed({ title, description, url, timestamp, color, children }) {
if (typeof timestamp === "number")
timestamp = new Date(timestamp);
if (typeof timestamp === "boolean")
timestamp = new Date();
return {
title,
type: "rich",
description,
url,
timestamp: timestamp?.toISOString(),
color,
...parseEmbedChildren(children)
};
}
function embedComponent(type, data) {
return { type, data };
}
export function EmbedFooter(props) {
return embedComponent("footer", props);
}
export function EmbedImage(props) {
return embedComponent("image", props);
}
export function EmbedThumbnail(props) {
return embedComponent("thumbnail", props);
}
export function EmbedVideo(props) {
return embedComponent("video", props);
}
export function EmbedProvider(props) {
return embedComponent("provider", props);
}
export function EmbedAuthor(props) {
return embedComponent("author", props);
}
export function EmbedField(props) {
return embedComponent("field", props);
}