@jsb188/calendar 1.1.11 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,333 @@
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import type { ScheduleResourceGQL } from '@jsb188/graphql/schedule.d';
3
+ import type { ShiftFormValues } from '../types/shift-form.d.ts';
4
+ import { memo } from 'react';
5
+
6
+ /* Render a local-time shift editor with structured slots and a bounded recurrence form. */
7
+ export const ShiftFormUI = memo(
8
+ (p: {
9
+ values: ShiftFormValues;
10
+ rooms: ScheduleResourceGQL[];
11
+ existing: boolean;
12
+ series: boolean;
13
+ busy: boolean;
14
+ error: string | null;
15
+ onChange: (values: ShiftFormValues) => void;
16
+ onSave: () => void;
17
+ }) => {
18
+ const v = p.values;
19
+ /* Change one controlled form value without mutating the caller's draft. */
20
+ const change = (key: keyof ShiftFormValues, value: unknown) =>
21
+ p.onChange({ ...v, [key]: value });
22
+ return (
23
+ <form
24
+ className="v_stretch gap_sm p_12"
25
+ onSubmit={(event) => {
26
+ event.preventDefault();
27
+ p.onSave();
28
+ }}
29
+ >
30
+ {!p.existing ? (
31
+ <label>
32
+ {i18n.t('calendar.kind_' + v.kind)}
33
+ <select
34
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
35
+ value={v.kind}
36
+ onChange={(e) => change('kind', e.target.value)}
37
+ >
38
+ {['SHIFT', 'EVENT', 'BLOCK'].map((kind) => (
39
+ <option
40
+ key={kind}
41
+ value={kind}
42
+ >
43
+ {i18n.t('calendar.kind_' + kind)}
44
+ </option>
45
+ ))}
46
+ </select>
47
+ </label>
48
+ ) : null}
49
+ <label>
50
+ {i18n.t('calendar.title')}
51
+ <input
52
+ required
53
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
54
+ value={v.title}
55
+ onChange={(e) => change('title', e.target.value)}
56
+ />
57
+ </label>
58
+ <label>
59
+ {i18n.t('calendar.room')}
60
+ <select
61
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
62
+ value={v.roomId}
63
+ onChange={(e) => change('roomId', e.target.value)}
64
+ >
65
+ <option value="">{i18n.t('calendar.no_room')}</option>
66
+ {p.rooms.map((room) => (
67
+ <option
68
+ key={room.id}
69
+ value={room.id}
70
+ >
71
+ {room.name}
72
+ </option>
73
+ ))}
74
+ </select>
75
+ </label>
76
+ {(['start', 'end'] as const).map((key) => (
77
+ <label key={key}>
78
+ {i18n.t('calendar.' + key)}
79
+ <input
80
+ required
81
+ type="datetime-local"
82
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
83
+ value={v[key]}
84
+ onChange={(e) => change(key, e.target.value)}
85
+ />
86
+ </label>
87
+ ))}
88
+ {v.kind !== 'SHIFT' ? (
89
+ <label>
90
+ <input
91
+ type="checkbox"
92
+ checked={v.allDay}
93
+ onChange={(e) => change('allDay', e.target.checked)}
94
+ />{' '}
95
+ {i18n.t('calendar.all_day')}
96
+ </label>
97
+ ) : null}
98
+ {(['location', 'note'] as const).map((key) => (
99
+ <label key={key}>
100
+ {i18n.t('calendar.' + key)}
101
+ <input
102
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
103
+ value={v[key]}
104
+ onChange={(e) => change(key, e.target.value)}
105
+ />
106
+ </label>
107
+ ))}
108
+ {v.kind === 'SHIFT' ? (
109
+ <fieldset className="v_stretch gap_sm">
110
+ <legend>{i18n.t('calendar.slots')}</legend>
111
+ {v.slots.map((slot, index) => (
112
+ <div
113
+ className="v_stretch gap_xs bd_1 bd_bd p_8 r_sm"
114
+ key={slot.key}
115
+ >
116
+ <label>
117
+ {i18n.t('calendar.slot_label')}
118
+ <input
119
+ className="w_f p_4 bd_1 bd_bd"
120
+ value={slot.label || ''}
121
+ onChange={(e) =>
122
+ change(
123
+ 'slots',
124
+ v.slots.map((row, i) =>
125
+ i === index ? { ...row, label: e.target.value } : row,
126
+ ),
127
+ )
128
+ }
129
+ />
130
+ </label>
131
+ <label>
132
+ {i18n.t('calendar.required_count')}
133
+ <input
134
+ type="number"
135
+ min="1"
136
+ max="500"
137
+ className="w_f p_4 bd_1 bd_bd"
138
+ value={slot.requiredCount}
139
+ onChange={(e) =>
140
+ change(
141
+ 'slots',
142
+ v.slots.map((row, i) =>
143
+ i === index
144
+ ? { ...row, requiredCount: Number(e.target.value) }
145
+ : row,
146
+ ),
147
+ )
148
+ }
149
+ />
150
+ </label>
151
+ {(['roles', 'qualifications', 'tags'] as const).map((key) => (
152
+ <label key={key}>
153
+ {i18n.t('calendar.predicate_' + key)}
154
+ <input
155
+ className="w_f p_4 bd_1 bd_bd"
156
+ defaultValue={slot.predicate?.[key]?.join(', ') || ''}
157
+ onBlur={(e) =>
158
+ change(
159
+ 'slots',
160
+ v.slots.map((row, i) =>
161
+ i === index
162
+ ? {
163
+ ...row,
164
+ predicate: {
165
+ ...row.predicate,
166
+ [key]: e.target.value
167
+ .split(',')
168
+ .map((part) => part.trim())
169
+ .filter(Boolean),
170
+ },
171
+ }
172
+ : row,
173
+ ),
174
+ )
175
+ }
176
+ />
177
+ </label>
178
+ ))}
179
+ <button
180
+ type="button"
181
+ className="cl_err a_l p_4"
182
+ onClick={() =>
183
+ change(
184
+ 'slots',
185
+ v.slots.filter((_, i) => i !== index),
186
+ )
187
+ }
188
+ >
189
+ {i18n.t('calendar.remove')}
190
+ </button>
191
+ </div>
192
+ ))}
193
+ <button
194
+ type="button"
195
+ className="p_8 bg_alt r_sm"
196
+ onClick={() => {
197
+ let n = v.slots.length + 1;
198
+ while (v.slots.some((slot) => slot.key === `S${n}`)) n++;
199
+ change('slots', [
200
+ ...v.slots,
201
+ {
202
+ key: `S${n}`,
203
+ label: '',
204
+ requiredCount: 1,
205
+ predicate: { roles: [], qualifications: [], tags: [] },
206
+ ordinal: v.slots.length,
207
+ },
208
+ ]);
209
+ }}
210
+ >
211
+ {i18n.t('calendar.add_slot')}
212
+ </button>
213
+ </fieldset>
214
+ ) : null}
215
+ {!p.existing ? (
216
+ <>
217
+ <label>
218
+ {i18n.t('calendar.repeat')}
219
+ <select
220
+ className="bl w_f p_8 bd_1 bd_bd"
221
+ value={v.repeat}
222
+ onChange={(e) => change('repeat', e.target.value)}
223
+ >
224
+ <option value="">{i18n.t('calendar.none')}</option>
225
+ {['DAILY', 'WEEKLY', 'MONTHLY'].map((freq) => (
226
+ <option
227
+ key={freq}
228
+ value={freq}
229
+ >
230
+ {i18n.t('calendar.repeat_' + freq)}
231
+ </option>
232
+ ))}
233
+ </select>
234
+ </label>
235
+ {v.repeat ? (
236
+ <>
237
+ <label>
238
+ {i18n.t('calendar.interval')}
239
+ <input
240
+ type="number"
241
+ min="1"
242
+ max="365"
243
+ value={v.interval}
244
+ onChange={(e) => change('interval', Number(e.target.value))}
245
+ />
246
+ </label>
247
+ {v.repeat === 'WEEKLY' ? (
248
+ <div className="h_item f_wrap gap_sm">
249
+ {Array.from({ length: 7 }, (_, day) => (
250
+ <label key={day}>
251
+ <input
252
+ type="checkbox"
253
+ checked={v.weekdays.includes(day)}
254
+ onChange={(e) =>
255
+ change(
256
+ 'weekdays',
257
+ e.target.checked
258
+ ? [...v.weekdays, day]
259
+ : v.weekdays.filter((value) => value !== day),
260
+ )
261
+ }
262
+ />
263
+ {new Intl.DateTimeFormat(undefined, {
264
+ weekday: 'short',
265
+ timeZone: 'UTC',
266
+ }).format(new Date(Date.UTC(2026, 0, 4 + day)))}
267
+ </label>
268
+ ))}
269
+ </div>
270
+ ) : null}
271
+ {v.repeat === 'MONTHLY' ? (
272
+ <label>
273
+ {i18n.t('calendar.month_day')}
274
+ <input
275
+ type="number"
276
+ min="1"
277
+ max="31"
278
+ value={v.monthDay}
279
+ onChange={(e) =>
280
+ change('monthDay', Number(e.target.value))
281
+ }
282
+ />
283
+ </label>
284
+ ) : null}
285
+ <label>
286
+ {i18n.t('calendar.repeat_count')}
287
+ <input
288
+ type="number"
289
+ min="1"
290
+ value={v.count}
291
+ onChange={(e) => change('count', e.target.value)}
292
+ />
293
+ </label>
294
+ <label>
295
+ {i18n.t('calendar.repeat_until')}
296
+ <input
297
+ type="date"
298
+ value={v.until}
299
+ onChange={(e) => change('until', e.target.value)}
300
+ />
301
+ </label>
302
+ </>
303
+ ) : null}
304
+ </>
305
+ ) : null}
306
+ {p.series ? (
307
+ <label>
308
+ <input
309
+ type="checkbox"
310
+ checked={v.future}
311
+ onChange={(e) => change('future', e.target.checked)}
312
+ />{' '}
313
+ {i18n.t('calendar.series_this_and_future')}
314
+ </label>
315
+ ) : null}
316
+ {p.error ? (
317
+ <p
318
+ role="alert"
319
+ className="cl_err ft_sm"
320
+ >
321
+ {p.error}
322
+ </p>
323
+ ) : null}
324
+ <button
325
+ className="p_12 bg_primary cl_white r_sm"
326
+ disabled={p.busy}
327
+ >
328
+ {i18n.t('calendar.save')}
329
+ </button>
330
+ </form>
331
+ );
332
+ },
333
+ );
@@ -46,7 +46,7 @@ function buildEvent(
46
46
  allDay: false,
47
47
  kind: 'SHIFT',
48
48
  status: 'PUBLISHED',
49
- assigneeAccountIds: [],
49
+ assigneeResourceIds: [],
50
50
  ...overrides,
51
51
  };
