refactor: interactive button

This commit is contained in:
MapleLeaf
2021-12-25 01:52:55 -06:00
parent 99430e0edc
commit 18bcf4828c
9 changed files with 145 additions and 93 deletions

View File

@@ -1,12 +1,31 @@
import type { CommandInteraction } from "discord.js"
import type {
Client,
CommandInteraction,
MessageComponentInteraction,
} from "discord.js"
import type { ReactNode } from "react"
import type { OpaqueRoot } from "react-reconciler"
import { reconciler } from "./reconciler.js"
import { RootNode } from "./root-node.js"
import { Renderer } from "./renderer.js"
export class InstanceManager {
private instances = new Set<Instance>()
private constructor() {}
static create(client: Client) {
const manager = new InstanceManager()
client.on("interactionCreate", (interaction) => {
if (!interaction.isMessageComponent()) return
for (const instance of manager.instances) {
if (instance.handleInteraction(interaction)) return
}
})
return manager
}
create(interaction: CommandInteraction) {
const instance = new Instance(interaction)
this.instances.add(instance)
@@ -19,15 +38,19 @@ export class InstanceManager {
}
class Instance {
private rootNode: RootNode
private renderer: Renderer
private container: OpaqueRoot
constructor(interaction: CommandInteraction) {
this.rootNode = new RootNode(interaction)
this.container = reconciler.createContainer(this.rootNode, 0, false, {})
this.renderer = new Renderer(interaction)
this.container = reconciler.createContainer(this.renderer, 0, false, {})
}
render(content: ReactNode) {
reconciler.updateContainer(content, this.container)
}
handleInteraction(interaction: MessageComponentInteraction) {
return this.renderer.handleInteraction(interaction)
}
}