@jsb188/calendar 1.1.10 → 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.
@@ -1,6 +1,16 @@
1
- import { cn } from '@jsb188/app/utils/string.ts';
2
1
  import { memo } from 'react';
3
- import type { CalendarDayObj } from '../types/calendar.d.ts';
2
+ import { MiniCalendar } from '../modules/MiniCalendar.tsx';
3
+
4
+ const FILE_CONSTANTS = {
5
+ gridStyle: {
6
+ display: 'grid' as const,
7
+ // Three across on a laptop, collapsing to one on a phone,
8
+ // without a media query or a new class
9
+ gridTemplateColumns: 'repeat(auto-fill, minmax(200px, 1fr))',
10
+ gap: 16,
11
+ padding: 16,
12
+ },
13
+ };
4
14
 
5
15
  /**
6
16
  * The year view: twelve mini month grids.
@@ -10,70 +20,46 @@ import type { CalendarDayObj } from '../types/calendar.d.ts';
10
20
  * and a grid built from a truncated list would quietly show empty days that
11
21
  * are not empty. Clicking a day jumps into it, which is what a year view is
12
22
  * actually for.
23
+ *
24
+ * Each month is a <MiniCalendar>, which draws its own grid from a single date.
25
+ * That is the same component the calendar page's sidebar uses, so the two can
26
+ * never drift apart on which day a month starts under.
13
27
  */
14
28
 
15
29
  export interface CalendarYearMonthObj {
16
30
  // Localized month name
17
31
  label: string;
18
- // Leading blanks before the first day, so the 1st lands under its weekday
19
- leadingBlankCount: number;
20
- days: CalendarDayObj[];
21
- dayNumberLabels: string[];
32
+ // Any instant inside the month; the mini grid derives the rest
33
+ monthDate: Date;
22
34
  }
23
35
 
24
36
  export interface CalendarYearUIProps {
25
37
  months: CalendarYearMonthObj[];
26
- // Single-letter weekday initials for each mini grid's header
27
- weekdayInitials: string[];
38
+ timeZone?: string | null;
39
+ locale?: string | null;
40
+ weekStartsOn?: number;
41
+ todayDateKey?: string | null;
28
42
  onSelectDay: (dateKey: string) => void;
29
43
  }
30
44
 
31
45
  export const CalendarYearUI = memo((p: CalendarYearUIProps) => {
32
- const { months, weekdayInitials, onSelectDay } = p;
46
+ const { months, timeZone, locale, weekStartsOn, todayDateKey, onSelectDay } =
47
+ p;
33
48
 
34
49
  return (
35
- <div className='f h_0 app_scr auto_y'>
36
- <div
37
- style={{
38
- display: 'grid',
39
- // Three across on a laptop, collapsing to one on a phone,
40
- // without a media query or a new class
41
- gridTemplateColumns: 'repeat(auto-fill, minmax(200px, 1fr))',
42
- gap: 16,
43
- padding: 16,
44
- }}
45
- >
50
+ <div className="f h_0 app_scr auto_y">
51
+ <div style={FILE_CONSTANTS.gridStyle}>
46
52
  {months.map((month) => (
47
- <div key={month.label}>
48
- <div className='ft_sm ft_bold cl_md' style={{ marginBottom: 4 }}>
49
- {month.label}
50
- </div>
51
-
52
- <div style={{ display: 'grid', gridTemplateColumns: 'repeat(7, minmax(0, 1fr))' }}>
53
- {weekdayInitials.map((initial, index) => (
54
- <div key={index} className='a_c ft_tn cl_lt'>
55
- {initial}
56
- </div>
57
- ))}
58
-
59
- {Array.from({ length: month.leadingBlankCount }, (_unused, index) => <div key={`blank${index}`} />)}
60
-
61
- {month.days.map((day, index) => (
62
- <button
63
- key={day.dateKey}
64
- type='button'
65
- className={cn(
66
- 'a_c ft_tn r_xs',
67
- day.isToday ? 'ft_bold bg_active' : 'cl_md',
68
- )}
69
- style={{ paddingTop: 2, paddingBottom: 2 }}
70
- onClick={() => onSelectDay(day.dateKey)}
71
- >
72
- {month.dayNumberLabels[index]}
73
- </button>
74
- ))}
75
- </div>
76
- </div>
53
+ <MiniCalendar
54
+ key={month.label}
55
+ title={month.label}
56
+ monthDate={month.monthDate}
57
+ timeZone={timeZone}
58
+ locale={locale}
59
+ weekStartsOn={weekStartsOn}
60
+ todayDateKey={todayDateKey}
61
+ onSelectDay={onSelectDay}
62
+ />
77
63
  ))}
78
64
  </div>
79
65
  </div>
@@ -0,0 +1,273 @@
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import { cn } from '@jsb188/app/utils/string.ts';
3
+ import { memo } from 'react';
4
+ import type { RosterLayoutObj } from '../libs/roster-layout.ts';
5
+
6
+ /* Render an accessible people-by-day grid with one row per worker and visible open staffing seats. */
7
+ export const RosterUI = memo(
8
+ (p: {
9
+ layout: RosterLayoutObj;
10
+ editable: boolean;
11
+ narrow?: boolean;
12
+ selectedDay?: string;
13
+ onSelectDay: (dateKey: string) => void;
14
+ onSelectOccurrence: (id: string) => void;
15
+ onCreate: (
16
+ dateKey: string,
17
+ resourceId: string | null,
18
+ roomId: string | null,
19
+ ) => void;
20
+ onReassign?: (
21
+ occurrenceId: string,
22
+ assignmentId: string,
23
+ resourceId: string,
24
+ ) => void;
25
+ onOpenShifts: (roomId: string | null, dateKey: string) => void;
26
+ }) => {
27
+ const indexes = p.narrow
28
+ ? [
29
+ Math.max(
30
+ 0,
31
+ p.layout.days.findIndex((day) => day.dateKey === p.selectedDay),
32
+ ),
33
+ ]
34
+ : p.layout.days.map((_, index) => index);
35
+ return (
36
+ <div className="f h_0 auto_x auto_y app_scr">
37
+ {p.narrow ? (
38
+ <div className="h_item gap_xs p_8">
39
+ {p.layout.days.map((day) => (
40
+ <button
41
+ key={day.dateKey}
42
+ type="button"
43
+ className={cn(
44
+ 'f p_8 r_sm',
45
+ day.dateKey === p.selectedDay ? 'bg_active' : 'bg_alt',
46
+ )}
47
+ onClick={() => p.onSelectDay(day.dateKey)}
48
+ >
49
+ {day.label}
50
+ </button>
51
+ ))}
52
+ </div>
53
+ ) : null}
54
+ <div
55
+ role="grid"
56
+ aria-label={i18n.t('calendar.roster')}
57
+ style={{
58
+ display: 'grid',
59
+ gridTemplateColumns: p.narrow
60
+ ? '114px minmax(160px,1fr) 56px'
61
+ : `200px repeat(${indexes.length},minmax(130px,1fr)) 64px`,
62
+ minWidth: p.narrow ? 0 : 1000,
63
+ }}
64
+ >
65
+ <div className="sticky_top sticky_left bg z3 p_8 ft_bold">
66
+ {i18n.t('schedule.resources')}
67
+ </div>
68
+ {indexes.map((index) => (
69
+ <button
70
+ key={index}
71
+ className="sticky_top bg z2 p_8 bd_b_1 bd_b_bd"
72
+ type="button"
73
+ onClick={() => p.onSelectDay(p.layout.days[index].dateKey)}
74
+ >
75
+ <span className="ft_bold">{p.layout.days[index].label}</span>
76
+ <span
77
+ className={cn(
78
+ 'bl ft_xxs',
79
+ p.layout.days[index].breach === 'BLOCK' ? 'cl_err' : 'cl_md',
80
+ )}
81
+ >
82
+ {i18n.t('calendar.staff_count', {
83
+ count: p.layout.days[index].staffCount,
84
+ })}{' '}
85
+ ·{' '}
86
+ {i18n.t('calendar.open_count', {
87
+ count: p.layout.days[index].openCount,
88
+ })}
89
+ </span>
90
+ </button>
91
+ ))}
92
+ <div
93
+ className="sticky_top bg z2 p_4 ft_xxs"
94
+ style={{ whiteSpace: 'nowrap' }}
95
+ >
96
+ {i18n.t('calendar.hours_week')}
97
+ </div>
98
+ {p.layout.rows.map((row) => (
99
+ <div
100
+ key={row.key}
101
+ role="row"
102
+ style={{ display: 'contents' }}
103
+ >
104
+ <div
105
+ role="rowheader"
106
+ style={{ top: 'auto', bottom: 'auto' }}
107
+ className={cn(
108
+ 'sticky_left bg z1 p_8 bd_b_1 bd_b_bd ellip',
109
+ row.kind === 'GROUP' ? 'ft_bold bg_alt' : 'ft_sm',
110
+ row.breach === 'BLOCK'
111
+ ? 'cl_err'
112
+ : row.breach === 'WARN'
113
+ ? 'cl_warn'
114
+ : '',
115
+ )}
116
+ >
117
+ {row.kind === 'OPEN'
118
+ ? i18n.t('calendar.roster_open_row')
119
+ : row.name || i18n.t('calendar.no_room')}
120
+ </div>
121
+ {indexes.map((index) => {
122
+ const cell = row.cells[index];
123
+ return (
124
+ <div
125
+ key={cell.dateKey}
126
+ role="gridcell"
127
+ onDragOver={(event) => {
128
+ if (p.editable && row.kind === 'PERSON')
129
+ event.preventDefault();
130
+ }}
131
+ onDrop={(event) => {
132
+ event.preventDefault();
133
+ if (!p.editable || !row.resourceId) return;
134
+ try {
135
+ const drag = JSON.parse(
136
+ event.dataTransfer.getData(
137
+ 'application/x-schedule-assignment',
138
+ ),
139
+ );
140
+ if (
141
+ drag.dateKey === cell.dateKey &&
142
+ drag.resourceId !== row.resourceId &&
143
+ typeof drag.occurrenceId === 'string' &&
144
+ typeof drag.assignmentId === 'string'
145
+ )
146
+ p.onReassign?.(
147
+ drag.occurrenceId,
148
+ drag.assignmentId,
149
+ row.resourceId,
150
+ );
151
+ } catch {}
152
+ }}
153
+ className={cn(
154
+ 'p_4 bd_b_1 bd_b_bd bd_l_1 bd_l_bd',
155
+ row.kind === 'GROUP' ? 'bg_alt' : '',
156
+ cell.timeOff?.status === 'APPROVED'
157
+ ? 'bg_alt'
158
+ : cell.timeOff
159
+ ? 'pattern_stripes medium_bf rel'
160
+ : '',
161
+ )}
162
+ style={{ minHeight: 48 }}
163
+ >
164
+ {cell.timeOff ? (
165
+ <span className="bl ft_xxs cl_md">
166
+ {i18n.t('schedule.time_off')}
167
+ </span>
168
+ ) : null}
169
+ {row.kind === 'OPEN' ? (
170
+ cell.openCount > 0 ? (
171
+ <button
172
+ type="button"
173
+ className={cn(
174
+ 'pill p_8',
175
+ cell.breach === 'BLOCK'
176
+ ? 'bg_red_alt_lt'
177
+ : 'bg_amber_alt_lt',
178
+ )}
179
+ onClick={() =>
180
+ p.onOpenShifts(row.roomId, cell.dateKey)
181
+ }
182
+ >
183
+ {i18n.t('calendar.open_count', {
184
+ count: cell.openCount,
185
+ })}
186
+ </button>
187
+ ) : null
188
+ ) : (
189
+ cell.chips.map((chip) => (
190
+ <button
191
+ type="button"
192
+ key={chip.assignmentId || chip.occurrenceId}
193
+ draggable={
194
+ p.editable &&
195
+ !!chip.assignmentId &&
196
+ chip.status !== 'CANCELED'
197
+ }
198
+ onDragStart={(event) => {
199
+ event.dataTransfer.setData(
200
+ 'application/x-schedule-assignment',
201
+ JSON.stringify({
202
+ occurrenceId: chip.occurrenceId,
203
+ assignmentId: chip.assignmentId,
204
+ resourceId: row.resourceId,
205
+ dateKey: cell.dateKey,
206
+ }),
207
+ );
208
+ event.dataTransfer.effectAllowed = 'move';
209
+ }}
210
+ className={cn(
211
+ 'bl w_f a_l r_sm px_4 py_2 ft_xxs bd_1 bd_bd',
212
+ chip.status === 'DRAFT'
213
+ ? 'pattern_stripes medium_bf rel'
214
+ : '',
215
+ chip.status === 'CANCELED'
216
+ ? 'strikethrough op_50'
217
+ : '',
218
+ chip.isMine ? 'bg_active' : 'bg_alt',
219
+ chip.breach === 'BLOCK'
220
+ ? 'bd_red_md'
221
+ : chip.breach === 'WARN'
222
+ ? 'bd_amber_md'
223
+ : '',
224
+ )}
225
+ onClick={() =>
226
+ p.onSelectOccurrence(chip.occurrenceId)
227
+ }
228
+ >
229
+ <span className="bl ellip ft_bold">{chip.title}</span>
230
+ <span>
231
+ {chip.startLabel}–{chip.endLabel}
232
+ {chip.overnight ? ' →' : ''}
233
+ </span>
234
+ {chip.roomName ? (
235
+ <span className="bl ellip cl_md">
236
+ {chip.roomName}
237
+ </span>
238
+ ) : null}
239
+ </button>
240
+ ))
241
+ )}
242
+ {row.kind === 'PERSON' &&
243
+ p.editable &&
244
+ !cell.chips.length ? (
245
+ <button
246
+ type="button"
247
+ className="w_f p_8 cl_lt"
248
+ aria-label={i18n.t('calendar.new_shift')}
249
+ onClick={() =>
250
+ p.onCreate(cell.dateKey, row.resourceId, row.roomId)
251
+ }
252
+ >
253
+ +
254
+ </button>
255
+ ) : null}
256
+ </div>
257
+ );
258
+ })}
259
+ <div
260
+ className={cn(
261
+ 'p_8 bd_b_1 bd_b_bd ft_xxs',
262
+ row.kind === 'GROUP' ? 'bg_alt' : '',
263
+ )}
264
+ >
265
+ {row.kind === 'PERSON' ? (row.weekMinutes / 60).toFixed(1) : ''}
266
+ </div>
267
+ </div>
268
+ ))}
269
+ </div>
270
+ </div>
271
+ );
272
+ },
273
+ );