Any way to restrict a theme component to only moderators?

I have developed a theme component specifically for our moderators. This component adds a modal that reminds moderators to consider suspending a user after handling a flag. I placed this component at javascripts/discourse/admin/components/modal/remove-post.gjs, and at the top, it imports AdminUser with import AdminUser from "admin/models/admin-user";.

For moderators, everything works perfectly. However, regular users are experiencing issues. Discourse still tries to load this component for all users, and since regular users don’t load any admin/** modules, it results in the following error:

Error: Could not find module `admin/models/admin-user`

Ideally, I would like this subcomponent (or even just this import) to only load for moderators and admins. Is there an officially supported way to ensure a Glimmer component within a theme component is loaded only for staff (moderators/admins) in Discourse, so that regular users never encounter missing module errors for admin-specific imports?

2 Likes

Right now, the best strategy is to use optionalRequire like this:

We’re doing a lot of work on our JS build systems right now, so there’s a change this will need change in future. If it does, there will be a deprecation message with instructions to upgrade.

3 Likes