52
52
  }
@@ -355,9 +355,9 @@ describe('getCalendarAllDayLayout', () => {
355
355
  expect(bars.map((bar) => bar.row).sort()).toEqual([0, 1]);
356
356
  });
357
357
 
358
- it('promotes a multi-day timed entry into the band', () => {
358
+ it('keeps a multi-day timed entry out of the all-day band', () => {
359
359
  const week = buildWeek();
360
- // A three-day closure is not usefully drawn as three tall columns
360
+ // Each timed segment belongs only to the timed grid.
361
361
  const bars = getCalendarAllDayLayout(
362
362
  [
363
363
  buildEvent('closure', '2026-07-20T18:00:00Z', '2026-07-23T02:00:00Z', {
@@ -367,9 +367,7 @@ describe('getCalendarAllDayLayout', () => {
367
367
  week.days,
368
368
  );
369
369
 
370
- expect(bars).toHaveLength(1);
371
- expect(bars[0].startDayIndex).toBe(1);
372
- expect(bars[0].endDayIndex).toBe(3);
370
+ expect(bars).toHaveLength(0);
373
371
  });
374
372
 
375
373
  it('leaves a single-day timed entry in the grid', () => {
@@ -36,6 +36,7 @@ describe('view slugs', () => {
36
36
 
37
37
  it('round-trips every slug', () => {
38
38
  for (const view of [
39
+ 'ROSTER',
39
40
  'DAY',
40
41
  'FOUR_DAYS',
41
42
  'WEEK',
@@ -165,3 +166,24 @@ describe('writeCalendarUrlState', () => {
165
166
  ).toBe('v=month&d=2026-08-01&me=1');
166
167
  });
167
168
  });
169
+
170
+ it('round-trips active calendar, room, panel and deep-linked occurrence without losing unrelated params', () => {
171
+ const query = write('external=1', {
172
+ view: 'ROSTER',
173
+ calendarId: 'cal',
174
+ roomId: 'room',
175
+ groupByRoom: true,
176
+ panel: 'requests',
177
+ openId: 'row',
178
+ });
179
+ const state = readCalendarUrlState(new URLSearchParams(query), NOW, LA);
180
+ expect(state).toMatchObject({
181
+ view: 'ROSTER',
182
+ calendarId: 'cal',
183
+ roomId: 'room',
184
+ groupByRoom: true,
185
+ panel: 'requests',
186
+ openId: 'row',
187
+ });
188
+ expect(query).toContain('external=1');
189
+ });
@@ -0,0 +1,210 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { buildRosterLayout } from '../src/libs/roster-layout.ts';
3
+ import { getCalendarRange } from '../src/libs/calendar-range.ts';
4
+ import type {
5
+ ScheduleOccurrenceGQL,
6
+ ScheduleResourceGQL,
7
+ ScheduleViolationGQL,
8
+ ScheduleTimeOffGQL,
9
+ } from '@jsb188/graphql/schedule.d';
10
+
11
+ /* Build the smallest realistic resource used by roster placement assertions. */
12
+ function resource(
13
+ id: string,
14
+ kind: 'LABOR' | 'SPACE' = 'LABOR',
15
+ ordinal = 0,
16
+ ): ScheduleResourceGQL {
17
+ return {
18
+ id,
19
+ kind,
20
+ name: id,
21
+ key: id,
22
+ ordinal,
23
+ accountId: kind === 'LABOR' ? `account:${id}` : null,
24
+ retiredAt: null,
25
+ config: { roles: [], qualifications: [], tags: [] },
26
+ } as ScheduleResourceGQL;
27
+ }
28
+ /* Build an overnight shift whose assignee account identity differs from the resource identity. */
29
+ function occurrence(
30
+ id: string,
31
+ roomId: string,
32
+ personId: string,
33
+ start = '2026-07-20T22:00:00Z',
34
+ end = '2026-07-21T06:00:00Z',
35
+ ): ScheduleOccurrenceGQL {
36
+ return {
37
+ id,
38
+ calendarId: 'cal',
39
+ resourceId: roomId,
40
+ title: id,
41
+ kind: 'SHIFT',
42
+ startAt: start,
43
+ endAt: end,
44
+ status: 'DRAFT',
45
+ version: '1',
46
+ openCount: 1,
47
+ slots: [],
48
+ assignments: [
49
+ {
50
+ id: `a:${id}`,
51
+ resourceId: personId,
52
+ accountId: `account:${personId}`,
53
+ slotKey: 'S1',
54
+ status: 'ASSIGNED',
55
+ },
56
+ ],
57
+ } as ScheduleOccurrenceGQL;
58
+ }
59
+ /* Supply a stable UTC week so tests isolate placement from browser locale. */
60
+ function input(): Parameters<typeof buildRosterLayout>[0] {
61
+ return {
62
+ days: getCalendarRange({
63
+ view: 'ROSTER',
64
+ anchorDate: new Date('2026-07-20T12:00Z'),
65
+ now: new Date('2026-07-20T12:00Z'),
66
+ timeZone: 'UTC',
67
+ weekStartsOn: 1,
68
+ }).days,
69
+ timeZone: 'UTC',
70
+ locale: 'en',
71
+ resources: [resource('one'), resource('two')],
72
+ rooms: [resource('A', 'SPACE', 0), resource('B', 'SPACE', 1)],
73
+ occurrences: [],
74
+ timeOff: [],
75
+ violations: [],
76
+ groupByRoom: false,
77
+ roomFilterId: null,
78
+ mineOnly: false,
79
+ viewerAccountId: 'account:one',
80
+ viewerResourceId: null,
81
+ formatTime: (at) => at.toISOString().slice(11, 16),
82
+ };
83
+ }
84
+
85
+ describe('roster placement', () => {
86
+ it('draws an overnight assignment once, retains real hours and matches the viewer account', () => {
87
+ const p = input();
88
+ p.occurrences = [occurrence('night', 'A', 'one')];
89
+ const result = buildRosterLayout(p),
90
+ person = result.rows.find((row) => row.resourceId === 'one')!;
91
+ expect(person.cells.flatMap((cell) => cell.chips)).toHaveLength(1);
92
+ expect(person.cells[0].chips[0]).toMatchObject({
93
+ overnight: true,
94
+ isMine: true,
95
+ roomName: 'A',
96
+ });
97
+ expect(person.cells[1].chips).toHaveLength(0);
98
+ expect(person.weekMinutes).toBe(480);
99
+ expect(result.openTotal).toBe(1);
100
+ });
101
+ it('groups a person once by most assigned minutes and preserves chips from other rooms', () => {
102
+ const p = input();
103
+ p.groupByRoom = true;
104
+ p.occurrences = [
105
+ occurrence('long', 'B', 'one'),
106
+ occurrence('short', 'A', 'one', '2026-07-22T09:00Z', '2026-07-22T10:00Z'),
107
+ ];
108
+ const result = buildRosterLayout(p);
109
+ const people = result.rows.filter((row) => row.resourceId === 'one');
110
+ expect(people).toHaveLength(1);
111
+ expect(people[0].roomId).toBe('B');
112
+ expect(people[0].weekMinutes).toBe(540);
113
+ expect(
114
+ people[0].cells
115
+ .flatMap((cell) => cell.chips)
116
+ .map((chip) => chip.roomName),
117
+ ).toEqual(['B', 'A']);
118
+ });
119
+ it('breaks primary-room ties by room ordinal', () => {
120
+ const p = input();
121
+ p.groupByRoom = true;
122
+ p.occurrences = [
123
+ occurrence('first', 'B', 'one'),
124
+ occurrence(
125
+ 'second',
126
+ 'A',
127
+ 'one',
128
+ '2026-07-22T22:00Z',
129
+ '2026-07-23T06:00Z',
130
+ ),
131
+ ];
132
+ expect(
133
+ buildRosterLayout(p).rows.find((row) => row.resourceId === 'one')?.roomId,
134
+ ).toBe('A');
135
+ });
136
+ it('keeps retired people with assignments and marks time off and person-only breaches', () => {
137
+ const p = input();
138
+ p.resources[0].retiredAt = '2026-07-01';
139
+ p.resources[1].retiredAt = '2026-07-01';
140
+ p.occurrences = [occurrence('night', 'A', 'one')];
141
+ p.timeOff = [
142
+ {
143
+ id: 't',
144
+ resourceId: 'one',
145
+ status: 'APPROVED',
146
+ startAt: '2026-07-21T00:00Z',
147
+ endAt: '2026-07-22T00:00Z',
148
+ } as ScheduleTimeOffGQL,
149
+ ];
150
+ p.violations = [
151
+ {
152
+ id: 'v',
153
+ resourceId: 'one',
154
+ occurrenceId: null,
155
+ severity: 'BLOCK',
156
+ startAt: '2026-07-20T22:00Z',
157
+ endAt: '2026-07-21T06:00Z',
158
+ } as ScheduleViolationGQL,
159
+ ];
160
+ const result = buildRosterLayout(p);
161
+ expect(result.rows.some((row) => row.resourceId === 'two')).toBe(false);
162
+ const person = result.rows.find((row) => row.resourceId === 'one')!;
163
+ expect(person.breach).toBe('BLOCK');
164
+ expect(person.cells[1].timeOff?.id).toBe('t');
165
+ });
166
+ it('builds a 150-person week without duplicating grouped workers', () => {
167
+ const p = input();
168
+ p.resources = Array.from({ length: 150 }, (_, i) => resource(String(i)));
169
+ p.occurrences = p.resources.flatMap((r, i) =>
170
+ Array.from({ length: 7 }, (_, day) =>
171
+ occurrence(
172
+ `${i}:${day}`,
173
+ i % 2 ? 'A' : 'B',
174
+ r.id,
175
+ `2026-07-${20 + day}T09:00Z`,
176
+ `2026-07-${20 + day}T17:00Z`,
177
+ ),
178
+ ),
179
+ );
180
+ p.groupByRoom = true;
181
+ const result = buildRosterLayout(p);
182
+ expect(result.rows.filter((row) => row.kind === 'PERSON')).toHaveLength(
183
+ 150,
184
+ );
185
+ expect(
186
+ result.rows.reduce(
187
+ (n, row) =>
188
+ n +
189
+ row.cells.reduce(
190
+ (n, cell) =>
191
+ n + cell.chips.filter((chip) => chip.assignmentId).length,
192
+ 0,
193
+ ),
194
+ 0,
195
+ ),
196
+ ).toBe(1050);
197
+ });
198
+ it('shows assigned people from a shared calendar without requiring their resource catalog', () => {
199
+ const p = input();
200
+ p.resources = [];
201
+ const shift = occurrence('shared', 'A', 'foreign');
202
+ shift.assignments[0].resourceName = 'Shared worker';
203
+ p.occurrences = [shift];
204
+ const person = buildRosterLayout(p).rows.find(
205
+ (row) => row.resourceId === 'foreign',
206
+ );
207
+ expect(person?.name).toBe('Shared worker');
208
+ expect(person?.weekMinutes).toBe(480);
209
+ });
210
+ });