SPB Git

Repository Public

Scheduled.tsx 7.9 KB 181 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : SP Mail8 * File             : web/src/ui/admin/automation/Scheduled.tsx9 * Module           : ui10 * Repository       : https://git.spboucher.ai/sp-mail11 * Project Website  : https://mail.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Onglet « Messages programmés » : envois en attente de leur heure (toutes boîtes, toutes clés), avec annulation.44import { useState } from "react"45import { addMessages, t } from "../../../i18n"46import { enAdmAutomation } from "../../../i18n/en.adm.automation"47import { frAdmAutomation } from "../../../i18n/fr.adm.automation"48import { apiDelete, apiGet } from "../../../jmap/http"49import { toast } from "../../../state/app"50import { Dialog } from "../../kit"51import { toastFail } from "../kit"52import { DASH, Loadable, asArray, asString, fmtWhen, isRecord, useLoad } from "../shared"53import "./automation.css"5455addMessages("fr", frAdmAutomation)56addMessages("en", enAdmAutomation)5758interface Row {59  id: string60  from: string61  to: string[]62  sendAt: string | null63  createdAt: string | null64  external: boolean65  byKey: boolean66}67function parseRow(raw: unknown): Row | null {68  if (!isRecord(raw)) return null69  const id = asString(raw.id)70  if (!id) return null71  const to = asArray(raw.recipients).flatMap((r) => (isRecord(r) && asString(r.address) ? [asString(r.address)!] : []))72  return { id, from: asString(raw.from) ?? DASH, to, sendAt: asString(raw.send_at), createdAt: asString(raw.created_at), external: raw.external === true, byKey: asString(raw.key_id) !== null }73}74const loadScheduled = async (signal: AbortSignal): Promise<Row[]> => asArray(await apiGet<unknown>("/api/admin/scheduled", signal)).map(parseRow).filter((r): r is Row => r !== null)7576export default function ScheduledTab() {77  const load = useLoad(loadScheduled)78  const [asking, setAsking] = useState<Row | null>(null)79  const [busy, setBusy] = useState(false)80  const [gone, setGone] = useState<string[]>([])8182  const cancel = async (row: Row) => {83    setBusy(true)84    try {85      await apiDelete(`/api/admin/scheduled/${encodeURIComponent(row.id)}`)86      setGone((g) => [...g, row.id])87      toast({ kind: "success", text: t("adm.sched.cancelled") })88    } catch (e) {89      toastFail(e, { not_cancellable: t("adm.sched.err.notCancellable"), not_found: t("adm.sched.err.notFound") })90    } finally {91      setBusy(false)92      setAsking(null)93      load.reload()94    }95  }9697  return (98    <Loadable load={load} toolbar={<p className="muted small adm-grow">{t("adm.sched.desc")}</p>}>99      {(all) => {100        const rows = all.filter((r) => !gone.includes(r.id))101        return (102          <>103            <p className="small muted" role="status" aria-live="polite" data-testid="scheduled-count">104              {rows.length === 0 ? t("adm.sched.empty") : t("adm.sched.count", { n: rows.length })}105            </p>106            {rows.length === 0 ? null : (107              <>108                <div className="table-wrap">109                  <table className="table cards-sm" data-testid="scheduled-table">110                    <caption className="sr-only">{t("adm.sched.caption")}</caption>111                    <thead>112                      <tr>113                        <th scope="col">{t("adm.sched.col.when")}</th>114                        <th scope="col">{t("adm.sched.col.from")}</th>115                        <th scope="col">{t("adm.sched.col.to")}</th>116                        <th scope="col">{t("adm.sched.col.origin")}</th>117                        <th scope="col">{t("adm.sched.col.created")}</th>118                        <th scope="col">{t("adm.sched.col.id")}</th>119                        <th scope="col">{t("adm.sched.col.actions")}</th>120                      </tr>121                    </thead>122                    <tbody>123                      {rows.map((r) => (124                        <tr key={r.id} data-testid="scheduled-row" data-outbound-id={r.id}>125                          <td data-label={t("adm.sched.col.when")}>126                            <span className="adm-sched-when">{fmtWhen(r.sendAt)}</span>127                          </td>128                          <td data-label={t("adm.sched.col.from")} className="adm-break">129                            {r.from}130                          </td>131                          <td data-label={t("adm.sched.col.to")}>132                            {r.to.length === 0 ? DASH : r.to.map((a) => (133                              <span key={a} className="adm-sched-to">134                                {a}135                              </span>136                            ))}137                            {r.external ? <span className="badge warn">{t("adm.sched.external")}</span> : null}138                          </td>139                          <td data-label={t("adm.sched.col.origin")}>{r.byKey ? t("adm.sched.origin.key") : t("adm.sched.origin.session")}</td>140                          <td data-label={t("adm.sched.col.created")}>{fmtWhen(r.createdAt)}</td>141                          <td data-label={t("adm.sched.col.id")}>142                            <span className="mono adm-sched-id">{r.id}</span>143                          </td>144                          <td data-label={t("adm.sched.col.actions")}>145                            <button type="button" className="btn small danger" aria-label={t("adm.sched.cancelLabel", { from: r.from, to: r.to.join(", ") || DASH })} onClick={() => setAsking(r)}>146                              {t("adm.sched.cancel")}147                            </button>148                          </td>149                        </tr>150                      ))}151                    </tbody>152                  </table>153                </div>154              </>155            )}156            {asking ? (157              <Dialog158                title={t("adm.sched.cancelTitle")}159                onClose={() => (busy ? undefined : setAsking(null))}160                alert161                footer={162                  <>163                    <button type="button" className="btn" onClick={() => setAsking(null)} disabled={busy} data-autofocus>164                      {t("adm.sched.keep")}165                    </button>166                    <button type="button" className="btn danger solid" onClick={() => void cancel(asking)} disabled={busy} data-testid="scheduled-cancel-confirm">167                      {t("adm.sched.cancel")}168                    </button>169                  </>170                }171              >172                <p>{t("adm.sched.cancelBody", { from: asking.from, to: asking.to.join(", ") || DASH, when: fmtWhen(asking.sendAt) })}</p>173              </Dialog>174            ) : null}175          </>176        )177      }}178    </Loadable>179  )180}181
Files main
Menu

Theme