Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

9.5 KB
Raw Blame
<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { unsubscribeListsApi } from '../../api/unsubscribeLists'
import type { UnsubscribeListItem, UnsubscribeListInput } from '../../api/types'
import { useNotificationStore } from '../../stores/notification'
import { useConfirm } from '../../composables/useConfirm'
import { useModalSafeClose } from '../../composables/useModalSafeClose'
import { useWorkspaceStore } from '../../stores/workspace'
import { usePagination } from '@/composables/usePagination'
import Pagination from '@/components/Pagination.vue'

const router = useRouter()
const notify = useNotificationStore()
const wsStore = useWorkspaceStore()
const { confirm } = useConfirm()

const lists = ref<UnsubscribeListItem[]>([])
const loading = ref(true)
const showInfo = ref(false)

async function copyListId(id: number) {
  try {
    await navigator.clipboard.writeText(String(id))
    notify.success(`List ID ${id} copied`)
  } catch {
    notify.error('Failed to copy')
  }
}

const showModal = ref(false)
const editing = ref<UnsubscribeListItem | null>(null)
const saving = ref(false)

const form = ref<UnsubscribeListInput>({
  name: '',
  public_name: '',
  description: '',
  active: true,
})

function resetForm() {
  form.value = { name: '', public_name: '', description: '', active: true }
  editing.value = null
}

function openCreate() {
  resetForm()
  showModal.value = true
}

function openEdit(list: UnsubscribeListItem) {
  editing.value = list
  form.value = {
    name: list.name,
    public_name: list.public_name || '',
    description: list.description || '',
    active: list.active,
  }
  showModal.value = true
}

function closeModal() {
  showModal.value = false
  resetForm()
}

const { pageable, goToPage } = usePagination(async (page) => {
  loading.value = true
  try {
    const res = await unsubscribeListsApi.list(page, pageable.value.size)
    lists.value = res.data.data
    pageable.value = res.data.pageable
  } catch (e) {
    console.error('Failed to load unsubscribe lists', e)
  } finally {
    loading.value = false
  }
})

async function saveList() {
  if (!form.value.name.trim()) return
  saving.value = true
  try {
    if (editing.value) {
      await unsubscribeListsApi.update(editing.value.id, form.value)
      notify.success('Unsubscribe list updated')
    } else {
      await unsubscribeListsApi.create(form.value)
      notify.success('Unsubscribe list created')
    }
    closeModal()
    await goToPage(pageable.value.current_page)
  } catch {
    notify.error(editing.value ? 'Failed to update unsubscribe list' : 'A list with that name already exists')
  } finally {
    saving.value = false
  }
}

async function deleteList(list: UnsubscribeListItem) {
  const confirmed = await confirm({
    title: 'Delete Unsubscribe List',
    message: `Delete "${list.name}"? Existing per-list opt-outs that reference it will remain on the suppression list.`,
    confirmText: 'Delete',
    variant: 'danger',
  })
  if (!confirmed) return
  try {
    await unsubscribeListsApi.delete(list.id)
    notify.success('Unsubscribe list deleted')
    await goToPage(pageable.value.current_page)
  } catch {
    notify.error('Failed to delete unsubscribe list')
  }
}

function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })
}

const { watchClickStart, confirmClickEnd } = useModalSafeClose(() => {
  closeModal()
})
</script>

<template>
  <div>
    <div class="page-header">
      <h1>Unsubscribe Lists</h1>
    </div>

    <div v-if="showInfo" class="card help-card">
      <div class="card-body">
        <h2 class="help-title">Where to use Unsubscribe Lists</h2>
        <p class="help-text">
          Reference a list from a transactional send with <code>unsubscribe.list_id</code>.
          Posta mints the signed one-click URL and emits the <code>List-Unsubscribe</code> header.
          A click opts the recipient out of <strong>that list only</strong> &mdash; receipts, password
          resets, and other transactional mail keep flowing.
        </p>
        <pre class="code-block"><code>POST /api/v1/emails/send
{
  "to": ["[email protected]"],
  "subject": "Product update",
  "html": "...",
  "unsubscribe": { "list_id": 42 }
}</code></pre>
        <p class="help-text help-text-muted">
          Click a list name below to see who opted out and to resubscribe individual addresses.
        </p>
      </div>
    </div>

    <div v-if="loading" class="loading-page">
      <div class="spinner"></div>
    </div>

    <div v-else class="card">
      <div class="card-header">
        <h2>Unsubscribe Lists</h2>
        <div class="flex gap-2">
          <button
            type="button"
            class="btn btn-secondary"
            @click="showInfo = !showInfo"
          >
            {{ showInfo ? 'Hide help' : 'How to use' }}
          </button>
          <button v-if="wsStore.canEdit" class="btn btn-primary" @click="openCreate">Add List</button>
        </div>
      </div>
      <div v-if="lists.length === 0" class="empty-state">
        <h3>No Unsubscribe Lists</h3>
        <p>Create a list, then reference it from a send with <code>unsubscribe.list_id</code>. A one-click then opts the recipient out of that list only.</p>
      </div>

      <template v-else>
        <div class="table-wrapper">
          <table>
            <thead>
              <tr>
                <th>Name</th>
                <th>Public Name</th>
                <th>Description</th>
                <th>Status</th>
                <th>Created</th>
                <th>Actions</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="list in lists" :key="list.id" @click="router.push(`/unsubscribe-lists/${list.id}`)" class="cursor-pointer">
                <td>
                  <a class="link">{{ list.name }}</a>
                  <span
                    class="badge badge-neutral"
                    :title="`Copy list ID ${list.id}`"
                    style="cursor:pointer;user-select:none;"
                    @click.stop="copyListId(list.id)"
                  >#{{ list.id }}</span>
                </td>
                <td>{{ list.public_name || '—' }}</td>
                <td>{{ list.description || '—' }}</td>
                <td>
                  <span v-if="list.active" class="badge badge-success">Active</span>
                  <span v-else class="badge badge-neutral">Archived</span>
                </td>
                <td>{{ formatDate(list.created_at) }}</td>
                <td>
                  <div class="flex gap-2">
                    <button class="btn btn-secondary btn-sm" @click.stop="router.push(`/unsubscribe-lists/${list.id}`)">View</button>
                    <button v-if="wsStore.canEdit" class="btn btn-secondary btn-sm" @click.stop="openEdit(list)">Edit</button>
                    <button v-if="wsStore.canEdit" class="btn btn-danger btn-sm" @click.stop="deleteList(list)">Delete</button>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>

        <Pagination :pageable="pageable" @page="goToPage" />
      </template>
    </div>

    <!-- Create/Edit Unsubscribe List Modal -->
    <div v-if="showModal" class="modal-overlay" @mousedown="watchClickStart" @mouseup="confirmClickEnd">
      <div class="modal" style="max-width: 480px;" @mousedown.stop @mouseup.stop>
        <div class="modal-header">
          <h3>{{ editing ? 'Edit Unsubscribe List' : 'Add Unsubscribe List' }}</h3>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label class="form-label">Name</label>
            <input v-model="form.name" class="form-input" placeholder="e.g. Product updates" />
            <span class="form-hint">Internal label, unique within your account.</span>
          </div>
          <div class="form-group">
            <label class="form-label">Public name</label>
            <input v-model="form.public_name" class="form-input" placeholder="Shown to recipients (optional)" />
            <span class="form-hint">Displayed on the unsubscribe confirmation page. Falls back to the name.</span>
          </div>
          <div class="form-group">
            <label class="form-label">Description</label>
            <input v-model="form.description" class="form-input" placeholder="Optional" />
          </div>
          <div v-if="editing" class="form-group">
            <label class="checkbox-label">
              <input type="checkbox" v-model="form.active" />
              Active
            </label>
            <span class="form-hint">Archive a list to retire it without deleting its opt-out history.</span>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="closeModal">Cancel</button>
          <button class="btn btn-primary" :disabled="saving || !form.name.trim()" @click="saveList">
            {{ saving ? 'Saving...' : (editing ? 'Update' : 'Create') }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.help-card {
  margin-bottom: 16px;
}
.help-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 10px;
}
.help-text {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-secondary);
  margin-bottom: 14px;
}
.help-text code {
  font-size: 12px;
}
.help-card .code-block {
  font-size: 12px;
  line-height: 1.6;
}
.help-text-muted {
  color: var(--text-muted);
  margin-top: 12px;
  margin-bottom: 0;
}
</style>