Files
dashtab/app/components/upcoming-events.gjs

114 lines
3.3 KiB
Plaintext

import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { formatTime, timeParts } from '../utils/time-format';
// Read-only dashboard widget. It renders the combined upcoming events from the
// calendar service (which reads the persistent cache), so opening a tab does
// not trigger a fetch.
export default class UpcomingEventsComponent extends Component {
@service calendar;
@service clock;
get events() {
return this.calendar.events;
}
get hasEvents() {
return this.events.today.length > 0 || this.events.days.length > 0;
}
get showEmptyState() {
return !this.hasEvents && !this.calendar.error;
}
// A fixed column keeps the rows aligned; it only needs room for a day period
// when the active format shows one (12-hour).
get timeColumnWidth() {
const { period } = timeParts(this.clock.time, this.clock.timeFormat);
return period ? 'w-20' : 'w-14';
}
@action
timeLabel(event) {
if (event.allDay) {
return 'All day';
}
return formatTime(new Date(event.startTime), this.clock.timeFormat);
}
formatDay(date) {
const today = new Date();
today.setHours(0, 0, 0, 0);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
if (date.getTime() === tomorrow.getTime()) {
return 'Tomorrow';
}
return new Intl.DateTimeFormat(undefined, {
weekday: 'long',
month: 'short',
day: 'numeric',
}).format(date);
}
<template>
{{#if this.calendar.enabled}}
<div
class="max-w-full max-h-full min-w-[240px] rounded-md bg-gray-950/20 p-6 text-white"
data-test-upcoming-events
>
{{#if this.calendar.error}}
<p class="mb-2 text-sm text-red-300">{{this.calendar.error}}</p>
{{/if}}
{{#if this.events.today.length}}
<h2
class="text-xs font-semibold tracking-wide text-white/70 uppercase"
>
Today
</h2>
<ul class="mt-2 space-y-1">
{{#each this.events.today as |event|}}
<li class="flex gap-1 text-base">
<span
class="{{this.timeColumnWidth}}
shrink-0 tabular-nums whitespace-nowrap text-white/80"
>{{this.timeLabel event}}</span>
<span class="font-medium">{{event.name}}</span>
</li>
{{/each}}
</ul>
{{/if}}
{{#each this.events.days as |day|}}
<h2
class="not-first:mt-4 text-xs font-semibold tracking-wide text-white/70 uppercase"
>
{{this.formatDay day.date}}
</h2>
<ul class="mt-2 space-y-1">
{{#each day.events as |event|}}
<li class="flex gap-1 text-base">
<span
class="{{this.timeColumnWidth}}
shrink-0 tabular-nums whitespace-nowrap text-white/80"
>{{this.timeLabel event}}</span>
<span class="font-medium">{{event.name}}</span>
</li>
{{/each}}
</ul>
{{/each}}
{{#if this.showEmptyState}}
<p class="text-base text-white/80">No upcoming events</p>
{{/if}}
</div>
{{/if}}
</template>
}