Add an osTicket integration: list open tickets from its database

osTicket's own REST API only supports creating tickets, not listing or
reading them, so this reads osTicket's MySQL/MariaDB database directly
with a read-only user instead - the only integration in this app that
isn't a REST API. Joins the ticket, status, priority, department,
staff, team, and user tables, filtered to tickets in the "open" state
(status names are customizable per install, but that state flag isn't).

Surfaces per-ticket subject, priority, department, assignee, requester,
and osTicket's own overdue/awaiting-reply flags, plus a page at
/osticket and a Dashboard widget with open/overdue/awaiting-reply
counts.

Not verified against a live instance: unlike the HTTP-based
integrations, there was no way to fake a MySQL server to test against
in this environment, so the query is built from osTicket's published
schema but has never actually run against a real database. See
INTEGRATIONS.md for the read-only grant needed and further caveats.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5 committed 2026-09-29 23:09:56 +02:00
1 parent 69e9325927
commit de5c39dddf
20 files changed
+754 -12

No files matched your search

+268
View File
@@ -0,0 +1,268 @@
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { api, type CurrentUser, type IntegrationSummary, type OsTicketTicket, type OsTicketTicketsResponse } from "../api/client";
import { useSortable } from "../hooks/useSortable";
import SortableTh from "../components/SortableTh";
import { downloadCsv } from "../utils/csv";
import { formatDateTime } from "../utils/date";
import { formatAgo } from "../utils/duration";
function priorityBadge(t: OsTicketTicket) {
if (!t.priorityName) return <span className="text-secondary">—</span>;
const color = t.priorityColor || "#6c757d";
return (
<span className="badge" style={{ backgroundColor: `${color}22`, color, border: `1px solid ${color}55` }}>
{t.priorityName}
</span>
);
}
export default function OsTicket({ user: _user }: { user: CurrentUser }) {
const [integrations, setIntegrations] = useState<IntegrationSummary[] | null>(null);
const [selectedId, setSelectedId] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
const [data, setData] = useState<OsTicketTicketsResponse | null>(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
api.integrations
.list()
.then((res) => {
const osticketIntegrations = res.integrations.filter((i) => i.type === "osticket");
setIntegrations(osticketIntegrations);
if (!selectedId && osticketIntegrations.length > 0) setSelectedId(osticketIntegrations[0].id);
})
.catch((err) => setError(err instanceof Error ? err.message : String(err)));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const selected = integrations?.find((i) => i.id === selectedId) ?? null;
function loadTickets(id: number) {
setLoading(true);
setError(null);
api.integrations.osticket
.tickets(id)
.then((res) => setData(res))
.catch((err) => setError(err instanceof Error ? err.message : String(err)))
.finally(() => setLoading(false));
}
useEffect(() => {
if (selected?.enabled) {
loadTickets(selected.id);
} else {
setData(null);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedId]);
const { sorted: sortedTickets, sortKey, sortDir, requestSort } = useSortable(data?.tickets);
function exportCsv() {
if (!sortedTickets) return;
downloadCsv(
"osticket-open-tickets.csv",
["Number", "Subject", "Status", "Priority", "Department", "Assigned to", "Requester", "Overdue", "Awaiting reply", "Created", "Last activity", "Due"],
sortedTickets.map((t) => [
t.number,
t.subject ?? "",
t.statusName,
t.priorityName ?? "",
t.departmentName ?? "",
t.staffName ?? t.teamName ?? "Unassigned",
t.requesterName ?? t.requesterEmail ?? "",
t.isOverdue ? "yes" : "no",
t.isAnswered ? "no" : "yes",
formatDateTime(new Date(t.createdAt)),
t.lastActivityAt ? formatDateTime(new Date(t.lastActivityAt)) : "",
t.dueAt ? formatDateTime(new Date(t.dueAt)) : "",
]),
);
}
return (
<>
<h2 className="page-title mb-3">osTicket</h2>
{error && <div className="alert alert-danger">{error}</div>}
{integrations?.length === 0 ? (
<div className="card">
<div className="card-body text-secondary">
No osTicket integration configured yet. <Link to="/integrations">Add one under Integrations</Link>.
</div>
</div>
) : (
<>
{integrations && integrations.length > 1 && (
<div className="mb-3" style={{ maxWidth: 320 }}>
<select
className="form-select"
value={selectedId ?? ""}
onChange={(e) => setSelectedId(Number(e.target.value))}
>
{integrations.map((i) => (
<option key={i.id} value={i.id} disabled={!i.enabled}>
{i.name}
{!i.enabled ? " — disabled" : ""}
</option>
))}
</select>
</div>
)}
{selected && !selected.enabled ? (
<div className="card">
<div className="card-body text-secondary">
"{selected.name}" is disabled. Enable it under Integrations → Manage integrations to see its
tickets.
</div>
</div>
) : (
<div className="row g-3">
<div className="col-12">
<div className="row g-3 mb-1">
<div className="col-6 col-md-4">
<div className="card card-sm">
<div className="card-body">
<div className="text-secondary">Open tickets</div>
<div className="h2 mb-0">{data?.summary.total ?? "—"}</div>
</div>
</div>
</div>
<div className="col-6 col-md-4">
<div className="card card-sm">
<div className="card-body">
<div className="text-secondary">Overdue</div>
<div className={`h2 mb-0 ${data && data.summary.overdue > 0 ? "text-red" : ""}`}>
{data?.summary.overdue ?? "—"}
</div>
</div>
</div>
</div>
<div className="col-6 col-md-4">
<div className="card card-sm">
<div className="card-body">
<div className="text-secondary">Awaiting our reply</div>
<div className={`h2 mb-0 ${data && data.summary.awaitingReply > 0 ? "text-yellow" : ""}`}>
{data?.summary.awaitingReply ?? "—"}
</div>
</div>
</div>
</div>
</div>
</div>
<div className="col-12">
<div className="card">
<div className="card-header">
<h3 className="card-title">Open tickets</h3>
<div className="card-actions">
<button
className="btn btn-sm btn-outline-secondary"
onClick={exportCsv}
disabled={!sortedTickets || sortedTickets.length === 0}
>
Export CSV
</button>
<button
className="btn btn-sm btn-outline-secondary"
onClick={() => selected && loadTickets(selected.id)}
disabled={loading}
>
{loading ? "Refreshing…" : "Refresh"}
</button>
</div>
</div>
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<SortableTh<OsTicketTicket> label="#" sortKeyName="number" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<OsTicketTicket> label="Subject" sortKeyName="subject" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<OsTicketTicket>
label="Priority"
sortKeyName="priorityName"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<OsTicketTicket>
label="Department"
sortKeyName="departmentName"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<OsTicketTicket> label="Assigned to" sortKeyName="staffName" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<OsTicketTicket>
label="Requester"
sortKeyName="requesterName"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<OsTicketTicket> label="Created" sortKeyName="createdAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<OsTicketTicket>
label="Last activity"
sortKeyName="lastActivityAt"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<OsTicketTicket>
label="Flags"
sortKeyName="isOverdue"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
</tr>
</thead>
<tbody>
{sortedTickets?.map((t) => (
<tr key={t.ticketId}>
<td>{t.number}</td>
<td>
{t.subject || <span className="text-secondary">(no subject)</span>}
<div className="text-secondary small">{t.statusName}</div>
</td>
<td>{priorityBadge(t)}</td>
<td className="text-secondary">{t.departmentName ?? "—"}</td>
<td className="text-secondary">{t.staffName ?? t.teamName ?? "Unassigned"}</td>
<td className="text-secondary">{t.requesterName ?? t.requesterEmail ?? "—"}</td>
<td className="text-secondary" title={formatDateTime(new Date(t.createdAt))}>
{formatAgo(t.createdAt)}
</td>
<td className="text-secondary">
{t.lastActivityAt ? (
<span title={formatDateTime(new Date(t.lastActivityAt))}>{formatAgo(t.lastActivityAt)}</span>
) : (
"—"
)}
</td>
<td>
{t.isOverdue && <span className="badge bg-red-lt text-red me-1">Overdue</span>}
{!t.isAnswered && <span className="badge bg-yellow-lt text-yellow">Awaiting reply</span>}
</td>
</tr>
))}
{sortedTickets?.length === 0 && (
<tr>
<td colSpan={9} className="text-secondary text-center">
No open tickets.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
</div>
</div>
)}
</>
)}
</>
);
}