diff --git a/src/Twig/Components/ReadingListDraftComponent.php b/src/Twig/Components/ReadingListDraftComponent.php index c071120..bd4267b 100644 --- a/src/Twig/Components/ReadingListDraftComponent.php +++ b/src/Twig/Components/ReadingListDraftComponent.php @@ -3,10 +3,16 @@ namespace App\Twig\Components; use App\Dto\CategoryDraft; +use App\Enum\KindsEnum; +use App\Service\NostrClient; +use nostriphant\NIP19\Bech32; +use nostriphant\NIP19\Data\NAddr; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\LiveAction; use Symfony\UX\LiveComponent\Attribute\LiveListener; +use Symfony\UX\LiveComponent\Attribute\LiveProp; use Symfony\UX\LiveComponent\DefaultActionTrait; #[AsLiveComponent] @@ -16,9 +22,20 @@ final class ReadingListDraftComponent public ?CategoryDraft $draft = null; - public function __construct(private readonly RequestStack $requestStack) - { - } + #[LiveProp(writable: true)] + public string $naddrInput = ''; + + #[LiveProp] + public string $naddrError = ''; + + #[LiveProp] + public string $naddrSuccess = ''; + + public function __construct( + private readonly RequestStack $requestStack, + private readonly NostrClient $nostrClient, + private readonly LoggerInterface $logger, + ) {} public function mount(): void { @@ -43,6 +60,81 @@ final class ReadingListDraftComponent } } + #[LiveAction] + public function addNaddr(): void + { + $this->naddrError = ''; + $this->naddrSuccess = ''; + $raw = trim($this->naddrInput); + if ($raw === '') { + $this->naddrError = 'Empty input.'; + return; + } + + // Extract naddr (accept nostr:naddr1... or raw naddr1...) + if (preg_match('/(naddr1[0-9a-zA-Z]+)/', $raw, $m) !== 1) { + $this->naddrError = 'No naddr found.'; + return; + } + $naddr = $m[1]; + + try { + $decoded = new Bech32($naddr); + if ($decoded->type !== 'naddr') { + $this->naddrError = 'Invalid naddr type.'; + return; + } + /** @var NAddr $data */ + $data = $decoded->data; + $slug = $data->identifier; + $pubkey = $data->pubkey; + $kind = $data->kind; + $relays = $data->relays; + + if ($kind !== KindsEnum::LONGFORM->value) { + $this->naddrError = 'Not a long-form article (kind '.$kind.').'; + return; + } + if (!$slug) { + $this->naddrError = 'Missing identifier (slug).'; + return; + } + + $coordinate = $kind . ':' . $pubkey . ':' . $slug; + + $session = $this->requestStack->getSession(); + $draft = $session->get('read_wizard'); + if (!$draft instanceof CategoryDraft) { + $draft = new CategoryDraft(); + } + if (!in_array($coordinate, $draft->articles, true)) { + // Attempt to fetch article so it exists locally (best-effort) + try { + $this->nostrClient->getLongFormFromNaddr($slug, $relays, $pubkey, $kind); + } catch (\Throwable $e) { + // Non-fatal; still add coordinate + $this->logger->warning('Failed fetching article from naddr', [ + 'error' => $e->getMessage(), + 'naddr' => $naddr + ]); + } + $draft->articles[] = $coordinate; + $session->set('read_wizard', $draft); + $this->draft = $draft; + $this->naddrSuccess = 'Added article: ' . $coordinate; + } else { + $this->naddrSuccess = 'Article already in list.'; + } + $this->naddrInput = ''; + } catch (\Throwable $e) { + $this->naddrError = 'Decode failed.'; + $this->logger->error('naddr decode failed', [ + 'input' => $raw, + 'error' => $e->getMessage() + ]); + } + } + private function reloadFromSession(): void { $session = $this->requestStack->getSession(); diff --git a/templates/components/ReadingListDraftComponent.html.twig b/templates/components/ReadingListDraftComponent.html.twig index d770ac5..c00099e 100644 --- a/templates/components/ReadingListDraftComponent.html.twig +++ b/templates/components/ReadingListDraftComponent.html.twig @@ -4,7 +4,7 @@ {% if draft.summary %}

