import m from '@garage44/mithril' import {hosted} from '@/lib/stream' import type {Hosted, ResearchOrigin} from '@/lib/stream' import {pips} from '@/lib/tree' import type {Pip} from '@/lib/tree' const WORD: Record = {ok: 'landed', on: 'at work', off: 'ended without landing'} /** A tech's sessions, a short bar each (lib/tree `pips`): green landed, blue at work, grey to go. */ export function pipsView(list: (Pip | null)[]): m.Vnode | null { if (!list.length) return null const said = list.map((p) => (p ? WORD[p] : 'to go')).join(', ') return ( {list.map((p) => ( ))} ) } /** A session's pip, from its own state: landed, at work, or ended without landing. */ function pipOfSession(h: Hosted): Pip { if (h.finished) return h.finished.outcome === 'landed' ? 'ok' : 'off' return h.status === 'stopped' || h.status === 'failed' ? 'off' : 'on' } /** * The pips of a tech a session researches, from the sessions the page knows: without the tree at hand, * one still at work means more to come, as its size expects; none at work, what ran is all there is. */ export function originPips(o: ResearchOrigin): m.Vnode | null { const done = hosted() .filter((h) => h.research?.tech === o.tech) .sort((a, b) => a.created - b.created) .map(pipOfSession) return pipsView(pips(done, o.size, !done.includes('on'))) } const FLAG = ( ) /** The goal a research serves, as a list says it: the theme under the goal, or the goal itself; `whole`, the goal above the theme too (but on a phone). */ export function goalLine(o: ResearchOrigin, whole = false): m.Children { const words = o.theme ?? o.goal return words ? ( {FLAG} {whole && o.goal && o.theme ? {o.goal} › : null} {words} ) : null } /** A tech's icon as its sessions carry it, in its theme's colour; its first letter until it has one. */ export function originIcon(o: ResearchOrigin): m.Children { return o.icon ? {m.trust(o.icon)} : {o.name.slice(0, 1).toUpperCase()} } /** A theme's colour class (app.css `tree-theme-*`). */ export const themeOf = (o: ResearchOrigin) => `tree-theme-${o.slot < 0 ? 'none' : o.slot}`