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.

8.5 KB
Raw Blame
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import NameField from '../../components/NameField.vue'
import { useRouter, useRoute } from 'vue-router'
import { workspaceApi } from '../../api/workspaces'
import type { Workspace, WorkspaceInvitation } from '../../api/types'
import { useNotificationStore } from '../../stores/notification'
import { useWorkspaceStore } from '../../stores/workspace'

const router = useRouter()
const route = useRoute()
const notify = useNotificationStore()
const wsStore = useWorkspaceStore()

const workspaces = ref<Workspace[]>([])
const invitations = ref<WorkspaceInvitation[]>([])
const loading = ref(true)

const showCreateModal = ref(false)
const newName = ref('')
const newDescription = ref('')
// On by default: an empty workspace has nothing to send and nothing to look at.
const newSeedDefaults = ref(true)
const creating = ref(false)

async function fetchData() {
  loading.value = true
  try {
    const [wsRes, invRes] = await Promise.all([
      workspaceApi.list(),
      workspaceApi.myInvitations(),
    ])
    workspaces.value = wsRes.data.data ?? []
    invitations.value = invRes.data.data ?? []
  } catch {
    notify.error('Failed to load workspaces')
  } finally {
    loading.value = false
  }
}

async function createWorkspace() {
  if (!newName.value.trim()) return
  creating.value = true
  try {
    await workspaceApi.create({
      name: newName.value.trim(),
      description: newDescription.value.trim(),
      seed_defaults: newSeedDefaults.value,
    })
    notify.success('Workspace created')
    showCreateModal.value = false
    newName.value = ''
    newDescription.value = ''
    newSeedDefaults.value = true
    await fetchData()
    await wsStore.fetchWorkspaces()
  } catch (err: any) {
    notify.error(err.response?.data?.error?.message || 'Failed to create workspace')
  } finally {
    creating.value = false
  }
}

async function acceptInvitation(inv: WorkspaceInvitation) {
  try {
    await workspaceApi.acceptInvitationById(inv.id)
    notify.success(`Joined workspace "${inv.workspace}"`)
    await fetchData()
    await wsStore.fetchWorkspaces()
  } catch (err: any) {
    notify.error(err.response?.data?.error?.message || 'Failed to accept invitation')
  }
}

async function declineInvitation(inv: WorkspaceInvitation) {
  try {
    await workspaceApi.declineInvitationById(inv.id)
    notify.success('Invitation declined')
    invitations.value = invitations.value.filter(i => i.id !== inv.id)
  } catch (err: any) {
    notify.error(err.response?.data?.error?.message || 'Failed to decline invitation')
  }
}

function switchToWorkspace(ws: Workspace) {
  wsStore.setWorkspace(ws.id)
  router.push('/')
}

function viewWorkspace(ws: Workspace) {
  router.push(`/workspaces/${ws.id}`)
}

function roleBadgeClass(role: string): string {
  if (role === 'owner') return 'badge-primary'
  if (role === 'admin') return 'badge-warning'
  return 'badge-neutral'
}

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

onMounted(fetchData)

if (route.query.create !== undefined) {
  showCreateModal.value = true
  router.replace({ query: {} })
}
</script>

<template>
  <div>
    <div class="page-header">
      <h1>Workspaces</h1>
      <button class="btn btn-primary" @click="showCreateModal = true">Create Workspace</button>
    </div>

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

    <template v-else>
      <!-- Pending Invitations -->
      <div v-if="invitations.length > 0" class="card" style="margin-bottom: 20px;">
        <div class="card-header" style="display: flex; align-items: center; justify-content: space-between; padding: 14px 20px; border-bottom: 1px solid var(--border-primary); font-weight: 600; font-size: 14px;">
          <span>Pending Invitations ({{ invitations.length }})</span>
        </div>
        <div class="table-wrapper">
          <table>
            <thead>
              <tr>
                <th>Workspace</th>
                <th>Role</th>
                <th>Expires</th>
                <th>Actions</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="inv in invitations" :key="inv.id">
                <td style="font-weight: 500;">{{ inv.workspace }}</td>
                <td><span class="badge" :class="roleBadgeClass(inv.role)">{{ inv.role }}</span></td>
                <td>{{ formatDate(inv.expires_at) }}</td>
                <td>
                  <div style="display: flex; gap: 6px;">
                    <button class="btn btn-primary btn-sm" @click="acceptInvitation(inv)">Accept</button>
                    <button class="btn btn-secondary btn-sm" @click="declineInvitation(inv)">Decline</button>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- Workspaces List -->
      <div class="card">
        <div v-if="workspaces.length === 0 && invitations.length === 0" class="empty-state">
          <h3>No workspaces</h3>
          <p>Create a workspace to share resources with your team.</p>
        </div>

        <template v-else-if="workspaces.length > 0">
          <div class="table-wrapper">
            <table>
              <thead>
                <tr>
                  <th>Display name</th>
                  <th>Name</th>
                  <th>Your Role</th>
                  <th>Created</th>
                  <th>Actions</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="ws in workspaces" :key="ws.id">
                  <td style="font-weight: 500;">{{ ws.display_name }}</td>
                  <td><code>{{ ws.name }}</code></td>
                  <td>
                    <span class="badge" :class="roleBadgeClass(ws.role)">{{ ws.role }}</span>
                  </td>
                  <td>{{ formatDate(ws.created_at) }}</td>
                  <td>
                    <div style="display: flex; gap: 6px;">
                      <button
                        class="btn btn-primary btn-sm"
                        :class="{ 'btn-success': wsStore.currentWorkspaceId === ws.id }"
                        @click="switchToWorkspace(ws)"
                      >
                        {{ wsStore.currentWorkspaceId === ws.id ? 'Active' : 'Switch' }}
                      </button>
                      <button class="btn btn-secondary btn-sm" @click="viewWorkspace(ws)">Manage</button>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </template>

        <div v-else class="empty-state">
          <h3>No workspaces yet</h3>
          <p>You have pending invitations above. Accept one to get started, or create your own workspace.</p>
        </div>
      </div>
    </template>

    <!-- Create Workspace Modal -->
    <div v-if="showCreateModal" class="modal-overlay" @click.self="showCreateModal = false">
      <div class="modal">
        <div class="modal-header">
          <h3>Create Workspace</h3>
        </div>
        <form @submit.prevent="createWorkspace">
          <div class="modal-body">
            <NameField v-model="newName" resource="workspace" usage="Unique on this Posta." placeholder="e.g. My Team"
              autofocus />
            <div class="form-group">
              <label class="form-label">Description (optional)</label>
              <input
                v-model="newDescription"
                class="form-input"
                placeholder="What is this workspace for?"
              />
            </div>
            <div class="form-group">
              <label class="checkbox-label">
                <input v-model="newSeedDefaults" type="checkbox" />
                Add starter content
              </label>
              <span class="form-hint">
                A welcome template in English, French and German, a default stylesheet, and the
                matching languages — enough to send something straight away. Turn this off for a
                workspace you will fill from an export or the API.
              </span>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" @click="showCreateModal = false">Cancel</button>
            <button type="submit" class="btn btn-primary" :disabled="creating || !newName.trim()">
              {{ creating ? 'Creating...' : 'Create' }}
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>