discourse/app/assets/javascripts/admin/addon/components/webhook-events.gjs
Kelv 7b062e24de
DEV: refactor load-more component to glimmer and use intersection observer (#32285)
This PR makes the following key changes to the load-more component:

* Updating to a Glimmer component
* Changing from an eyeline/scrolling-based mechanism to an
IntersectionObserver to determine when to load
* Keeping track of a single invisible sentinel element to help trigger
the loadMore action instead of having to find and track the last item of
the collection upon every load
* The component can now be used without wrapping around some content to
be yielded - the intent is to use this for cases like
[DiscoveryTopicsList](f0057c7353/app/assets/javascripts/discourse/app/components/discovery/topics.gjs (L222))
where we might want more precise placement of the sentinel element.
* Added utility toggle functions to control observer behaviour in this
class for testing

We will replace the load-more mixin in DiscoveryTopicsList in another
PR.



https://github.com/user-attachments/assets/50d9763f-b5f8-40f6-8630-41bdf107baf7


### Technical Considerations
1. Keeping track of a single sentinel element simplifies the logic
greatly and is also more robust to changes in the collection that's
being loaded. (ref: a [previous
commit](https://github.com/discourse/discourse/pull/32285/commits/2279519081eef9649864453c90d72dbb2bd8970c)
that was following the previous approach of tracking specifically the
last item of the loaded collection); this also sidesteps odd edge cases
like if the tracked element is larger than the entire viewport.
2. Using
[isIntersecting](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/isIntersecting)
instead of calculating manually whether the element is in the viewport
is also less flaky - I ran into issues with the boundingClientRect
inconsistently being calculated as outside the viewport on different
sized screens.
3. We need to properly bind `loadMore` functions with the action
decorator, otherwise the way we pass the loadMore callbacks through to
the observe-intersection modifier results in attempting to call it on
the loadMore component context instead. I've done this for all such
functions except for the one in
[`category-list`](0ed4b09527/app/assets/javascripts/discourse/app/models/category-list.js (L117))
which uses `@bind` that should be equivalent in terms of binding to the
correct `this`.
2025-04-28 10:22:35 +08:00

282 lines
7.8 KiB
Text

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { fn, hash } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { gt, readOnly } from "@ember/object/computed";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
import { service } from "@ember/service";
import { not } from "truth-helpers";
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
import CountI18n from "discourse/components/count-i18n";
import DButton from "discourse/components/d-button";
import LoadMore from "discourse/components/load-more";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
import { i18n } from "discourse-i18n";
import WebhookEvent from "admin/components/webhook-event";
import ComboBox from "select-kit/components/combo-box";
export default class WebhookEvents extends Component {
@service messageBus;
@service store;
@service dialog;
@tracked pingEnabled = true;
@tracked events = [];
@tracked incomingEventIds = [];
@tracked redeliverEnabled = true;
@readOnly("incomingEventIds.length") incomingCount;
@gt("incomingCount", 0) hasIncoming;
constructor() {
super(...arguments);
this.loadEvents();
}
async loadEvents() {
this.loading = true;
try {
this.events = await this.store.findAll("web-hook-event", {
webhookId: this.args.webhookId,
status: this.args.status,
});
} catch (error) {
popupAjaxError(error);
} finally {
this.loading = false;
}
this.redeliverEnabled = this.failedEventIds.length;
}
get failedEventIds() {
return this.events.content
.filter(
(event) =>
(event.status < 200 || event.status > 299) && event.status !== 0
)
.map((event) => event.id);
}
get statuses() {
return [
{
id: "successful",
name: i18n("admin.web_hooks.events.filter_status.successful"),
},
{
id: "failed",
name: i18n("admin.web_hooks.events.filter_status.failed"),
},
];
}
@bind
reloadEvents() {
if (this.loading) {
return;
}
this.loadEvents();
}
@bind
subscribe() {
const channel = `/web_hook_events/${this.args.webhookId}`;
this.messageBus.subscribe(channel, this._addIncoming);
}
@bind
unsubscribe() {
this.messageBus.unsubscribe("/web_hook_events/*", this._addIncoming);
}
@bind
_addIncoming(data) {
if (data.event_type === "ping") {
this.pingEnabled = true;
}
if (data.type === "redelivered") {
const event = this.events.find((e) => e.id === data.web_hook_event.id);
event.setProperties({
response_body: data.web_hook_event.response_body,
response_headers: data.web_hook_event.response_headers,
status: data.web_hook_event.status,
redelivering: false,
});
return;
}
if (data.type === "redelivery_failed") {
const event = this.events.find((e) => e.id === data.web_hook_event_id);
event.set("redelivering", false);
return;
}
if (!this.incomingEventIds.includes(data.web_hook_event_id)) {
this.incomingEventIds.pushObject(data.web_hook_event_id);
}
}
@action
async showInserted(event) {
event?.preventDefault();
const path = `/admin/api/web_hooks/${this.args.webhookId}/events/bulk`;
const data = await ajax(path, {
data: { ids: this.incomingEventIds },
});
const objects = data.map((webhookEvent) =>
this.store.createRecord("web-hook-event", webhookEvent)
);
this.events.unshiftObjects(objects);
this.incomingEventIds = [];
}
@action
loadMore() {
this.events.loadMore();
}
@action
async ping() {
this.pingEnabled = false;
try {
await ajax(`/admin/api/web_hooks/${this.args.webhookId}/ping`, {
type: "POST",
});
} catch (error) {
this.pingEnabled = true;
popupAjaxError(error);
}
}
@action
async redeliverFailed() {
if (!this.failedEventIds.length) {
this.dialog.alert(i18n("admin.web_hooks.events.no_events_to_redeliver"));
this.redeliverEnabled = false;
return;
}
return this.dialog.yesNoConfirm({
message: i18n("admin.web_hooks.events.redeliver_failed_confirm", {
count: this.failedEventIds.length,
}),
didConfirm: async () => {
try {
const response = await ajax(
`/admin/api/web_hooks/${this.args.webhookId}/events/failed_redeliver`,
{ type: "POST", data: { event_ids: this.failedEventIds } }
);
if (response.event_ids?.length) {
response.event_ids.map((id) => {
const event = this.events.find((e) => e.id === id);
event.set("redelivering", true);
});
} else {
this.dialog.alert(
i18n("admin.web_hooks.events.no_events_to_redeliver")
);
}
} catch (error) {
popupAjaxError(error);
} finally {
this.redeliverEnabled = false;
}
},
});
}
<template>
<div
class="web-hook-events-listing"
{{didInsert this.subscribe}}
{{didUpdate this.reloadEvents @status}}
{{willDestroy this.unsubscribe}}
>
<div class="web-hook-events-actions">
<ComboBox
@value={{@status}}
@content={{this.statuses}}
@onChange={{fn (mut @status)}}
@options={{hash none="admin.web_hooks.events.filter_status.all"}}
class="delivery-status-filters"
/>
<DButton
@icon="arrows-rotate"
@label="admin.web_hooks.events.redeliver_failed"
@action={{this.redeliverFailed}}
@disabled={{not this.redeliverEnabled}}
/>
<DButton
@icon="paper-plane"
@label="admin.web_hooks.events.ping"
@action={{this.ping}}
@disabled={{not this.pingEnabled}}
class="webhook-events__ping-button"
/>
</div>
{{#if this.events}}
<LoadMore @action={{this.loadMore}}>
<div class="web-hook-events content-list">
<div class="heading-container">
<div class="col heading first status">
{{i18n "admin.web_hooks.events.status"}}
</div>
<div class="col heading event-id">
{{i18n "admin.web_hooks.events.event_id"}}
</div>
<div class="col heading timestamp">
{{i18n "admin.web_hooks.events.timestamp"}}
</div>
<div class="col heading completion">
{{i18n "admin.web_hooks.events.completion"}}
</div>
<div class="col heading actions">
{{i18n "admin.web_hooks.events.actions"}}
</div>
</div>
{{#if this.hasIncoming}}
<a
href
tabindex="0"
{{on "click" this.showInserted}}
class="alert alert-info clickable"
>
<CountI18n
@key="admin.web_hooks.events.incoming"
@count={{this.incomingCount}}
/>
</a>
{{/if}}
<ul>
{{#each this.events as |event|}}
<WebhookEvent @event={{event}} />
{{/each}}
</ul>
</div>
<ConditionalLoadingSpinner @condition={{this.events.loadingMore}} />
</LoadMore>
{{else}}
<p>{{i18n "admin.web_hooks.events.none"}}</p>
{{/if}}
</div>
</template>
}