From 070bee4fb48d64ab18ff7a2dee7de2209164014c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Sun, 26 Oct 2025 20:46:32 +0100 Subject: [PATCH] Refactor comments --- assets/controllers/comments_mercure_controller.js | 4 ---- src/Twig/Components/Organisms/Comments.php | 15 ++++++++++----- templates/components/Organisms/Comments.html.twig | 1 - 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/assets/controllers/comments_mercure_controller.js b/assets/controllers/comments_mercure_controller.js index 9be4631..fd7f861 100644 --- a/assets/controllers/comments_mercure_controller.js +++ b/assets/controllers/comments_mercure_controller.js @@ -10,9 +10,6 @@ export default class extends Controller { this.component = await getComponent(this.element); console.log("[comments_mercure] connected to Live Component:", this.component); - // Initial render from cache so UI isn’t empty - await this.component.render(); - // Subscribe to Mercure and re-render on each ping const hubUrl = window.MercureHubUrl || document.querySelector('meta[name="mercure-hub"]')?.content; if (!hubUrl) return; @@ -24,7 +21,6 @@ export default class extends Controller { this.es.onmessage = async (msg) => { this.component.set('payloadJson', msg.data); this.component.action('loadComments', { 'payload': msg.data }); - await this.component.render(); }; } diff --git a/src/Twig/Components/Organisms/Comments.php b/src/Twig/Components/Organisms/Comments.php index f7e1526..341ab6f 100644 --- a/src/Twig/Components/Organisms/Comments.php +++ b/src/Twig/Components/Organisms/Comments.php @@ -60,11 +60,16 @@ final class Comments #[LiveAction] public function loadComments(#[LiveArg] string $payload): void { - $data = json_decode($payload); + $data = json_decode($payload, true); // If your handler doesn’t compute zaps/links yet, reuse your helpers here: $this->list = $data->comments; - $this->authorsMetadata = (array)$data->profiles ?? []; + if (empty($this->list)) { + $this->loading = false; + return; + } + + $this->authorsMetadata = $data->profiles ?? []; $this->parseZaps(); // your existing method – fills $zapAmounts & $zappers $this->parseNostrLinks(); // your existing method – fills $commentLinks & $processedContent @@ -78,7 +83,7 @@ final class Comments private function parseNostrLinks(): void { foreach ($this->list as $comment) { - $content = $comment->content ?? ''; + $content = $comment['content'] ?? ''; if (empty($content)) { continue; } @@ -103,11 +108,11 @@ final class Comments { foreach ($this->list as $comment) { // check if kind is 9735 to get zaps - if ($comment->kind !== 9735) { + if ($comment['kind'] !== 9735) { continue; } - $tags = $comment->tags ?? []; + $tags = $comment['tags'] ?? []; if (empty($tags) || !is_array($tags)) { continue; } diff --git a/templates/components/Organisms/Comments.html.twig b/templates/components/Organisms/Comments.html.twig index 5c9c21c..397c0d0 100644 --- a/templates/components/Organisms/Comments.html.twig +++ b/templates/components/Organisms/Comments.html.twig @@ -4,7 +4,6 @@ .add(stimulus_target('comments-mercure', 'loading')) .add(stimulus_target('comments-mercure', 'list')) }} - id="comments-{{ current|e('html_attr') }}" class="comments" >