git clone https://garage44.eu/jeroen/midas.git.. /web/src/components/needs.tsx
import m from '@garage44/mithril'
import {Button} from '@garage44/mithril/components'
import {adminOpenRow, adminRequestRow} from '@/components/admin'
import {openDecision} from '@/components/decisions'
import {icon} from '@/components/icons'
import {age, now, span} from '@/lib/format'
import {needs} from '@/lib/needs'
import type {Need} from '@/lib/needs'
import {$s} from '@/lib/store'
import {go} from '@/lib/route'
import {hosted} from '@/lib/stream'
/** Everything that waits for the person now: the tab's title and the nav count this. */
export const waitingForYou = (): Need[] => needs($s.decisions, hosted(), now(), $s.admin?.requests ?? [])
const open = (id: string) => go(`sessions/${id}`)
function row(n: Need): m.Children {
if (n.kind === 'admin') return adminRequestRow(n.request)
if (n.kind === 'decision') return openDecision(n.decision)
const s = n.session
if (n.kind === 'awaits')
return (
<div class="needs__row needs__row--awaits">
<span>
{icon.key()} <b>{s.awaits?.text}</b> <span class="hint">· {s.title}, since {age(s.awaits?.at)}</span>
</span>
<Button size="sm" variant="primary" onclick={() => open(s.id)}>
{s.awaits?.secret ? 'Paste' : 'Open'}
</Button>
</div>
)
return (
<div class={`needs__row needs__row--${n.kind}`}>
<span>
{n.kind === 'permission' ? (
<>
<b>{s.title}</b> waits for your answer
</>
) : (
<>
<b>{s.title}</b> has been starting for {span(n.for_s)}: it may be stuck
</>
)}
</span>
<Button size="sm" variant={n.kind === 'permission' ? 'primary' : 'secondary'} onclick={() => open(s.id)}>
{n.kind === 'permission' ? 'Answer' : 'Open'}
</Button>
</div>
)
}
/**
* The top of the board: questions to answer (here, inline), permissions a
* session waits for, and sessions that never got going. Nothing when nothing waits.
*/
export function NeedsYou(): m.Children {
const list = waitingForYou()
// Root open on the hub's machine is shown while it lasts, though it asks nothing of them.
const open = adminOpenRow()
if (!list.length && !open) return null
return (
<section class="needs" aria-label="Needs you">
<header class="needs__head">
<b>Needs you</b> <span class="board__count">{list.length}</span>
</header>
{open}
{list.map(row)}
</section>
)
}