add react hook for updating, fix type to be record
This commit is contained in:
parent
2f8fbbb03e
commit
0bc1e4c882
1 changed files with 16 additions and 10 deletions
26
index.tsx
26
index.tsx
|
@ -27,8 +27,7 @@ type FlagRegistryEntry = {
|
||||||
emoji: string;
|
emoji: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
// worst typing ever?
|
const flagRegistry: Record<FlagType, FlagRegistryEntry> = {
|
||||||
const flagRegistry: { [key in FlagType]: FlagRegistryEntry } = {
|
|
||||||
[FlagType.DANGER]: {
|
[FlagType.DANGER]: {
|
||||||
label: "Danger",
|
label: "Danger",
|
||||||
color: "#ff7473",
|
color: "#ff7473",
|
||||||
|
@ -57,14 +56,22 @@ type Flag = {
|
||||||
text: string;
|
text: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const subscribers = new Set<() => void>();
|
||||||
|
function subscribe(callback: () => void) {
|
||||||
|
subscribers.add(callback);
|
||||||
|
return () => subscribers.delete(callback);
|
||||||
|
};
|
||||||
|
|
||||||
function Flag({ id }: { id: string; }) {
|
function Flag({ id }: { id: string; }) {
|
||||||
|
const flag = React.useSyncExternalStore(subscribe, () => userFlags.get(id));
|
||||||
|
if (!flag) return null;
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Text
|
<Text
|
||||||
variant="text-md/bold"
|
variant="text-md/bold"
|
||||||
style={{ color: flagRegistry[userFlags.get(id)?.type || ""]?.color }}
|
style={{ color: flagRegistry[flag.type].color }}
|
||||||
>
|
>
|
||||||
{Parser.parse(flagRegistry[userFlags.get(id)?.type || ""]?.emoji)} {userFlags.get(id)?.text}
|
{Parser.parse(flagRegistry[flag.type].emoji)} {flag.text}
|
||||||
</Text>
|
</Text>
|
||||||
</div >
|
</div >
|
||||||
);
|
);
|
||||||
|
@ -84,12 +91,9 @@ export default definePlugin({
|
||||||
userFlags = new Map(savedFlags);
|
userFlags = new Map(savedFlags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addAccessory("flag", (props: Record<string, any>) => {
|
addAccessory("flag", (props: Record<string, any>) => (
|
||||||
if (userFlags.has((props.message as Message).author.id)) {
|
<Flag id={props.message.author.id} />
|
||||||
return <Flag id={props.message.author.id} />;
|
), 4);
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}, 4);
|
|
||||||
},
|
},
|
||||||
commands: [
|
commands: [
|
||||||
{
|
{
|
||||||
|
@ -130,6 +134,7 @@ export default definePlugin({
|
||||||
type,
|
type,
|
||||||
text
|
text
|
||||||
});
|
});
|
||||||
|
subscribers.forEach(cb => cb());
|
||||||
sendBotMessage(ctx.channel.id, {
|
sendBotMessage(ctx.channel.id, {
|
||||||
content: `Flag set on <@${user}> with content \`${text}\`!`
|
content: `Flag set on <@${user}> with content \`${text}\`!`
|
||||||
});
|
});
|
||||||
|
@ -152,6 +157,7 @@ export default definePlugin({
|
||||||
execute: async (args: Argument[], ctx: CommandContext) => {
|
execute: async (args: Argument[], ctx: CommandContext) => {
|
||||||
const user = findOption(args, "user", "");
|
const user = findOption(args, "user", "");
|
||||||
userFlags.delete(user);
|
userFlags.delete(user);
|
||||||
|
subscribers.forEach(cb => cb());
|
||||||
sendBotMessage(ctx.channel.id, {
|
sendBotMessage(ctx.channel.id, {
|
||||||
content: `Flag removed from <@${user}>`
|
content: `Flag removed from <@${user}>`
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue