diff src/DisplayEvent.ts @ 15:20d1fa4250c0

refactor
author drewp@bigasterisk.com
date Thu, 06 Jun 2024 17:52:28 -0700
parents
children a90cb6927c7d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/DisplayEvent.ts	Thu Jun 06 17:52:28 2024 -0700
@@ -0,0 +1,45 @@
+import { addHours, endOfToday, endOfTomorrow, format, isAfter, isWithinInterval, parseISO, startOfToday } from "date-fns";
+import { TemplateResult, html } from "lit";
+import { DataFactory, NamedNode, Quad_Subject, Store, Term } from "n3";
+import { getLiteral } from "./parseRdf";
+import { hideFeeds, hideTitles } from "./private";
+const EV = "http://bigasterisk.com/event#";
+const { namedNode } = DataFactory;
+
+export class DisplayEvent {
+  constructor(private store: Store, private graph: Term, public uri: Quad_Subject) {}
+  get title(): string {
+    return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "title"), "(unnamed)");
+  }
+  get start(): string {
+    return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "start"), null);
+  }
+  get feed(): NamedNode {
+    return namedNode(getLiteral(this.store, this.graph, this.uri, namedNode(EV + "feed"), null));
+  }
+  shortDate(): TemplateResult {
+    const t = parseISO(this.start);
+    return html`<span class="d">${format(t, "EEE, MMM d,")}</span> <span class="t">${format(t, "HH:mm")}</span>`;
+  }
+  inHowLong(): TemplateResult {
+    // returns start()-now, like '5 days'
+    const t = parseISO(this.start).valueOf();
+    const now = Date.now();
+    const daysAway = (t - now) / 1000 / 86400;
+    const prec = daysAway < 2 ? 1 : 0;
+    const cls = "until " + (daysAway < 2 ? "until-2d" : daysAway < 7 ? "until-7d" : daysAway < 30 ? "until-1mo" : "");
+    return html`<span class="${cls}">${daysAway.toFixed(prec)} days</span>`;
+  }
+  show(): boolean {
+    const now = new Date();
+    const t = parseISO(this.start);
+
+    const start = startOfToday();
+    let end = endOfToday();
+    if (isAfter(now, addHours(startOfToday(), 18))) {
+      end = endOfTomorrow();
+    }
+
+    return isWithinInterval(t, { start, end }) && !hideTitles.has(this.title) && !hideFeeds.has(this.feed.value);
+  }
+}