git clone https://garage44.eu/jeroen/midas.git.. /web/src/components/pips.tsx
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<Pip, string> = {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 (
<span class="pips" role="img" aria-label={`sessions: ${said}`} title={`sessions: ${said}`}>
{list.map((p) => (
<i class={p ? `pips__${p}` : undefined} />
))}
</span>
)
}
/** 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 = (
<svg class="tech-head__flag" viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M5 21V4" />
<path d="M5 4h12l-2.5 4.5L17 13H5" />
</svg>
)
/** 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 ? (
<span class="tech-head__goal">
{FLAG}
<span>
{whole && o.goal && o.theme ? <span class="tech-head__top">{o.goal} › </span> : null}
{words}
</span>
</span>
) : 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 ? <span class="tech-head__icon">{m.trust(o.icon)}</span> : <span class="tech-head__icon tech-head__icon--letter">{o.name.slice(0, 1).toUpperCase()}</span>
}
/** A theme's colour class (app.css `tree-theme-*`). */
export const themeOf = (o: ResearchOrigin) => `tree-theme-${o.slot < 0 ? 'none' : o.slot}`