git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/groups.ts
import type {Phase} from '@/lib/landing'
import {asksPerson, awaitsPerson} from '@/lib/status'
import type {Hosted, ResearchOrigin} from '@/lib/stream'
/**
* The sessions in the nav and on Sessions, by where they are rather than one
* list sorted by a dot's colour: what asks the person, what is on its way to
* live, what is at work, what is set to carry on later, what landed today,
* and the rest, quiet.
*/
export type Group = 'asks' | 'landing' | 'work' | 'later' | 'landed' | 'idle'
export const GROUPS: {key: Group; title: string}[] = [
{key: 'asks', title: 'Needs you'},
{key: 'landing', title: 'Landing'},
{key: 'work', title: 'At work'},
{key: 'later', title: 'Scheduled'},
{key: 'landed', title: 'Landed today'},
{key: 'idle', title: 'Idle'},
]
/** Waiting this long since it last did something, a session open but quiet is idle. */
export const IDLE_S = 86400
/**
* Which group a session is in, or null for one the lists leave to the search:
* imported, put away, or finished other than by landing today. `phase` is its
* way to live (lib/landing), null while it is not on it.
*/
export function groupOf(h: Hosted, phase: Phase | null, t: number): Group | null {
if (h.imported) return null
if (h.finished) return h.finished.outcome === 'landed' && h.finished.at > t - IDLE_S ? 'landed' : null
if (h.archived) return null
if (asksPerson(h)) return 'asks'
// On autopilot and asking: it is deciding, which is work.
if (h.status === 'needs_permission') return 'work'
if (phase === 'queued' || phase === 'gate' || phase === 'on_main') return 'landing'
const busy = h.status === 'working' || h.status === 'starting' || h.status === 'queued'
if (busy) return 'work'
// Its turn over and waiting on the person for something only they can do: theirs, landed or not.
if (awaitsPerson(h, t)) return 'asks'
// Its turn over and a time set to carry on, or what it waits for put off: waiting for that time.
if (h.wake || h.awaits?.snooze) return 'later'
// Landed and still open ("keep working"), its turn over: landed, until it works again.
if (phase === 'live') return 'landed'
if (h.status === 'waiting' && t - (h.active ?? h.updated) < IDLE_S) return 'work'
return 'idle'
}
/** Sessions into their groups, each newest first (Scheduled: the soonest first), empty groups left out. */
export function grouped(list: Hosted[], phase: (h: Hosted) => Phase | null, t: number): {key: Group; title: string; these: Hosted[]}[] {
const by = new Map<Group, Hosted[]>()
for (const h of list) {
const g = groupOf(h, phase(h), t)
if (g) by.set(g, [...(by.get(g) ?? []), h])
}
const at = (h: Hosted) => h.finished?.at ?? h.active ?? h.updated
const due = (h: Hosted) => h.wake?.at ?? h.awaits?.snooze ?? 0
const order = (g: Group) => (a: Hosted, b: Hosted) => (g === 'later' ? due(a) - due(b) : at(b) - at(a))
return GROUPS.map((g) => ({...g, these: (by.get(g.key) ?? []).sort(order(g.key))})).filter((g) => g.these.length)
}
/** A group's sessions as the lists show them: one alone, or those researching one tech together under it. */
export type Run = {h: Hosted; tech?: undefined} | {tech: ResearchOrigin; these: Hosted[]}
/**
* A group's sessions with those researching one tech of the research tree gathered under it, where the
* first of them stands; within a tech, the oldest first, as they ran.
*/
export function byTech(these: Hosted[]): Run[] {
const out: Run[] = []
const at = new Map<string, Hosted[]>()
for (const h of these) {
const t = h.research?.tech
if (!t || !h.research) {
out.push({h})
continue
}
const list = at.get(t)
if (list) list.push(h)
else {
const fresh = [h]
at.set(t, fresh)
out.push({tech: h.research, these: fresh})
}
}
for (const list of at.values()) list.sort((a, b) => a.created - b.created)
return out
}