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}}
<div class="sidebar-section-link-wrapper">
<div class="sidebar-section-link-wrapper" {{did-insert this.didInsert this.args}}>
{{#if @href}}
<a href={{@href}} rel="noopener noreferrer" target="_blank" class={{this.classNames}} title={{@title}}>
<Sidebar::SectionLinkPrefix

View File

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

View File

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

View File

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

View File

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