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) => ( {projectName(r)} )) } /** 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 (