diff --git a/assets/controllers/comments_mercure_controller.js b/assets/controllers/comments_mercure_controller.js index 1ccb4ec..0033a6a 100644 --- a/assets/controllers/comments_mercure_controller.js +++ b/assets/controllers/comments_mercure_controller.js @@ -7,7 +7,6 @@ export default class extends Controller { connect() { this._debounceId = null; this._liveRoot = this._findLiveRoot(); - console.log(this._liveRoot); // If the live controller isn't ready yet, wait for it. if (!this._getLiveController()) { @@ -36,7 +35,15 @@ export default class extends Controller { this.eventSource = new EventSource(url.toString()); this.eventSource.onopen = () => this._debouncedRefresh(50); this.eventSource.onerror = (e) => console.warn("[comments-mercure] EventSource error", e); - this.eventSource.onmessage = () => this._debouncedRefresh(); + this.eventSource.onmessage = (event) => { + console.log("[comments-mercure] Received update", event.data); + const live = this._getLiveController(); + if (!live) return; + + // Send the Mercure payload to the component + // LiveComponent will re-render after the action resolves + live.action('ingest', { payload: event.data }); + }; } disconnect() { @@ -58,11 +65,7 @@ export default class extends Controller { _getLiveController() { if (!this._liveRoot) return null; - // Try both identifiers - return ( - this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live') || - this.application.getControllerForElementAndIdentifier(this._liveRoot, 'symfony--ux-live-component--live') - ); + return this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live'); } _debouncedRefresh(delay = 150) { diff --git a/src/Twig/Components/Organisms/Comments.php b/src/Twig/Components/Organisms/Comments.php index c9301d9..853ab31 100644 --- a/src/Twig/Components/Organisms/Comments.php +++ b/src/Twig/Components/Organisms/Comments.php @@ -8,6 +8,8 @@ use App\Service\RedisCacheService; use Symfony\Component\Messenger\Exception\ExceptionInterface; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; +use Symfony\UX\LiveComponent\Attribute\LiveAction; +use Symfony\UX\LiveComponent\Attribute\LiveArg; use Symfony\UX\LiveComponent\Attribute\LiveProp; use Symfony\UX\LiveComponent\DefaultActionTrait; @@ -51,6 +53,23 @@ 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 */ public function getPayload(): array {