import m, {MithrilComponent} from '@garage44/mithril' import {Button, Card, FieldCheckbox, FieldSegmented, FieldText} from '@garage44/mithril/components' import {minLength, required, validation, validations} from '@garage44/mithril/validation' import {$s} from '@/app' import {Account, MIN_PASSWORD} from '@/components/elements/account/account' import {activity} from '@/lib/account-stats' import {createAccount, visibleAccounts} from '@/lib/accounts' import {bind, local} from '@/lib/reactive' class NewAccount extends MithrilComponent { s = local({admin: false, busy: false, open: false, password: '', username: ''}) // `required` trims, so a username of spaces is refused here as it was by the // old `trim().length > 0` check; the server has the rest of the rules. v = validations({ password: validation(bind(this.s, 'password'), [required, minLength(MIN_PASSWORD)]), username: validation(bind(this.s, 'username'), [required]), }) submit = async (e: Event) => { e.preventDefault() const s = this.s if (!this.v.$validate() || s.busy) { return } s.busy = true const ok = await createAccount(s.username.trim(), s.password, s.admin) s.busy = false if (ok) { s.username = '' s.password = '' s.admin = false s.open = false this.v.$reset() } } view() { const s = this.s if (!s.open) { return ( ) } return (
) } } export class Accounts extends MithrilComponent { view() { const rows = visibleAccounts() const total = $s.accounts.length const seen = Object.values(activity.seen) const online = seen.filter((s) => s.online).length const weekAgo = Date.now() / 1000 - 7 * 86_400 const thisWeek = seen.filter((s) => s.online || (s.last_seen ?? 0) >= weekAgo).length return (