Browse Source

Refactor comments

imwald
Nuša Pukšič 3 months ago
parent
commit
abb975ec05
  1. 16
      assets/controllers/comments_mercure_controller.js
  2. 39
      src/Twig/Components/Organisms/Comments.php

16
assets/controllers/comments_mercure_controller.js

@ -34,12 +34,12 @@ export default class extends Controller {
this.eventSource = new EventSource(url.toString()); this.eventSource = new EventSource(url.toString());
this.eventSource.onmessage = (event) => { this.eventSource.onmessage = (event) => {
// buffer if live not ready yet const data = JSON.parse(event.data); // { comments, profiles, ... }
if (!this._liveReady) { const live = this._getLiveController();
this._queue.push(event.data); if (live) {
return; live.set('payload', data); // <- updates the writable LiveProp
live.render(); // <- asks server to re-render
} }
this._ingest(event.data);
}; };
} }
@ -55,13 +55,15 @@ export default class extends Controller {
_findLiveRoot() { _findLiveRoot() {
return this.element.closest( return this.element.closest(
'[data-controller~="live"]' '[data-controller~="live"],' +
'[data-controller~="symfony--ux-live-component--live"]'
); );
} }
_getLiveController() { _getLiveController() {
if (!this._liveRoot) return null; if (!this._liveRoot) return null;
return this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live'); return this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live')
|| this.application.getControllerForElementAndIdentifier(this._liveRoot, 'symfony--ux-live-component--live');
} }
_renderWhenReady() { _renderWhenReady() {

39
src/Twig/Components/Organisms/Comments.php

@ -18,6 +18,10 @@ final class Comments
{ {
use DefaultActionTrait; use DefaultActionTrait;
// Writable prop the browser can set
#[LiveProp(writable: true)]
public array $payload = []; // { comments, profiles, ... }
// Live input // Live input
#[LiveProp(writable: false)] #[LiveProp(writable: false)]
public string $current; public string $current;
@ -53,34 +57,19 @@ final class Comments
} }
} }
#[LiveAction]
public function ingest(#[LiveArg('payload')] string $json): void
{
$data = json_decode($json, true) ?: [];
// Validate/normalize as needed
$this->list = $data['comments'] ?? [];
$this->authorsMetadata = $data['profiles'] ?? [];
// If you send these in the event:
$this->zappers = $data['zappers'] ?? [];
$this->zapAmounts = $data['zapAmounts'] ?? [];
$this->commentLinks = $data['commentLinks'] ?? [];
$this->loading = false;
}
/** Expose a view model to the template; keeps all parsing server-side */ /** Expose a view model to the template; keeps all parsing server-side */
public function getPayload(): array public function getPayload(): array
{ {
// Uses the helper we added earlier: getCommentsPayload($coordinate) $payload = $this->payload;
$payload = $this->redisCacheService->getCommentsPayload($this->current) ?? [ if (!$payload) {
'comments' => [], $payload = $this->redisCacheService->getCommentsPayload($this->current) ?? [
'profiles' => [], 'comments' => [],
'zappers' => [], 'profiles' => [],
'zapAmounts' => [], 'zappers' => [],
'commentLinks' => [], 'zapAmounts' => [],
]; 'commentLinks' => [],
];
}
// If your handler doesn’t compute zaps/links yet, reuse your helpers here: // If your handler doesn’t compute zaps/links yet, reuse your helpers here:
$this->list = $payload['comments']; $this->list = $payload['comments'];

Loading…
Cancel
Save