Files
uptimesebi/internal/web/support.templ
T
Sebastian UnterschützandClaude Opus 5.5 f3d402705e
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 7m0s
ci / test (pull_request) Successful in 12m1s
ci / build (pull_request) Successful in 18m14s
ci / image (pull_request) Successful in 5m9s
feat: support tickets between accounts and platform admins
Accounts open tickets under Support and admins answer them under Admin →
Support tickets.

- A ticket is open while it waits for the admins and answered while it
  waits for the account. Either side can close or reopen it, and a reply
  reopens it.
- The admins are mailed about new tickets and replies, and the account
  about answers.
- A ticket can name the active organization and point at a monitor,
  notification channel, alert group, status page or incident in it.
  Each must be something the account can read.
- Next to the conversation, admins see the account at a glance: state,
  sign-in, organizations with their limits and usage, and open tickets.
- Admins switch support off and limit how many tickets an account has
  open at once.
- Suspended accounts and those yet to accept the terms reach support too;
  a suspended account opens one ticket.
- Tickets are part of the account export and are deleted with the
  account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-24 18:01:50 +01:00

414 lines
15 KiB
Templ
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package web
import (
"strconv"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// SupportTicket is a ticket as the support pages show it; User and Org
// are filled for admins.
type SupportTicket struct {
ID, Subject, Status string
User, Org string
Updated string
References []SupportReference
Messages []SupportMessage
}
// SupportReference is something a ticket points at; Kind is org, monitor,
// channel, alert_group, status_page or incident.
type SupportReference struct {
Kind, Name string
}
// SupportMessage is one message of a ticket, rendered from Markdown.
type SupportMessage struct {
FromStaff bool
// Author is the admin who answered, for admins only.
Author string
HTML string
At string
}
// SupportOptions are what of the active organization a new ticket can
// point at; Org is empty without one.
type SupportOptions struct {
Org string
Monitors, Channels, AlertGroups, StatusPages, Incidents []SupportOption
}
// SupportOption is one choice of SupportOptions.
type SupportOption struct {
ID, Name string
}
// SupportSettings are how the platform runs support, for its admins.
type SupportSettings struct {
Enabled bool
MaxOpen int
}
// SupportAccount is what admins see about the account behind a ticket.
type SupportAccount struct {
ID, Email, Name, Created, Locale string
Verified, TOTP, Password, Admin bool
Suspended bool
SuspendedReason string
OrgsOwned, OrgLimit int
OpenTickets int64
Orgs []SupportOrg
}
// SupportOrg is an organization of the account with its limits; Current
// marks the one the ticket names. Limits of 0 are unlimited.
type SupportOrg struct {
Name, Slug string
Owner, Current, Suspended bool
SuspendedReason string
Monitors, MonitorLimit int
StatusPages, StatusPageLimit int
RegionsPerMonitor int
}
// SupportStatuses are the states of a ticket, in the order admins filter
// them.
var SupportStatuses = []string{"open", "answered", "closed"}
var supportFilters = append(append([]string{}, SupportStatuses...), "all")
// supportBase is where the support pages live for staff or the account.
func supportBase(staff bool) string {
if staff {
return "/admin/support"
}
return "/support"
}
func supportAPI(t SupportTicket, staff bool) string {
return supportBase(staff) + "/tickets/" + t.ID
}
// usageText shows used of limit, ∞ for no limit.
func usageText(used, limit int) string {
if limit <= 0 {
return strconv.Itoa(used) + " / ∞"
}
return strconv.Itoa(used) + " / " + strconv.Itoa(limit)
}
templ supportStatusLabel(status string) {
<span
class={ "whitespace-nowrap border px-1.5 py-0.5 font-mono text-xs",
templ.KV("border-down text-down", status == "open"),
templ.KV("border-up text-up", status == "answered"),
templ.KV("border-rule text-muted", status == "closed") }
>{ i18n.T(ctx, "support.status."+status) }</span>
}
templ supportList(tickets []SupportTicket, base string, staff bool) {
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, t := range tickets {
<li>
<a href={ templ.SafeURL(base + t.ID) } class="flex flex-wrap items-center gap-x-4 gap-y-1 py-3 hover:bg-rule/30">
@supportStatusLabel(t.Status)
<span class="order-last min-w-0 basis-full truncate font-medium sm:order-none sm:flex-1 sm:basis-0">{ t.Subject }</span>
if staff {
<span class="order-last min-w-0 basis-full truncate text-muted sm:order-none sm:basis-auto">
{ t.User }
if t.Org != "" {
{ " · " + t.Org }
}
</span>
}
<span class="ml-auto font-mono text-xs text-muted">{ t.Updated }</span>
</a>
</li>
}
</ul>
}
templ supportSelect(labelID, name string, opts []SupportOption) {
if len(opts) > 0 {
<label class="mb-3 block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, labelID) }</span>
<select name={ name } class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm">
<option value="">{ "– " + i18n.T(ctx, "support.attach_none") }</option>
for _, o := range opts {
<option value={ o.ID }>{ o.Name }</option>
}
</select>
</label>
}
}
templ SupportPage(p Page, me Me, tickets []SupportTicket, opts SupportOptions) {
@consoleShell(p, me) {
@settingsHeading("support.title", "support.intro")
<div class="grid gap-12 md:grid-cols-[1fr_22rem]">
<section>
@sectionHeading("support.mine")
if len(tickets) == 0 {
<p class="border-y border-rule py-10 text-center text-sm text-muted">{ i18n.T(ctx, "support.none") }</p>
} else {
@supportList(tickets, "/support/", false)
}
</section>
<aside>
@sectionHeading("support.new")
<form data-api="/support/tickets" data-next="/support/" data-next-from="id">
@formError()
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "support.subject") }</span>
<input name="subject" required maxlength="200" class="w-full border border-rule bg-transparent px-2.5 py-1.5 outline-none focus:border-ink"/>
</label>
<div class="mb-4">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "support.message") }</span>
@MarkdownEditor("message", "", 8, 10000, MarkdownInline)
</div>
if opts.Org != "" {
<details class="mb-4 border border-rule p-3">
<summary class="cursor-pointer text-sm">{ i18n.T(ctx, "support.attach") }</summary>
<p class="mt-2 mb-3 text-xs text-muted">{ i18n.T(ctx, "support.attach_hint") }</p>
@checkbox(i18n.T(ctx, "support.attach_org", "Org", opts.Org), "attach_org", false)
@supportSelect("support.ref.monitor", "monitor_id", opts.Monitors)
@supportSelect("support.ref.channel", "channel_id", opts.Channels)
@supportSelect("support.ref.alert_group", "alert_group_id", opts.AlertGroups)
@supportSelect("support.ref.status_page", "status_page_id", opts.StatusPages)
@supportSelect("support.ref.incident", "incident_id", opts.Incidents)
</details>
}
@submit(i18n.T(ctx, "support.send"))
</form>
</aside>
</div>
}
}
templ SupportDisabledPage(p Page, me Me) {
@consoleShell(p, me) {
@settingsHeading("support.title", "")
<p class="border-y border-rule py-10 text-center text-sm text-muted">{ i18n.T(ctx, "support.disabled") }</p>
}
}
templ supportReferences(refs []SupportReference) {
if len(refs) > 0 {
<dl class="mb-8 grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 border-y border-rule py-3 text-sm">
for _, r := range refs {
<dt class="text-muted">{ i18n.T(ctx, "support.ref."+r.Kind) }</dt>
<dd class="min-w-0 break-words">{ r.Name }</dd>
}
</dl>
}
}
templ supportFlag(on bool, yesID, noID string) {
if on {
<span class="text-up">{ i18n.T(ctx, yesID) }</span>
} else {
<span class="text-muted">{ i18n.T(ctx, noID) }</span>
}
}
// supportAccountBox gives admins the account behind a ticket at a glance.
templ supportAccountBox(a SupportAccount) {
<aside class="self-start border border-rule p-4 text-sm">
@sectionHeading("support.account")
<dl class="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1">
<dt class="text-muted">{ i18n.T(ctx, "support.account.email") }</dt>
<dd class="min-w-0 break-all">{ a.Email }</dd>
if a.Name != "" {
<dt class="text-muted">{ i18n.T(ctx, "support.account.name") }</dt>
<dd class="min-w-0 break-words">{ a.Name }</dd>
}
<dt class="text-muted">{ i18n.T(ctx, "support.account.created") }</dt>
<dd class="font-mono text-xs">{ a.Created }</dd>
<dt class="text-muted">{ i18n.T(ctx, "support.account.state") }</dt>
<dd>
if a.Suspended {
<span class="text-down">{ i18n.T(ctx, "support.account.suspended") }</span>
if a.SuspendedReason != "" {
<span class="text-muted">{ " · " + a.SuspendedReason }</span>
}
} else {
<span class="text-up">{ i18n.T(ctx, "support.account.active") }</span>
}
</dd>
<dt class="text-muted">{ i18n.T(ctx, "support.account.email_verified") }</dt>
<dd>
@supportFlag(a.Verified, "support.yes", "support.no")
</dd>
<dt class="text-muted">{ i18n.T(ctx, "support.account.sign_in") }</dt>
<dd>
if a.Password {
{ i18n.T(ctx, "support.account.password") }
} else {
{ i18n.T(ctx, "support.account.sso_only") }
}
if a.TOTP {
{ " · 2FA" }
}
</dd>
if a.Admin {
<dt class="text-muted">{ i18n.T(ctx, "support.account.role") }</dt>
<dd class="font-medium">{ i18n.T(ctx, "console.nav.admin") }</dd>
}
if a.Locale != "" {
<dt class="text-muted">{ i18n.T(ctx, "support.account.locale") }</dt>
<dd class="font-mono text-xs">{ a.Locale }</dd>
}
<dt class="text-muted">{ i18n.T(ctx, "support.account.orgs_owned") }</dt>
<dd class="font-mono text-xs">{ usageText(a.OrgsOwned, a.OrgLimit) }</dd>
<dt class="text-muted">{ i18n.T(ctx, "support.account.open_tickets") }</dt>
<dd class="font-mono text-xs">{ strconv.FormatInt(a.OpenTickets, 10) }</dd>
<dt class="text-muted">{ "ID" }</dt>
<dd class="min-w-0 break-all font-mono text-xs text-muted">{ a.ID }</dd>
</dl>
if len(a.Orgs) > 0 {
<h3 class="mt-6 mb-2 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "support.account.orgs") }</h3>
<ul class="space-y-3">
for _, o := range a.Orgs {
<li class={ "border-l-2 pl-3", templ.KV("border-ink", o.Current), templ.KV("border-rule", !o.Current) }>
<p class="flex flex-wrap items-baseline gap-x-2">
<span class="font-medium">{ o.Name }</span>
<span class="font-mono text-xs text-muted">{ o.Slug }</span>
if o.Owner {
<span class="text-xs">{ i18n.T(ctx, "support.account.owner") }</span>
}
if o.Current {
<span class="text-xs text-muted">{ i18n.T(ctx, "support.account.this_ticket") }</span>
}
</p>
if o.Suspended {
<p class="text-xs text-down">
{ i18n.T(ctx, "support.account.org_suspended") }
if o.SuspendedReason != "" {
{ " · " + o.SuspendedReason }
}
</p>
}
<p class="mt-1 font-mono text-xs text-muted">
{ i18n.T(ctx, "support.account.monitors") + " " + usageText(o.Monitors, o.MonitorLimit) }
{ " · " + i18n.T(ctx, "support.account.status_pages") + " " + usageText(o.StatusPages, o.StatusPageLimit) }
{ " · " + i18n.T(ctx, "support.account.regions") + " " + limitText(ctx, o.RegionsPerMonitor) }
</p>
</li>
}
</ul>
}
</aside>
}
// SupportTicketPage shows a ticket's conversation to its account or, with
// the account's details, to the admins.
templ SupportTicketPage(p Page, me Me, t SupportTicket, account *SupportAccount) {
@consoleShell(p, me) {
<p class="mb-2 text-sm"><a href={ templ.SafeURL(supportBase(account != nil)) } class="text-muted hover:text-ink">{ "← " + i18n.T(ctx, "support.all") }</a></p>
<h1 class="mb-2 break-words text-xl font-semibold">{ t.Subject }</h1>
<div class="mb-6 flex flex-wrap items-center gap-x-4 gap-y-1 text-sm">
@supportStatusLabel(t.Status)
if account != nil {
<span>{ t.User }</span>
}
</div>
<div class={ templ.KV("grid gap-10 lg:grid-cols-[1fr_20rem]", account != nil) }>
<div class="min-w-0">
@supportReferences(t.References)
<ol class="mb-10 space-y-4">
for _, m := range t.Messages {
<li class={ "border p-4", templ.KV("border-rule", !m.FromStaff), templ.KV("border-ink bg-rule/20", m.FromStaff) }>
<p class="mb-2 flex flex-wrap items-baseline gap-x-3 text-xs">
<span class="font-medium">
switch {
case m.FromStaff && m.Author != "":
{ i18n.T(ctx, "support.from_staff") + " · " + m.Author }
case m.FromStaff:
{ i18n.T(ctx, "support.from_staff") }
case account != nil:
{ t.User }
default:
{ i18n.T(ctx, "support.from_you") }
}
</span>
<span class="font-mono text-muted">{ m.At }</span>
</p>
<div class="legal-text break-words text-sm">
@templ.Raw(m.HTML)
</div>
</li>
}
</ol>
<section class="max-w-2xl">
@sectionHeading("support.reply")
if t.Status == "closed" && account == nil {
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, "support.closed_hint") }</p>
}
<form data-api={ supportAPI(t, account != nil) + "/messages" } data-next={ supportBase(account != nil) + "/" + t.ID }>
@formError()
<div class="mb-4">
@MarkdownEditor("message", "", 6, 10000, MarkdownInline)
</div>
@submit(i18n.T(ctx, "support.send"))
</form>
<form data-api={ supportAPI(t, account != nil) + "/status" } data-method="put" data-next={ supportBase(account != nil) + "/" + t.ID } class="mt-6">
@formError()
if t.Status == "closed" {
<input type="hidden" name="status" value="open"/>
<button type="submit" class="text-sm text-muted underline underline-offset-2 hover:text-ink">{ i18n.T(ctx, "support.reopen") }</button>
} else {
<input type="hidden" name="status" value="closed"/>
<button type="submit" class="text-sm text-muted underline underline-offset-2 hover:text-ink">{ i18n.T(ctx, "support.close") }</button>
}
</form>
</section>
</div>
if account != nil {
@supportAccountBox(*account)
}
</div>
}
}
templ AdminSupportPage(p Page, me Me, tickets []SupportTicket, filter string, settings SupportSettings) {
@consoleShell(p, me) {
@settingsHeading("support.admin_title", "support.admin_intro")
@adminLinks()
<details class="mb-8 border border-rule p-4" open?={ !settings.Enabled }>
<summary class="cursor-pointer text-sm">
{ i18n.T(ctx, "support.settings") }
if settings.Enabled {
<span class="ml-2 text-up">{ i18n.T(ctx, "support.settings.on") }</span>
} else {
<span class="ml-2 text-down">{ i18n.T(ctx, "support.settings.off") }</span>
}
</summary>
<form data-api="/admin/support/settings" data-method="put" class="mt-4 max-w-md">
@formError()
@checkbox(i18n.T(ctx, "support.settings.enabled"), "enabled", settings.Enabled)
<p class="-mt-2 mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "support.settings.enabled_hint") }</p>
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "support.settings.max_open") }</span>
<input name="max_open_tickets" data-number type="number" min="0" max="1000" value={ strconv.Itoa(settings.MaxOpen) } class="w-32 border border-rule bg-transparent px-2.5 py-1.5 outline-none focus:border-ink"/>
<span class="mt-1 block text-xs text-muted">{ i18n.T(ctx, "support.settings.max_open_hint") }</span>
</label>
@submit(i18n.T(ctx, "support.settings.save"))
</form>
</details>
<nav class="mb-4 flex flex-wrap gap-x-4 gap-y-1 text-sm" aria-label={ i18n.T(ctx, "support.filter") }>
for _, f := range supportFilters {
if f == filter {
<span class="font-medium" aria-current="true">{ i18n.T(ctx, "support.filter."+f) }</span>
} else {
<a href={ templ.SafeURL("/admin/support?status=" + f) } class="text-muted hover:text-ink">{ i18n.T(ctx, "support.filter."+f) }</a>
}
}
</nav>
if len(tickets) == 0 {
<p class="border-y border-rule py-10 text-center text-sm text-muted">{ i18n.T(ctx, "support.none_admin") }</p>
} else {
@supportList(tickets, "/admin/support/", true)
}
}
}