diff --git a/library/link.tsx b/library/link.tsx new file mode 100644 index 0000000..fd26051 --- /dev/null +++ b/library/link.tsx @@ -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 new LinkNode(props)} /> +} + +class LinkNode extends Node { + 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, + }) + } +} diff --git a/library/main.ts b/library/main.ts index f4f830b..a200167 100644 --- a/library/main.ts +++ b/library/main.ts @@ -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" diff --git a/library/message.ts b/library/message.ts index e6d4a70..d8c56ef 100644 --- a/library/message.ts +++ b/library/message.ts @@ -3,7 +3,9 @@ import type { EmbedOptions } from "./embed/embed-options" export type MessageOptions = { content: string embeds: EmbedOptions[] - actionRows: Array> + actionRows: Array< + Array + > } 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 diff --git a/notes.md b/notes.md index f26b848..2c09446 100644 --- a/notes.md +++ b/notes.md @@ -16,7 +16,7 @@ - [x] test - message components - [x] buttons - - [ ] links + - [x] links - [ ] select - [ ] action row - [x] button onClick diff --git a/test/link.test.tsx b/test/link.test.tsx new file mode 100644 index 0000000..d09b1fa --- /dev/null +++ b/test/link.test.tsx @@ -0,0 +1,49 @@ +import React from "react" +import { + Link, + Reacord, + TestAdapter, + TestCommandInteraction, +} from "../library/main" +import { assertMessages } from "./assert-messages" + +const adapter = new TestAdapter() +const reacord = new Reacord({ adapter }) +const reply = reacord.createCommandReply(new TestCommandInteraction(adapter)) + +test("link", async () => { + reply.render( + <> + link text + + + , + ) + + await assertMessages(adapter, [ + { + content: "", + embeds: [], + actionRows: [ + [ + { + type: "link", + url: "https://example.com/", + label: "link text", + }, + { + type: "link", + url: "https://example.com/", + label: "link text", + }, + { + type: "link", + url: "https://example.com/", + label: "link text", + disabled: true, + }, + ], + ], + }, + ]) +})