add link component

This commit is contained in:
MapleLeaf
2021-12-26 14:38:18 -06:00
parent ad482d84f8
commit 7efc7d53c9
5 changed files with 102 additions and 2 deletions

40
library/link.tsx Normal file
View File

@@ -0,0 +1,40 @@
import React from "react"
import { last } from "../helpers/last.js"
import { ReacordElement } from "./element.js"
import type { MessageOptions } from "./message"
import { Node } from "./node.js"
export type LinkProps = {
label?: string
children?: string
emoji?: string
disabled?: boolean
url: string
}
export function Link(props: LinkProps) {
return <ReacordElement props={props} createNode={() => new LinkNode(props)} />
}
class LinkNode extends Node<LinkProps> {
override modifyMessageOptions(options: MessageOptions): void {
let actionRow = last(options.actionRows)
if (
actionRow == undefined ||
actionRow.length >= 5 ||
actionRow[0]?.type === "select"
) {
actionRow = []
options.actionRows.push(actionRow)
}
actionRow.push({
type: "link",
disabled: this.props.disabled,
emoji: this.props.emoji,
label: this.props.label || this.props.children,
url: this.props.url,
})
}
}

View File

@@ -10,5 +10,6 @@ export * from "./embed/embed-image"
export * from "./embed/embed-thumbnail"
export * from "./embed/embed-title"
export * from "./interaction"
export * from "./link"
export * from "./message"
export * from "./reacord"

View File

@@ -3,7 +3,9 @@ import type { EmbedOptions } from "./embed/embed-options"
export type MessageOptions = {
content: string
embeds: EmbedOptions[]
actionRows: Array<Array<MessageButtonOptions | MessageSelectOptions>>
actionRows: Array<
Array<MessageButtonOptions | MessageLinkOptions | MessageSelectOptions>
>
}
export type MessageButtonOptions = {
@@ -15,6 +17,14 @@ export type MessageButtonOptions = {
emoji?: string
}
export type MessageLinkOptions = {
type: "link"
url: string
label?: string
emoji?: string
disabled?: boolean
}
export type MessageSelectOptions = {
type: "select"
customId: string