lazily create action rows

This commit is contained in:
itsMapleLeaf
2022-08-05 23:48:36 -05:00
parent 6b261d647b
commit 69d29d2aa3

View File

@@ -7,6 +7,7 @@ import type {
} from "discord-api-types/v10" } from "discord-api-types/v10"
import { ButtonStyle, ComponentType } from "discord-api-types/v10" import { ButtonStyle, ComponentType } from "discord-api-types/v10"
import type { Node } from "./node" import type { Node } from "./node"
import { TextNode } from "./node"
import { ActionRowNode } from "./react/action-row" import { ActionRowNode } from "./react/action-row"
import type { ButtonProps } from "./react/button" import type { ButtonProps } from "./react/button"
import { ButtonNode } from "./react/button" import { ButtonNode } from "./react/button"
@@ -154,7 +155,7 @@ function makeActionRows(root: Node) {
APIActionRowComponent<APIButtonComponent | APISelectMenuComponent> APIActionRowComponent<APIButtonComponent | APISelectMenuComponent>
> = [] > = []
for (const node of root.children) { function getNextActionRow() {
let currentRow = actionRows[actionRows.length - 1] let currentRow = actionRows[actionRows.length - 1]
if ( if (
!currentRow || !currentRow ||
@@ -167,9 +168,12 @@ function makeActionRows(root: Node) {
} }
actionRows.push(currentRow) actionRows.push(currentRow)
} }
return currentRow
}
for (const node of root.children) {
if (node instanceof ButtonNode) { if (node instanceof ButtonNode) {
currentRow.components.push({ getNextActionRow().components.push({
type: ComponentType.Button, type: ComponentType.Button,
custom_id: node.customId, custom_id: node.customId,
label: node.extractText(Number.POSITIVE_INFINITY), label: node.extractText(Number.POSITIVE_INFINITY),
@@ -180,7 +184,7 @@ function makeActionRows(root: Node) {
} }
if (node instanceof LinkNode) { if (node instanceof LinkNode) {
currentRow.components.push({ getNextActionRow().components.push({
type: ComponentType.Button, type: ComponentType.Button,
label: node.extractText(Number.POSITIVE_INFINITY), label: node.extractText(Number.POSITIVE_INFINITY),
url: node.props.url, url: node.props.url,