- Table search, status, tag and sort live in the address; Save view pins one as a favourite. - Dashboard cards can be moved and hidden per user (users.dashboard_layout, migration 0022). - Compare two to four servers' load, memory or disk on one chart. - j/k move through table rows that open a detail panel. - Share links open a read-only wall without signing in, numbers only or with names (share_links, migration 0023); the wall is split into a shared view over two data sources. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
274 lines
12 KiB
TypeScript
274 lines
12 KiB
TypeScript
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";
|
|
import { ErrorNotice } from "../components/Notice";
|
|
import { SearchBox } from "../components/TableToolbar";
|
|
import { useTableSearch } from "../hooks/useTableSearch";
|
|
|
|
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 tableSearch = useTableSearch(data?.tickets);
|
|
const { sorted: sortedTickets, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, undefined, "asc", { url: true });
|
|
|
|
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>
|
|
<ErrorNotice error={error} />
|
|
|
|
{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>
|
|
<SearchBox value={tableSearch.query} onChange={tableSearch.setQuery} className="ms-3" />
|
|
<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>
|
|
)}
|
|
</>
|
|
)}
|
|
</>
|
|
);
|
|
}
|