Use the clock time format in the calendar widget

Upcoming events follow the shared format and size the time column to the format (narrower without a day period).
This commit is contained in:
raucao committed 2026-10-27 21:49:03 +04:00
1 parent f7b174017a
commit 808ed43d2f
2 files changed
+78 -15

No files matched your search

+19 -10
View File
@@ -1,11 +1,14 @@
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;
@@ -15,16 +18,20 @@ export default class UpcomingEventsComponent extends Component {
return this.events.today.length > 0 || this.events.days.length > 0;
}
formatTime(event) {
// 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';
}
const date = new Date(event.startTime);
return new Intl.DateTimeFormat(undefined, {
hour: 'numeric',
minute: '2-digit',
}).format(date);
return formatTime(new Date(event.startTime), this.clock.timeFormat);
}
formatDay(date) {
@@ -61,8 +68,9 @@ export default class UpcomingEventsComponent extends Component {
{{#each this.events.today as |event|}}
<li class="flex gap-1 text-base">
<span
class="w-20 shrink-0 tabular-nums whitespace-nowrap text-white/80"
>{{this.formatTime event}}</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}}
@@ -79,8 +87,9 @@ export default class UpcomingEventsComponent extends Component {
{{#each day.events as |event|}}
<li class="flex gap-1 text-base">
<span
class="w-20 shrink-0 tabular-nums whitespace-nowrap text-white/80"
>{{this.formatTime event}}</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}}
@@ -2,16 +2,22 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render } from '@ember/test-helpers';
import UpcomingEvents from 'dashtab/components/upcoming-events';
import { timeParts } from 'dashtab/utils/time-format';
function registerCalendar(owner, calendar) {
function registerServices(owner, calendar, timeFormat = '24h') {
owner.register('service:calendar', calendar, { instantiate: false });
owner.register(
'service:clock',
{ time: new Date(), timeFormat },
{ instantiate: false }
);
}
module('Integration | Component | upcoming-events', function (hooks) {
setupRenderingTest(hooks);
test('renders nothing while no calendar is enabled', async function (assert) {
registerCalendar(this.owner, {
registerServices(this.owner, {
enabled: false,
events: { today: [], days: [] },
});
@@ -22,7 +28,7 @@ module('Integration | Component | upcoming-events', function (hooks) {
});
test('renders today and the upcoming days with events', async function (assert) {
registerCalendar(this.owner, {
registerServices(this.owner, {
enabled: true,
events: {
today: [
@@ -59,7 +65,7 @@ module('Integration | Component | upcoming-events', function (hooks) {
});
test('shows an empty state when enabled but nothing is upcoming', async function (assert) {
registerCalendar(this.owner, {
registerServices(this.owner, {
enabled: true,
events: { today: [], days: [] },
});
@@ -70,7 +76,7 @@ module('Integration | Component | upcoming-events', function (hooks) {
});
test('labels all-day events', async function (assert) {
registerCalendar(this.owner, {
registerServices(this.owner, {
enabled: true,
events: {
today: [
@@ -89,4 +95,52 @@ module('Integration | Component | upcoming-events', function (hooks) {
assert.dom().containsText('All day');
});
test('formats event times with the clock preference', async function (assert) {
const start = new Date(2026, 7, 3, 13, 5);
const event = {
id: 'e1',
name: 'Review',
startTime: start.toISOString(),
allDay: false,
};
registerServices(
this.owner,
{ enabled: true, events: { today: [event], days: [] } },
'24h'
);
await render(<template><UpcomingEvents /></template>);
assert
.dom('[data-test-upcoming-events]')
.containsText(timeParts(start, '24h').time);
assert.dom('[data-test-upcoming-events] .w-14').exists();
assert.dom('[data-test-upcoming-events] .w-20').doesNotExist();
});
test('uses the 12-hour preference when selected', async function (assert) {
const start = new Date(2026, 7, 3, 13, 5);
const event = {
id: 'e1',
name: 'Review',
startTime: start.toISOString(),
allDay: false,
};
registerServices(
this.owner,
{ enabled: true, events: { today: [event], days: [] } },
'12h'
);
await render(<template><UpcomingEvents /></template>);
const { period } = timeParts(start, '12h');
assert.ok(period.length > 0);
assert.dom('[data-test-upcoming-events]').containsText(period);
assert.dom('[data-test-upcoming-events] .w-20').exists();
assert.dom('[data-test-upcoming-events] .w-14').doesNotExist();
});
});