garage44.eu/jeroen/midas

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

.. /web/src/components/memory.tsx

raw

import m, {MithrilComponent} from '@garage44/mithril'
import {Alert, Badge, Card, FieldSelect, FieldText} from '@garage44/mithril/components'
import type {Tone} from '@garage44/mithril/components'

import {api} from '@/lib/api'
import {age, when} from '@/lib/format'
import {markdown} from '@/lib/markdown'
import {notes, search} from '@/lib/memory'
import type {MemoryNote} from '@/lib/memory'
import {speaker} from '@/lib/names'
import {bind, local} from '@/lib/reactive'

const TONE: Record<string, Tone> = {user: 'accent', feedback: 'warning', project: 'info', reference: 'neutral'}

/** Notes drawn at a time, before "more". */
const PAGE = 60

/** One note: its name, where it holds and what it says it is; opened, its text. */
function note(n: MemoryNote, open: boolean, toggle: () => void, gone: string[] = []): m.Children {
    return (
        <li class={`memory__note${open ? ' memory__note--open' : ''}${gone.length ? ' memory__note--stale' : ''}`} key={`${n.set}/${n.file}`}>
            <button class="memory__head" type="button" aria-expanded={open} onclick={toggle}>
                <span class="memory__name">{n.name.replace(/^workspace--/, '')}</span>
                <Badge tone={n.set === 'workspace' ? 'accent' : 'neutral'}>{n.set === 'workspace' ? 'workspace' : n.set.split('/').pop()}</Badge>
                {n.type ? <Badge tone={TONE[n.type] ?? 'neutral'}>{n.type}</Badge> : null}
                <span class="hint memory__age" title={`${when(n.ts)} · ${speaker(n.by).title}`}>
                    {age(n.ts)}
                </span>
                {n.description ? <span class="memory__description">{n.description}</span> : null}
                {gone.length ? <span class="memory__stale">may be out of date: {gone.join(', ')} {gone.length === 1 ? 'is' : 'are'} gone</span> : null}
            </button>
            {open ? (
                <div class="memory__body">
                    {markdown(n.body)}
                    <p class="hint">
                        {n.file} · {n.set} · changed {when(n.ts)} by {speaker(n.by).title}
                    </p>
                </div>
            ) : null}
        </li>
    )
}

/**
 * What agents remember, as the hub keeps it for every machine (memory.rs):
 * the workspace's notes and each repository's, searchable as you type.
 */
export class Memory extends MithrilComponent {
    s = local({
        all: [] as MemoryNote[],
        query: '',
        set: '' as string | null,
        open: '',
        shown: PAGE,
        error: '',
        loaded: false,
        /** `set/file` → the files it names that are gone. */
        stale: {} as Record<string, string[]>,
        onlyStale: false,
    })

    oninit() {
        api.memory()
            .then((r) => {
                this.s.all = notes(r.sets)
                this.s.stale = r.stale ?? {}
            })
            .catch((e) => (this.s.error = (e as Error).message))
            .finally(() => (this.s.loaded = true))
    }

    view() {
        const {all, query, set, open} = this.s
        if (!this.s.loaded) return <p class="hint">Loading…</p>
        if (this.s.error) return <p class="hint">{this.s.error}</p>
        const sets = [...new Set(all.map((n) => n.set))].sort((a, b) => (a === 'workspace' ? -1 : b === 'workspace' ? 1 : a.localeCompare(b)))
        const stale = this.s.stale
        const isStale = (n: MemoryNote) => (stale[`${n.set}/${n.file}`] ?? []).length > 0
        const staleCount = all.filter(isStale).length
        const found = search((set ? all.filter((n) => n.set === set) : all).filter((n) => !this.s.onlyStale || isStale(n)), query)
        const more = found.length - this.s.shown
        return (
            <div class="stack">
                {staleCount ? (
                    <Alert tone="warning">
                        {staleCount} note{staleCount === 1 ? '' : 's'} may be out of date: {staleCount === 1 ? 'it names' : 'they name'} a file its repository no longer has.{' '}
                        <button class="home__link" type="button" onclick={() => (this.s.onlyStale = !this.s.onlyStale)}>
                            {this.s.onlyStale ? 'show all' : 'show only those'}
                        </button>
                    </Alert>
                ) : null}
                <Card subtitle={query || set || this.s.onlyStale ? `${found.length} of ${all.length} notes` : `${all.length} notes, newest first`} flush>
                    <div class="history__bar memory__bar">
                        <FieldText
                            model={bind(this.s, 'query')}
                            type="search"
                            placeholder="Search names, descriptions and text"
                            autocomplete="off"
                        />
                        <FieldSelect
                            model={bind(this.s, 'set')}
                            placeholder="Everywhere"
                            options={sets.map((x) => ({value: x, label: x === 'workspace' ? 'Workspace (every repository)' : x}))}
                        />
                    </div>
                    {found.length ? (
                        <ul class="memory">{found.slice(0, this.s.shown).map((n) => note(n, open === `${n.set}/${n.file}`, () => (this.s.open = open === `${n.set}/${n.file}` ? '' : `${n.set}/${n.file}`), stale[`${n.set}/${n.file}`]))}</ul>
                    ) : (
                        <p class="hint log__more">{all.length ? 'No note has all of those words.' : 'No memory yet: sessions send theirs with `midas memory sync`, which the hooks run.'}</p>
                    )}
                    {more > 0 ? (
                        <p class="hint log__more">
                            <button class="home__link" type="button" onclick={() => (this.s.shown += PAGE)}>
                                show {Math.min(PAGE, more)} more
                            </button>
                        </p>
                    ) : null}
                </Card>
            </div>
        )
    }
}