git clone https://garage44.eu/jeroen/midas.git.. /web/src/components/decisions.tsx
import m, {MithrilComponent} from '@garage44/mithril'
import {Badge, FieldText} from '@garage44/mithril/components'
import {confirmButton} from '@/components/confirm'
import {who} from '@/components/who'
import {act} from '@/lib/actions'
import {api} from '@/lib/api'
import {age, when} from '@/lib/format'
import {sessions} from '@/lib/markdown'
import {speaker} from '@/lib/names'
import {bind, local} from '@/lib/reactive'
import {$s} from '@/lib/store'
import type {Decision} from '@/lib/types'
/** What is being written to each open question, kept while the page redraws. */
const words = local({notes: {} as Record<string, string>, free: {} as Record<string, string>})
function decide(d: Decision, choice: string) {
const note = (words.notes[d.id] ?? '').trim() || undefined
void act(`${d.id}: ${choice}`, () => api.decide(d.id, choice, note))
}
/** The element a question is drawn in, for a card's "?" to scroll to. */
export const decisionAnchor = (id: string) => `decision-${id}`
/**
* One question a session put to the person (`midas ask`): a button per option,
* each asking for a second tap, or a box for a free answer. The answer goes to
* the asking session, and to those it named, as a note.
*/
export function openDecision(d: Decision): m.Children {
return (
<div class="decision" id={decisionAnchor(d.id)}>
<div>
<Badge tone="warning">{d.id}</Badge> {sessions(d.question)}
</div>
<div class="hint">
asked by {who(d.by, {max: 40})} · {age(d.at)} ago
{d.with.length ? [' · concerns ', d.with.map((w, i) => [i ? ', ' : '', who(w, {max: 30})])] : ''}
</div>
<div class="decision__choices">
{d.options.length
? [
d.options.map((o) =>
confirmButton(`decide-${d.id}-${o}`, {
label: o,
armedLabel: `Tap again: “${o}”`,
variant: 'secondary',
disabled: !$s.hasKey,
run: () => decide(d, o),
}),
),
<FieldText model={bind(words.notes, d.id)} placeholder="a word with it (optional)" />,
]
: [
<FieldText model={bind(words.free, d.id)} placeholder="your answer" />,
confirmButton(`decide-${d.id}`, {
label: 'Answer',
armedLabel: 'Tap again to send',
variant: 'secondary',
disabled: !$s.hasKey || !(words.free[d.id] ?? '').trim(),
run: () => decide(d, words.free[d.id].trim()),
}),
]}
</div>
</div>
)
}
/**
* The questions already answered, beside the board: folded under "show N
* decided", newest first. The open ones are at the top of the board.
*/
export class Decided extends MithrilComponent {
s = local({open: false})
view() {
const done = $s.decisions.filter((d) => d.answer != null).sort((a, b) => (b.answered_at ?? 0) - (a.answered_at ?? 0))
if (!done.length) return null
return (
<section>
<h3>Decided</h3>
<button class="home__link" type="button" onclick={() => (this.s.open = !this.s.open)}>
{this.s.open ? 'hide them' : `show ${done.length} decided`}
</button>
{this.s.open ? (
<ul class="pairs">
{done.map((d) => (
<li class="decision decision--done">
✓ {sessions(d.question)} — <strong>{d.answer}</strong>
<div class="hint">
{d.id} · {speaker(d.answered_by ?? '').title} · {when(d.answered_at)}
{d.note ? ` — ${d.note}` : ''}
</div>
</li>
))}
</ul>
) : null}
</section>
)
}
}