garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/components/log.tsx

raw

import m, {MithrilComponent} from '@garage44/mithril'
import {Card, FieldSegmented} from '@garage44/mithril/components'

import {when} from '@/lib/format'
import {speaker} from '@/lib/names'
import {local} from '@/lib/reactive'
import {sentence} from '@/lib/sentence'
import type {Part} from '@/lib/sentence'
import {$s, loadLog} from '@/lib/store'
import type {WorkspaceEvent} from '@/lib/types'

/** Fields a raw line shows in its own place, so not again in the details. */
const SHOWN = new Set(['ts', 'kind', 'actor', 'node', 'host', 'via_hub_from'])

/** The kinds that are the workspace's routine noise, folded away unless asked for. */
const NOISE = new Set(['ssh_session', 'git_fetch', 'git_serve', 'barrier', 'activity', 'lfs', 'warm_step', 'mcp_call', 'forgejo_call'])

/** Lines drawn at a time: a week of the log is tens of thousands. */
const PAGE = 500

function details(e: WorkspaceEvent): string {
    return Object.entries(e)
        .filter(([k, v]) => !SHOWN.has(k) && v != null && v !== '')
        .map(([k, v]) => `${k}=${typeof v === 'string' ? v : JSON.stringify(v)}`)
        .join('  ')
}

/** A sentence, its names set apart. */
const words = (parts: Part[]) =>
    parts.map((x) =>
        typeof x === 'string' ? (
            x
        ) : (
            <b class="log__name" title={x.full}>
                {x.name}
            </b>
        ),
    )

/** One line: a sentence where there is one for its kind, else its kind, who and its fields as they are. */
function line(e: WorkspaceEvent): m.Children {
    const said = sentence(e)
    const who = [e.node, e.actor ? speaker(e.actor).title : null].filter(Boolean).join(' · ')
    return (
        <li data-kind={e.kind}>
            <span class="log__when">{when(e.ts)}</span>
            {said ? (
                <span class="log__text">{words(said)}</span>
            ) : (
                <span class="log__text">
                    <span class="log__kind">{e.kind.replace(/_/g, ' ')}</span>
                    {who ? <span class="log__who"> · {who}</span> : null}
                    <span class="log__details"> {details(e)}</span>
                </span>
            )}
        </li>
    )
}

/** The hub's event log, newest first: what `midas log` shows. */
export class Log extends MithrilComponent {
    s = local({all: false, shown: PAGE})

    oninit() {
        void loadLog()
    }

    hours = ((s) => ({
        get value() {
            return $s.logHours
        },
        set value(v: number) {
            $s.logHours = v
            s.shown = PAGE
            void loadLog()
        },
    }))(this.s)

    view() {
        const all = this.s.all
        const events = all ? $s.events : $s.events.filter((e) => !NOISE.has(e.kind))
        const more = events.length - this.s.shown
        return (
            <Card
                subtitle={`${events.length} events${all ? '' : `, ${$s.events.length - events.length} routine ones hidden`}`}
                actions={
                    <label class="toggle">
                        <input type="checkbox" checked={all} onchange={(e: Event) => (this.s.all = (e.target as HTMLInputElement).checked)} />{' '}
                        routine too
                    </label>
                }
                flush
            >
                <div class="history__bar">
                    <FieldSegmented
                        model={this.hours}
                        size="sm"
                        options={[
                            {value: 2, label: '2 h'},
                            {value: 6, label: '6 h'},
                            {value: 24, label: 'day'},
                            {value: 168, label: 'week'},
                        ]}
                    />
                </div>
                <ul class="log">{events.slice(0, this.s.shown).map(line)}</ul>
                {more > 0 ? (
                    <p class="hint log__more">
                        The newest {this.s.shown} are shown; {more} older ones are not.{' '}
                        <button class="home__link" type="button" onclick={() => (this.s.shown += PAGE)}>
                            show {Math.min(PAGE, more)} more
                        </button>
                    </p>
                ) : null}
            </Card>
        )
    }
}