Mercurial > code > home > repos > front-door-display
view src/scheduleLcd.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 | affb3c8f3f58 |
children |
line wrap: on
line source
import { addHours, endOfToday, endOfTomorrow, format, isAfter, isBefore, isToday, isTomorrow, isWithinInterval, parseISO, startOfToday } from "date-fns"; import { html, LitElement, TemplateResult } from "lit"; import { customElement, property } from "lit/decorators.js"; import { sortBy } from "lodash"; import { DataFactory, NamedNode, Parser, Quad_Predicate, Quad_Subject, Store, Term } from "n3"; import { hideFeeds, hideTitles } from "./private"; import { shared } from "./shared"; const { namedNode } = DataFactory; export { WeekGuide } from "./WeekGuide"; const EV = "http://bigasterisk.com/event#"; const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; const es = new EventSource("http://localhost:8005/events"); es.addEventListener("message", (msg) => { (document.querySelector("#time") as HTMLElement).innerText = JSON.parse(msg.data).time.label; }); function notifyWebScraperOnAllDomChanges() { function sendPostRequest(data: any) { fetch("https://example.com/api", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }) .then((response) => { if (!response.ok) { throw new Error("Network response was not ok"); } return response.json(); }) .then((data) => { console.log("POST request successful"); }) .catch((error) => { console.error("There was a problem with the POST request:", error); }); } function handleDomChanges(mutationsList: MutationRecord[], observer: MutationObserver): void { sendPostRequest({ domChanges: mutationsList }); } const observer = new MutationObserver(handleDomChanges); observer.observe(document.body, { subtree: true, childList: true, attributes: true }); } function getLiteral(store: Store, graph: Term, subj: Quad_Subject, pred: Quad_Predicate, missing: string | null): string { let out = null; store.getObjects(subj, pred, graph).forEach((attr) => { out = attr.value; }); if (!out) { if (missing === null) { throw new Error(); } return missing; } return out; } 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>`; } 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); } toHtml(): TemplateResult { return html` <li> <span class="date">${this.shortDate()}</span> ${this.title} <!--${this.feed}--> </li> `; } } @customElement("fd-upcoming-events") export class UpcomingEvents extends LitElement { @property() evs: DisplayEvent[]; constructor() { super(); this.evs = []; this.load(); setInterval(this.load.bind(this), 5 * 60 * 1000); } async load() { const store = new Store(); const r = await fetch( "/gcalendarwatch/graph/calendar/upcoming", { method: "GET", headers: { Accept: "application/json", "X-Pomerium-Authorization": document.cookie.substring(document.cookie.indexOf("=") + 1), }, } ); const n3txt = await r.text(); const parser = new Parser({ format: "application/trig" }); parser.parse(n3txt, (error, quad, prefixes) => { if (quad) { store.addQuad(quad); } else { const graph = namedNode(EV + "gcalendar"); this.evs = []; store.getSubjects(namedNode(RDF + "type"), namedNode(EV + "Event"), graph).forEach((ev: Quad_Subject) => { const de = new DisplayEvent(store, graph, ev); if (de.show()) { this.evs = [...this.evs, de]; } }); this.evs = sortBy(this.evs, "start"); } }); } static styles = [ shared, ]; render() { return html` <h1 data-text="Calendar">Calendar</h1> <ol> ${this.evs.map((d) => d.toHtml())} </ol> `; } }