@colveor/admin 0.0.0-stage → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +106 -0
- package/README.md +365 -2
- package/TAILWINDADMIN-LICENSE +21 -0
- package/dist/account/data.d.ts +1 -0
- package/dist/account/data.js +44 -0
- package/dist/account/profile.d.ts +6 -0
- package/dist/account/profile.js +118 -0
- package/dist/account/sessions.d.ts +1 -0
- package/dist/account/sessions.js +26 -0
- package/dist/account/shared.d.ts +26 -0
- package/dist/account/shared.js +44 -0
- package/dist/account/two-factor.d.ts +5 -0
- package/dist/account/two-factor.js +60 -0
- package/dist/actions.d.ts +39 -0
- package/dist/actions.js +81 -0
- package/dist/api/audit.gen.d.ts +80 -0
- package/dist/api/audit.gen.js +43 -0
- package/dist/api/auth.gen.d.ts +537 -0
- package/dist/api/auth.gen.js +313 -0
- package/dist/api/errors.gen.d.ts +16 -0
- package/dist/api/errors.gen.js +2 -0
- package/dist/api/gormx.gen.d.ts +60 -0
- package/dist/api/gormx.gen.js +16 -0
- package/dist/api/health.gen.d.ts +103 -0
- package/dist/api/health.gen.js +23 -0
- package/dist/api/index.gen.d.ts +21 -0
- package/dist/api/index.gen.js +22 -0
- package/dist/api/invitations.gen.d.ts +128 -0
- package/dist/api/invitations.gen.js +57 -0
- package/dist/api/ledger.gen.d.ts +269 -0
- package/dist/api/ledger.gen.js +113 -0
- package/dist/api/msgtemplate.gen.d.ts +31 -0
- package/dist/api/msgtemplate.gen.js +2 -0
- package/dist/api/notifications.gen.d.ts +66 -0
- package/dist/api/notifications.gen.js +28 -0
- package/dist/api/outbox.gen.d.ts +8 -0
- package/dist/api/outbox.gen.js +2 -0
- package/dist/api/queue.gen.d.ts +112 -0
- package/dist/api/queue.gen.js +15 -0
- package/dist/api/rbac.gen.d.ts +406 -0
- package/dist/api/rbac.gen.js +191 -0
- package/dist/api/reauth.gen.d.ts +289 -0
- package/dist/api/reauth.gen.js +122 -0
- package/dist/api/recorded_payments.gen.d.ts +171 -0
- package/dist/api/recorded_payments.gen.js +79 -0
- package/dist/api/sessions.gen.d.ts +64 -0
- package/dist/api/sessions.gen.js +48 -0
- package/dist/api/settings.gen.d.ts +147 -0
- package/dist/api/settings.gen.js +69 -0
- package/dist/api/storage.gen.d.ts +327 -0
- package/dist/api/storage.gen.js +208 -0
- package/dist/api/support_access.gen.d.ts +155 -0
- package/dist/api/support_access.gen.js +131 -0
- package/dist/api/users.gen.d.ts +407 -0
- package/dist/api/users.gen.js +159 -0
- package/dist/api/verification.gen.d.ts +427 -0
- package/dist/api/verification.gen.js +280 -0
- package/dist/api/webhooks.gen.d.ts +110 -0
- package/dist/api/webhooks.gen.js +45 -0
- package/dist/auth-layout.d.ts +9 -0
- package/dist/auth-layout.js +5 -0
- package/dist/banner.d.ts +1 -0
- package/dist/banner.js +49 -0
- package/dist/client.d.ts +41 -0
- package/dist/client.js +72 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.js +82 -0
- package/dist/dates.d.ts +4 -0
- package/dist/dates.js +16 -0
- package/dist/header.d.ts +12 -0
- package/dist/header.js +105 -0
- package/dist/icons.d.ts +2 -0
- package/dist/icons.js +15 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +42 -0
- package/dist/kit/avatar.d.ts +5 -0
- package/dist/kit/avatar.js +16 -0
- package/dist/kit/badge.d.ts +10 -0
- package/dist/kit/badge.js +31 -0
- package/dist/kit/card.d.ts +12 -0
- package/dist/kit/card.js +7 -0
- package/dist/kit/code-area.d.ts +15 -0
- package/dist/kit/code-area.js +69 -0
- package/dist/kit/dropdown.d.ts +26 -0
- package/dist/kit/dropdown.js +67 -0
- package/dist/kit/filter-tabs.d.ts +9 -0
- package/dist/kit/filter-tabs.js +7 -0
- package/dist/kit/modal.d.ts +8 -0
- package/dist/kit/modal.js +15 -0
- package/dist/kit/page-header.d.ts +13 -0
- package/dist/kit/page-header.js +13 -0
- package/dist/kit/search.d.ts +4 -0
- package/dist/kit/search.js +16 -0
- package/dist/kit/tooltip.d.ts +17 -0
- package/dist/kit/tooltip.js +52 -0
- package/dist/message-editor.d.ts +9 -0
- package/dist/message-editor.js +80 -0
- package/dist/money.d.ts +10 -0
- package/dist/money.js +17 -0
- package/dist/next.d.ts +6 -0
- package/dist/next.js +19 -0
- package/dist/pages/account.d.ts +1 -0
- package/dist/pages/account.js +47 -0
- package/dist/pages/audit.d.ts +1 -0
- package/dist/pages/audit.js +88 -0
- package/dist/pages/forgot-password.d.ts +1 -0
- package/dist/pages/forgot-password.js +73 -0
- package/dist/pages/ledger.d.ts +1 -0
- package/dist/pages/ledger.js +132 -0
- package/dist/pages/roles.d.ts +1 -0
- package/dist/pages/roles.js +102 -0
- package/dist/pages/settings.d.ts +1 -0
- package/dist/pages/settings.js +86 -0
- package/dist/pages/sign-in.d.ts +5 -0
- package/dist/pages/sign-in.js +117 -0
- package/dist/pages/status.d.ts +1 -0
- package/dist/pages/status.js +122 -0
- package/dist/pages/support.d.ts +1 -0
- package/dist/pages/support.js +90 -0
- package/dist/pages/teams.d.ts +1 -0
- package/dist/pages/teams.js +94 -0
- package/dist/pages/users.d.ts +1 -0
- package/dist/pages/users.js +158 -0
- package/dist/pages/webhooks.d.ts +1 -0
- package/dist/pages/webhooks.js +88 -0
- package/dist/permissions.d.ts +5 -0
- package/dist/permissions.js +31 -0
- package/dist/proof.d.ts +10 -0
- package/dist/proof.js +78 -0
- package/dist/proxy.d.ts +10 -0
- package/dist/proxy.js +166 -0
- package/dist/retry.d.ts +8 -0
- package/dist/retry.js +30 -0
- package/dist/router.d.ts +13 -0
- package/dist/router.js +14 -0
- package/dist/setting-fields.d.ts +12 -0
- package/dist/setting-fields.js +102 -0
- package/dist/setting-list.d.ts +22 -0
- package/dist/setting-list.js +78 -0
- package/dist/shell.d.ts +40 -0
- package/dist/shell.js +97 -0
- package/dist/support.d.ts +37 -0
- package/dist/support.js +76 -0
- package/dist/table.d.ts +40 -0
- package/dist/table.js +73 -0
- package/dist/ui.d.ts +39 -0
- package/dist/ui.js +56 -0
- package/dist/user-actions.d.ts +30 -0
- package/dist/user-actions.js +129 -0
- package/dist/user-roles.d.ts +4 -0
- package/dist/user-roles.js +51 -0
- package/dist/user-row-menu.d.ts +14 -0
- package/dist/user-row-menu.js +45 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/package.json +63 -4
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
// The ledger's admin side (docs/ledger.md): the adjustments waiting for a
|
|
4
|
+
// second admin (ledger.approve), an account looked up by ID with freeze and
|
|
5
|
+
// unfreeze (ledger.manage), and a request for an adjustment (ledger.adjust,
|
|
6
|
+
// with re-authentication). Every change asks for a reason. Shown only when
|
|
7
|
+
// the ledger module is on (its permissions exist only then).
|
|
8
|
+
import { useEffect, useState } from "react";
|
|
9
|
+
import { Cancelled, useAction } from "../actions";
|
|
10
|
+
import * as ledger from "../api/ledger.gen";
|
|
11
|
+
import { ApiError, api } from "../client";
|
|
12
|
+
import { formatDateTime } from "../dates";
|
|
13
|
+
import { PageHeader } from "../kit/page-header";
|
|
14
|
+
import { formatMoney } from "../money";
|
|
15
|
+
import { useCan } from "../shell";
|
|
16
|
+
import { DataTable } from "../table";
|
|
17
|
+
import { Alert } from "../ui";
|
|
18
|
+
const input = "w-full rounded-lg border border-gray-300 bg-transparent px-3 py-2 text-sm text-gray-800 focus:border-brand-300 focus:outline-none dark:border-gray-700 dark:text-white/90";
|
|
19
|
+
const card = "space-y-4 rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]";
|
|
20
|
+
const small = "rounded-lg border border-gray-300 px-3 py-1.5 text-xs text-gray-700 hover:bg-gray-50 disabled:opacity-40 dark:border-gray-700 dark:text-gray-300";
|
|
21
|
+
/** Runs a change, showing its error unless the user cancelled a dialog. */
|
|
22
|
+
function useChange() {
|
|
23
|
+
const { run, dialogs } = useAction();
|
|
24
|
+
const [error, setError] = useState("");
|
|
25
|
+
const change = async (fn) => {
|
|
26
|
+
setError("");
|
|
27
|
+
try {
|
|
28
|
+
return await run(fn);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
if (!(err instanceof Cancelled)) {
|
|
32
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
return { change, error, dialogs };
|
|
38
|
+
}
|
|
39
|
+
export default function LedgerPage() {
|
|
40
|
+
const canApprove = useCan(ledger.PermissionApprove);
|
|
41
|
+
const canManage = useCan(ledger.PermissionManage);
|
|
42
|
+
const canAdjust = useCan(ledger.PermissionAdjust);
|
|
43
|
+
const [accountId, setAccountId] = useState("");
|
|
44
|
+
return (_jsxs("div", { className: "space-y-6", children: [_jsx(PageHeader, { title: "Ledger" }), canApprove && _jsx(Approvals, { onAccount: setAccountId }), canManage && _jsx(AccountLookup, { id: accountId, onId: setAccountId }), canAdjust && _jsx(RequestAdjustment, { accountId: accountId })] }));
|
|
45
|
+
}
|
|
46
|
+
function Approvals({ onAccount }) {
|
|
47
|
+
const [status, setStatus] = useState(ledger.AdjustmentPending);
|
|
48
|
+
const [reload, setReload] = useState(0);
|
|
49
|
+
const { change, error, dialogs } = useChange();
|
|
50
|
+
const decide = async (a, approve) => {
|
|
51
|
+
const done = await change((o) => api.post(`/v1/ledger/adjustments/${a.id}/${approve ? "approve" : "reject"}`, undefined, o));
|
|
52
|
+
if (done !== undefined) {
|
|
53
|
+
setReload((n) => n + 1);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
return (_jsxs("section", { className: card, children: [_jsxs("div", { className: "flex flex-wrap items-center justify-between gap-3", children: [_jsx("h2", { className: "font-medium text-gray-800 dark:text-white/90", children: "Adjustments" }), _jsxs("select", { "aria-label": "Status", value: status, onChange: (e) => setStatus(e.target.value), className: `${input} max-w-48`, children: [_jsx("option", { value: ledger.AdjustmentPending, children: "Waiting for approval" }), _jsx("option", { value: ledger.AdjustmentApproved, children: "Approved" }), _jsx("option", { value: ledger.AdjustmentRejected, children: "Rejected" })] })] }), error !== "" && _jsx(Alert, { children: error }), _jsx(DataTable, { path: "/v1/ledger/adjustments", query: { status }, reload: reload, empty: "None.", columns: [
|
|
57
|
+
{ header: "Requested", cell: (a) => formatDateTime(a.created_at) },
|
|
58
|
+
{
|
|
59
|
+
header: "Account",
|
|
60
|
+
cell: (a) => (_jsx("button", { type: "button", onClick: () => onAccount(a.account_id), className: "font-mono text-xs text-brand-500 hover:underline", children: a.account_id })),
|
|
61
|
+
},
|
|
62
|
+
{ header: "Amount", cell: (a) => formatMoney(a.amount) },
|
|
63
|
+
{ header: "Reason", cell: (a) => a.reason },
|
|
64
|
+
{
|
|
65
|
+
header: "By",
|
|
66
|
+
cell: (a) => _jsx("span", { className: "font-mono text-xs", children: a.requested_by }),
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
header: "",
|
|
70
|
+
cell: (a) => a.status === ledger.AdjustmentPending && (_jsxs("span", { className: "flex gap-2", children: [_jsx("button", { type: "button", className: small, onClick: () => decide(a, true), children: "Approve" }), _jsx("button", { type: "button", className: small, onClick: () => decide(a, false), children: "Reject" })] })),
|
|
71
|
+
},
|
|
72
|
+
] }), _jsx("p", { className: "text-xs text-gray-500 dark:text-gray-400", children: "The admin who requested an adjustment can't decide it." }), dialogs] }));
|
|
73
|
+
}
|
|
74
|
+
function AccountLookup({ id, onId }) {
|
|
75
|
+
const [account, setAccount] = useState(null);
|
|
76
|
+
const [loadError, setLoadError] = useState("");
|
|
77
|
+
const { change, error, dialogs } = useChange();
|
|
78
|
+
// The account typed in, or picked in the adjustments table.
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (id === "") {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
let current = true;
|
|
84
|
+
setLoadError("");
|
|
85
|
+
setAccount(null);
|
|
86
|
+
api
|
|
87
|
+
.get(`/v1/ledger/accounts/${id}`)
|
|
88
|
+
.then((a) => current && setAccount(a))
|
|
89
|
+
.catch((err) => current &&
|
|
90
|
+
setLoadError(err instanceof ApiError && err.status === 404 ? "No account has this ID." : err.message));
|
|
91
|
+
return () => {
|
|
92
|
+
current = false;
|
|
93
|
+
};
|
|
94
|
+
}, [id]);
|
|
95
|
+
const find = (event) => {
|
|
96
|
+
event.preventDefault();
|
|
97
|
+
onId(String(new FormData(event.currentTarget).get("id")).trim());
|
|
98
|
+
};
|
|
99
|
+
const freeze = async (frozen) => {
|
|
100
|
+
if (account === null) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const res = await change((o) => api.post(`/v1/ledger/accounts/${account.id}/${frozen ? "freeze" : "unfreeze"}`, undefined, o));
|
|
104
|
+
if (res !== undefined) {
|
|
105
|
+
setAccount(res);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
return (_jsxs("section", { className: card, children: [_jsx("h2", { className: "font-medium text-gray-800 dark:text-white/90", children: "Account" }), _jsxs("form", { onSubmit: find, className: "flex gap-2", children: [_jsx("input", { name: "id", required: true, "aria-label": "Account ID", placeholder: "Account ID", defaultValue: id, className: `${input} max-w-md font-mono` }, id), _jsx("button", { type: "submit", className: "rounded-lg bg-brand-500 px-4 text-sm font-medium text-white hover:bg-brand-600", children: "Find" })] }), loadError !== "" && _jsx(Alert, { children: loadError }), error !== "" && _jsx(Alert, { children: error }), account !== null && (_jsxs("div", { className: "grid gap-4 md:grid-cols-[1fr_auto]", children: [_jsxs("dl", { className: "grid grid-cols-[auto_1fr] gap-x-4 gap-y-1.5 text-sm", children: [_jsx("dt", { className: "text-gray-500", children: "Owner" }), _jsxs("dd", { className: "font-mono text-xs", children: [account.owner.type, ": ", account.owner.id] }), _jsx("dt", { className: "text-gray-500", children: "Kind" }), _jsxs("dd", { children: [account.type, " \u00B7 ", account.kind, " \u00B7 ", account.currency] }), _jsx("dt", { className: "text-gray-500", children: "Status" }), _jsx("dd", { className: account.status === ledger.StatusActive ? "" : "text-red-600", children: account.status }), _jsx("dt", { className: "text-gray-500", children: "Balance" }), _jsx("dd", { children: formatMoney(account.balance) }), _jsx("dt", { className: "text-gray-500", children: "Held" }), _jsx("dd", { children: formatMoney(account.held) }), _jsx("dt", { className: "text-gray-500", children: "Available" }), _jsx("dd", { className: "font-medium", children: formatMoney(account.available) })] }), _jsxs("div", { children: [account.status === ledger.StatusActive && (_jsx("button", { type: "button", className: small, onClick: () => freeze(true), children: "Freeze (nothing can leave it)" })), account.status === ledger.StatusFrozen && (_jsx("button", { type: "button", className: small, onClick: () => freeze(false), children: "Unfreeze" }))] })] })), dialogs] }));
|
|
109
|
+
}
|
|
110
|
+
function RequestAdjustment({ accountId }) {
|
|
111
|
+
const [minor, setMinor] = useState("");
|
|
112
|
+
const [currency, setCurrency] = useState("");
|
|
113
|
+
const [result, setResult] = useState(null);
|
|
114
|
+
const { change, error, dialogs } = useChange();
|
|
115
|
+
const submit = async (event) => {
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
const f = new FormData(event.currentTarget);
|
|
118
|
+
const reason = String(f.get("reason")).trim();
|
|
119
|
+
setResult(null);
|
|
120
|
+
// The form's reason is the adjustment's, and the audit log's: the API
|
|
121
|
+
// then asks only for re-authentication.
|
|
122
|
+
const res = await change((o) => api.post("/v1/ledger/adjustments", { account_id: String(f.get("account_id")).trim(), amount: Number(minor), reason }, { ...o, reason: o.reason ?? reason }));
|
|
123
|
+
if (res !== undefined) {
|
|
124
|
+
setResult(res);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
return (_jsxs("section", { className: card, children: [_jsx("h2", { className: "font-medium text-gray-800 dark:text-white/90", children: "Request an adjustment" }), _jsxs("form", { onSubmit: submit, className: "grid gap-3 md:grid-cols-2", children: [_jsx("input", { name: "account_id", required: true, "aria-label": "Account ID", placeholder: "Account ID", defaultValue: accountId, className: `${input} font-mono` }, accountId), _jsxs("div", { className: "flex items-center gap-2", children: [_jsx("input", { required: true, type: "number", step: 1, "aria-label": "Amount in minor units", placeholder: "Minor units: 5000, or -5000 to take", value: minor, onChange: (e) => setMinor(e.target.value), className: input }), _jsx("input", { "aria-label": "Currency, for the preview", placeholder: "NGN", maxLength: 3, value: currency, onChange: (e) => setCurrency(e.target.value.toUpperCase()), className: `${input} w-20` })] }), _jsx("input", { name: "reason", required: true, "aria-label": "Reason", placeholder: "Reason (kept with the adjustment)", className: `${input} md:col-span-2` }), _jsx("p", { className: "text-sm text-gray-500 dark:text-gray-400 md:col-span-2", children: minor !== "" && currency.length === 3
|
|
128
|
+
? `${formatMoney({ amount: Number(minor), currency })}: check it.`
|
|
129
|
+
: "The amount is in minor units (kobo, cents), as the API takes it." }), _jsx("div", { className: "md:col-span-2", children: _jsx("button", { type: "submit", className: "rounded-lg bg-brand-500 px-4 py-2 text-sm font-medium text-white hover:bg-brand-600", children: "Request" }) })] }), error !== "" && _jsx(Alert, { children: error }), result !== null && (_jsxs("p", { className: "text-sm text-gray-700 dark:text-gray-300", children: [formatMoney(result.amount), ":", " ", result.status === ledger.AdjustmentApproved
|
|
130
|
+
? "posted (within the amount one admin may post)."
|
|
131
|
+
: "waiting for a second admin to approve it."] })), dialogs] }));
|
|
132
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function RolesPage(): import("react").JSX.Element;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
// Roles and their permissions: list, create, edit, delete. The API keeps
|
|
4
|
+
// the rules (system roles stay, a role is managed only by a higher one, a
|
|
5
|
+
// permission is granted only by someone holding it) and the page shows its
|
|
6
|
+
// refusals. Every change asks for a reason (useAction).
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
import { Cancelled, useAction } from "../actions";
|
|
9
|
+
import * as rbac from "../api/rbac.gen";
|
|
10
|
+
import { api } from "../client";
|
|
11
|
+
import { PageHeader } from "../kit/page-header";
|
|
12
|
+
import { PermissionPicker } from "../permissions";
|
|
13
|
+
import { useCan } from "../shell";
|
|
14
|
+
import { Alert } from "../ui";
|
|
15
|
+
const input = "w-full rounded-lg border border-gray-300 bg-transparent px-3 py-2 text-sm text-gray-800 focus:border-brand-300 focus:outline-none disabled:opacity-60 dark:border-gray-700 dark:text-white/90";
|
|
16
|
+
const primary = "rounded-lg bg-brand-500 px-4 py-2 text-sm font-medium text-white hover:bg-brand-600 disabled:opacity-40";
|
|
17
|
+
const secondary = "rounded-lg border border-gray-300 px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 disabled:opacity-40 dark:border-gray-700 dark:text-gray-300";
|
|
18
|
+
export default function RolesPage() {
|
|
19
|
+
const [roles, setRoles] = useState(null);
|
|
20
|
+
// The role open in the editor, or "new".
|
|
21
|
+
const [open, setOpen] = useState(null);
|
|
22
|
+
const [error, setError] = useState("");
|
|
23
|
+
const canManage = useCan(rbac.PermissionManage);
|
|
24
|
+
const load = () => api
|
|
25
|
+
.get("/v1/rbac/roles")
|
|
26
|
+
.then(setRoles)
|
|
27
|
+
.catch((err) => setError(err.message));
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
load();
|
|
30
|
+
}, []);
|
|
31
|
+
const role = roles?.find((r) => r.id === open);
|
|
32
|
+
return (_jsxs("div", { className: "space-y-6", children: [_jsx(PageHeader, { title: "Roles", actions: canManage &&
|
|
33
|
+
open === null && (_jsx("button", { type: "button", className: primary, onClick: () => setOpen("new"), children: "New role" })) }), error !== "" && _jsx(Alert, { children: error }), open !== null ? (_jsx(RoleEditor, { role: open === "new" ? undefined : role, canManage: canManage, onDone: () => {
|
|
34
|
+
setOpen(null);
|
|
35
|
+
load();
|
|
36
|
+
} })) : (
|
|
37
|
+
// Small by design (the roles a project declares): not paged.
|
|
38
|
+
_jsx("div", { className: "overflow-hidden rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]", children: _jsxs("table", { className: "min-w-full text-left text-sm", children: [_jsx("thead", { className: "border-b border-gray-100 text-gray-500 dark:border-gray-800 dark:text-gray-400", children: _jsxs("tr", { children: [_jsx("th", { className: "px-5 py-3 font-medium", children: "Role" }), _jsx("th", { className: "px-5 py-3 font-medium", children: "Level" }), _jsx("th", { className: "px-5 py-3 font-medium", children: "Permissions" })] }) }), _jsx("tbody", { className: "divide-y divide-gray-100 text-gray-800 dark:divide-gray-800 dark:text-white/90", children: [...(roles ?? [])]
|
|
39
|
+
.sort((a, b) => b.level - a.level)
|
|
40
|
+
.map((r) => (_jsxs("tr", { onClick: () => setOpen(r.id), className: "cursor-pointer hover:bg-gray-50 dark:hover:bg-white/5", children: [_jsxs("td", { className: "px-5 py-3", children: [_jsx("span", { className: "font-medium", children: r.name }), " ", _jsx("span", { className: "font-mono text-xs text-gray-500", children: r.slug }), r.system && _jsx(Badge, { children: "built in" }), r.default && _jsx(Badge, { children: "new users" })] }), _jsx("td", { className: "px-5 py-3", children: r.level }), _jsx("td", { className: "px-5 py-3 text-gray-500 dark:text-gray-400", children: r.grants_all ? "Every permission" : r.permissions.length })] }, r.id))) })] }) }))] }));
|
|
41
|
+
}
|
|
42
|
+
function RoleEditor(props) {
|
|
43
|
+
const { role, canManage } = props;
|
|
44
|
+
const [permissions, setPermissions] = useState(role?.permissions ?? []);
|
|
45
|
+
const [error, setError] = useState("");
|
|
46
|
+
const [busy, setBusy] = useState(false);
|
|
47
|
+
const { run, dialogs } = useAction();
|
|
48
|
+
const editable = canManage;
|
|
49
|
+
const attempt = async (fn) => {
|
|
50
|
+
setError("");
|
|
51
|
+
setBusy(true);
|
|
52
|
+
try {
|
|
53
|
+
await fn();
|
|
54
|
+
props.onDone();
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
if (!(err instanceof Cancelled)) {
|
|
58
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
setBusy(false);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const save = (event) => {
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
const f = new FormData(event.currentTarget);
|
|
68
|
+
const fields = {
|
|
69
|
+
name: String(f.get("name")),
|
|
70
|
+
description: String(f.get("description")),
|
|
71
|
+
level: Number(f.get("level")),
|
|
72
|
+
};
|
|
73
|
+
attempt(async () => {
|
|
74
|
+
if (role === undefined) {
|
|
75
|
+
await run((o) => api.post("/v1/rbac/roles", { ...fields, slug: String(f.get("slug")), permissions }, o));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
// One run, so one reason for both. Sent again after a refusal,
|
|
79
|
+
// the first request sets the same values.
|
|
80
|
+
await run(async ({ reason, reauthToken }) => {
|
|
81
|
+
const o = { reason, reauthToken };
|
|
82
|
+
await api.patch(`/v1/rbac/roles/${role.id}`, { ...fields, default: f.get("default") === "on" }, o);
|
|
83
|
+
if (!role.grants_all && !sameSet(permissions, role.permissions)) {
|
|
84
|
+
await api.put(`/v1/rbac/roles/${role.id}/permissions`, { permissions }, o);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
const remove = () => role !== undefined &&
|
|
90
|
+
window.confirm(`Delete the role ${role.name}? Users lose what it grants.`) &&
|
|
91
|
+
attempt(() => run((o) => api.delete(`/v1/rbac/roles/${role.id}`, o)));
|
|
92
|
+
return (_jsxs(_Fragment, { children: [_jsxs("form", { onSubmit: save, className: "space-y-6 rounded-2xl border border-gray-200 bg-white p-6 dark:border-gray-800 dark:bg-white/[0.03]", children: [error !== "" && _jsx(Alert, { children: error }), _jsxs("div", { className: "grid gap-4 md:grid-cols-2", children: [_jsx(Labelled, { label: "Name", htmlFor: "role-name", children: _jsx("input", { required: true, id: "role-name", name: "name", defaultValue: role?.name, disabled: !editable || role?.system, className: input }) }), _jsx(Labelled, { label: "Slug (fixed once created)", htmlFor: "role-slug", children: _jsx("input", { required: true, id: "role-slug", name: "slug", pattern: "[a-z][a-z0-9_]*", defaultValue: role?.slug, disabled: role !== undefined, className: `${input} font-mono` }) }), _jsx(Labelled, { label: "Description", htmlFor: "role-description", children: _jsx("input", { id: "role-description", name: "description", defaultValue: role?.description, disabled: !editable, className: input }) }), _jsx(Labelled, { label: "Level (manages the roles below it)", htmlFor: "role-level", children: _jsx("input", { required: true, id: "role-level", name: "level", type: "number", min: 0, defaultValue: role?.level ?? 10, disabled: !editable, className: input }) })] }), role !== undefined && (_jsxs("label", { className: "flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300", children: [_jsx("input", { type: "checkbox", name: "default", defaultChecked: role.default, disabled: !editable, className: "size-4 rounded border-gray-300 text-brand-500" }), "Given to new users"] })), _jsxs("div", { children: [_jsx("h2", { className: "mb-3 font-medium text-gray-800 dark:text-white/90", children: "Permissions" }), role?.grants_all ? (_jsx("p", { className: "text-sm text-gray-500 dark:text-gray-400", children: "This role holds every permission, including those added later." })) : (_jsx(PermissionPicker, { value: permissions, onChange: setPermissions, disabled: !editable }))] }), _jsxs("div", { className: "flex flex-wrap gap-3", children: [editable && (_jsx("button", { type: "submit", disabled: busy, className: primary, children: role === undefined ? "Create role" : "Save" })), _jsx("button", { type: "button", className: secondary, onClick: props.onDone, children: "Back" }), editable && role !== undefined && !role.system && (_jsx("button", { type: "button", disabled: busy, onClick: remove, className: "ml-auto rounded-lg px-4 py-2 text-sm text-red-600 hover:bg-red-50 disabled:opacity-40", children: "Delete role" }))] })] }), dialogs] }));
|
|
93
|
+
}
|
|
94
|
+
function sameSet(a, b) {
|
|
95
|
+
return a.length === b.length && a.every((x) => b.includes(x));
|
|
96
|
+
}
|
|
97
|
+
function Labelled(props) {
|
|
98
|
+
return (_jsxs("div", { children: [_jsx("label", { htmlFor: props.htmlFor, className: "mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-400", children: props.label }), props.children] }));
|
|
99
|
+
}
|
|
100
|
+
function Badge({ children }) {
|
|
101
|
+
return (_jsx("span", { className: "ml-2 rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-600 dark:bg-white/5 dark:text-gray-400", children: children }));
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function SettingsPage(): import("react").JSX.Element;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
// The settings page: the groups on the left (one per module, with the
|
|
4
|
+
// title and description its manifest gives), and the open group's settings
|
|
5
|
+
// on the right, each with a form for its type. A change asks for a reason, and a secret one for re-authentication
|
|
6
|
+
// (useAction does both when the API asks). The sms and mail groups can
|
|
7
|
+
// send a test. The project's app/(admin)/(colveor)/settings/page.tsx
|
|
8
|
+
// re-exports it.
|
|
9
|
+
import { useEffect, useMemo, useState } from "react";
|
|
10
|
+
import * as settings from "../api/settings.gen";
|
|
11
|
+
import { api } from "../client";
|
|
12
|
+
import { Card } from "../kit/card";
|
|
13
|
+
import { PageHeader } from "../kit/page-header";
|
|
14
|
+
import { InfoTip } from "../kit/tooltip";
|
|
15
|
+
import { input, SendTest, SettingRow } from "../setting-fields";
|
|
16
|
+
import { useCan } from "../shell";
|
|
17
|
+
import { Alert } from "../ui";
|
|
18
|
+
export default function SettingsPage() {
|
|
19
|
+
const [all, setAll] = useState(null);
|
|
20
|
+
const [groups, setGroups] = useState([]);
|
|
21
|
+
// The group shown on the right: its name ("auth").
|
|
22
|
+
const [open, setOpen] = useState("");
|
|
23
|
+
const [error, setError] = useState("");
|
|
24
|
+
const [filter, setFilter] = useState("");
|
|
25
|
+
const canManage = useCan(settings.PermissionSettingManage);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
// Small by design (one row per declared setting, one group per
|
|
28
|
+
// module): not paged.
|
|
29
|
+
Promise.all([
|
|
30
|
+
api.get("/v1/settings"),
|
|
31
|
+
api.get("/v1/settings/groups"),
|
|
32
|
+
])
|
|
33
|
+
.then(([list, g]) => {
|
|
34
|
+
setAll(list);
|
|
35
|
+
setGroups(g);
|
|
36
|
+
setOpen((o) => o || (g[0]?.name ?? ""));
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => setError(err.message));
|
|
39
|
+
}, []);
|
|
40
|
+
// The settings the search matches, by group name.
|
|
41
|
+
const matches = useMemo(() => {
|
|
42
|
+
const out = new Map();
|
|
43
|
+
const f = filter.trim().toLowerCase();
|
|
44
|
+
for (const s of all ?? []) {
|
|
45
|
+
// The key as shown ("test phones") as well as written ("test_phones").
|
|
46
|
+
const key = s.key.toLowerCase();
|
|
47
|
+
if (f === "" ||
|
|
48
|
+
key.includes(f) ||
|
|
49
|
+
key.replaceAll("_", " ").includes(f) ||
|
|
50
|
+
s.description.toLowerCase().includes(f)) {
|
|
51
|
+
const name = groupOf(s);
|
|
52
|
+
out.set(name, [...(out.get(name) ?? []), s]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}, [all, filter]);
|
|
57
|
+
// A search that leaves the open group empty opens the first that matches.
|
|
58
|
+
const shown = matches.has(open) ? open : (groups.find((g) => matches.has(g.name))?.name ?? open);
|
|
59
|
+
const group = groups.find((g) => g.name === shown);
|
|
60
|
+
const list = matches.get(shown) ?? [];
|
|
61
|
+
// The open group's settings, under headings by the rest of their group
|
|
62
|
+
// ("rate_limit.login"); its own settings first, with no heading.
|
|
63
|
+
const sections = new Map();
|
|
64
|
+
for (const s of list) {
|
|
65
|
+
const rest = s.group === shown ? "" : s.group.slice(shown.length + 1);
|
|
66
|
+
sections.set(rest, [...(sections.get(rest) ?? []), s]);
|
|
67
|
+
}
|
|
68
|
+
const saved = (s) => setAll((current) => current?.map((x) => (x.key === s.key ? s : x)) ?? null);
|
|
69
|
+
return (_jsxs("div", { className: "space-y-6", children: [_jsx(PageHeader, { title: "Settings", actions: _jsx("input", { type: "search", placeholder: "Find a setting", "aria-label": "Find a setting", value: filter, onChange: (e) => setFilter(e.target.value), className: `${input} max-w-xs` }) }), error !== "" && _jsx(Alert, { children: error }), _jsxs("div", { className: "grid items-start gap-6 lg:grid-cols-[20rem_minmax(0,1fr)]", children: [_jsx("nav", { "aria-label": "Groups of settings", className: "space-y-1 rounded-2xl border border-gray-200 bg-white p-3 lg:sticky lg:top-24 lg:max-h-[calc(100vh-8rem)] lg:overflow-y-auto dark:border-gray-800 dark:bg-white/[0.03]", children: groups.map((g) => {
|
|
70
|
+
const count = matches.get(g.name)?.length ?? 0;
|
|
71
|
+
const active = g.name === shown;
|
|
72
|
+
return (_jsxs("div", { className: `flex items-center gap-2 rounded-xl pr-3 transition-all duration-200 ${active
|
|
73
|
+
? "bg-brand-500 text-white shadow-sm"
|
|
74
|
+
: "hover:translate-x-0.5 hover:bg-brand-50 dark:hover:bg-brand-500/10"} ${count === 0 ? "opacity-40" : ""}`, children: [_jsxs("button", { type: "button", disabled: count === 0, onClick: () => setOpen(g.name), className: "flex min-w-0 flex-1 items-center justify-between gap-3 py-3 pl-4 text-left", children: [_jsx("span", { className: `truncate font-medium ${active ? "" : "text-gray-800 dark:text-white/90"}`, children: g.title }), _jsx("span", { className: `rounded-full px-2 py-0.5 text-xs ${active
|
|
75
|
+
? "bg-white/20"
|
|
76
|
+
: "bg-gray-100 text-gray-600 dark:bg-white/5 dark:text-gray-400"}`, children: count })] }), g.description !== "" && (_jsx(InfoTip, { label: `About ${g.title}`, className: active ? "border-white/60 text-white hover:bg-white/15" : undefined, children: g.description }))] }, g.name));
|
|
77
|
+
}) }), group !== undefined && (_jsx(Card, { title: group.title, info: group.description || undefined, children: _jsxs("div", { className: "-mx-6 -mb-6 border-t border-gray-100 dark:border-gray-800", children: [list.length === 0 && (_jsx("p", { className: "px-6 py-5 text-sm text-gray-500 dark:text-gray-400", children: "No setting here matches." })), [...sections].map(([rest, items]) => (_jsxs("section", { children: [rest !== "" && (_jsx("h3", { className: "bg-gray-50 px-6 py-2.5 text-xs font-medium tracking-wide text-gray-500 uppercase dark:bg-white/[0.02] dark:text-gray-400", children: rest.replaceAll(".", " · ").replaceAll("_", " ") })), _jsx("div", { className: "divide-y divide-gray-100 dark:divide-gray-800", children: items.map((s) => (_jsx(SettingRow, { setting: s, canManage: canManage, onSaved: saved }, s.key))) }), canManage && (s0(items) === "sms" || s0(items) === "mail") && (_jsx(SendTest, { channel: s0(items) }))] }, rest)))] }) }, shown))] })] }));
|
|
78
|
+
}
|
|
79
|
+
/** A setting's group on the left: the first part of its key ("auth"). */
|
|
80
|
+
function groupOf(s) {
|
|
81
|
+
return s.key.split(".")[0] ?? s.key;
|
|
82
|
+
}
|
|
83
|
+
/** The settings' own group ("sms"), which has a test to send. */
|
|
84
|
+
function s0(items) {
|
|
85
|
+
return items[0]?.group ?? "";
|
|
86
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
// The sign-in page: with a password, or with a code emailed to the admin
|
|
4
|
+
// (no password), then the second factor when the account has two-factor
|
|
5
|
+
// sign-in on (docs/two-factor.md). The project's app/login/page.tsx
|
|
6
|
+
// re-exports it.
|
|
7
|
+
import { useState } from "react";
|
|
8
|
+
import * as verification from "../api/verification.gen";
|
|
9
|
+
import { AuthLayout } from "../auth-layout";
|
|
10
|
+
import { ApiError, call } from "../client";
|
|
11
|
+
import { ProofForm } from "../proof";
|
|
12
|
+
import { useConfig } from "../shell";
|
|
13
|
+
import { Alert, Button, CodeInput, Field } from "../ui";
|
|
14
|
+
export default function SignInPage({ home = "/" }) {
|
|
15
|
+
const { config, router } = useConfig();
|
|
16
|
+
const { Link } = router;
|
|
17
|
+
const [mode, setMode] = useState("password");
|
|
18
|
+
const [step, setStep] = useState({ kind: "form" });
|
|
19
|
+
const [code, setCode] = useState("");
|
|
20
|
+
const [error, setError] = useState("");
|
|
21
|
+
const [busy, setBusy] = useState(false);
|
|
22
|
+
const attempt = async (fn) => {
|
|
23
|
+
setError("");
|
|
24
|
+
setBusy(true);
|
|
25
|
+
try {
|
|
26
|
+
await fn();
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
setBusy(false);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
// Signed in, or asked for the second factor.
|
|
36
|
+
const done = async (res) => {
|
|
37
|
+
if (res.mfa_required === true && res.mfa_token !== undefined) {
|
|
38
|
+
const { methods } = await call("/v1/reauth/2fa/methods", {
|
|
39
|
+
body: { mfa_token: res.mfa_token },
|
|
40
|
+
});
|
|
41
|
+
setStep({ kind: "mfa", token: res.mfa_token, methods });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const next = new URLSearchParams(window.location.search).get("next");
|
|
45
|
+
// Only a path on this site, never another origin.
|
|
46
|
+
window.location.assign(next?.startsWith("/") && !next.startsWith("//") ? next : home);
|
|
47
|
+
};
|
|
48
|
+
const withPassword = (event) => {
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
const f = new FormData(event.currentTarget);
|
|
51
|
+
attempt(async () => done(await call("/v1/auth/login", {
|
|
52
|
+
body: { email: f.get("email"), password: f.get("password") },
|
|
53
|
+
})));
|
|
54
|
+
};
|
|
55
|
+
const sendCode = (event) => {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
const email = String(new FormData(event.currentTarget).get("email")).trim();
|
|
58
|
+
attempt(async () => {
|
|
59
|
+
const sent = await call("/v1/auth/verifications", {
|
|
60
|
+
body: {
|
|
61
|
+
channel: "email",
|
|
62
|
+
email,
|
|
63
|
+
purpose: verification.PurposeLogin,
|
|
64
|
+
mode: "code",
|
|
65
|
+
},
|
|
66
|
+
idempotencyKey: crypto.randomUUID(),
|
|
67
|
+
});
|
|
68
|
+
setCode("");
|
|
69
|
+
setStep({ kind: "code", email, verificationId: sent.data.verification_id });
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const withCode = (value) => {
|
|
73
|
+
if (step.kind !== "code") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
attempt(async () => {
|
|
77
|
+
const verified = await call("/v1/auth/verifications/verify", {
|
|
78
|
+
body: { verification_id: step.verificationId, code: value },
|
|
79
|
+
});
|
|
80
|
+
await done(await call("/v1/auth/login/verified", {
|
|
81
|
+
body: { verification_token: verified.data.verification_token },
|
|
82
|
+
}));
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
if (step.kind === "mfa") {
|
|
86
|
+
return (_jsx(AuthLayout, { name: config.name, title: "Two-step verification", subtitle: "Your account asks for a second proof that it's you.", children: _jsx(ProofForm, { methods: step.methods, sendCode: async (channel) => {
|
|
87
|
+
const sent = await call("/v1/reauth/2fa/codes", {
|
|
88
|
+
body: { mfa_token: step.token, channel },
|
|
89
|
+
idempotencyKey: crypto.randomUUID(),
|
|
90
|
+
});
|
|
91
|
+
return sent.data.verification_id;
|
|
92
|
+
}, submit: async (proof) => {
|
|
93
|
+
try {
|
|
94
|
+
await done(await call("/v1/reauth/2fa/verify", {
|
|
95
|
+
body: { ...proof, mfa_token: step.token },
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
// The 5 minutes ran out, or it was finished: start again.
|
|
100
|
+
if (err instanceof ApiError && err.code === "auth.invalid_mfa_token") {
|
|
101
|
+
setStep({ kind: "form" });
|
|
102
|
+
setError(err.message);
|
|
103
|
+
}
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
} }) }));
|
|
107
|
+
}
|
|
108
|
+
if (step.kind === "code") {
|
|
109
|
+
return (_jsx(AuthLayout, { name: config.name, title: "Check your email", subtitle: _jsxs(_Fragment, { children: ["We sent a code to ", _jsx("span", { className: "font-medium", children: step.email }), ". Enter it below."] }), footer: _jsx("button", { type: "button", className: "text-brand-500 hover:text-brand-600", onClick: () => setStep({ kind: "form" }), children: "Use a different email" }), children: _jsxs("div", { className: "animate-slide-in space-y-4", children: [error !== "" && _jsx(Alert, { children: error }), _jsx(CodeInput, { value: code, onChange: setCode, onComplete: withCode, disabled: busy }), _jsx(Button, { busy: busy, onClick: () => withCode(code), children: "Sign in" })] }) }));
|
|
110
|
+
}
|
|
111
|
+
return (_jsxs(AuthLayout, { name: config.name, title: "Sign in", subtitle: "Welcome back. Sign in to continue.", children: [_jsx("div", { className: "mb-6 grid grid-cols-2 gap-1 rounded-lg bg-gray-100 p-1 text-sm dark:bg-gray-800", children: ["password", "code"].map((m) => (_jsx("button", { type: "button", onClick: () => {
|
|
112
|
+
setMode(m);
|
|
113
|
+
setError("");
|
|
114
|
+
}, className: `rounded-md px-3 py-2 transition ${mode === m
|
|
115
|
+
? "bg-white text-gray-800 shadow-sm dark:bg-gray-900 dark:text-white/90"
|
|
116
|
+
: "text-gray-500 hover:text-gray-700 dark:text-gray-400"}`, children: m === "password" ? "Password" : "Email code" }, m))) }), error !== "" && _jsx(Alert, { children: error }), mode === "password" ? (_jsxs("form", { className: "animate-fade-in space-y-5", onSubmit: withPassword, children: [_jsx(Field, { label: "Email", name: "email", type: "email", autoComplete: "username" }), _jsx(Field, { label: "Password", name: "password", type: "password", autoComplete: "current-password" }), _jsx("div", { className: "flex justify-end", children: _jsx(Link, { href: "/forgot-password", className: "text-sm text-brand-500 hover:text-brand-600", children: "Forgot password?" }) }), _jsx(Button, { type: "submit", busy: busy, children: "Sign in" })] }, "password")) : (_jsxs("form", { className: "animate-fade-in space-y-5", onSubmit: sendCode, children: [_jsx(Field, { label: "Email", name: "email", type: "email", autoComplete: "username" }), _jsx("p", { className: "text-sm text-gray-500 dark:text-gray-400", children: "We'll email you a code: no password needed." }), _jsx(Button, { type: "submit", busy: busy, children: "Email me a code" })] }, "code"))] }));
|
|
117
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function StatusPage(): import("react").JSX.Element;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
// The system status report (docs/health.md): each check, the job queues
|
|
4
|
+
// and workers, and the API process. Loaded again every 15 seconds (the API
|
|
5
|
+
// runs the checks at most every 5). Warns when the API's colveor-go release
|
|
6
|
+
// isn't this panel's.
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
import { ApiError, api } from "../client";
|
|
9
|
+
import { formatDateTime } from "../dates";
|
|
10
|
+
import { PageHeader } from "../kit/page-header";
|
|
11
|
+
import { Alert } from "../ui";
|
|
12
|
+
import { version } from "../version";
|
|
13
|
+
const TONE = {
|
|
14
|
+
ok: "bg-green-50 text-green-700 dark:bg-green-500/15 dark:text-green-500",
|
|
15
|
+
degraded: "bg-amber-50 text-amber-700 dark:bg-amber-500/15 dark:text-amber-400",
|
|
16
|
+
down: "bg-red-50 text-red-600 dark:bg-red-500/15 dark:text-red-500",
|
|
17
|
+
disabled: "bg-gray-100 text-gray-600 dark:bg-white/5 dark:text-gray-400",
|
|
18
|
+
};
|
|
19
|
+
export default function StatusPage() {
|
|
20
|
+
const [report, setReport] = useState(null);
|
|
21
|
+
const [error, setError] = useState("");
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const load = () => api
|
|
24
|
+
.get("/v1/health/status")
|
|
25
|
+
.then((r) => {
|
|
26
|
+
setReport(r);
|
|
27
|
+
setError("");
|
|
28
|
+
})
|
|
29
|
+
.catch((err) => {
|
|
30
|
+
// Down is a 503 that still carries the report.
|
|
31
|
+
const body = err instanceof ApiError ? err.body : {};
|
|
32
|
+
if (body.checks !== undefined) {
|
|
33
|
+
setReport(body);
|
|
34
|
+
setError("");
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
setError(err.message);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
load();
|
|
41
|
+
const timer = setInterval(load, 15_000);
|
|
42
|
+
return () => clearInterval(timer);
|
|
43
|
+
}, []);
|
|
44
|
+
const queueCheck = report?.checks.queue;
|
|
45
|
+
const others = Object.entries(report?.checks ?? {})
|
|
46
|
+
.filter(([name]) => name !== "queue")
|
|
47
|
+
.sort(([a], [b]) => a.localeCompare(b));
|
|
48
|
+
return (_jsxs("div", { className: "space-y-6", children: [_jsx(PageHeader, { title: "System status", actions: _jsxs(_Fragment, { children: [report !== null && _jsx(Badge, { status: report.status }), report !== null && (_jsxs("span", { className: "text-sm text-gray-500 dark:text-gray-400", children: ["checked ", new Date(report.checked_at).toLocaleTimeString()] }))] }) }), error !== "" && _jsx(Alert, { children: error }), report !== null && _jsx(VersionWarning, { api: report.process.framework_version }), _jsx("div", { className: "grid gap-4 md:grid-cols-2 xl:grid-cols-3", children: others.map(([name, check]) => (_jsx(Card, { title: name, check: check, children: _jsx(Details, { value: check.details }) }, name))) }), queueCheck !== undefined && (_jsx(Card, { title: "queue", check: queueCheck, children: queueCheck.details !== undefined && _jsx(Queues, { stats: queueCheck.details }) })), report !== null && (_jsx(Card, { title: "API process", children: _jsx(Details, { value: {
|
|
49
|
+
app: report.process.app,
|
|
50
|
+
env: report.process.env,
|
|
51
|
+
framework: report.process.framework_version,
|
|
52
|
+
started: formatDateTime(report.process.started_at),
|
|
53
|
+
uptime: duration(report.process.uptime_seconds),
|
|
54
|
+
go: report.process.go_version,
|
|
55
|
+
cpus: report.process.cpus,
|
|
56
|
+
goroutines: report.process.goroutines,
|
|
57
|
+
heap: bytes(report.process.heap_bytes),
|
|
58
|
+
} }) }))] }));
|
|
59
|
+
}
|
|
60
|
+
/** The panel and the API should be on the same colveor-go major and minor. */
|
|
61
|
+
function VersionWarning({ api: apiVersion }) {
|
|
62
|
+
const minor = (v) => v.replace(/^v/, "").split(".").slice(0, 2).join(".");
|
|
63
|
+
if (!apiVersion.startsWith("v") || minor(apiVersion) === minor(version)) {
|
|
64
|
+
// "(devel)" or empty: a local checkout, nothing to compare.
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return (_jsxs(Alert, { children: ["The API runs colveor-go ", apiVersion, ", but this panel's @colveor/admin is ", version, ". Update the one that is behind, so the pages match the API."] }));
|
|
68
|
+
}
|
|
69
|
+
function Card(props) {
|
|
70
|
+
const { check } = props;
|
|
71
|
+
return (_jsxs("section", { className: "rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]", children: [_jsxs("div", { className: "mb-3 flex flex-wrap items-center gap-2", children: [_jsx("h2", { className: "font-medium text-gray-800 dark:text-white/90", children: props.title }), check !== undefined && _jsx(Badge, { status: check.status }), check?.critical && _jsx("span", { className: "text-xs text-gray-500", children: "critical" }), check !== undefined && (_jsxs("span", { className: "ml-auto text-xs text-gray-500", children: [check.latency_ms.toFixed(1), " ms"] }))] }), check?.message && _jsx("p", { className: "mb-3 text-sm text-red-600", children: check.message }), props.children] }));
|
|
72
|
+
}
|
|
73
|
+
function Badge({ status }) {
|
|
74
|
+
return (_jsx("span", { className: `rounded-full px-2.5 py-0.5 text-xs font-medium ${TONE[status] ?? ""}`, children: status }));
|
|
75
|
+
}
|
|
76
|
+
/** A check's details: whatever the check reports, as name and value rows. */
|
|
77
|
+
function Details({ value }) {
|
|
78
|
+
if (value === null || typeof value !== "object") {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
return (_jsx("dl", { className: "grid grid-cols-2 gap-x-4 gap-y-1 text-sm", children: Object.entries(value).map(([k, v]) => (_jsxs("div", { className: "contents", children: [_jsx("dt", { className: "text-gray-500 dark:text-gray-400", children: k.replaceAll("_", " ") }), _jsx("dd", { className: "text-gray-800 dark:text-white/90", children: k.endsWith("_bytes") && typeof v === "number"
|
|
82
|
+
? bytes(v)
|
|
83
|
+
: typeof v === "object"
|
|
84
|
+
? JSON.stringify(v)
|
|
85
|
+
: String(v) })] }, k))) }));
|
|
86
|
+
}
|
|
87
|
+
function Queues({ stats }) {
|
|
88
|
+
const th = "px-3 py-2 font-medium";
|
|
89
|
+
const td = "px-3 py-2";
|
|
90
|
+
return (_jsxs("div", { className: "space-y-5 overflow-x-auto text-sm", children: [_jsxs("table", { className: "min-w-full text-left", children: [_jsx("thead", { className: "text-gray-500 dark:text-gray-400", children: _jsx("tr", { children: [
|
|
91
|
+
"Queue",
|
|
92
|
+
"Pending",
|
|
93
|
+
"Active",
|
|
94
|
+
"Scheduled",
|
|
95
|
+
"Retrying",
|
|
96
|
+
"Archived",
|
|
97
|
+
"Done today",
|
|
98
|
+
"Failed today",
|
|
99
|
+
"Oldest waits",
|
|
100
|
+
].map((h) => (_jsx("th", { className: th, children: h }, h))) }) }), _jsx("tbody", { className: "text-gray-800 dark:text-white/90", children: stats.queues.map((q) => (_jsxs("tr", { children: [_jsxs("td", { className: td, children: [q.name, q.paused && _jsx("span", { className: "ml-2 text-xs text-amber-600", children: "paused" })] }), _jsx("td", { className: td, children: q.pending }), _jsx("td", { className: td, children: q.active }), _jsx("td", { className: td, children: q.scheduled }), _jsx("td", { className: td, children: q.retry }), _jsx("td", { className: q.archived > 0 ? `${td} text-red-600` : td, children: q.archived }), _jsx("td", { className: td, children: q.processed_today }), _jsx("td", { className: q.failed_today > 0 ? `${td} text-red-600` : td, children: q.failed_today }), _jsx("td", { className: td, children: duration(q.latency_seconds) })] }, q.name))) })] }), _jsxs("div", { children: [_jsx("h3", { className: "mb-2 font-medium text-gray-800 dark:text-white/90", children: "Workers" }), stats.workers.length === 0 ? (_jsx("p", { className: "text-red-600", children: "No worker is running: jobs wait until one starts." })) : (_jsx("ul", { className: "space-y-1 text-gray-700 dark:text-gray-300", children: stats.workers.map((w) => (_jsxs("li", { children: [w.host, " (pid ", w.pid, "): ", w.busy, " of ", w.concurrency, " busy on ", w.queues.join(", "), ", up ", duration((Date.now() - Date.parse(w.started_at)) / 1000)] }, `${w.host}-${w.pid}-${w.queues.join()}`))) }))] }), stats.jobs.length > 0 && (_jsxs("table", { className: "min-w-full text-left", children: [_jsx("thead", { className: "text-gray-500 dark:text-gray-400", children: _jsx("tr", { children: ["Job type (today)", "Done", "Failed", "Average", "Last run"].map((h) => (_jsx("th", { className: th, children: h }, h))) }) }), _jsx("tbody", { className: "text-gray-800 dark:text-white/90", children: stats.jobs.map((j) => (_jsxs("tr", { children: [_jsx("td", { className: `${td} font-mono text-xs`, children: j.type }), _jsx("td", { className: td, children: j.processed_today }), _jsx("td", { className: j.failed_today > 0 ? `${td} text-red-600` : td, children: j.failed_today }), _jsxs("td", { className: td, children: [j.average_ms.toFixed(0), " ms"] }), _jsx("td", { className: td, children: new Date(j.last_run_at).toLocaleTimeString() })] }, j.type))) })] }))] }));
|
|
101
|
+
}
|
|
102
|
+
function duration(seconds) {
|
|
103
|
+
if (seconds < 60) {
|
|
104
|
+
return `${Math.round(seconds)} s`;
|
|
105
|
+
}
|
|
106
|
+
if (seconds < 3600) {
|
|
107
|
+
return `${Math.round(seconds / 60)} min`;
|
|
108
|
+
}
|
|
109
|
+
if (seconds < 86_400) {
|
|
110
|
+
return `${(seconds / 3600).toFixed(1)} h`;
|
|
111
|
+
}
|
|
112
|
+
return `${(seconds / 86_400).toFixed(1)} days`;
|
|
113
|
+
}
|
|
114
|
+
function bytes(n) {
|
|
115
|
+
const units = ["B", "KB", "MB", "GB"];
|
|
116
|
+
let i = 0;
|
|
117
|
+
while (n >= 1024 && i < units.length - 1) {
|
|
118
|
+
n /= 1024;
|
|
119
|
+
i++;
|
|
120
|
+
}
|
|
121
|
+
return `${n.toFixed(i === 0 ? 0 : 1)} ${units[i]}`;
|
|
122
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function SupportPage(): import("react").JSX.Element;
|