mrbbot / slshx

⚔️ Strongly-typed Discord commands on Cloudflare Workers
MIT License
210 stars 9 forks source link

Hooks can't be used in a modal? #5

Open kotx opened 2 years ago

kotx commented 2 years ago

For example, anything that uses useCustomId like useSelect can't be used in a useModal context. Is this intentional (e.x. a constraint of the Discord API) or an oversight?

kotx commented 2 years ago

@mrbbot I still can't get this to work, any tips?

mrbbot commented 2 years ago

Hey! 👋 Apologies for the delayed reply. Yes, this is intentional. All custom IDs need to be declared at the top level so they're the same across calls. Don't worry if you only use them in your useModal callback, they'll just get ignored if you don't use them all the time.

export function cmd() {
  const selectId = useSelectMenu(...);
  const modalId = useModal(() => {
    return (
      <Message>
        <Select id={selectId}>...</Select>
      </Message>
    );
  });

  // Ok to ignore selectId here
  return <Modal id={modalId}>...</Modal>;
}
kotx commented 2 years ago

That makes sense, thanks! Works now.

Also it turns out I can't put a Select in an Embed, so it was outputting [object Object] in the Embed instead of the actual Select element. Might be worthwhile to add a check for this if possible?