From 0bc1e4c88266bd14c538791bbb2d33000aad1bbf Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 17 Oct 2024 16:55:32 +1300 Subject: [PATCH 1/5] add react hook for updating, fix type to be record --- index.tsx | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/index.tsx b/index.tsx index ada4bc1..21f2ee9 100644 --- a/index.tsx +++ b/index.tsx @@ -27,8 +27,7 @@ type FlagRegistryEntry = { emoji: string; }; -// worst typing ever? -const flagRegistry: { [key in FlagType]: FlagRegistryEntry } = { +const flagRegistry: Record = { [FlagType.DANGER]: { label: "Danger", color: "#ff7473", @@ -57,14 +56,22 @@ type Flag = { text: string; }; +const subscribers = new Set<() => void>(); +function subscribe(callback: () => void) { + subscribers.add(callback); + return () => subscribers.delete(callback); +}; + function Flag({ id }: { id: string; }) { + const flag = React.useSyncExternalStore(subscribe, () => userFlags.get(id)); + if (!flag) return null; return (
- {Parser.parse(flagRegistry[userFlags.get(id)?.type || ""]?.emoji)} {userFlags.get(id)?.text} + {Parser.parse(flagRegistry[flag.type].emoji)} {flag.text}
); @@ -84,12 +91,9 @@ export default definePlugin({ userFlags = new Map(savedFlags); } } - addAccessory("flag", (props: Record) => { - if (userFlags.has((props.message as Message).author.id)) { - return ; - } - return null; - }, 4); + addAccessory("flag", (props: Record) => ( + + ), 4); }, commands: [ { @@ -130,6 +134,7 @@ export default definePlugin({ type, text }); + subscribers.forEach(cb => cb()); sendBotMessage(ctx.channel.id, { content: `Flag set on <@${user}> with content \`${text}\`!` }); @@ -152,6 +157,7 @@ export default definePlugin({ execute: async (args: Argument[], ctx: CommandContext) => { const user = findOption(args, "user", ""); userFlags.delete(user); + subscribers.forEach(cb => cb()); sendBotMessage(ctx.channel.id, { content: `Flag removed from <@${user}>` }); From bffa2a128eea6c53ca9f28093b9ca7698cd12f63 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 17 Oct 2024 16:57:15 +1300 Subject: [PATCH 2/5] remove as FlagRegistryEntry --- index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.tsx b/index.tsx index 21f2ee9..5f610cc 100644 --- a/index.tsx +++ b/index.tsx @@ -32,17 +32,17 @@ const flagRegistry: Record = { label: "Danger", color: "#ff7473", emoji: "🛑" - } as FlagRegistryEntry, + }, [FlagType.WARNING]: { label: "Warning", color: "#ffb02e", emoji: "⚠ī¸" - } as FlagRegistryEntry, + }, [FlagType.INFO]: { label: "Info", color: "#62a8ff", emoji: "đŸ”ĩ" - } as FlagRegistryEntry, + }, [FlagType.POSITIVE]: { label: "Positive", color: "#62ff74", From 3b7c752adb432d82441e4d10805d0255394aa933 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 17 Oct 2024 17:42:42 +1300 Subject: [PATCH 3/5] cleanup --- index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.tsx b/index.tsx index 5f610cc..5da955a 100644 --- a/index.tsx +++ b/index.tsx @@ -88,7 +88,7 @@ export default definePlugin({ if (typeof savedFlags === "string") { userFlags = new Map(JSON.parse(savedFlags)); } else { - userFlags = new Map(savedFlags); + userFlags = new Map(savedFlags); } } addAccessory("flag", (props: Record) => ( @@ -108,9 +108,9 @@ export default definePlugin({ required: true }, { - "name": "type", - "type": ApplicationCommandOptionType.STRING, - "description": "The type of flag to add", + name: "type", + type: ApplicationCommandOptionType.STRING, + description: "The type of flag to add", choices: Object.entries(flagRegistry).map(([key, flag]) => ({ name: key, label: flag.label, From c91e21e2f58c42ac4b10e11b0889087711bd8ca5 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 17 Oct 2024 17:51:55 +1300 Subject: [PATCH 4/5] even more cleanup --- index.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.tsx b/index.tsx index 5da955a..4b2ecbd 100644 --- a/index.tsx +++ b/index.tsx @@ -10,7 +10,6 @@ import { addAccessory } from "@api/MessageAccessories"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; import { Parser, React, Text } from "@webpack/common"; -import { Message } from "discord-types/general"; let userFlags = new Map(); @@ -73,7 +72,7 @@ function Flag({ id }: { id: string; }) { > {Parser.parse(flagRegistry[flag.type].emoji)} {flag.text} - + ); } From b6c834f062076acbb2df47ee000b4a05af134f81 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 17 Oct 2024 17:58:11 +1300 Subject: [PATCH 5/5] update info icon --- index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.tsx b/index.tsx index 4b2ecbd..f34694f 100644 --- a/index.tsx +++ b/index.tsx @@ -40,7 +40,7 @@ const flagRegistry: Record = { [FlagType.INFO]: { label: "Info", color: "#62a8ff", - emoji: "đŸ”ĩ" + emoji: "ℹī¸" }, [FlagType.POSITIVE]: { label: "Positive",