FIX: Allow sidebar links to register didInsert actions (#19010)

Previously `this.chatService.appEvents.on(
"chat:user-tracking-state-changed"...)` was registered on constructor and disabled on `willDestroy`. Constructor is evaluated only once, so when the section was collapsed and collapsed then the events were not observed anymore.

didInsert allows evaluating code each time a component is rendered.
This commit is contained in:
Krzysztof Kotlarek 2022-11-14 20:36:46 +11:00 committed by GitHub
parent 895898b363
commit 3d5753c42b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 5 deletions

View File

@ -1,5 +1,5 @@
{{#if this.shouldDisplay}} {{#if this.shouldDisplay}}
<div class="sidebar-section-link-wrapper"> <div class="sidebar-section-link-wrapper" {{did-insert this.didInsert this.args}}>
{{#if @href}} {{#if @href}}
<a href={{@href}} rel="noopener noreferrer" target="_blank" class={{this.classNames}} title={{@title}}> <a href={{@href}} rel="noopener noreferrer" target="_blank" class={{this.classNames}} title={{@title}}>
<Sidebar::SectionLinkPrefix <Sidebar::SectionLinkPrefix

View File

@ -7,6 +7,12 @@ export default class SectionLink extends Component {
} }
} }
didInsert(_element, [args]) {
if (args.didInsert) {
args.didInsert();
}
}
get shouldDisplay() { get shouldDisplay() {
if (this.args.shouldDisplay === undefined) { if (this.args.shouldDisplay === undefined) {
return true; return true;

View File

@ -43,6 +43,7 @@
@hoverAction={{link.hoverAction}} @hoverAction={{link.hoverAction}}
@hoverTitle={{link.hoverTitle}} @hoverTitle={{link.hoverTitle}}
@currentWhen={{link.currentWhen}} @currentWhen={{link.currentWhen}}
@didInsert={{link.didInsert}}
@willDestroy={{link.willDestroy}} @willDestroy={{link.willDestroy}}
@content={{link.text}} /> @content={{link.text}} />
{{/each}} {{/each}}

View File

@ -19,11 +19,12 @@ acceptance("Sidebar - Plugin API", function (needs) {
}); });
needs.hooks.afterEach(() => { needs.hooks.afterEach(() => {
linkDidInsert = undefined;
linkDestroy = undefined; linkDestroy = undefined;
sectionDestroy = undefined; sectionDestroy = undefined;
}); });
let linkDestroy, sectionDestroy; let linkDidInsert, linkDestroy, sectionDestroy;
test("Multiple header actions and links", async function (assert) { test("Multiple header actions and links", async function (assert) {
withPluginApi("1.3.0", (api) => { withPluginApi("1.3.0", (api) => {
@ -117,6 +118,11 @@ acceptance("Sidebar - Plugin API", function (needs) {
return "unread"; return "unread";
} }
@bind
didInsert() {
linkDidInsert = "link test";
}
@bind @bind
willDestroy() { willDestroy() {
linkDestroy = "link test"; linkDestroy = "link test";
@ -201,6 +207,12 @@ acceptance("Sidebar - Plugin API", function (needs) {
await visit("/"); await visit("/");
assert.strictEqual(
linkDidInsert,
"link test",
"calls link didInsert function"
);
assert.strictEqual( assert.strictEqual(
query( query(
".sidebar-section-test-chat-channels .sidebar-section-header-text" ".sidebar-section-test-chat-channels .sidebar-section-header-text"

View File

@ -34,16 +34,19 @@ export default {
super(...arguments); super(...arguments);
this.channel = channel; this.channel = channel;
this.chatService = chatService; this.chatService = chatService;
}
this.chatService.appEvents.on( @bind
willDestroy() {
this.chatService.appEvents.off(
"chat:user-tracking-state-changed", "chat:user-tracking-state-changed",
this._refreshTrackingState this._refreshTrackingState
); );
} }
@bind @bind
willDestroy() { didInsert() {
this.chatService.appEvents.off( this.chatService.appEvents.on(
"chat:user-tracking-state-changed", "chat:user-tracking-state-changed",
this._refreshTrackingState this._refreshTrackingState
); );