Live Update Remote
==================

Live Update Remote turns a remote JSON API into a live-updating Drupal block.
It is intended for site builders who need simple dashboards for values such as
currencies, crypto prices, stock quotes, weather, status pages or public data
APIs.

The browser never calls the third-party API directly. Drupal fetches the JSON,
caches it, renders a table and lets Live Update refresh the placed block.

Setup
-----

1. Enable the submodule:

       vendor/bin/drush en liveupdate_remote -y

2. Add a source at `/admin/config/services/liveupdate/remote`.

3. Enter the JSON URL and click `Fetch preview`. Drupal fetches the endpoint,
   detects likely rows and columns, and shows a table preview. Adjust the
   suggested mapping only when needed.

4. Place the `Live Update remote table` block at `/admin/structure/block` and
   select the source.

5. Enable `Update this block live` in the same block form.

Quick test API
--------------

Use this URL to test without an API key:

    https://api.frankfurter.app/latest?from=USD&to=BRL,EUR,GBP

Click `Fetch preview`. The suggested mapping should be:

    Rows path: rates

    Key | _key
    Value | _value

This renders one row per currency.

Source fields
-------------

* JSON URL: a public HTTP or HTTPS endpoint Drupal can reach server-side.
  Localhost, private IP ranges, reserved IP ranges and oversized responses are
  blocked to reduce SSRF and provider-abuse risk. Redirects are followed only
  when each target also passes the same public URL validation, and redirect
  chains are capped.
* Rows path: dot-separated path to the array of rows. Preview fills this
  automatically when it can. Leave empty when the whole response is the row
  array.
* Cache TTL: minimum 60 seconds. This protects third-party APIs from per-user
  polling traffic and from manual refresh abuse.
* Row limit: maximum number of rows rendered.
* Columns: one per line using `Label | JSON path`. Preview suggests these
  automatically. Associative JSON objects may use `_key` and `_value`.

Display options
---------------

Each remote source controls its own block display:

* Refresh indicator:
  * Text countdown.
  * Animated progress bar.
  * Animated donut.
  * Hidden.
* Show refresh icon: adds a discreet manual refresh button.
* Refresh status position: above or below the table.
* Display style:
  * Compact card: provides a simple plug-and-play visual style.
  * Plain table: keeps the output minimal for theme control.

Placed blocks include a small refresh status area by default: a countdown and a
discreet refresh icon below the table. Each source can configure the refresh
indicator as text, animated progress bar, animated donut or hidden. It can also
configure whether to show the refresh icon, whether the status appears above or
below the table, and whether to use the compact base style or a plain table.
Saving a source clears its cached response so the next block refresh uses the
latest mapping.

The refresh icon triggers the same lightweight fragment refresh used by the
automatic interval. It refreshes the Drupal block fragment, but it does not
bypass the remote JSON cache. Visitors do not call the third-party API directly;
Drupal serves the cached rendered table until the TTL expires.

The indicator shows the remaining lifetime of the shared Drupal cache item. If
the cache TTL is 300 seconds and a visitor opens the page when the cached data
has 45 seconds left, the block starts at 45 seconds, not 300. This keeps all
visitors aligned with the same protected provider fetch cycle.

JSON mapping
------------

Most site builders should use `Fetch preview` and keep the suggested values.
When manual adjustment is needed:

* Use dot notation for nested values, for example `price.usd`.
* Leave `Rows path` empty when the whole JSON response is an array of rows.
* Use `Rows path` when rows live inside a property, for example `data.items`.
* Use `_key` for the key of an associative JSON object.
* Use `_value` for the value of an associative JSON object.

Examples:

    Name | name
    Email | email
    City | address.city

    Coin | _key
    Price | _value

The first example works with JSON arrays such as:

    https://jsonplaceholder.typicode.com/users

The second example works with associative JSON objects such as currency rates.

Theming and customization
-------------------------

The final block markup is rendered by
`templates/liveupdate-remote-table.html.twig`. Themes can override this template
to take full control of the table, status area and refresh button markup.

To override it, copy the template to your theme and clear caches:

    themes/custom/MY_THEME/templates/liveupdate-remote-table.html.twig

Useful template variables:

* `attributes`: wrapper attributes.
* `header`: table header labels.
* `rows`: mapped table rows.
* `empty`: empty message.
* `status_position`: `top` or `bottom`.
* `indicator`: `text`, `bar`, `donut` or `hidden`.
* `show_refresh`: whether the refresh button is enabled.
* `refresh_label`: accessible label for the refresh button.
* `refresh_icon`: visible refresh icon.
* `ttl`: refresh interval in seconds.
* `remaining_ttl`: current remaining cache lifetime in seconds.

Useful CSS classes:

* `.liveupdate-remote`
* `.liveupdate-remote--compact`
* `.liveupdate-remote--plain`
* `.liveupdate-remote--status-top`
* `.liveupdate-remote--status-bottom`
* `.liveupdate-remote--indicator-text`
* `.liveupdate-remote--indicator-bar`
* `.liveupdate-remote--indicator-donut`
* `.liveupdate-remote__table`
* `.liveupdate-remote__status`
* `.liveupdate-remote__countdown`
* `.liveupdate-remote__bar`
* `.liveupdate-remote__bar-fill`
* `.liveupdate-remote__donut`
* `.liveupdate-remote__refresh`

Useful data attributes:

* `data-liveupdate-remote`: wrapper marker.
* `data-liveupdate-remote-url`: Drupal fragment URL.
* `data-liveupdate-remote-ttl`: refresh interval.
* `data-liveupdate-remote-remaining`: remaining cache lifetime.
* `data-liveupdate-remote-fragment`: rendered fragment marker.
* `data-liveupdate-remote-countdown`: text updated by JavaScript.
* `data-liveupdate-remote-progress`: progress bar or donut updated by
  JavaScript.
* `data-liveupdate-remote-refresh`: manual refresh trigger.

The remote block uses a small JavaScript fetcher for its own Drupal fragment
route. Theme overrides should keep the refresh button data attribute when manual
refresh is desired.

Cache and rate limits
---------------------

Remote responses are cached by Drupal using the source cache TTL. The minimum
TTL is 60 seconds to avoid turning every visitor into direct API traffic. Saving
a source clears that source cache, so changed mappings are visible on the next
block refresh.

The admin preview also uses a short 60 second cache per URL. This keeps repeated
`Fetch preview` clicks from hammering the provider while the site builder tunes
the mapping.

Limitations
-----------

The first version supports GET and JSON only. Complex authentication, OAuth and
API-specific integrations should be added later or handled by API-specific
modules. Remote URLs must resolve to public hosts; this submodule is not intended
to expose internal network services.
