import m, {MithrilComponent} from '@garage44/mithril' import {Badge, Button, Card, FieldCheckbox, FieldNumber, FieldSelect, FieldText} from '@garage44/mithril/components' import type {FeedDataset, FeedDef, FeedListed, FeedPreview} from '@/types' import { colour, deleteFeed, inspect, loadFeeds, preview, project, publisherSlug, refreshFeed, saveFeed, slug, when, } from '@/lib/feeds' import {bind, local} from '@/lib/reactive' const MAP_W = 560 const MAP_H = 360 /** A string field of a `local()` as a select's model: the select may write `null`. */ function choice(s: T, key: {[K in keyof T]: T[K] extends string ? K : never}[keyof T]) { return { get value(): string | null { return s[key] as string }, set value(v: string | null) { ;(s[key] as string) = v ?? '' }, } } interface Draft { url: string dataset: FeedDataset | null query: string preview: FeedPreview | null name: string group: string title: string colour_by: string fields: string[] credit: string licence: string refresh_hours: number | null follow: boolean busy: boolean } function fresh(): Draft { return { busy: false, colour_by: '', credit: '', dataset: null, fields: [], group: '', licence: '', name: '', preview: null, query: '', refresh_hours: 6, follow: true, title: '', url: '', } } /** The preview's dots on a plain ground, north up (`lib/feeds.project`). */ function previewMap(p: FeedPreview) { const dots = project(p.points, MAP_W, MAP_H) return ( {`${p.placed.toLocaleString()} records`} {dots.map(([x, y, v]) => ( ))} ) } /** Adding a layer: the source, what it draws, how it shows (the mockup's sheet 2). */ class NewFeed extends MithrilComponent<{onDone: (saved: boolean) => void}> { d = local(fresh()) async look() { const d = this.d d.busy = true const found = await inspect(d.url) d.busy = false d.dataset = found d.preview = null if (found) { const first = found.queries.find((q) => q.map && !q.parameters) d.credit = found.name d.licence = found.licence d.group = found.publisher || found.name if (first) { d.query = first.name void this.ask() } } } async ask() { const d = this.d if (!d.dataset || !d.query) { return } const q = d.dataset.queries.find((x) => x.name === d.query) d.busy = true const p = await preview(d.dataset.dataset, d.query, d.colour_by || q?.colour || '') d.busy = false d.preview = p if (p) { const named = (c: string) => p.columns.includes(c) d.colour_by = d.colour_by && named(d.colour_by) ? d.colour_by : q?.colour && named(q.colour) ? q.colour : '' d.title = ['naam', 'name', 'titel', 'title', 'label'].find(named) ?? p.columns.find((c) => c !== p.geometry) ?? '' d.fields = p.columns.filter((c) => c !== p.geometry && c !== d.title && !looksLikeIri(p, c)).slice(0, 4) if (!d.name && q) { const bare = q.description.replace(/ op de kaart.*$/i, '').replace(/^De /, '') || q.name d.name = bare.charAt(0).toUpperCase() + bare.slice(1) } } } def(): FeedDef | null { const d = this.d if (!d.dataset || !d.preview) { return null } return { colour_by: d.colour_by, credit: d.credit.trim(), fields: d.fields, geometry: d.preview.geometry, group: d.group.trim(), id: slug(`${publisherSlug(d.dataset.dataset)}-${d.name}`), licence: d.licence.trim(), link: '', name: d.name.trim(), refresh_hours: d.refresh_hours ?? 6, follow: d.follow, source: {dataset: d.dataset.dataset, kind: 'triplexl', query: d.query}, title: d.title, } } view(vnode: m.Vnode<{onDone: (saved: boolean) => void}>) { const d = this.d const p = d.preview // A new query or colour column asks the source again. const askAgain = () => this.ask() const queries = d.dataset?.queries ?? [] const def = this.def() const ready = !!def && !!def.name && !!def.title && !!def.credit && !!def.group && p!.placed > 0 const columns = p ? p.columns.filter((c) => c !== p.geometry) : [] return (

1 · Source

{d.dataset && (
{d.dataset.name} — {d.dataset.publisher}, {d.dataset.licence},{' '} {d.dataset.statements.toLocaleString()} statements
)} {d.dataset && ( ({ disabled: q.parameters > 0, label: `${q.description || q.name}${q.map ? ' · map' : ''}${q.parameters ? ' · asks for a value' : ''}`, value: q.name, }))} /> )}
{p && (

2 · What it draws

{previewMap(p)}

{p.rows.toLocaleString()} records, {p.placed.toLocaleString()} with a place {p.geometry ? `, from the column “${p.geometry}”` : ' — no column holds points'}

{!!p.values.length && (
{p.values.map((v, i) => ( {v} ))} {p.values.length >= 7 && ( other )}
)}
)} {p && (

3 · How it shows

({label: c, value: c}))} /> ({label: c, value: c}))]} />
On the card {columns .filter((c) => c !== d.title) .map((c) => ( f !== c) }, }} /> ))}
)} {!p && (
)}
) } } /** A column whose sample values are all addresses: the record's own, not a field. */ function looksLikeIri(p: FeedPreview, column: string): boolean { const values = p.sample.map((r) => r[column]).filter(Boolean) return values.length > 0 && values.every((v) => /^https?:\/\//.test(v)) } /** * Admin ▸ Data ▸ Data layers: the layers atlas reads from open data, and adding one * from a pasted address. */ export class Feeds extends MithrilComponent { s = local<{feeds: FeedListed[] | null; adding: boolean; busy: string; confirming: string}>({ adding: false, busy: '', confirming: '', feeds: null, }) async load() { this.s.feeds = (await loadFeeds()) ?? [] } oninit() { void this.load() } view() { const s = this.s return (
{s.feeds === null ? '' : `${s.feeds.length} ${s.feeds.length === 1 ? 'layer' : 'layers'}`} {!s.adding && ( )}

Records from open data, placed on the map under Layers ▸ Data. Atlas asks the source on each layer's clock and sends viewers only the parts of the map that changed.

{s.adding && ( { s.adding = false if (saved) void this.load() }} /> )} {s.feeds === null ? (

Loading…

) : !s.feeds.length ? (

No data layer yet.

) : (
    {s.feeds.map(({def, state}) => (
  • {def.name} {def.group} {state.error ? source failed : {`${state.placed.toLocaleString()} records`}} {s.confirming === def.id ? ( ) : ( )}
    {def.source.dataset.replace(/^https:\/\//, '')} · {def.source.query} ·{' '} {def.follow ? 'follows its changes, and ' : ''}every {def.refresh_hours} h · asked {when(state.tried_ms)} · changed {when(state.changed_ms)}
    {state.error &&
    {state.error}
    }
  • ))}
)}
) } }