import m, {MithrilComponent} from '@garage44/mithril' import type {CommunityStats as Stats} from '@/types' import {Card, compared, LineDays, Period, Ranked, Tiles} from '@/components/pages/stats/charts' import {local} from '@/lib/reactive' import {count, dayLabel, loadCommunityStats, placeName} from '@/lib/stats' interface Attrs { id: string name: string } /** * A community's own numbers, for its owner and moderators * (`docs/mockups/stats/v2-community.html`): members, joining and leaving, * topics, public posts on the fediverse, and visitors who are not members. * * Counts, never names: there is no list of who is most active, and a topic * fewer than 5 people wrote in says so instead of showing numbers. The server * enforces both; this only draws what it sends. */ export class CommunityStats extends MithrilComponent { s = local({data: null as null | Stats, days: 30, id: '', loading: true}) oninit(vnode: m.Vnode) { void this.load(vnode.attrs.id) } onupdate(vnode: m.VnodeDOM) { if (vnode.attrs.id !== this.s.id) void this.load(vnode.attrs.id) } async load(id: string) { this.s.id = id this.s.loading = true const got = await loadCommunityStats(id, this.s.days) if (this.s.id === id) { this.s.data = got this.s.loading = false m.redraw() } } weeks(rows: Stats['weeks']) { const w = 440 const [x0, mid, sc] = [26, 100, 7] const max = Math.max(4, ...rows.map((r) => r.joined)) const scale = Math.min(sc, 80 / max) const bw = (w - x0) / Math.max(1, rows.length) return (
Joined Left
{rows.map((r, i) => { const x = x0 + i * bw + 5 const bwi = bw - 10 return ( {r.joined > 0 && ( {`Week of ${dayLabel(r.week)}: ${r.joined} joined`} )} {r.left > 0 && ( {`Week of ${dayLabel(r.week)}: ${r.left} left`} )} {i % 4 === 0 && {dayLabel(r.week)}} ) })}
) } view(vnode: m.Vnode) { const c = this.s.data return (

Stats

{vnode.attrs.name} · seen only by the owner and moderators
{!c ? (

{this.s.loading ? 'Loading…' : 'No numbers to show.'}

) : (
{Period(this.s.days, (d) => { this.s.days = d void this.load(vnode.attrs.id) })} {Tiles([ {label: 'Members', note: `${c.joined} joined, ${c.left} left`, value: count(c.members)}, {label: 'Active members', note: 'wrote or joined a call', value: c.active_members === null ? 'fewer than 5' : count(c.active_members)}, compared('Messages', c.messages, c.messages_prev, 'all sealed'), {label: 'Public posts', note: `${count(c.followers)} followers elsewhere`, value: count(c.posts)}, compared('Visitors', c.visitors, c.visitors_prev, 'not members'), ])}
{Card('Joining and leaving', 'people, per week', this.weeks(c.weeks))} {Card( 'Topics', `${c.days} days`, {c.topics.map((t) => ( {t.writers === null ? ( ) : ( [, ] )} ))}
TopicMessagesPeople writing
# {t.name}fewer than 5 people wrote{count(t.messages)}{count(t.writers)}
, )}
{Card( 'Public posts on the fediverse', `${c.followers_new} new followers`, Ranked( c.followers_by_server.slice(0, 6).map((f) => ({label: f.server, n: f.n})), ['Server following the community', 'Followers'], 'Nobody follows the community from another server yet', ),

Posts brought {count(c.replies_in)} replies and {count(c.boosts_in)} posts from followed sources in from outside Cartopolis.

, )} {Card('Visitors who are not members', 'per day', LineDays(c.visitors_daily, 150))}
{Card( 'Where the community posts from', 'places on public posts', Ranked( c.post_places.map((p) => ({label: placeName(p.lat, p.lon), n: p.people})), ['Place', 'Posts'], 'No public post with a place yet', ), )}
)}
) } }