Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

14.8 KB
Raw Blame
{{ define "campaigns-table" -}}
{{- $root := .Root -}}
{{- $d := $root.Data -}}
{{- $readonly := .Readonly -}}
{{- $base := URI "/admin/campaigns" -}}
{{- $tableBase := .Base -}}
{{- $defaultAnalyticsDate := now.AddDate 0 0 -30 -}}

{{- $dateFields := list (dict "Field" "created_at" "Label" "globals.fields.createdAt")
						(dict "Field" "started_at" "Label" "globals.fields.startedAt")
						(dict "Field" "updated_at" "Label" "globals.fields.endedAt")
						(dict "Field" "send_at" "Label" "globals.terms.scheduled") -}}
{{- $dateField := "" -}}
{{- $dateLabel := $root.L.T "globals.terms.timestamps" -}}
{{- range $dateFields -}}
	{{- if eq ($d.Page.Param "order_by") .Field -}}
		{{- $dateField = .Field -}}
		{{- $dateLabel = $root.L.T .Label -}}
	{{- end -}}
{{- end -}}

<div class="table-wrapper" x-data="table({ query: {{ ToJSON $d.Page.Query }}, filters: {{ ToJSON $d.Page.QueryParams }}, total: {{ $d.Page.Total }} })" :aria-busy="isLoading('campaigns')" @change="onChange($event)">
	{{- if .ClearFilters }}
		<div class="bulk-actions">
			<a href="{{ $tableBase }}" class="button outline small">{{ $root.L.T "globals.buttons.clear" }}</a>
		</div>
	{{- end }}
	{{- if and (not $readonly) ($d.Can "campaigns:manage" "campaigns:manage_all") }}
		<template x-if="numSelected > 0">
			<ot-dropdown class="bulk-actions" data-variant="primary">
				<button popovertarget="camp-bulk-actions" type="button" class="small">
					<span x-text="numSelectedLabel"></span> {{ Icon "more-vertical" }}
				</button>
				<menu popover id="camp-bulk-actions">
					{{- if gt $d.Page.Total $d.Page.PerPage }}
						<button type="button" role="menuitem" class="ghost" x-cloak x-show="!allSelected" @click.prevent="onSelectAllQuery()">
							{{ Icon "check-square" }} <span x-text="selectAllLabel"></span>
						</button>
					{{- end }}
					<button type="button" role="menuitem" class="ghost" data-variant="danger" :disabled="isLoading('campaigns')" @click.prevent="onDeleteSelected(numSelected, selectedQueryParams().toString())">
						{{ Icon "trash-2" }} {{ $root.L.T "globals.buttons.delete" }} <span x-text="numSelected"></span>
					</button>
				</menu>
			</ot-dropdown>
		</template>
	{{- end }}

	<table{{ if not $readonly }} class="has-select"{{ end }}>
		<thead>
			<tr>
				{{ if not $readonly }}<th width="5%"><input type="checkbox" aria-label="{{ $root.L.T "globals.buttons.toggleSelect" }}"></th>{{ end }}
				<th width="38%" class="cy-name">{{ $d.Page.SortLink $tableBase "name" ($root.L.T "globals.fields.name") }}</th>
				<th width="12%" class="cy-type">{{ $root.L.T "globals.fields.status" }}</th>
				<th width="12%">{{ $root.L.T "globals.terms.stats" }}</th>
				<th width="10%">{{ $root.L.T "globals.terms.analytics" }}</th>
				<th width="18%" class="cy-timestamp">
					<div class="timestamp-sort">
						{{ $d.Page.SortLink $tableBase (default "created_at" $dateField) $dateLabel }}
						<ot-dropdown>
							<button type="button" class="ghost small icon" popovertarget="camp-timestamp-sort" aria-label="{{ $root.L.T "globals.terms.timestamps" }}" aria-haspopup="menu">
								{{ Icon "chevron-down" }}
							</button>
							<menu popover id="camp-timestamp-sort">
								<a role="menuitem" class="button ghost" href="{{ $d.Page.FilterURL $tableBase "order_by" "order" }}">{{ $root.L.T "globals.terms.all" }}</a>
								{{- range $dateFields }}
									{{- $order := "asc" -}}
									{{- if and (eq ($d.Page.Param "order_by") .Field) (eq ($d.Page.Param "order") "asc") }}{{ $order = "desc" }}{{ end -}}
									<a role="menuitem" class="button ghost" href="{{ printf "%s?%s&order_by=%s&order=%s" $tableBase ($d.Page.Encode "order_by" "order" "page") .Field $order }}" data-sort-field="{{ .Field }}"{{ if eq ($d.Page.Param "order_by") .Field }} data-sorted="{{ $d.Page.Param "order" }}"{{ end }}>{{ $root.L.T .Label }}</a>
								{{- end }}
							</menu>
						</ot-dropdown>
					</div>
				</th>
				<th width="5%" class="align-right"></th>
			</tr>
		</thead>
		<tbody>
			{{- range $d.Campaigns }}
				{{- $canStart := and (eq .Status "draft") (not .SendAt.Valid) -}}
				{{- $canSchedule := and (eq .Status "draft") .SendAt.Valid -}}
				{{- $canPause := eq .Status "running" -}}
				{{- $canResume := eq .Status "paused" -}}
				{{- $canCancel := or (eq .Status "running") (eq .Status "paused") -}}
				{{- $isDone := or (eq .Status "finished") (eq .Status "cancelled") -}}
				{{- $hasEnded := and (or $isDone $canResume) .StartedAt.Valid .UpdatedAt.Valid -}}
				<tr data-campaign-id="{{ .ID }}" class="status-{{ .Status }}">
					{{ if not $readonly }}<td><input name="id" type="checkbox" value="{{ .ID }}"></td>{{ end }}
					<td class="name">
						<div class="avatar{{ if eq .Status "cancelled" }} disabled{{ end }}">
							{{ template "avatar-icon" dict "Kind" "campaign" "Name" .Name "Href" (printf "%s/%d" $base .ID) "Title" (printf "%s %s" ($root.L.T "globals.buttons.view") .Name) }}
							<div class="avatar-label vstack gap-1">
								<div>
									<a href="{{ $base }}/{{ .ID }}" class="unstyled">{{ .Name }}</a>
									{{- if eq .Type "optin" }}
										<a href="{{ $tableBase }}?{{ $d.Page.Encode "type" "page" }}&type=optin" title="{{ $root.L.T "globals.buttons.filter" }} {{ $root.L.T "lists.optin" }}">{{ Badge (list (Icon "user-check") ($root.L.T "lists.optin")) "small outline" }}</a>
									{{- end }}
									<div class="subject text-8 text-light">
										<a href="{{ $base }}/{{ .ID }}" class="unstyled">{{ .Subject }}</a>
									</div>
								</div>
								{{- if .Tags }}
									<div class="hstack gap-2 text-8">
										{{ range .Tags }}<a href="?{{ $d.Page.Encode "tag" "page" }}&tag={{ . }}" title="{{ $root.L.T "globals.buttons.filter" }} {{ . }}" class="tag">#{{ . }}</a>{{ end }}
									</div>
								{{- end }}
								{{- if .ParsedLists }}
								<div class="hstack lists">
									{{- range $i, $l := .ParsedLists }}
										{{- if lt $i 3 }}
											<div class="avatar xsmall">
												{{ template "avatar-icon" dict "Kind" "list" "Name" .Name "Href" (printf "%s/%d" (URI "/admin/lists") .ID) "Title" (printf "%s %s" ($root.L.T "globals.buttons.view") .Name) }}
												<a class="unstyled text-light avatar-label" href="{{ URI "/admin/subscribers/lists" }}/{{ .ID }}" title="{{ $root.L.T "globals.buttons.filter" }} {{ .Name }}">{{ .Name }}</a>
											</div>
										{{- end }}
									{{- end }}
									{{- if gt (len .ParsedLists) 3 }}
										<ot-dropdown>
											<button type="button" class="ghost small text-8" popovertarget="camp-lists-{{ .ID }}" aria-label="{{ $root.L.T "globals.terms.lists" }}">+{{ sub (len .ParsedLists) 3 }} {{ $root.L.T "globals.terms.lists" | lower }}</button>
											<menu popover id="camp-lists-{{ .ID }}">
												{{- range slice .ParsedLists 3 }}
													<a role="menuitem" class="button ghost" href="{{ URI "/admin/subscribers/lists" }}/{{ .ID }}" title="{{ $root.L.T "globals.buttons.filter" }} {{ .Name }}">
														<span class="avatar xsmall">
															{{ template "avatar-icon" dict "Kind" "list" "Name" .Name }}
															<span class="avatar-label text-light">{{ .Name }}</span>
														</span>
													</a>
												{{- end }}
											</menu>
										</ot-dropdown>
									{{- end }}
								</div>
								{{- end }}
							</div>
						</div>
					</td>
					<td class="type">
						<div class="vstack gap-1">
							<a href="{{ $tableBase }}?{{ $d.Page.Encode "status" "page" }}&status={{ .Status }}" title="{{ $root.L.T "globals.buttons.filter" }} {{ $root.L.T (printf "globals.terms.%s" .Status) }}">
								{{ Badge (list (Dot) ($root.L.T (printf "globals.terms.%s" .Status))) (printf "enum status-%s" .Status) }}
								{{- if eq .Status "running" }}<span class="spinner" data-spinner="small" aria-busy="true"></span>{{ end }}
							</a>
							{{- if and (eq .Status "scheduled") .SendAt.Valid }}
								<span class="text-8 text-light" title="{{ ExactDate .SendAt }}">{{ Icon "clock" }} {{ CompactDate .SendAt true true }}</span>
							{{- end }}
						</div>
					</td>
					<td class="stats" data-label="{{ $root.L.T "globals.terms.stats" }}">
						<div class="vstack gap-1">
								<div><div data-sent-count{{ if and (eq .Sent 0) (ne .Status "running") }} hidden{{ end }}><span data-stat="sent">{{ FormatNumber .Sent }}</span>{{ if ne .Status "finished" }}<span data-send-total{{ if not .ToSend }} hidden{{ end }}> / <span data-stat="to_send">{{ FormatNumber .ToSend }}</span></span>{{ end }}</div><span class="text-8 text-light"><span data-stat="percent">{{ if .ToSend }}{{ div (mul .Sent 100) .ToSend }}{{ else }}0{{ end }}%</span> {{ $root.L.T "globals.terms.sent" | lower }}{{ with Duration .StartedAt .UpdatedAt }} · {{ . }}{{ end }}</span></div>
								<div><a class="unstyled" href="{{ URI "/admin/subscribers/bounces" }}?campaign_id={{ .ID }}">{{ FormatNumber .Bounces }} <span class="text-8 text-light">{{ $root.L.T "globals.terms.bounces" | lower }}</span></a></div>
							{{- if eq .Status "running" }}
								<div class="hstack gap-1 text-8 campaign-progress">
									<progress data-stat="progress" value="{{ .Sent }}" max="{{ max .ToSend 1 }}" aria-label="{{ $root.L.T "globals.terms.progress" }}"></progress>
									<span><span data-stat="rate">0</span> <span class="text-light">/ {{ $root.L.T "globals.terms.minuteShort" }}</span></span>
								</div>
							{{- end }}
						</div>
					</td>
					<td class="stats analytics" data-label="{{ $root.L.T "globals.terms.analytics" }}">
						<div class="vstack gap-1">
							<div>{{ FormatNumber .Views }} <span class="text-8 text-light">{{ $root.L.T "globals.terms.views" | lower }}</span></div>
								<div>{{ FormatNumber .Clicks }} <span class="text-8 text-light">{{ $root.L.T "globals.terms.clicks" | lower }}</span></div>
						</div>
					</td>
					<td class="timestamp" data-label="{{ $dateLabel }}">
						{{- if $dateField }}
							{{- $date := .CreatedAt -}}
							{{- if eq $dateField "started_at" }}{{ $date = .StartedAt }}{{ end -}}
							{{- if eq $dateField "updated_at" }}{{ $date = .UpdatedAt }}{{ end -}}
							{{- if eq $dateField "send_at" }}{{ $date = .SendAt }}{{ end -}}
							{{- if and $date.Valid (or (ne $dateField "updated_at") $hasEnded) }}
								<span tabindex="0" title="{{ ExactDate $date }}" @click="$el.focus()" data-tooltip-placement="bottom">{{ NiceDate $date true }}</span>
							{{- else }}
								—
							{{- end }}
						{{- else }}
							{{- $timeline := printf "%s: %s" ($root.L.T "globals.fields.createdAt") (ExactDate .CreatedAt) }}
							{{- if .SendAt.Valid }}{{ $timeline = printf "%s\n%s: %s" $timeline ($root.L.T "globals.terms.scheduled") (ExactDate .SendAt) }}{{ end }}
							{{- if .StartedAt.Valid }}{{ $timeline = printf "%s\n%s: %s" $timeline ($root.L.T "globals.fields.startedAt") (ExactDate .StartedAt) }}{{ end }}
							{{- if $hasEnded }}
								{{- $timeline = printf "%s\n%s: %s" $timeline ($root.L.T "globals.fields.endedAt") (ExactDate .UpdatedAt) }}
							{{- end }}
							{{- with Duration .StartedAt .UpdatedAt }}{{ $timeline = printf "%s\n%s: %s" $timeline ($root.L.T "globals.fields.duration") . }}{{ end }}
							<span tabindex="0" title="{{ $timeline }}" @click="$el.focus()" data-tooltip-placement="bottom">
								{{- if $hasEnded }}
									{{ FormatTimestamps .StartedAt .UpdatedAt }}
								{{- else if and (not (or (eq .Status "draft") (eq .Status "scheduled"))) .StartedAt.Valid }}
									{{ CompactDate .StartedAt true }} <span class="text-8 text-light">{{ $root.L.T "globals.fields.startedAt" | lower }}</span>
								{{- else }}
									{{ NiceDate .CreatedAt }}
								{{- end }}
							</span>
						{{- end }}
					</td>
					<td class="actions align-right">
						<ot-dropdown>
							<button popovertarget="camp-actions-{{ .ID }}" type="button" class="ghost more icon" aria-label="{{ $root.L.T "globals.buttons.more" }}">
								{{ Icon "more-vertical" }}
							</button>
							<menu popover id="camp-actions-{{ .ID }}">
								<button type="button" role="menuitem" class="ghost" @click.prevent="onPreview({{ .ID }}, {{ ToJSON .Name }})">{{ Icon "eye" }} {{ $root.L.T "globals.buttons.preview" }}</button>
								{{- if and (not $readonly) ($d.Can "campaigns:send") $canStart }}
									<button type="button" role="menuitem" class="ghost" @click.prevent="onChangeStatus({{ .ID }}, {{ ToJSON .Name }}, 'running')">{{ Icon "send" }} {{ $root.L.T "campaigns.start" }}</button>
								{{- end }}
								{{- if and (not $readonly) ($d.Can "campaigns:send") }}
									{{- if $canSchedule }}
										<button type="button" role="menuitem" class="ghost" @click.prevent="onChangeStatus({{ .ID }}, {{ ToJSON .Name }}, 'scheduled', true)">{{ Icon "clock" }} {{ $root.L.T "campaigns.schedule" }}</button>
									{{- end }}
									{{- if $canPause }}
										<button type="button" role="menuitem" class="ghost" @click.prevent="onChangeStatus({{ .ID }}, {{ ToJSON .Name }}, 'paused')">{{ Icon "pause" }} {{ $root.L.T "globals.buttons.pause" }}</button>
									{{- end }}
									{{- if $canResume }}
										<button type="button" role="menuitem" class="ghost" @click.prevent="onChangeStatus({{ .ID }}, {{ ToJSON .Name }}, 'running')">{{ Icon "send" }} {{ $root.L.T "globals.buttons.send" }}</button>
									{{- end }}
									{{- if $canCancel }}
										<button type="button" role="menuitem" class="ghost" data-variant="danger" @click.prevent="onChangeStatus({{ .ID }}, {{ ToJSON .Name }}, 'cancelled')">{{ Icon "x-circle" }} {{ $root.L.T "globals.buttons.cancel" }}</button>
									{{- end }}
								{{- end }}
								<hr />
								<a href="{{ $base }}/{{ .ID }}" role="menuitem" class="button ghost">{{ Icon "edit-2" }} {{ $root.L.T "globals.buttons.edit" }}</a>
								{{- if and (not $readonly) ($d.Can "campaigns:manage" "campaigns:manage_all") }}
									<button type="button" role="menuitem" class="ghost" @click.prevent="onClone({{ .ID }}, {{ ToJSON .Name }})">{{ Icon "copy" }} {{ $root.L.T "globals.buttons.clone" }}</button>
								{{- end }}
								{{- if $d.Can "campaigns:get_analytics" }}
									{{- $from := $defaultAnalyticsDate -}}
									{{- if .StartedAt.Valid }}{{ $from = .StartedAt.Time.Local }}{{ end -}}
									<a href="{{ URI "/admin/campaigns/analytics" }}?id={{ .ID }}&from={{ $from.Format "2006-01-02" }}" role="menuitem" class="button ghost">{{ Icon "bar-chart-2" }} {{ $root.L.T "globals.terms.analytics" }}</a>
								{{- end }}
								{{- if and (not $readonly) ($d.Can "campaigns:manage" "campaigns:manage_all") }}
									<hr />
									<button type="button" role="menuitem" class="ghost" data-variant="danger" :disabled="isLoading('campaigns')" @click.prevent="onDelete({{ .ID }}, {{ ToJSON .Name }})">{{ Icon "trash-2" }} {{ $root.L.T "globals.buttons.delete" }}</button>
								{{- end }}
							</menu>
						</ot-dropdown>
					</td>
				</tr>
			{{- else }}
				{{ if $readonly }}{{ template "empty-state" 6 }}{{ else }}{{ template "empty-state" 7 }}{{ end }}
			{{- end }}
		</tbody>
	</table>

	{{- template "pagination" $d.Page.Pagination }}
</div>
{{ end }}