import m, {MithrilComponent} from '@garage44/mithril' import {Badge, Button, FieldCheckbox, FieldText} from '@garage44/mithril/components' import {minLength, validation} from '@garage44/mithril/validation' import type {Account as AccountRow} from '@/types' import {$s} from '@/app' import {activity, ago, duration, toggleOpen} from '@/lib/account-stats' import {activateAccount, deleteAccount, updateAccount} from '@/lib/accounts' import {closeContext, contextShows, openContext} from '@/lib/layout' import {bind, local} from '@/lib/reactive' import {loadRoles, may, rolesOf, setAccountRoles} from '@/lib/roles' import {formatTimestamp} from '@/lib/utils' import {placeName} from '@/lib/stats' /** The server's own minimum (`MIN_PASSWORD_LEN` in the simulator's `http.rs`). */ export const MIN_PASSWORD = 8 /** The context panel's key for one account's form. */ const contextKey = (id: string) => `account:${id}` /** * Open one account's form in the context panel. * * The form's state lives here, in the closure the panel's functions share, and * not on the row: the row can leave the list while the form is open (a search * that no longer matches it), and the form has to keep working beside it. Every * open starts fresh, as the inline editor did on each "Manage". */ function manage(id: string) { const s = local({ busy: false, /* * Delete is two presses, not a `confirm()`: the native dialog is suppressible, * looks nothing like the rest of the app, and on a phone appears somewhere * unrelated to the row it is about. */ confirming: false, password: '', roles: rolesOf(id), }) // Not `required`: an empty field means "leave the password alone", and the // button is what stays disabled then. const password = validation(bind(s, 'password'), [minLength(MIN_PASSWORD)]) const act = async (fn: () => Promise) => { s.busy = true const ok = await fn() s.busy = false return ok } if (may('roles_manage') && !$s.roles.length) { void loadRoles() } // Read live on every redraw: every action reloads the list, and the panel // must show the row the server handed back, not the one that was clicked. const current = () => $s.accounts.find((a) => a.id === id) const resetPassword = async (account: AccountRow) => { if (!s.password || !password.valid) { return } const ok = await act(() => updateAccount(account.id, {password: s.password}, `Set a new password for ${account.username}`), ) if (ok) { s.password = '' password.reset() } } openContext({ key: contextKey(id), title: () => current()?.username ?? 'Account', body: () => { const account = current() if (!account) { return

This account no longer exists.

} const held = rolesOf(account.id) const roles = s.roles const rolesChanged = roles.slice().sort().join(',') !== held.slice().sort().join(',') // Only an admin may change an admin's account or who is an admin; the server // refuses it from anybody else, so the controls are not offered. const mayTouch = $s.me.admin || !account.admin return (

Created {formatTimestamp(account.created_at)} {/* Personal data, and here because the operator is the person who has to answer a deletion request and needs to be able to find the account it names. */} {account.email ? ` · ${account.email}` : ''}

{may('roles_manage') && ( )} {!mayTouch ? (

Only an admin can change an admin's account.

) : ( <> )}
) }, footer: () => { const account = current() const close = ( ) if (!account || !($s.me.admin || !account.admin)) { return close } const isSelf = account.id === $s.profile.principal return ( <> {s.confirming ? ( <> ) : ( <> {close} )} ) }, }) } /** What an account is, as badges: shared by the row and the form's head. */ class AccountTags extends MithrilComponent<{account: AccountRow; compact?: boolean}> { view(vnode: m.Vnode<{account: AccountRow; compact?: boolean}>) { const {account, compact} = vnode.attrs const isSelf = account.id === $s.profile.principal return ( ) } } interface Props { account: AccountRow } /** A tile of the opened line: one number and what it counts. */ const tile = (n: number, one: string, many = `${one}s`) => ( ) /** What an opened line shows: the account's numbers, then Manage. */ class AccountDetails extends MithrilComponent { view(vnode: m.Vnode) { const {account} = vnode.attrs const stats = activity.stats[account.id] const roles = rolesOf(account.id).map((r) => $s.roles.find((x) => x.id === r)?.name ?? r) const manageButton = ( ) if (stats === undefined) { return } const busiest = Math.max(1, ...(stats?.minutes ?? [])) const total = (stats?.minutes ?? []).reduce((a, b) => a + b, 0) return ( ) } } /** * One line of the account list: who, whether they are here, since when. A tap * opens the line in place with the account's numbers; "Manage…" there opens its * form in the context panel as before, so on a wide screen the list stays in * view beside it. */ export class Account extends MithrilComponent { view(vnode: m.Vnode) { const {account} = vnode.attrs const opened = activity.open === account.id const managed = contextShows(contextKey(account.id)) const seen = activity.seen[account.id] return (
  • {opened && }
  • ) } }