garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/projects.tsx

raw

import m from '@garage44/mithril'

import {api} from '@/lib/api'
import {slidingPill} from '@/lib/motion'
import {local} from '@/lib/reactive'

/**
 * The workspace's projects (hub.conf's repos, sessions.rs `projects`): every
 * one on the same machines, so the page never switches between them. It
 * names each session's project, and narrows the board and the sessions list
 * to one when asked: a view, never what runs.
 */
export interface Project {
    repo: string
    name: string
}

function kept(key: string): string {
    try {
        return localStorage.getItem(key) ?? ''
    } catch {
        return ''
    }
}

function keep(key: string, v: string) {
    try {
        localStorage.setItem(key, v)
    } catch {
        // A browser that keeps nothing: the choice holds for this page.
    }
}

export const projects = local({list: [] as Project[], asked: false, shown: kept('midas-project-shown')})

/** Asked once a page: the list changes when the hub's config does. */
export function askProjects() {
    if (projects.asked) return
    projects.asked = true
    api.projects()
        .then((list) => (projects.list = list))
        .catch(() => (projects.asked = false))
}

/** Several projects: only then is a session's worth naming. */
export const several = () => projects.list.length > 1

/** A repository's project name (`acme/podium` → podium). */
export const projectName = (repo: string | null | undefined) => (repo ? (projects.list.find((p) => p.repo === repo)?.name ?? repo.split('/').pop()!) : null)

/** The project the board and the list are narrowed to, while the workspace has it; '' for all. */
export function shown(): string {
    return projects.list.some((p) => p.repo === projects.shown) ? projects.shown : ''
}

/** Narrows the page to one project ('' for all), kept for the next visit. */
export function show(repo: string) {
    projects.shown = repo
    keep('midas-project-shown', repo)
}

/** A session's project, or the projects of one that spans several. */
type Repos = string | string[] | null | undefined

const listOf = (r: Repos): string[] => (Array.isArray(r) ? r : r ? [r] : [])

/** The projects a session works in: its folder's (sessions.rs `repos_of`), else its one. */
export const reposOf = (h: {repo?: string | null; repos?: string[] | null}): string[] => (h.repos?.length ? h.repos : listOf(h.repo))

/** In view: every project's, or the one shown. A session that names none is in every view but one project's. */
export const inView = (repo: Repos) => !shown() || listOf(repo).includes(shown())

/** A session's projects as small tags, when there are several in the workspace. */
export function projectTag(repo: Repos): m.Children {
    if (!several()) return null
    return listOf(repo).map((r) => (
        <span class="ptag" title={`project ${projectName(r)} (${r})`}>
            {projectName(r)}
        </span>
    ))
}

/** All · cartopolis · podium · midas: narrows what is shown, with how many each has. */
export function projectFilter(count: (repo: string) => number): m.Children {
    if (!several()) return null
    const now = shown()
    return (
        <div class="pfilter" role="group" aria-label="project" {...slidingPill}>
            <span class="seg__pill" aria-hidden="true" />
            <button type="button" class={`pfilter__one${now ? '' : ' pfilter__one--on'}`} aria-pressed={String(!now)} onclick={() => show('')}>
                All
            </button>
            {projects.list.map((p) => (
                <button
                    type="button"
                    class={`pfilter__one${now === p.repo ? ' pfilter__one--on' : ''}`}
                    aria-pressed={String(now === p.repo)}
                    title={p.repo}
                    onclick={() => show(now === p.repo ? '' : p.repo)}
                >
                    {p.name}
                    {count(p.repo) ? <span class="pfilter__n">{count(p.repo)}</span> : null}
                </button>
            ))}
        </div>
    )
}

/**
 * The same choice as one small dropdown, for the nav and Sessions: there the
 * groups do the sorting, and a row of tabs took a line for a choice made rarely.
 */
export function projectSelect(count: (repo: string) => number, title = "show one project's sessions"): m.Children {
    if (!several()) return null
    const now = shown()
    return (
        <select class="pselect" aria-label="project" title={title} value={now} onchange={(e: Event) => show((e.target as HTMLSelectElement).value)}>
            <option value="">all projects</option>
            {projects.list.map((p) => (
                <option value={p.repo} selected={now === p.repo}>
                    {p.name}
                    {count(p.repo) ? ` (${count(p.repo)})` : ''}
                </option>
            ))}
        </select>
    )
}