Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(calendar): merge consecutive working-location events into one bar (#6192)

gbirmanc665fcf3aaac
diff --git a/apps/web/src/features/calendar/components/CalendarGrid.tsx b/apps/web/src/features/calendar/components/CalendarGrid.tsx
index 428e6b53c6..9daa856557 100644
--- a/apps/web/src/features/calendar/components/CalendarGrid.tsx
+++ b/apps/web/src/features/calendar/components/CalendarGrid.tsx
@@ -30,6 +30,7 @@ import { isSameLocalDate } from '../utils/calendar-date';
import {
type CalendarEventTimeChange,
calendarEventRenderId,
+ calendarEventRenderIds,
} from '../utils/event-interaction';
import {
isMultiDaySelectionPreview,
@@ -41,6 +42,7 @@ import {
formatCompactCalendarTime,
} from '../utils/time-format';
import { TIME_GRID_OPENING_SCROLL_TIME } from '../utils/time-grid-scroller';
+import { mergeWorkingLocationEvents } from '../utils/working-location-events';
import { EventContent } from './EventContent';
import '../calendar.css';
@@ -150,9 +152,11 @@ function CalendarGridHost(props: {
/** Query-free, single-page calendar grid. */
export function CalendarGrid(props: CalendarGridProps) {
const mappedEvents = createMemo<EventInput[]>(() =>
- props.events.map((event) => {
+ mergeWorkingLocationEvents(props.events).map(({ event, occurrenceIds }) => {
const mapped = mapCalendarEventToFullCalendar(event);
- const emphasized = props.emphasizedEventIds?.has(event.id);
+ const emphasized = occurrenceIds.some((id) =>
+ props.emphasizedEventIds?.has(id)
+ );
return {
...mapped,
classNames: emphasized ? ['calendar-event-emphasized'] : undefined,
@@ -160,6 +164,10 @@ export function CalendarGrid(props: CalendarGridProps) {
durationEditable: props.onEventTimeChange
? mapped.durationEditable
: false,
+ extendedProps:
+ occurrenceIds.length > 1
+ ? { ...mapped.extendedProps, mergedOccurrenceIds: occurrenceIds }
+ : mapped.extendedProps,
};
})
);
@@ -274,9 +282,11 @@ export function CalendarGrid(props: CalendarGridProps) {
}
if (isMirror || isMultiDaySelectionPreview(event)) return;
- eventElements.set(eventId, el);
- if (props.selection.eventId === eventId) {
- const selected = eventByRenderId(eventId);
+ const occurrenceIds = calendarEventRenderIds(event);
+ for (const id of occurrenceIds) eventElements.set(id, el);
+ const selectedId = props.selection.eventId;
+ if (selectedId !== undefined && occurrenceIds.includes(selectedId)) {
+ const selected = eventByRenderId(selectedId);
if (selected) props.selection.onEventSelect?.(selected, el);
}
notifyChipMount();
@@ -284,9 +294,10 @@ export function CalendarGrid(props: CalendarGridProps) {
eventWillUnmount={({ el, event, isMirror }) => {
if (isMirror || isMultiDaySelectionPreview(event)) return;
- const eventId = calendarEventRenderId(event);
- if (eventElements.get(eventId) === el) {
- eventElements.delete(eventId);
+ for (const id of calendarEventRenderIds(event)) {
+ if (eventElements.get(id) === el) {
+ eventElements.delete(id);
+ }
}
}}
datesSet={(info) => props.onDatesSet?.(info)}
@@ -361,11 +372,15 @@ export function CalendarGrid(props: CalendarGridProps) {
}
if (!event) return null;
+ const selectedId = props.selection.eventId;
return (
<EventContent
event={event}
renderProps={renderProps}
- isSelected={props.selection.eventId === event.id}
+ isSelected={
+ selectedId !== undefined &&
+ calendarEventRenderIds(renderProps.event).includes(selectedId)
+ }
timeFormat={props.settings.timeFormat}
isNarrow={props.settings.useNarrowEventContent}
/>
diff --git a/apps/web/src/features/calendar/utils/event-interaction.ts b/apps/web/src/features/calendar/utils/event-interaction.ts
index 1a3a681aca..b9682574b0 100644
--- a/apps/web/src/features/calendar/utils/event-interaction.ts
+++ b/apps/web/src/features/calendar/utils/event-interaction.ts
@@ -17,6 +17,20 @@ export function calendarEventRenderId(
return typeof calendarEventId === 'string' ? calendarEventId : event.id;
}
+/**
+ * Every occurrence id a rendered bar stands in for. A merged working-location
+ * run covers several days, only one of which has a chip, so selection, focus,
+ * and chip registration key off all of them.
+ */
+export function calendarEventRenderIds(
+ event: Pick<EventApi, 'extendedProps' | 'id'>
+): string[] {
+ const merged = event.extendedProps.mergedOccurrenceIds;
+ return Array.isArray(merged) && merged.length > 0
+ ? merged
+ : [calendarEventRenderId(event)];
+}
+
/** Whether an occurrence can safely be moved or resized from the calendar. */
export function canEditCalendarEventTime(event: CalendarEvent) {
return (
diff --git a/apps/web/src/features/calendar/utils/working-location-events.test.ts b/apps/web/src/features/calendar/utils/working-location-events.test.ts
new file mode 100644
index 0000000000..0952ad1410
--- /dev/null
+++ b/apps/web/src/features/calendar/utils/working-location-events.test.ts
@@ -0,0 +1,179 @@
+import { EventType } from '@service-storage/generated/schemas/eventType';
+import { describe, expect, it } from 'vitest';
+import type { CalendarEvent, CalendarSource } from '../types';
+import {
+ type MergedCalendarEvent,
+ mergeWorkingLocationEvents,
+} from './working-location-events';
+
+const OFFICE: CalendarSource = { id: 'cal-1', name: 'Work', color: 'blue' };
+const OTHER_CAL: CalendarSource = { id: 'cal-2', name: 'Home', color: 'green' };
+
+interface EventOverrides {
+ title?: string;
+ location?: string;
+ eventType?: EventType;
+ allDay?: boolean;
+ calendar?: CalendarSource;
+ isCancelled?: boolean;
+}
+
+/** An all-day event spanning `[start, end)` as RFC 5545 date strings. */
+function event(
+ id: string,
+ start: string,
+ end: string,
+ overrides: EventOverrides = {}
+): CalendarEvent {
+ return {
+ id,
+ eventId: id,
+ occurrenceKey: id,
+ isCancelled: overrides.isCancelled ?? false,
+ isReadOnly: true,
+ attendees: [],
+ recurrenceLines: ['RRULE:FREQ=WEEKLY'],
+ title: overrides.title ?? 'Office',
+ start,
+ end,
+ allDay: overrides.allDay ?? true,
+ calendar: overrides.calendar ?? OFFICE,
+ eventType: overrides.eventType ?? EventType.working_location,
+ location: overrides.location,
+ };
+}
+
+/** Compact `[id, start, end]` view of each rendered bar. */
+function spans(events: MergedCalendarEvent[]) {
+ return events.map((m) => [m.event.id, m.event.start, m.event.end]);
+}
+
+/** The occurrence ids each rendered bar stands in for. */
+function occurrenceIds(events: MergedCalendarEvent[]) {
+ return events.map((m) => m.occurrenceIds);
+}
+
+describe('mergeWorkingLocationEvents', () => {
+ it('merges a run of consecutive working-location days into one bar', () => {
+ const result = mergeWorkingLocationEvents([
+ event('mon', '2026-08-31', '2026-09-01'),
+ event('tue', '2026-09-01', '2026-09-02'),
+ event('wed', '2026-09-02', '2026-09-03'),
+ event('thu', '2026-09-03', '2026-09-04'),
+ event('fri', '2026-09-04', '2026-09-05'),
+ ]);
+
+ expect(spans(result)).toEqual([['mon', '2026-08-31', '2026-09-05']]);
+ // The bar answers for every day it covers, not just the rendered Monday.
+ expect(occurrenceIds(result)).toEqual([
+ ['mon', 'tue', 'wed', 'thu', 'fri'],
+ ]);
+ });
+
+ it('keeps runs separated by a gap as distinct bars', () => {
+ const result = mergeWorkingLocationEvents([
+ event('mon', '2026-08-31', '2026-09-01'),
+ event('tue', '2026-09-01', '2026-09-02'),
+ // Wednesday off.
+ event('thu', '2026-09-03', '2026-09-04'),
+ event('fri', '2026-09-04', '2026-09-05'),
+ ]);
+
+ expect(spans(result)).toEqual([
+ ['mon', '2026-08-31', '2026-09-02'],
+ ['thu', '2026-09-03', '2026-09-05'],
+ ]);
+ expect(occurrenceIds(result)).toEqual([
+ ['mon', 'tue'],
+ ['thu', 'fri'],
+ ]);
+ });
+
+ it('collapses a duplicate occurrence on the same day', () => {
+ const result = mergeWorkingLocationEvents([
+ event('thu-a', '2026-09-03', '2026-09-04'),
+ event('thu-b', '2026-09-03', '2026-09-04'),
+ ]);
+
+ expect(spans(result)).toEqual([['thu-a', '2026-09-03', '2026-09-04']]);
+ expect(occurrenceIds(result)).toEqual([['thu-a', 'thu-b']]);
+ });
+
+ it('does not merge different working-location titles', () => {
+ const result = mergeWorkingLocationEvents([
+ event('mon', '2026-08-31', '2026-09-01', { title: 'Office' }),
+ event('tue', '2026-09-01', '2026-09-02', { title: 'Home' }),
+ ]);
+
+ expect(spans(result)).toEqual([
+ ['mon', '2026-08-31', '2026-09-01'],
+ ['tue', '2026-09-01', '2026-09-02'],
+ ]);
+ });
+
+ it('does not merge the same title across different locations', () => {
+ const result = mergeWorkingLocationEvents([
+ event('mon', '2026-08-31', '2026-09-01', {
+ title: 'Office',
+ location: 'SF',
+ }),
+ event('tue', '2026-09-01', '2026-09-02', {
+ title: 'Office',
+ location: 'NYC',
+ }),
+ ]);
+
+ expect(spans(result)).toEqual([
+ ['mon', '2026-08-31', '2026-09-01'],
+ ['tue', '2026-09-01', '2026-09-02'],
+ ]);
+ });
+
+ it('does not merge working locations from different calendars', () => {
+ const result = mergeWorkingLocationEvents([
+ event('mon', '2026-08-31', '2026-09-01', { calendar: OFFICE }),
+ event('tue', '2026-09-01', '2026-09-02', { calendar: OTHER_CAL }),
+ ]);
+
+ expect(result).toHaveLength(2);
+ });
+
+ it('leaves non-working-location events untouched and by reference', () => {
+ const meeting = event('meet', '2026-09-01', '2026-09-01', {
+ eventType: EventType.default,
+ allDay: false,
+ });
+ const result = mergeWorkingLocationEvents([meeting]);
+
+ expect(result).toEqual([{ event: meeting, occurrenceIds: ['meet'] }]);
+ expect(result[0].event).toBe(meeting);
+ });
+
+ it('does not merge cancelled occurrences into a run', () => {
+ const result = mergeWorkingLocationEvents([
+ event('mon', '2026-08-31', '2026-09-01'),
+ event('tue', '2026-09-01', '2026-09-02', { isCancelled: true }),
+ event('wed', '2026-09-02', '2026-09-03'),
+ ]);
+
+ const officeSpans = result
+ .filter((m) => !m.event.isCancelled)
+ .map((m) => [m.event.start, m.event.end]);
+ // Tuesday cancelled leaves a gap, so Monday and Wednesday stay separate.
+ expect(officeSpans).toEqual([
+ ['2026-08-31', '2026-09-01'],
+ ['2026-09-02', '2026-09-03'],
+ ]);
+ });
+
+ it('sorts an out-of-order run before merging', () => {
+ const result = mergeWorkingLocationEvents([
+ event('wed', '2026-09-02', '2026-09-03'),
+ event('mon', '2026-08-31', '2026-09-01'),
+ event('tue', '2026-09-01', '2026-09-02'),
+ ]);
+
+ expect(spans(result)).toEqual([['mon', '2026-08-31', '2026-09-03']]);
+ expect(occurrenceIds(result)).toEqual([['mon', 'tue', 'wed']]);
+ });
+});
diff --git a/apps/web/src/features/calendar/utils/working-location-events.ts b/apps/web/src/features/calendar/utils/working-location-events.ts
new file mode 100644
index 0000000000..75c8d89b2e
--- /dev/null
+++ b/apps/web/src/features/calendar/utils/working-location-events.ts
@@ -0,0 +1,100 @@
+import { EventType } from '@service-storage/generated/schemas/eventType';
+import type { CalendarEvent } from '../types';
+
+/** A rendered calendar bar and every occurrence id it stands in for. */
+export interface MergedCalendarEvent {
+ /** The occurrence to render; a merged run uses its earliest occurrence. */
+ event: CalendarEvent;
+ /** Every occurrence id the bar covers, so selection and focus match any day. */
+ occurrenceIds: string[];
+}
+
+/** The calendar and label a working-location run must share to merge. */
+function workingLocationKey(event: CalendarEvent) {
+ return `${event.calendar.id} ${event.title} ${event.location ?? ''}`;
+}
+
+function isMergeableWorkingLocation(event: CalendarEvent) {
+ return (
+ event.eventType === EventType.working_location &&
+ event.allDay &&
+ !event.isCancelled
+ );
+}
+
+function singleton(event: CalendarEvent): MergedCalendarEvent {
+ return { event, occurrenceIds: [event.id] };
+}
+
+/**
+ * Collapses runs of consecutive all-day working-location occurrences from the
+ * same calendar into a single spanning bar so they render linked, matching
+ * Google Calendar. Google models working location as a separate per-weekday
+ * recurring event, so a Mon–Fri "Office" arrives as five distinct one-day
+ * occurrences that would otherwise draw five separate chips.
+ *
+ * A run collapses into its earliest occurrence with the end extended to cover
+ * the run; every day's occurrence id is reported in `occurrenceIds` so click,
+ * selection, and focus navigation resolve for any day the bar covers, even the
+ * ones with no chip of their own. All-day bounds are RFC 5545 date strings
+ * (`YYYY-MM-DD`, exclusive end), which compare chronologically as text.
+ */
+export function mergeWorkingLocationEvents(
+ events: CalendarEvent[]
+): MergedCalendarEvent[] {
+ const groups = new Map<string, CalendarEvent[]>();
+ for (const event of events) {
+ if (!isMergeableWorkingLocation(event)) continue;
+ const key = workingLocationKey(event);
+ const bucket = groups.get(key);
+ if (bucket) bucket.push(event);
+ else groups.set(key, [event]);
+ }
+ if (groups.size === 0) return events.map(singleton);
+
+ const passthrough = events
+ .filter((event) => !isMergeableWorkingLocation(event))
+ .map(singleton);
+ const merged: MergedCalendarEvent[] = [];
+ for (const bucket of groups.values()) {
+ bucket.sort((a, b) =>
+ a.start < b.start
+ ? -1
+ : a.start > b.start
+ ? 1
+ : a.end < b.end
+ ? -1
+ : a.end > b.end
+ ? 1
+ : 0
+ );
+
+ let representative = bucket[0];
+ let runEnd = representative.end;
+ let occurrenceIds = [representative.id];
+ const flush = () => {
+ merged.push({
+ event:
+ occurrenceIds.length > 1
+ ? { ...representative, end: runEnd }
+ : representative,
+ occurrenceIds,
+ });
+ };
+ for (let i = 1; i < bucket.length; i++) {
+ const next = bucket[i];
+ if (next.start <= runEnd) {
+ if (next.end > runEnd) runEnd = next.end;
+ occurrenceIds.push(next.id);
+ } else {
+ flush();
+ representative = next;
+ runEnd = next.end;
+ occurrenceIds = [next.id];
+ }
+ }
+ flush();
+ }
+
+ return [...passthrough, ...merged];
+}