{{ define "title" }}Timeline &middot; Tangled{{ end }}

{{ define "extrameta" }}
    <meta property="og:title" content="Timeline · Tangled" />
    <meta property="og:type" content="object" />
    <meta property="og:url" content="https://tangled.org" />
    <meta property="og:description" content="The next-generation social coding platform" />
{{ end }}

{{ define "mainLayout" }}
<div class="flex-grow">
    <div class="max-w-screen-lg mx-auto flex flex-col gap-4">
        <main>{{ block "content" . }}{{ end }}</main>
    </div>
</div>
{{ end }}

{{ define "content" }}
<div id="timeline-grid" class="flex flex-col gap-4 md:grid md:grid-cols-3">

    <div class="order-3 md:order-none md:col-span-2 md:row-start-1">
        {{ template "timeline/fragments/timeline" . }}
    </div>

    <div class="order-1 md:order-none flex flex-col gap-6 md:col-start-3 md:row-start-1 md:pt-4">
        {{ if .ShowNewsletter }}
        <div id="newsletter-col" class="order-first md:order-last">
            {{ template "timeline/fragments/newsletterWidget" . }}
        </div>
        {{ end }}
        {{ template "timeline/fragments/announcements" . }}
        <div class="hidden md:block">
            {{ template "timeline/fragments/vouchSuggestions" . }}
        </div>
        {{ template "timeline/fragments/trending" . }}
    </div>
</div>

{{ if .ShowNewsletter }}
<script>
  (function() {
    var DISMISS_KEY = 'newsletter-dismissed';
    var newsletterCol = document.getElementById('newsletter-col');
    if (!newsletterCol) return;

    // hide removes the widget from the DOM without persisting anything.
    // Used when this browser's localStorage says we already dismissed in a
    // past session — the server has already told us .ShowNewsletter is true
    // (no DB row), so we deliberately do NOT re-POST /newsletter/dismiss
    // here: that would clobber a 'subscribed' row created from another
    // device after this localStorage entry was set.
    function hide() {
      newsletterCol.remove();
    }

    // dismiss is the user-initiated path. Persists both locally and (for
    // logged-in users) server-side so the widget stays hidden on all their
    // devices.
    function dismiss() {
      hide();
      try { localStorage.setItem(DISMISS_KEY, '1'); } catch (e) {}
      fetch('/newsletter/dismiss', { method: 'POST', credentials: 'same-origin' })
        .catch(function () { /* localStorage is the fallback */ });
    }

    try {
      if (localStorage.getItem(DISMISS_KEY) === '1') {
        hide();
        return;
      }
    } catch (e) { /* storage disabled; keep widget visible */ }

    newsletterCol.addEventListener('click', function(e) {
      if (e.target.closest('[data-newsletter-dismiss]')) dismiss();
    });
  })();
</script>
{{ end }}
{{ end }}
