wip embeds

This commit is contained in:
MapleLeaf
2021-12-16 10:10:47 -06:00
parent b7a3be73ee
commit 803252290d
8 changed files with 88 additions and 18 deletions

View File

@@ -0,0 +1,11 @@
import React from "react"
import { Embed } from "../src/components.js"
export function KitchenSink() {
return (
<>
content
<Embed color="BLUE"></Embed>
</>
)
}

View File

@@ -3,6 +3,7 @@ import test from "ava"
import { Client, TextChannel } from "discord.js" import { Client, TextChannel } from "discord.js"
import { nanoid } from "nanoid" import { nanoid } from "nanoid"
import React, { useState } from "react" import React, { useState } from "react"
import { Embed } from "../src/components/embed.js"
import { raise } from "../src/helpers/raise.js" import { raise } from "../src/helpers/raise.js"
import { createRoot } from "../src/root.js" import { createRoot } from "../src/root.js"
import { testBotToken, testChannelId } from "./test-environment.js" import { testBotToken, testChannelId } from "./test-environment.js"
@@ -32,7 +33,20 @@ test.after(() => {
client.destroy() client.destroy()
}) })
test("rendering text", async (t) => { test.only("test", async (t) => {
const root = createRoot(channel)
await root.render(
<>
<Embed color="BLUE">
<Embed color="DARKER_GREY" />
</Embed>
<Embed color="DARKER_GREY" />
</>,
)
t.pass()
})
test("kitchen sink", async (t) => {
const root = createRoot(channel) const root = createRoot(channel)
const content = nanoid() const content = nanoid()
@@ -50,7 +64,7 @@ test("rendering text", async (t) => {
await assertNoMessageHasContent(t, newContent) await assertNoMessageHasContent(t, newContent)
}) })
test("rapid updates", async (t) => { test("kitchen sink, rapid updates", async (t) => {
const root = createRoot(channel) const root = createRoot(channel)
const content = nanoid() const content = nanoid()

View File

@@ -33,7 +33,6 @@
}, },
"devDependencies": { "devDependencies": {
"@itsmapleleaf/configs": "^1.1.0", "@itsmapleleaf/configs": "^1.1.0",
"@types/scheduler": "^0.16.2",
"@typescript-eslint/eslint-plugin": "^5.6.0", "@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0", "@typescript-eslint/parser": "^5.6.0",
"ava": "^4.0.0-rc.1", "ava": "^4.0.0-rc.1",
@@ -54,7 +53,6 @@
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"prettier": "^2.5.1", "prettier": "^2.5.1",
"react": "^17.0.2", "react": "^17.0.2",
"scheduler": "^0.20.2",
"tsup": "^5.10.3", "tsup": "^5.10.3",
"typescript": "^4.5.2" "typescript": "^4.5.2"
}, },
@@ -74,7 +72,9 @@
"--experimental-specifier-resolution=node", "--experimental-specifier-resolution=node",
"--no-warnings" "--no-warnings"
], ],
"files": "{src,test,integration}/**/*.test.{ts,tsx}", "files": [
"{src,test,integration}/**/*.test.{ts,tsx}"
],
"serial": true "serial": true
} }
} }

19
src/components/embed.tsx Normal file
View File

@@ -0,0 +1,19 @@
import type { ColorResolvable } from "discord.js"
import type { ReactNode } from "react"
import * as React from "react"
export type EmbedProps = {
color?: ColorResolvable
children?: ReactNode
}
export function Embed(props: EmbedProps) {
return (
<reacord-element
modifyOptions={(options) => {
options.embeds ??= []
options.embeds.push({ color: props.color })
}}
/>
)
}

View File