{{ draft.summary }}

{% endif %} -

Articles

+

Articles

{% if draft.articles is not empty %} {% else %} -

No articles yet. Use search to add some.

+

No articles yet. Use search or paste an naddr to add some.

{% endif %} +
+
+ +
+ +
+
+ {% if naddrError %}
{{ naddrError }}
{% endif %} + {% if naddrSuccess %}
{{ naddrSuccess }}
{% endif %} +
+
Review & Sign
- diff --git a/templates/components/UserMenu.html.twig b/templates/components/UserMenu.html.twig index 155271a..57e24e7 100644 --- a/templates/components/UserMenu.html.twig +++ b/templates/components/UserMenu.html.twig @@ -14,9 +14,9 @@
  • Write Article
  • -
  • - Compose List -
  • +{#
  • #} +{# Compose List#} +{#
  • #} {% if is_granted('ROLE_ADMIN') %}
  • Create Magazine diff --git a/templates/home.html.twig b/templates/home.html.twig index de6b6a7..49e30f1 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -36,18 +36,18 @@ -
    -
    -

    Reading Lists

    -

    for collections, curations, courses and more

    -
    -
    -

    Create ordered reading lists. Add more articles, reorder, republish.

    - -
    -
    +{#
    #} +{#
    #} +{#

    Reading Lists

    #} +{#

    for collections, curations, courses and more

    #} +{#
    #} +{#
    #} +{#

    Create ordered reading lists. Add more articles, reorder, republish.

    #} +{#
    #} +{# Start a list#} +{#
    #} +{#
    #} +{#
    #}
    diff --git a/templates/layout.html.twig b/templates/layout.html.twig index 35ba9e3..a60e0e5 100644 --- a/templates/layout.html.twig +++ b/templates/layout.html.twig @@ -11,9 +11,9 @@
  • Newsstand
  • -
  • - Lists -
  • +{#
  • #} +{# Lists#} +{#
  • #}
  • {{ 'heading.search'|trans }}
  • diff --git a/templates/pages/category.html.twig b/templates/pages/category.html.twig index f45f93c..5c554e5 100644 --- a/templates/pages/category.html.twig +++ b/templates/pages/category.html.twig @@ -10,6 +10,7 @@ {% block body %} - - +
    + +
    {% endblock %} diff --git a/templates/reading_list/compose.html.twig b/templates/reading_list/compose.html.twig index ff86169..454e034 100644 --- a/templates/reading_list/compose.html.twig +++ b/templates/reading_list/compose.html.twig @@ -1,17 +1,17 @@ {% extends 'layout.html.twig' %} {% block body %} -

    Compose Reading List

    +
    +
    +

    Compose Reading List

    +
    +
    -
    +
    +
    +

    Search articles and click “Add to list”.

    + +
    {% endblock %} - -{% block aside %} -
    -

    Search & Add

    -

    Search articles and click “Add to list”.

    - -
    -{% endblock %} diff --git a/templates/reading_list/reading_articles.html.twig b/templates/reading_list/reading_articles.html.twig index 7d9b722..051524f 100644 --- a/templates/reading_list/reading_articles.html.twig +++ b/templates/reading_list/reading_articles.html.twig @@ -23,7 +23,7 @@
    -
    +
    Back Cancel diff --git a/templates/static/landing.html.twig b/templates/static/landing.html.twig index a836a14..48ff97b 100644 --- a/templates/static/landing.html.twig +++ b/templates/static/landing.html.twig @@ -36,18 +36,18 @@
    -
    -
    -

    Reading Lists

    -

    for collections, curations, courses and more

    -
    -
    -

    Create ordered reading lists. Add more articles, reorder, republish.

    -
    - Browse -
    -
    -
    +{#
    #} +{#
    #} +{#

    Reading Lists

    #} +{#

    for collections, curations, courses and more

    #} +{#
    #} +{#
    #} +{#

    Create ordered reading lists. Add more articles, reorder, republish.

    #} +{#
    #} +{# Browse#} +{#
    #} +{#
    #} +{#
    #}