Mercurial > code > home > repos > front-door-display
annotate src/DisplayEvent.ts @ 21:a90cb6927c7d default tip
fix countdown queries. Display "now" instead of "In -0.4 hours"
author | drewp@bigasterisk.com |
---|---|
date | Sat, 07 Sep 2024 17:47:36 -0700 |
parents | 20d1fa4250c0 |
children |
rev | line source |
---|---|
15 | 1 import { addHours, endOfToday, endOfTomorrow, format, isAfter, isWithinInterval, parseISO, startOfToday } from "date-fns"; |
2 import { TemplateResult, html } from "lit"; | |
3 import { DataFactory, NamedNode, Quad_Subject, Store, Term } from "n3"; | |
4 import { getLiteral } from "./parseRdf"; | |
5 import { hideFeeds, hideTitles } from "./private"; | |
6 const EV = "http://bigasterisk.com/event#"; | |
7 const { namedNode } = DataFactory; | |
8 | |
9 export class DisplayEvent { | |
10 constructor(private store: Store, private graph: Term, public uri: Quad_Subject) {} | |
11 get title(): string { | |
12 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "title"), "(unnamed)"); | |
13 } | |
14 get start(): string { | |
21
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
15 var ret = getLiteral(this.store, this.graph, this.uri, namedNode(EV + "start"), null); |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
16 if (ret == null || ret === "") { |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
17 ret = getLiteral(this.store, this.graph, this.uri, namedNode(EV + "startDate"), null); |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
18 } |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
19 return ret; |
15 | 20 } |
21
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
21 get calendar(): NamedNode { |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
22 // todo: broken- it's now (calendar, :event, event) |
15 | 23 return namedNode(getLiteral(this.store, this.graph, this.uri, namedNode(EV + "feed"), null)); |
24 } | |
25 shortDate(): TemplateResult { | |
26 const t = parseISO(this.start); | |
27 return html`<span class="d">${format(t, "EEE, MMM d,")}</span> <span class="t">${format(t, "HH:mm")}</span>`; | |
28 } | |
29 inHowLong(): TemplateResult { | |
21
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
30 // returns start()-now, like 'In 5 days' |
15 | 31 const t = parseISO(this.start).valueOf(); |
32 const now = Date.now(); | |
33 const daysAway = (t - now) / 1000 / 86400; | |
21
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
34 if (daysAway < 0) { |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
35 return html`<span class="until until-2d">NOW</span>`; |
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
36 } |
15 | 37 const prec = daysAway < 2 ? 1 : 0; |
38 const cls = "until " + (daysAway < 2 ? "until-2d" : daysAway < 7 ? "until-7d" : daysAway < 30 ? "until-1mo" : ""); | |
21
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
39 return html`In <span class="${cls}">${daysAway.toFixed(prec)} days</span>`; |
15 | 40 } |
41 show(): boolean { | |
42 const now = new Date(); | |
43 const t = parseISO(this.start); | |
44 | |
45 const start = startOfToday(); | |
46 let end = endOfToday(); | |
47 if (isAfter(now, addHours(startOfToday(), 18))) { | |
48 end = endOfTomorrow(); | |
49 } | |
50 | |
21
a90cb6927c7d
fix countdown queries. Display "now" instead of "In -0.4 hours"
drewp@bigasterisk.com
parents:
15
diff
changeset
|
51 return isWithinInterval(t, { start, end }) && !hideTitles.has(this.title) && !hideFeeds.has(this.calendar.value); |
15 | 52 } |
53 } |