summaryrefslogtreecommitdiff
path: root/garden/src/pages/council/auditdetail.tsx
blob: d259b56917c8dd15bbce112aba7fcc6357222781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { createSignal, onMount, Show, For } from "solid-js";
import { useParams, A } from "@solidjs/router";
import { api } from "../../api";
import { auth } from "../../store/auth";
import type { AuditLogDetail } from "../../types/admin";
import { AUDIT_ACTION_LABELS } from "../../types/admin";
import StaffGuard from "../../components/StaffGuard";

interface FieldChange {
  field: string;
  old: string | number | boolean | null;
  new: string | number | boolean | null;
}

export default function CouncilAuditDetail() {
  const params = useParams();
  const [entry, setEntry] = createSignal<AuditLogDetail | null>(null);
  const [error, setError] = createSignal("");

  onMount(async () => {
    const response = await api<AuditLogDetail>(`/council/audit/${params.ref}`, {
      token: auth.token(),
    });
    if (response.ok) {
      setEntry(response.data);
    } else {
      setError("Audit log not found.");
    }
  });

  function formatDate(date: string) {
    const d = new Date(date);
    return d.toLocaleDateString() + " " + d.toLocaleTimeString();
  }

  function actionLabel(action: string) {
    return AUDIT_ACTION_LABELS[action] || action;
  }

  function parseDetails(details: string): Record<string, unknown> | null {
    if (!details) return null;
    try {
      return JSON.parse(details);
    } catch {
      return null;
    }
  }

  function formatValue(val: string | number | boolean | null | undefined): string {
    if (val === null || val === undefined) return "—";
    if (typeof val === "boolean") return val ? "Yes" : "No";
    return String(val);
  }

  function renderDetails(action: string, data: Record<string, unknown>) {
    switch (action) {
      case "user.ban":
        return renderBanDetails(data as { reason: string; system_ref: string });
      case "user.disable":
        return renderDisableDetails(data as { reason: string; disabled_until: string | null; system_ref: string });
      case "user.role_change":
        return renderRoleChange(data as { old_role: string; new_role: string });
      case "user.warn":
        return renderWarning(data as { warning_ref: string; title: string; message: string });
      case "user.unwarn":
        return renderDeactivateWarning(data as { warning_ref: string });
      case "user.edit":
        return renderEditUser(data as { changes: FieldChange[] });
      default:
        return renderGeneric(data);
    }
  }

  function renderBanDetails(data: { reason: string; system_ref: string }) {
    return (
      <div class="council-detail-table">
        <div class="council-detail-row">
          <span class="council-detail-label">Reason</span>
          <span class="council-detail-value council-detail-html" innerHTML={data.reason} />
        </div>
        <div class="council-detail-row">
          <span class="council-detail-label">Letter Ref</span>
          <span class="council-detail-value council-audit-ref">{data.system_ref}</span>
        </div>
      </div>
    );
  }

  function renderDisableDetails(data: { reason: string; disabled_until: string | null; system_ref: string }) {
    return (
      <div class="council-detail-table">
        <div class="council-detail-row">
          <span class="council-detail-label">Reason</span>
          <span class="council-detail-value council-detail-html" innerHTML={data.reason} />
        </div>
        <Show when={data.disabled_until}>
          <div class="council-detail-row">
            <span class="council-detail-label">Until</span>
            <span class="council-detail-value">{formatDate(data.disabled_until!)}</span>
          </div>
        </Show>
        <div class="council-detail-row">
          <span class="council-detail-label">Letter Ref</span>
          <span class="council-detail-value council-audit-ref">{data.system_ref}</span>
        </div>
      </div>
    );
  }

  function renderRoleChange(data: { old_role: string; new_role: string }) {
    return (
      <div class="council-detail-table">
        <div class="council-detail-row">
          <span class="council-detail-label">Old Role</span>
          <span class="council-detail-value">
            <span class={`council-role council-role-${data.old_role}`}>{data.old_role}</span>
          </span>
        </div>
        <div class="council-detail-row">
          <span class="council-detail-label">New Role</span>
          <span class="council-detail-value">
            <span class={`council-role council-role-${data.new_role}`}>{data.new_role}</span>
          </span>
        </div>
      </div>
    );
  }

  function renderWarning(data: { warning_ref: string; title: string; message: string }) {
    return (
      <div class="council-detail-table">
        <div class="council-detail-row">
          <span class="council-detail-label">Ref</span>
          <span class="council-detail-value council-audit-ref">{data.warning_ref}</span>
        </div>
        <div class="council-detail-row">
          <span class="council-detail-label">Title</span>
          <span class="council-detail-value">{data.title}</span>
        </div>
        <div class="council-detail-row">
          <span class="council-detail-label">Message</span>
          <span class="council-detail-value council-detail-html" innerHTML={data.message} />
        </div>
      </div>
    );
  }

  function renderDeactivateWarning(data: { warning_ref: string }) {
    return (
      <div class="council-detail-table">
        <div class="council-detail-row">
          <span class="council-detail-label">Warning Ref</span>
          <span class="council-detail-value council-audit-ref">{data.warning_ref}</span>
        </div>
      </div>
    );
  }

  function renderEditUser(data: { changes: FieldChange[] }) {
    return (
      <div class="council-detail-table">
        <For each={data.changes}>
          {(change: FieldChange) => (
            <div class="council-detail-row">
              <span class="council-detail-label">{change.field}</span>
              <span class="council-detail-value council-audit-change">
                <Show when={change.old !== null && change.old !== undefined}>
                  <span class="council-audit-old">{formatValue(change.old)}</span>
                  <span class="council-audit-arrow">→</span>
                </Show>
                <span class="council-audit-new">{formatValue(change.new)}</span>
              </span>
            </div>
          )}
        </For>
      </div>
    );
  }

  function renderGeneric(data: Record<string, unknown>) {
    return (
      <div class="council-detail-table">
        <For each={Object.entries(data)}>
          {([key, val]: [string, unknown]) => (
            <div class="council-detail-row">
              <span class="council-detail-label">{key}</span>
              <span class="council-detail-value">{typeof val === "object" ? JSON.stringify(val) : formatValue(val as string | number | boolean | null)}</span>
            </div>
          )}
        </For>
      </div>
    );
  }

  return (
    <StaffGuard>
    <section>
      <A href="/council/auditlog" class="council-detail-back">← Back to Audit Log</A>
      <h2 class="page-title">Audit Log Detail</h2>

      <Show when={error()}>
        <div class="form-error">{error()}</div>
      </Show>

      <Show when={entry()}>
        {(e) => {
          const details = parseDetails(e().details);
          return (
            <>
              <div class="council-detail-section">
                <div class="council-detail-section-header">Entry</div>
                <div class="council-detail-table">
                  <div class="council-detail-row">
                    <span class="council-detail-label">Ref</span>
                    <span class="council-detail-value council-audit-ref">{e().system_ref}</span>
                  </div>
                  <div class="council-detail-row">
                    <span class="council-detail-label">Date</span>
                    <span class="council-detail-value">{formatDate(e().created_at)}</span>
                  </div>
                  <div class="council-detail-row">
                    <span class="council-detail-label">Action</span>
                    <span class="council-detail-value council-audit-action">{actionLabel(e().action)}</span>
                  </div>
                  <div class="council-detail-row">
                    <span class="council-detail-label">Actor</span>
                    <span class="council-detail-value">
                      <A href={`/council/users/${e().actor}`}>{e().actor}</A>
                    </span>
                  </div>
                  <div class="council-detail-row">
                    <span class="council-detail-label">Target</span>
                    <span class="council-detail-value">
                      <span class="council-audit-target-type">{e().target_type}</span>
                      <Show when={e().target_type === "user"} fallback={<span>{e().target_ref}</span>}>
                        <A href={`/council/users/${e().target_ref}`}>{e().target_ref}</A>
                      </Show>
                    </span>
                  </div>
                  <div class="council-detail-row">
                    <span class="council-detail-label">Summary</span>
                    <span class="council-detail-value">{e().summary}</span>
                  </div>
                </div>
              </div>

              <Show when={details}>
                <div class="council-detail-section">
                  <div class="council-detail-section-header">Details</div>
                  {renderDetails(e().action, details!)}
                </div>
              </Show>
            </>
          );
        }}
      </Show>
    </section>
    </StaffGuard>
  );
}