@@ -1,4 +1,5 @@
import type { Message, MessageOptions, TextBasedChannels } from "discord.js" import type { Message, MessageOptions, TextBasedChannels } from "discord.js"
import type { ReacordElement } from "./element.js"
type Action = type Action =
| { type: "updateMessage"; options: MessageOptions } | { type: "updateMessage"; options: MessageOptions }
@@ -14,7 +15,7 @@ export class ReacordContainer {
this.channel = channel this.channel = channel
} }
render(instances: string[]) { render(instances: ReacordElement[]) {
const messageOptions: MessageOptions = { const messageOptions: MessageOptions = {
content: instances.join("") || undefined, // empty strings are not allowed content: instances.join("") || undefined, // empty strings are not allowed
} }

15
src/element.ts Normal file
View File

@@ -0,0 +1,15 @@
import type { MessageOptions } from "discord.js"
export type ReacordElementJsxTag = "reacord-element"
export type ReacordElement = {
modifyOptions: (options: MessageOptions) => void
}
declare global {
namespace JSX {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface IntrinsicElements
extends Record<ReacordElementJsxTag, ReacordElement> {}
}
}

View File

@@ -1,15 +1,16 @@
/* eslint-disable unicorn/no-null */ /* eslint-disable unicorn/no-null */
import ReactReconciler from "react-reconciler" import ReactReconciler from "react-reconciler"
import type { ReacordContainer } from "./container.js" import type { ReacordContainer } from "./container.js"
import type { ReacordElement, ReacordElementJsxTag } from "./element.js"
import { raise } from "./helpers/raise.js" import { raise } from "./helpers/raise.js"
export const reconciler = ReactReconciler< export const reconciler = ReactReconciler<
unknown, ReacordElementJsxTag,
Record<string, unknown>, ReacordElement,
ReacordContainer, ReacordContainer,
ReacordElement,
string, string,
string, unknown,
string,
unknown, unknown,
unknown, unknown,
unknown, unknown,
@@ -33,12 +34,12 @@ export const reconciler = ReactReconciler<
createInstance: ( createInstance: (
type, type,
properties, props,
rootContainerInstance, rootContainerInstance,
hostContext, hostContext,
internalInstanceHandle, internalInstanceHandle,
) => { ) => {
throw new Error("Not implemented") return props
}, },
createTextInstance: ( createTextInstance: (
@@ -54,24 +55,30 @@ export const reconciler = ReactReconciler<
resetAfterCommit: () => null, resetAfterCommit: () => null,
appendInitialChild: (parent, child) => raise("Not implemented"), appendInitialChild: (parent, child) => raise("Not implemented"),
finalizeInitialChildren: () => raise("Not implemented"), finalizeInitialChildren: (...args) => {
console.log("finalizeInitialChildren", args)
return false
},
getPublicInstance: () => raise("Not implemented"), getPublicInstance: () => raise("Not implemented"),
prepareUpdate: () => raise("Not implemented"), prepareUpdate: () => raise("Not implemented"),
preparePortalMount: () => raise("Not implemented"), preparePortalMount: () => raise("Not implemented"),
createContainerChildSet: (container: ReacordContainer): string[] => { createContainerChildSet: (): ReacordElement[] => {
// console.log("createContainerChildSet", [container]) // console.log("createContainerChildSet", [container])
return [] return []
}, },
appendChildToContainerChildSet: (children: string[], child: string) => { appendChildToContainerChildSet: (
children: ReacordElement[],
child: ReacordElement,
) => {
// console.log("appendChildToContainerChildSet", [children, child]) // console.log("appendChildToContainerChildSet", [children, child])
children.push(child) children.push(child)
}, },
finalizeContainerChildren: ( finalizeContainerChildren: (
container: ReacordContainer, container: ReacordContainer,
children: string[], children: ReacordElement[],
) => { ) => {
// console.log("finalizeContainerChildren", [container, children]) // console.log("finalizeContainerChildren", [container, children])
return false return false
@@ -79,9 +86,9 @@ export const reconciler = ReactReconciler<
replaceContainerChildren: ( replaceContainerChildren: (
container: ReacordContainer, container: ReacordContainer,
children: string[], children: ReacordElement[],
) => { ) => {
// console.log("replaceContainerChildren", [container, children]) console.log("replaceContainerChildren", [container, children])
container.render(children) container.render(children)
}, },
}) })

View File

@@ -1,4 +1,7 @@
{ {
"extends": "@itsmapleleaf/configs/tsconfig.base", "extends": "@itsmapleleaf/configs/tsconfig.base",
"compilerOptions": {
"skipLibCheck": false
},
"exclude": ["node_modules", ".vscode", "dist", "coverage"] "exclude": ["node_modules", ".vscode", "dist", "coverage"]
} }