Browse Source

Refactor comments

imwald
Nuša Pukšič 3 months ago
parent
commit
070bee4fb4
  1. 4
      assets/controllers/comments_mercure_controller.js
  2. 15
      src/Twig/Components/Organisms/Comments.php
  3. 1
      templates/components/Organisms/Comments.html.twig

4
assets/controllers/comments_mercure_controller.js

@ -10,9 +10,6 @@ export default class extends Controller {
this.component = await getComponent(this.element); this.component = await getComponent(this.element);
console.log("[comments_mercure] connected to Live Component:", this.component); 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 // Subscribe to Mercure and re-render on each ping
const hubUrl = window.MercureHubUrl || document.querySelector('meta[name="mercure-hub"]')?.content; const hubUrl = window.MercureHubUrl || document.querySelector('meta[name="mercure-hub"]')?.content;
if (!hubUrl) return; if (!hubUrl) return;
@ -24,7 +21,6 @@ export default class extends Controller {
this.es.onmessage = async (msg) => { this.es.onmessage = async (msg) => {
this.component.set('payloadJson', msg.data); this.component.set('payloadJson', msg.data);
this.component.action('loadComments', { 'payload': msg.data }); this.component.action('loadComments', { 'payload': msg.data });
await this.component.render();
}; };
} }

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

@ -60,11 +60,16 @@ final class Comments
#[LiveAction] #[LiveAction]
public function loadComments(#[LiveArg] string $payload): void 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: // If your handler doesn’t compute zaps/links yet, reuse your helpers here:
$this->list = $data->comments; $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->parseZaps(); // your existing method – fills $zapAmounts & $zappers
$this->parseNostrLinks(); // your existing method – fills $commentLinks & $processedContent $this->parseNostrLinks(); // your existing method – fills $commentLinks & $processedContent
@ -78,7 +83,7 @@ final class Comments
private function parseNostrLinks(): void private function parseNostrLinks(): void
{ {
foreach ($this->list as $comment) { foreach ($this->list as $comment) {
$content = $comment->content ?? ''; $content = $comment['content'] ?? '';
if (empty($content)) { if (empty($content)) {
continue; continue;
} }
@ -103,11 +108,11 @@ final class Comments
{ {
foreach ($this->list as $comment) { foreach ($this->list as $comment) {
// check if kind is 9735 to get zaps // check if kind is 9735 to get zaps
if ($comment->kind !== 9735) { if ($comment['kind'] !== 9735) {
continue; continue;
} }
$tags = $comment->tags ?? []; $tags = $comment['tags'] ?? [];
if (empty($tags) || !is_array($tags)) { if (empty($tags) || !is_array($tags)) {
continue; continue;
} }

1
templates/components/Organisms/Comments.html.twig

@ -4,7 +4,6 @@
.add(stimulus_target('comments-mercure', 'loading')) .add(stimulus_target('comments-mercure', 'loading'))
.add(stimulus_target('comments-mercure', 'list')) .add(stimulus_target('comments-mercure', 'list'))
}} }}
id="comments-{{ current|e('html_attr') }}"
class="comments" class="comments"
> >

Loading…
Cancel
Save