Repository Public
1// Onglet « Appareils » : sessions et appareils de notification de tous les utilisateurs (GET /api/admin/devices), filtres par2// utilisateur et plateforme, révocation d'une session et retrait d'un appareil — sous réauthentification par clé d'accès3// (403 reauth_required → invite → nouvel essai), avec le fil des dernières révocations lu dans le journal de sécurité.4import { useId, useState } from "react"5import { filterDevices, handlesOf, parseDevices, parseSecurity, platformFamily, type DeviceSession, type Devices, type PushDevice, type SecurityEvent } from "../../../admin/ops"6import { fmtDateTime, fmtNumber, t } from "../../../i18n"7import { apiDelete, apiGet } from "../../../jmap/http"8import { toast } from "../../../state/app"9import { ConfirmHost, Field, Icon, confirmAsync } from "../../kit"10import { Section } from "../../Page"11import { sensitive, toastFail } from "../kit"12import { DASH, Loadable, useLoad } from "../shared"13import { Stat, ago } from "./common"1415const PLATFORMS = ["web", "mac", "ios", "android", "other"] as const16interface Data {17 devices: Devices18 trail: SecurityEvent[]19}20const userFromUrl = (): string => new URLSearchParams(location.search).get("user") ?? ""2122export default function DevicesTab() {23 const load = useLoad<Data>(async (signal) => {24 const [devices, trail] = await Promise.all([25 apiGet<unknown>("/api/admin/devices", signal).then(parseDevices),26 apiGet<unknown>("/api/admin/security-events?action=admin.&limit=10", signal).then((r) => parseSecurity(r).events, () => [] as SecurityEvent[]),27 ])28 return { devices, trail }29 })30 const [user, setUser] = useState(userFromUrl)31 const [platform, setPlatform] = useState("")32 const [busy, setBusy] = useState<string | null>(null)33 const id = useId()3435 const revoke = async (s: DeviceSession) => {36 const ok = await confirmAsync({ title: t("adm.ops.dev.revoke.title"), body: <p>{t(s.current ? "adm.ops.dev.revoke.bodySelf" : "adm.ops.dev.revoke.body", { user: s.handle, device: s.deviceName ?? s.platform ?? s.kind })}</p>, confirmLabel: t("adm.ops.dev.revoke"), danger: true })37 if (!ok) return38 setBusy(s.id)39 try {40 await sensitive(() => apiDelete(`/api/admin/devices/sessions/${encodeURIComponent(s.id)}`))41 toast({ kind: "success", text: t("adm.ops.dev.revoked", { user: s.handle }) })42 } catch (e) {43 toastFail(e, { not_found: t("adm.ops.dev.gone") })44 } finally {45 setBusy(null)46 load.reload()47 }48 }49 const removePush = async (d: PushDevice) => {50 const ok = await confirmAsync({ title: t("adm.ops.dev.remove.title"), body: <p>{t("adm.ops.dev.remove.body", { user: d.handle })}</p>, confirmLabel: t("adm.ops.dev.remove"), danger: true })51 if (!ok) return52 setBusy(d.id)53 try {54 await sensitive(() => apiDelete(`/api/admin/devices/push/${encodeURIComponent(d.id)}`))55 toast({ kind: "success", text: t("adm.ops.dev.removed", { user: d.handle }) })56 } catch (e) {57 toastFail(e, { not_found: t("adm.ops.dev.gone") })58 } finally {59 setBusy(null)60 load.reload()61 }62 }6364 return (65 <Loadable load={load} toolbar={<p className="muted small adm-grow">{t("adm.ops.dev.note")}</p>}>66 {({ devices, trail }) => {67 const handles = handlesOf(devices)68 const f = { user, platform }69 const sessions = filterDevices(devices.sessions, f)70 const push = filterDevices(devices.push, f)71 return (72 <>73 <div className="adm-stats">74 <Stat label={t("adm.ops.dev.stat.sessions")} value={fmtNumber(devices.sessions.length)} testId="devices-sessions-count" />75 <Stat label={t("adm.ops.dev.stat.push")} value={fmtNumber(devices.push.length)} testId="devices-push-count" />76 <Stat label={t("adm.ops.dev.stat.users")} value={fmtNumber(handles.length)} />77 </div>78 <div className="adm-ops-filters">79 <Field label={t("adm.ops.dev.filter.user")} id={`${id}-u`}>80 <select id={`${id}-u`} className="select" value={handles.includes(user) ? user : ""} onChange={(e) => setUser(e.target.value)} data-testid="devices-user">81 <option value="">{t("adm.ops.dev.filter.allUsers")}</option>82 {handles.map((h) => (83 <option key={h} value={h}>84 {h}85 </option>86 ))}87 </select>88 </Field>89 <Field label={t("adm.ops.dev.filter.platform")} id={`${id}-p`}>90 <select id={`${id}-p`} className="select" value={platform} onChange={(e) => setPlatform(e.target.value)} data-testid="devices-platform">91 <option value="">{t("adm.ops.dev.filter.allPlatforms")}</option>92 {PLATFORMS.map((p) => (93 <option key={p} value={p}>94 {t(`adm.ops.dev.platform.${p}`)}95 </option>96 ))}97 </select>98 </Field>99 </div>100101 <Section title={t("adm.ops.dev.sessions.title", { n: sessions.length })} description={t("adm.ops.dev.sessions.desc")}>102 {sessions.length === 0 ? (103 <p className="muted">{t("adm.ops.dev.sessions.empty")}</p>104 ) : (105 <div className="table-wrap">106 <table className="table cards-sm" data-testid="devices-sessions">107 <caption className="sr-only">{t("adm.ops.dev.sessions.title", { n: sessions.length })}</caption>108 <thead>109 <tr>110 <th scope="col">{t("adm.ops.dev.col.user")}</th>111 <th scope="col">{t("adm.ops.dev.col.device")}</th>112 <th scope="col">{t("adm.ops.dev.col.lastSeen")}</th>113 <th scope="col">{t("adm.ops.dev.col.created")}</th>114 <th scope="col">{t("adm.ops.dev.col.ip")}</th>115 <th scope="col">116 <span className="sr-only">{t("admin.col.actions")}</span>117 </th>118 </tr>119 </thead>120 <tbody>121 {sessions.map((s) => (122 <tr key={s.id} data-session-id={s.id} data-user={s.handle} data-current={s.current}>123 <th scope="row" className="adm-rowhead">124 <span className="mono">{s.handle}</span>125 {s.current ? <span className="badge accent adm-storage-flag">{t("adm.ops.dev.current")}</span> : null}126 </th>127 <td data-label={t("adm.ops.dev.col.device")}>128 <span className="row">129 <Icon name={s.kind === "web" ? "desktop" : "phone"} small />130 <span className="adm-break">131 {s.deviceName ?? DASH}132 <span className="muted small adm-block">133 {t(`adm.ops.dev.platform.${platformFamily(s)}`)}134 {s.platform ? ` · ${s.platform}` : ""}135 </span>136 </span>137 </span>138 </td>139 <td data-label={t("adm.ops.dev.col.lastSeen")} title={s.lastSeenAt ? fmtDateTime(s.lastSeenAt) : undefined}>140 {ago(s.lastSeenAt)}141 </td>142 <td data-label={t("adm.ops.dev.col.created")}>{s.createdAt ? fmtDateTime(s.createdAt) : DASH}</td>143 <td data-label={t("adm.ops.dev.col.ip")} className="mono adm-break">144 {s.ip ?? DASH}145 </td>146 <td>147 <button type="button" className="btn small danger" disabled={busy !== null} onClick={() => void revoke(s)} aria-label={t("adm.ops.dev.revoke.aria", { user: s.handle, device: s.deviceName ?? s.kind })}>148 {t("adm.ops.dev.revoke")}149 </button>150 </td>151 </tr>152 ))}153 </tbody>154 </table>155 </div>156 )}157 </Section>158159 <Section title={t("adm.ops.dev.push.title", { n: push.length })} description={t("adm.ops.dev.push.desc")}>160 {push.length === 0 ? (161 <p className="muted" data-testid="devices-push-empty">162 {t("adm.ops.dev.push.empty")}163 </p>164 ) : (165 <div className="table-wrap">166 <table className="table cards-sm" data-testid="devices-push">167 <caption className="sr-only">{t("adm.ops.dev.push.title", { n: push.length })}</caption>168 <thead>169 <tr>170 <th scope="col">{t("adm.ops.dev.col.user")}</th>171 <th scope="col">{t("adm.ops.dev.col.app")}</th>172 <th scope="col">{t("adm.ops.dev.col.token")}</th>173 <th scope="col">{t("adm.ops.dev.col.created")}</th>174 <th scope="col">175 <span className="sr-only">{t("admin.col.actions")}</span>176 </th>177 </tr>178 </thead>179 <tbody>180 {push.map((d) => (181 <tr key={d.id} data-push-id={d.id} data-user={d.handle}>182 <th scope="row" className="adm-rowhead mono">183 {d.handle}184 </th>185 <td data-label={t("adm.ops.dev.col.app")}>186 <span className="mono adm-break">{d.bundleId ?? DASH}</span>187 {d.environment ? <span className="badge adm-storage-flag">{d.environment}</span> : null}188 </td>189 <td data-label={t("adm.ops.dev.col.token")} className="mono">190 {d.tokenTail ? `…${d.tokenTail}` : DASH}191 </td>192 <td data-label={t("adm.ops.dev.col.created")}>{d.createdAt ? fmtDateTime(d.createdAt) : DASH}</td>193 <td>194 <button type="button" className="btn small danger" disabled={busy !== null} onClick={() => void removePush(d)} aria-label={t("adm.ops.dev.remove.aria", { user: d.handle })}>195 {t("adm.ops.dev.remove")}196 </button>197 </td>198 </tr>199 ))}200 </tbody>201 </table>202 </div>203 )}204 </Section>205206 <Section title={t("adm.ops.dev.trail.title")} description={t("adm.ops.dev.trail.desc")}>207 {trail.length === 0 ? (208 <p className="muted" data-testid="devices-trail-empty">209 {t("adm.ops.dev.trail.empty")}210 </p>211 ) : (212 <ul className="adm-list" data-testid="devices-trail">213 {trail.map((e) => (214 <li key={e.id} className="row wrap" data-action={e.action}>215 <span className="badge warn">{t(e.action === "admin.push.remove" ? "adm.ops.dev.trail.push" : "adm.ops.dev.trail.session")}</span>216 <span>{t("adm.ops.dev.trail.by", { actor: e.actor ?? DASH })}</span>217 <span className="muted small">{e.at ? fmtDateTime(e.at) : DASH}</span>218 </li>219 ))}220 </ul>221 )}222 </Section>223 {/* la console n'est pas rendue dans la coque du courrier : l'hôte des confirmations est monté ici */}224 <ConfirmHost />225 </>226 )227 }}228 </Loadable>229 )230}231