Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Add compatibility with the Glimmer post menu #310

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

megothss
Copy link

@megothss megothss commented Sep 13, 2024

const ReactionsActionButton = <template>
<MountWidget
@widget="discourse-reactions-actions"
@args={{hash post=@post}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to add the comment so that ember-template-lint ignores this

(it's not worth us spending the time to update <MountWidget, since it'll hopefully be removed in the next few months)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the comments

static extraControls = true;

static shouldRender(args) {
const site = getOwner(this).lookup("service:site");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to fail? In static methods, this refers to the Class itself (not the instance), so it won't have an owner 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, I did want to set the owner on context arg, so that this would work:

static shouldRender(args, context) {
  getOwner(context)

I think I have this core change stashed somewhere 😅

we could make this work too/instead but using context is less surprising, I think? (though we use getOwner(this) almost anywhere so I see why having the same "incantation" here could be nice)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup agreed - adding the owner on context and/or args makes sense. I'd prefer that to doing anything unusual with this.. (I don't think we should be setting owners on Klass objects - they persist beyond the lifetime of the ApplicationInstance)

BTW, worth noting that this particular case, it's not a plugin outlet. We're just using a similar shouldRender pattern for the new post-menu. But still, we should aim for the same ownership approach for this and for plugin outlets 💯

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, worth noting that this particular case, it's not a plugin outlet.

Ah, missed that fact 😃

I don't think we should be setting owners on Klass objects - they persist beyond the lifetime of the ApplicationInstance

I wouldn't be setting owner on a class itself but doing something like:

setOwner(context, owner);
shouldRender.call(context, args, context);

// vs `shouldRender(args, context);`
// which btw, means `this` currently is `window` I think?

Copy link
Member

@davidtaylorhq davidtaylorhq Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// which btw, means this currently is window I think?

In a static method, this. refers to the class itself. So you can do stuff like

class Blah {
  static shouldRender(){
    return this.someMethod();
  }
  static someMethod(){
    return true;
  }
}

so if we use .call/.bind/.apply to change the this. value, it'll break this fundamental functionality of static methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it in the plugin outlet context. This is the way we call shouldRender:

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-connectors.js#L229-L230

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh.... Then I think we should fix is so it works as a normal static method 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API is now receiving the owner as an explicit parameter and the variables match the (args, context, owner) signature for consistency

Comment on lines +197 to +200
const silencedKey =
transformerRegistered && "discourse.post-menu-widget-overrides";

withSilencedDeprecations(silencedKey, () => customizeWidgetPostMenu(api));
Copy link
Member

@davidtaylorhq davidtaylorhq Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! We should make sure to document this pattern.

Does silencing the deprecation also stop it from affecting the 'glimmer_post_menu_mode=auto' system?

We also need to think about how we'll notify developers to remove the old logic, once the widget menu is removed. I guess at that point, we could introduce a new deprecation with a different name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Silencing the deprecations stops them triggering the legacy mode.

I guess introducing a new deprecation may be a good idea on this case. Or we can log an error message in the console like we did in the header.

const currentUser = container.lookup("service:current-user");

const transformerRegistered =
currentUser?.use_glimmer_post_menu &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta think about what will happen to this logic after we remove the use_glimmer_post_menu boolean in future 🤔

Could we just register the transformer unconditionally? It'll be a no-op if the user/site is still using the widget implementation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is not needed anymore.

This was from an earlier commit of the API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants