react-weekly-planning 1.0.44 → 1.0.46

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.
package/README.md CHANGED
@@ -54,27 +54,7 @@ It is possible to use either Weekoffset or Date, or even both simultaneously.
54
54
  ```
55
55
 
56
56
 
57
- #### `scope`
58
57
 
59
- - **Description**: This prop sets the view scope of the calendar.
60
- - **Type**: `"day" | "week"`
61
- - **Use Case**: Use this prop to define whether the calendar should display a full week or only a single day. If omitted, it defaults to a weekly view.
62
-
63
- **Example**:
64
- ```jsx
65
- <Calendar scope="day" dayOffset={0} ... />
66
- ```
67
-
68
- #### `dayOffset`
69
-
70
- - **Description**: This prop specifies which day of the week to display when the scope is set to "day".
71
- - **Type**: `0 | 1 | 2 | 3 | 4 | 5 | 6`
72
- - **Use Case**: If `scope="day"`, use this prop to target a specific day of the week (`0` being the first day of the week, up to `6`).
73
-
74
- **Example**:
75
- ```jsx
76
- <Calendar scope="day" dayOffset={2} ... />
77
- ```
78
58
 
79
59
 
80
60
  ---
@@ -106,7 +86,6 @@ const App = () => {
106
86
  date={date}
107
87
  weekOffset={0}
108
88
  groups={groups}
109
- scope="week"
110
89
  />
111
90
  </div>
112
91
  </CalendarTaskContextProvider>
@@ -126,8 +105,6 @@ Props for the Calendar component.
126
105
  | Prop Name | Type | Description |
127
106
  |------------------------------|---------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
128
107
  | `weekOffset` | number | Offset for the week (e.g., -7 for last week, 0 for current week, 7 for next week). |
129
- | `scope` | "day" \| "week" | Sets the calendar view to either a full week or a single day. |
130
- | `dayOffset` | 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 | Offset index for the day column (0 = first day of week, …, 6 = last day) when scope is "day". |
131
108
  | `groups` | GroupFeildsType[] | Array of group data to be displayed in the calendar. |
132
109
  | `className` | string | Additional class names for the calendar component. |
133
110
  | `style` | React.CSSProperties \| undefined | Additional styles for the calendar component. |
@@ -300,7 +277,7 @@ const App = () => (
300
277
 
301
278
  ### 2. Default Behavior of `<Calendar />`
302
279
 
303
- It's important to note that the main `<Calendar />` component uses the `"week"` hash scope by default. If you use the standard component, you are implicitly using this scope. Explicitly defining a `hashScope` on the `CalendarTaskContextProvider` is only necessary when you want to change how tasks are indexed or when building a fully custom UI as shown above.
280
+ It's important to note that the main `<Calendar />` component uses the `"day"` hash scope by default. If you use the standard component, you are implicitly using this scope. Explicitly defining a `hashScope` on the `CalendarTaskContextProvider` is only necessary when you want to change how tasks are indexed or when building a fully custom UI as shown above.
304
281
 
305
282
  ### 3. Understanding Hashes and Scopes
306
283
 
@@ -392,6 +369,34 @@ to create an organization that truly reflects you.
392
369
  console.log(offset); // Logs the week offset for the given date
393
370
  ```
394
371
 
372
+ ### `updateOffsetWithDateCalendarForMonth`
373
+
374
+ - **Description**: Calculates the month offset from a given calendar date.
375
+ - **Parameters**:
376
+ - `calendarDate` (Date): The date to compare.
377
+ - `timeZone` (string, optional): The timezone to use.
378
+ - **Returns**: The calculated month offset.
379
+
380
+ **Example**:
381
+ ```javascript
382
+ import { updateOffsetWithDateCalendarForMonth } from "react-weekly-planning";
383
+ const offset = updateOffsetWithDateCalendarForMonth(new Date());
384
+ ```
385
+
386
+ ### `updateOffsetWithDateCalendarForDay`
387
+
388
+ - **Description**: Calculates the day offset from a given calendar date.
389
+ - **Parameters**:
390
+ - `calendarDate` (Date): The date to compare.
391
+ - `timeZone` (string, optional): The timezone to use.
392
+ - **Returns**: The calculated day offset.
393
+
394
+ **Example**:
395
+ ```javascript
396
+ import { updateOffsetWithDateCalendarForDay } from "react-weekly-planning";
397
+ const offset = updateOffsetWithDateCalendarForDay(new Date());
398
+ ```
399
+
395
400
  ### `millisecondsToHours`
396
401
 
397
402
  - **Description**: Converts milliseconds to a formatted hour string.
@@ -440,6 +445,66 @@ to create an organization that truly reflects you.
440
445
  console.log(diff); // Logs 0 if it's the current week
441
446
  ```
442
447
 
448
+ ### `calculateDayDifference`
449
+
450
+ - **Description**: Calculates the absolute difference in days between a selected date and the current date (both normalized to midnight).
451
+ - **Parameters**:
452
+ - `dateSelectionnee` (Date): The date to compare.
453
+ - `timeZone` (string, optional): The timezone to use.
454
+ - **Returns**: The difference in days.
455
+
456
+ **Example**:
457
+ ```javascript
458
+ import { calculateDayDifference } from "react-weekly-planning";
459
+ const diff = calculateDayDifference(new Date());
460
+ console.log(diff); // Logs the day difference
461
+ ```
462
+
463
+ ### `calculateMonthDifference`
464
+
465
+ - **Description**: Calculates the difference in months between a selected date and the current date.
466
+ - **Parameters**:
467
+ - `dateSelectionnee` (Date): The date to compare.
468
+ - `timeZone` (string, optional): The timezone to use.
469
+ - **Returns**: The difference in months (e.g., 0, 1, -1...).
470
+
471
+ **Example**:
472
+ ```javascript
473
+ import { calculateMonthDifference } from "react-weekly-planning";
474
+ const diff = calculateMonthDifference(new Date());
475
+ console.log(diff); // Logs the month difference
476
+ ```
477
+
478
+ ### `getMonthDay`
479
+
480
+ - **Description**: Returns an array containing the metadata for all days in a specified month, based on a month offset from the current date.
481
+ - **Parameters**:
482
+ - `monthOffset` (number): The number of months to offset from the current month (e.g., 0 for current, 1 for next).
483
+ - `timeZone` (string, optional): The timezone to use.
484
+ - **Returns**: An array of objects, where each object contains `day`, `dayMonth`, `dayYear`, and `dayOfTheMonth`.
485
+
486
+ **Example**:
487
+ ```javascript
488
+ import { getMonthDay } from "react-weekly-planning";
489
+ const days = getMonthDay(0); // Current month
490
+ console.log(days); // Logs all days of the current month
491
+ ```
492
+
493
+ ### `getDayHourlyForMonth`
494
+
495
+ - **Description**: Returns an array of day objects (start and end timestamps) for every day in a specified month, based on a month offset.
496
+ - **Parameters**:
497
+ - `monthOffset` (number): The number of months to offset from the current month.
498
+ - `timeZone` (string, optional): The timezone to use.
499
+ - **Returns**: An array of objects containing `positionDay`, `day`, `start`, and `end`.
500
+
501
+ **Example**:
502
+ ```javascript
503
+ import { getDayHourlyForMonth } from "react-weekly-planning";
504
+ const hours = getDayHourlyForMonth(0); // Current month
505
+ console.log(hours);
506
+ ```
507
+
443
508
  ### `getUniqueId`
444
509
 
445
510
  - **Description**: Generates a unique identifier (UUID v4).
@@ -491,6 +556,8 @@ to create an organization that truly reflects you.
491
556
  ### `useIntersectionObserver`
492
557
 
493
558
  - **Description**: Utility hook designed to help virtualize scrolling components. It leverages the Intersection Observer API to detect when an element enters or leaves the viewport, allowing you to mount or unmount heavy DOM elements dynamically for better performance in long lists or large grids.
559
+ 👉 **Watch the performance demo (rendering 7,000 tasks in a single week):**
560
+ [![Performance Demo](https://img.youtube.com/vi/st4QmsaHoDM/0.jpg)](https://youtu.be/st4QmsaHoDM)
494
561
  - **Parameters**:
495
562
  - `ref` (React.RefObject): The ref assigned to the DOM element you want to observe.
496
563
  - `options` (IntersectionObserverInit, optional): Configuration object (e.g., `rootMargin`, `threshold`).
@@ -521,4 +588,32 @@ to create an organization that truly reflects you.
521
588
  };
522
589
  ```
523
590
 
591
+ ### `useCalendarDateState`
592
+
593
+ - **Description**: A hook that calculates and manages the foundational date state for the calendar. It generates the necessary grid data (days of the week and hourly slots) based on a reference date and offset.
594
+ - **Parameters**:
595
+ - `date` (Date): The reference date to center the calendar on.
596
+ - `weekOffset` (number, optional): The week offset (e.g., 0 for current week, 7 for next week).
597
+ - `timeZone` (string, optional): The timezone context.
598
+ - **Returns**: An object containing:
599
+ - `weekDays`: An array of day metadata (day name, month, year, day of month).
600
+ - `dailyHours`: An array of hourly slot configurations for the grid.
601
+
602
+ **Example**:
603
+ ```tsx
604
+ import { useCalendarDateState } from "react-weekly-planning";
605
+
606
+ const MyComponent = ({ date, weekOffset }) => {
607
+ const { weekDays, dailyHours } = useCalendarDateState(date, weekOffset);
608
+
609
+ return (
610
+ <div>
611
+ {weekDays.map(d => <span key={d.dayOfTheMonth}>{d.day}</span>)}
612
+ </div>
613
+ );
614
+ };
615
+ ```
616
+
617
+
618
+
524
619
  ---
@@ -13,12 +13,7 @@ const CalendarForWeek = (props) => {
13
13
  const { dailyHours, weekDays } = useCalendarDateState(props.date, props.weekOffset, props.timeZone);
14
14
  const memoizedHeader = useMemo(() => (_jsx("div", { className: "planningCalendarHeader", children: _jsxs("div", { className: `planningCalendarRow ${props.rowsClassName}`, style: Object.assign(Object.assign({}, theadTrStyle), props.rowsStyle), children: [_jsx("div", { className: `dayTh ${props.groupsColsClassName}`, style: Object.assign({}, props.groupsColsStyle), children: _jsx(GroupsHeadContainer, { className: `${props.groupHeadContainerClassName}`, style: props.groupHeadContainerStyle, groupsHeadRender: props.groupsHeadRender }) }), weekDays.map((day, i) => (_jsx("div", { className: `dayCol ${props.daysColsClassName}`, style: Object.assign({}, props.daysColsStyle), children: _jsx(DayContainer, { style: props.dayStyle, className: props.dayClassName, dayIndex: i, dayRender: props.dayRender, day: day.day, dayOfTheMonth: day.dayOfTheMonth, dayMonth: day.dayMonth, dayYear: day.dayYear }) }, i)))] }, "header") })), [weekDays, props.rowsClassName, props.rowsStyle, props.groupsColsClassName, props.groupsColsStyle, props.groupHeadContainerClassName, props.groupHeadContainerStyle, props.groupsHeadRender, props.daysColsClassName, props.daysColsStyle, props.dayStyle, props.dayClassName, props.dayRender]);
15
15
  const offset = useMemo(() => updateOffsetWithDateCalendar(props.date), [props.date]);
16
- return (_jsx("div", { className: "calendarForWeek", style: { position: "relative" }, children: _jsxs("div", { className: `planningCalendar ${props.className}`, style: Object.assign({}, props.style), children: [memoizedHeader, _jsx("div", { className: "planningCalendarBody", style: {
17
- // position: "absolute",
18
- // width: "100%",
19
- // // top: `${36 + 5}px`,
20
- // top: `${36 + 5 + ((65 + 4) * Math.max(rowSliceIndexStart, 0))}px`,
21
- }, children: (_a = props.groups) === null || _a === void 0 ? void 0 : _a.map((group, i) => {
16
+ return (_jsx("div", { className: "calendarForWeek", style: { position: "relative" }, children: _jsxs("div", { className: `planningCalendar ${props.className}`, style: Object.assign({}, props.style), children: [memoizedHeader, _jsx("div", { className: "planningCalendarBody", style: {}, children: (_a = props.groups) === null || _a === void 0 ? void 0 : _a.map((group, i) => {
22
17
  var _a;
23
18
  const scope = hashScope || "week";
24
19
  const groupHash = getHash(offset, group.id);
@@ -1,16 +1,17 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import "./style.css";
3
3
  import { memo, useMemo } from "react";
4
- import useCalendarDateState from "../hooks/useCalendarDateState";
5
4
  import VirtualGroupRowDay from "./VirtualGroupRowDay";
6
5
  import DayContainer from "./DayContainer";
7
6
  import { useCalendarTaskContext } from "../contexts/CalendarTaskContext";
7
+ import useCalendarDateStateForDay from "../hooks/useCalendarDateStateForDay";
8
8
  function CalendarForDay(props) {
9
- const { dailyHours, weekDays } = useCalendarDateState(props.date, props.weekOffset, props.timeZone);
9
+ const { dailyHours, weekDays, weekOffset: currentWeekOffset } = useCalendarDateStateForDay(props.date, props.weekOffset, props.timeZone, props.dayOffset);
10
10
  const { getTasks, isValidTask, addTask, deleteTask, updateTask, getTask, hashScope, tasks } = useCalendarTaskContext();
11
- const currentDay = weekDays[props.dayOffset || 0];
12
- const memoizedHeader = useMemo(() => (currentDay ? (_jsx(DayContainer, { style: props.dayStyle, className: props.dayClassName, dayIndex: props.dayOffset || 0, dayRender: props.dayRender, day: currentDay.day, dayOfTheMonth: currentDay.dayOfTheMonth, dayMonth: currentDay.dayMonth, dayYear: currentDay.dayYear })) : null), [weekDays, props.rowsClassName, props.rowsStyle, props.groupsColsClassName, props.groupsColsStyle, props.groupHeadContainerClassName, props.groupHeadContainerStyle, props.groupsHeadRender, props.daysColsClassName, props.daysColsStyle, props.dayStyle, props.dayClassName, props.dayRender]);
13
- return (_jsxs("div", { className: `CalendarTableForDay ${props.className}`, style: Object.assign({ position: "relative" }, props.style), children: [memoizedHeader, _jsx("div", { className: `CalendarTableForDayTasksContainer`, children: props.groups.map((group, i) => (_jsx(VirtualGroupRowDay, { group: group, i: i, props: props, getTasks: getTasks, isValidTask: isValidTask, addTask: addTask, deleteTask: deleteTask, updateTask: updateTask, getTask: getTask, dailyHours: dailyHours, dayOffset: props.dayOffset || 0, hashScope: hashScope || "day", tasks: tasks }, `${group.id}-${i}`))) })] }));
11
+ const dayIndex = props.dayOffset !== undefined ? props.dayOffset : props.date.getDay();
12
+ const currentDay = weekDays[dayIndex];
13
+ const memoizedHeader = useMemo(() => (currentDay ? (_jsx(DayContainer, { style: props.dayStyle, className: props.dayClassName, dayIndex: dayIndex, dayRender: props.dayRender, day: currentDay.day, dayOfTheMonth: currentDay.dayOfTheMonth, dayMonth: currentDay.dayMonth, dayYear: currentDay.dayYear })) : null), [weekDays, dayIndex, props.dayStyle, props.dayClassName, props.dayRender]);
14
+ return (_jsxs("div", { className: `CalendarTableForDay ${props.className}`, style: Object.assign({ position: "relative" }, props.style), children: [memoizedHeader, _jsx("div", { className: `CalendarTableForDayTasksContainer`, children: props.groups.map((group, i) => (_jsx(VirtualGroupRowDay, { group: group, i: i, props: props, getTasks: getTasks, isValidTask: isValidTask, addTask: addTask, deleteTask: deleteTask, updateTask: updateTask, getTask: getTask, dailyHours: dailyHours, dayOffset: dayIndex, weekOffset: currentWeekOffset, hashScope: hashScope || "day", tasks: tasks }, `${group.id}-${i}`))) })] }));
14
15
  }
15
16
  export default memo(CalendarForDay, (prevProps, nextProps) => {
16
17
  var _a, _b;
@@ -7,7 +7,7 @@ import TaskVirtual from "./TaskContainer/TaskVirtual";
7
7
  import AddTask from "./AddTask";
8
8
  import { getHash, getNewTaskForDropOrPaste, getUniqueId, updateOffsetWithDateCalendar, } from "../lib/utils";
9
9
  import { groupTdStyle } from "../lib/slyles";
10
- const VirtualGroupRow = ({ group, i, props, getTasks, isValidTask, addTask, deleteTask, getTask, dailyHours, hashScope, tasks, sumHoursByGroupsCount, }) => {
10
+ const VirtualGroupRow = ({ group, i, props, getTasks, isValidTask, addTask, deleteTask, getTask, dailyHours, hashScope, tasks, }) => {
11
11
  const ref = useRef(null);
12
12
  const { entry, height } = useIntersectionObserver(ref, {
13
13
  rootMargin: "600px",
@@ -4,17 +4,16 @@ import { useIntersectionObserver } from "../hooks/useIntersectionObserver";
4
4
  import GroupContainer from "./GroupContainer";
5
5
  import TaskVirtual from "./TaskContainer/TaskVirtual";
6
6
  import AddTask from "./AddTask";
7
- import { getHash, getNewTaskForDropOrPaste, getUniqueId, updateOffsetWithDateCalendar, } from "../lib/utils";
8
- const VirtualGroupRowDay = ({ group, i, props, getTasks, isValidTask, addTask, deleteTask, updateTask, getTask, dailyHours, dayOffset, hashScope, tasks, }) => {
7
+ import { getHash, getNewTaskForDropOrPaste, getUniqueId, } from "../lib/utils";
8
+ const VirtualGroupRowDay = ({ group, i, props, getTasks, isValidTask, addTask, deleteTask, updateTask, getTask, dailyHours, dayOffset, weekOffset, hashScope, tasks, }) => {
9
9
  const ref = useRef(null);
10
10
  const { entry, height } = useIntersectionObserver(ref, {
11
11
  rootMargin: "600px",
12
12
  threshold: 0,
13
13
  });
14
14
  const isVisible = !!(entry === null || entry === void 0 ? void 0 : entry.isIntersecting);
15
- const offset = useMemo(() => updateOffsetWithDateCalendar(props.date), [props.date]);
16
15
  const currentDailyHours = dailyHours[dayOffset];
17
- const hash = useMemo(() => getHash(offset, group.id, dayOffset), [offset, group.id, dayOffset]);
16
+ const hash = useMemo(() => getHash(weekOffset, group.id, dayOffset), [weekOffset, group.id, dayOffset]);
18
17
  const cellTasks = useMemo(() => getTasks(hash[hashScope]), [getTasks, hash, hashScope, tasks]);
19
18
  const handleDragOver = (event) => {
20
19
  event.preventDefault();
@@ -45,5 +44,6 @@ export default memo(VirtualGroupRowDay, (prev, next) => {
45
44
  prev.i === next.i &&
46
45
  prev.tasks === next.tasks &&
47
46
  prev.props.date.getTime() === next.props.date.getTime() &&
48
- prev.dayOffset === next.dayOffset);
47
+ prev.dayOffset === next.dayOffset &&
48
+ prev.weekOffset === next.weekOffset);
49
49
  });
@@ -1,7 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import "./style.css";
3
3
  import CalendarForWeek from "./CalendarForWeek";
4
- import CalendarForDay from "./CalendarForday";
5
4
  import CalendarTaskContextProvider from "../contexts/CalendarTaskContext";
6
5
  /**
7
6
  * Calendar component to display tasks and groups in a weekly view.
@@ -58,13 +57,12 @@ import CalendarTaskContextProvider from "../contexts/CalendarTaskContext";
58
57
  * @param {() => React.ReactNode} [props.sumHoursHeadRender] - Custom render function for the sum-of-hours header.
59
58
  * @param {(currentTask: TaskFeildsType) => void} [props.handleClickTask] - Handler function for clicking a task.
60
59
  * @param {(currentGroup: GroupFeildsType) => void} [props.handleClickGroup] - Handler function for clicking a group.
61
- * @param {0|1|2|3|4|5|6} [props.dayOffset] - Offset index for the day column (0 = first day of week, …, 6 = last day).
62
60
  * @param {React.CSSProperties} [props.dayColsStyle] - Additional styles for the day columns.
63
61
  * @param {string} [props.dayColsClassName] - Additional class names for the day columns.
64
62
  * @param {React.CSSProperties} [props.hoursColsStyle] - Additional styles for the hours columns.
65
63
  * @param {string} [props.hoursColsClassName] - Additional class names for the hours columns.
66
64
  */
67
65
  const Calendar = (props) => {
68
- return (_jsx(CalendarTaskContextProvider, { hashScope: "day", children: props.scope === "day" ? _jsx(CalendarForDay, Object.assign({}, props)) : _jsx(CalendarForWeek, Object.assign({}, props)) }));
66
+ return (_jsx(CalendarTaskContextProvider, { hashScope: "day", children: _jsx(CalendarForWeek, Object.assign({}, props)) }));
69
67
  };
70
68
  export default Calendar;
@@ -87,24 +87,66 @@
87
87
  .planningCalendar .dayTh,
88
88
  .planningCalendar .groupCol {
89
89
  flex: 0 0 150px;
90
+ min-width: 150px;
90
91
  color: #0f5173;
91
92
  padding-left: 5px;
92
93
  display: flex;
93
94
  align-items: center;
95
+ background-color: inherit;
94
96
  }
95
97
 
96
98
  .planningCalendar .dayCol {
97
99
  flex: 1;
100
+ min-width: 120px;
98
101
  border-left: 0.74px solid rgba(198, 219, 225, 0.68);
99
- min-width: 0;
100
102
  display: flex;
101
103
  flex-direction: column;
102
104
  justify-content: center;
103
105
  }
104
106
 
107
+ @media (max-width: 768px) {
108
+
109
+ .planningCalendar .dayTh,
110
+ .planningCalendar .groupCol {
111
+ flex: 0 0 100px;
112
+ min-width: 100px;
113
+ font-size: 13px;
114
+ position: sticky;
115
+ left: 0;
116
+ z-index: 50;
117
+ box-shadow: 2px 0 5px rgba(0, 0, 0, 0.05);
118
+ background-color: #fff;
119
+ overflow: hidden;
120
+ white-space: nowrap;
121
+ text-overflow: ellipsis;
122
+ padding-left: 10px;
123
+ }
124
+
125
+ .planningCalendar .dayCol {
126
+ min-width: 120px;
127
+ }
128
+
129
+ .planningCalendarHeader {
130
+ z-index: 201;
131
+ /* Layer over the sticky row column */
132
+ }
133
+
134
+ /* Make sure the group container labels also truncate */
135
+ .groupCol label {
136
+ overflow: hidden;
137
+ text-overflow: ellipsis;
138
+ max-width: 100%;
139
+ }
140
+
141
+ .planningCalendar {
142
+ width: auto;
143
+ }
144
+ }
145
+
105
146
  .planningCalendar .totalTh,
106
147
  .planningCalendar .totalCol {
107
148
  flex: 0 0 50px;
149
+ min-width: 50px;
108
150
  color: #0f5173;
109
151
  text-align: right;
110
152
  padding-right: 5px;
@@ -152,18 +194,15 @@
152
194
  min-height: 80px;
153
195
  }
154
196
 
155
- .calendarForWeek,
156
- .calendarForDay {
197
+ .calendarForWeek {
157
198
  display: flex;
158
199
  width: 100%;
159
200
  flex: 1;
160
201
  overflow: auto;
161
202
  position: relative;
162
-
163
203
  }
164
204
 
165
- .calendarForWeek-container,
166
- .calendarForDay-container {
205
+ .calendarForWeek-container {
167
206
  display: flex;
168
207
  flex-direction: column;
169
208
  width: 100%;
@@ -0,0 +1,19 @@
1
+ import { useMemo } from "react";
2
+ import { calculateWeekDifference, getDateObjectInTimeZone, getDayHourly, getWeekDays, } from "../lib/utils";
3
+ function useCalendarDateStateForDay(date, weekOffset, timeZone, dayOffset) {
4
+ let calendarDateState = useMemo(() => {
5
+ const currentWeekOffset = (weekOffset !== undefined) ? weekOffset : (timeZone
6
+ ? calculateWeekDifference(getDateObjectInTimeZone(timeZone), timeZone)
7
+ : calculateWeekDifference(date, timeZone));
8
+ const weekDays = getWeekDays(currentWeekOffset, timeZone);
9
+ const dailyHours = getDayHourly(currentWeekOffset, timeZone);
10
+ const calData = {
11
+ dailyHours: dailyHours,
12
+ weekDays,
13
+ weekOffset: currentWeekOffset,
14
+ };
15
+ return calData;
16
+ }, [date, weekOffset, dayOffset, timeZone]);
17
+ return Object.assign({}, calendarDateState);
18
+ }
19
+ export default useCalendarDateStateForDay;
package/dist/index.js CHANGED
@@ -15,6 +15,6 @@ export * from './components/GroupContainer';
15
15
  export * from './components/VirtualGroupRow';
16
16
  export * from './components/VirtualGroupRowDay';
17
17
  export * from './components/DayContainer';
18
- export * from './hooks/useCalendarDateState';
18
+ export { default as useCalendarDateState } from './hooks/useCalendarDateState';
19
19
  export * from './contexts/CalendarTaskContext';
20
20
  export * from './hooks/useIntersectionObserver';
package/dist/lib/utils.js CHANGED
@@ -52,6 +52,34 @@ export function getDayHourly(weekOffset, timeZone) {
52
52
  }
53
53
  return dailyHours;
54
54
  }
55
+ /**
56
+ * Get daily hours for all days of a specific month.
57
+ * @param monthOffset - The number of months to offset from current month.
58
+ * @param timeZone - The optional timezone.
59
+ * @returns An array of day objects containing start and end timestamps.
60
+ */
61
+ export function getDayHourlyForMonth(monthOffset, timeZone) {
62
+ const dailyHours = [];
63
+ const currentDate = getCalendarDate(timeZone);
64
+ const targetDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + monthOffset, 1);
65
+ const year = targetDate.getFullYear();
66
+ const month = targetDate.getMonth();
67
+ const daysInMonth = new Date(year, month + 1, 0).getDate();
68
+ for (let i = 1; i <= daysInMonth; i++) {
69
+ const dayDate = new Date(year, month, i);
70
+ const dayStart = new Date(dayDate);
71
+ dayStart.setHours(1, 0, 0, 0);
72
+ const dayEnd = new Date(dayDate);
73
+ dayEnd.setHours(23, 59, 59, 99); // Adjusted for consistency
74
+ dailyHours.push({
75
+ positionDay: i - 1,
76
+ day: dayDate,
77
+ start: dayStart.getTime(),
78
+ end: dayEnd.getTime(),
79
+ });
80
+ }
81
+ return dailyHours;
82
+ }
55
83
  // Convert milliseconds to a readable date format
56
84
  export function millisecondsToDate(milliseconds) {
57
85
  const date = new Date(milliseconds);
@@ -133,6 +161,46 @@ export function getWeekDays(jump, timeZone) {
133
161
  }
134
162
  return weekDays;
135
163
  }
164
+ /**
165
+ * Get days of a specific month with an offset.
166
+ * @param monthOffset - The number of months to offset from current month.
167
+ * @param timeZone - The optional timezone.
168
+ * @returns An array of day metadata for the target month.
169
+ */
170
+ export function getMonthDay(monthOffset, timeZone) {
171
+ const days = ["Sun", "Mon", "Tues", "Wed", "Thur", "Frid", "Sat"];
172
+ const monthNames = [
173
+ "Jan",
174
+ "Feb",
175
+ "Mar",
176
+ "Apr",
177
+ "May",
178
+ "Jun",
179
+ "Jul",
180
+ "Aug",
181
+ "Sept",
182
+ "Oct",
183
+ "Nov",
184
+ "Dec",
185
+ ];
186
+ const currentDate = getCalendarDate(timeZone);
187
+ // Focus on the first of the month targetted by monthOffset
188
+ const targetDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + monthOffset, 1);
189
+ const year = targetDate.getFullYear();
190
+ const month = targetDate.getMonth();
191
+ const daysInMonth = new Date(year, month + 1, 0).getDate();
192
+ let monthDays = [];
193
+ for (let d = 1; d <= daysInMonth; d++) {
194
+ const dayDate = new Date(year, month, d);
195
+ monthDays.push({
196
+ day: days[dayDate.getDay()],
197
+ dayMonth: monthNames[dayDate.getMonth()],
198
+ dayYear: dayDate.getFullYear(),
199
+ dayOfTheMonth: dayDate.getDate(),
200
+ });
201
+ }
202
+ return monthDays;
203
+ }
136
204
  // The remaining functions follow the same structure. Ensure all comments are in English and make the functions exportable as required.
137
205
  /**
138
206
  * Get the ISO week number for a given date.
@@ -180,6 +248,32 @@ export function calculateWeekDifference(dateSelectionnee, timeZone) {
180
248
  const ecartJours = Math.round((dimancheSelected - dimancheActuelle) / MS_PAR_JOUR);
181
249
  return ecartJours; // Retournera 0, 7, -7, 14, -14...
182
250
  }
251
+ /**
252
+ * Calculate the day difference between the selected date and the current date.
253
+ * @param dateSelectionnee - The selected date.
254
+ * @param timeZone - The optional timezone.
255
+ * @returns The difference in days.
256
+ */
257
+ export function calculateDayDifference(dateSelectionnee, timeZone) {
258
+ const dateActuelle = getCalendarDate(timeZone);
259
+ const utcSelected = Date.UTC(typeof dateSelectionnee === "string" ? new Date(dateSelectionnee).getFullYear() : dateSelectionnee.getFullYear(), typeof dateSelectionnee === "string" ? new Date(dateSelectionnee).getMonth() : dateSelectionnee.getMonth(), typeof dateSelectionnee === "string" ? new Date(dateSelectionnee).getDate() : dateSelectionnee.getDate());
260
+ const utcActuelle = Date.UTC(dateActuelle.getFullYear(), dateActuelle.getMonth(), dateActuelle.getDate());
261
+ const MS_PAR_JOUR = 86400000;
262
+ return Math.round((utcSelected - utcActuelle) / MS_PAR_JOUR);
263
+ }
264
+ /**
265
+ * Calculate the month difference between the selected date and the current date.
266
+ * @param dateSelectionnee - The selected date.
267
+ * @param timeZone - The optional timezone.
268
+ * @returns The difference in months.
269
+ */
270
+ export function calculateMonthDifference(dateSelectionnee, timeZone) {
271
+ const dateActuelle = getCalendarDate(timeZone);
272
+ const selectedDate = typeof dateSelectionnee === "string" ? new Date(dateSelectionnee) : dateSelectionnee;
273
+ const yearDiff = selectedDate.getFullYear() - dateActuelle.getFullYear();
274
+ const monthDiff = selectedDate.getMonth() - dateActuelle.getMonth();
275
+ return yearDiff * 12 + monthDiff;
276
+ }
183
277
  /**
184
278
  * Calculate the number of weeks since an arbitrary origin date (January 1, 2022).
185
279
  * @param annee - The year.
@@ -228,6 +322,14 @@ export function compareWeekOffset(calendarDate, weekOffset, taskDate, timeZone)
228
322
  const ecartTask = calculateWeekDifference(taskDate, timeZone);
229
323
  return weekOffset === ecartTask;
230
324
  }
325
+ export function compareMonthOffset(monthOffset, taskDate, timeZone) {
326
+ const ecartTask = calculateMonthDifference(taskDate, timeZone);
327
+ return monthOffset === ecartTask;
328
+ }
329
+ export function compareDayOffset(dayOffset, taskDate, timeZone) {
330
+ const ecartTask = calculateDayDifference(taskDate, timeZone);
331
+ return dayOffset === ecartTask;
332
+ }
231
333
  export const sumHoursByGroups = (groupId, tasks, weekOffset, calendarDate, timeZone) => {
232
334
  let sum = 0;
233
335
  if (tasks)
@@ -239,6 +341,28 @@ export const sumHoursByGroups = (groupId, tasks, weekOffset, calendarDate, timeZ
239
341
  });
240
342
  return sum;
241
343
  };
344
+ export const sumHoursByGroupsForMonth = (groupId, tasks, monthOffset, timeZone) => {
345
+ let sum = 0;
346
+ if (tasks)
347
+ tasks.forEach((task) => {
348
+ if (task.groupId === groupId &&
349
+ compareMonthOffset(monthOffset, task.taskDate, timeZone) === true) {
350
+ sum += task.taskEnd - task.taskStart;
351
+ }
352
+ });
353
+ return sum;
354
+ };
355
+ export const sumHoursByGroupsForDay = (groupId, tasks, dayOffset, timeZone) => {
356
+ let sum = 0;
357
+ if (tasks)
358
+ tasks.forEach((task) => {
359
+ if (task.groupId === groupId &&
360
+ compareDayOffset(dayOffset, task.taskDate, timeZone) === true) {
361
+ sum += task.taskEnd - task.taskStart;
362
+ }
363
+ });
364
+ return sum;
365
+ };
242
366
  export function saveTasksToLocalStorage(tasks) {
243
367
  if (typeof window !== "undefined") {
244
368
  window.localStorage.setItem("Calendar", "je marche");
@@ -272,6 +396,18 @@ export const updateOffsetWithDateCalendar = (calendarDate, timeZone) => {
272
396
  }
273
397
  return calculateWeekDifference(calendarDate, timeZone);
274
398
  };
399
+ export const updateOffsetWithDateCalendarForMonth = (calendarDate, timeZone) => {
400
+ if (typeof calendarDate === 'string') {
401
+ return calculateMonthDifference(new Date(calendarDate), timeZone);
402
+ }
403
+ return calculateMonthDifference(calendarDate, timeZone);
404
+ };
405
+ export const updateOffsetWithDateCalendarForDay = (calendarDate, timeZone) => {
406
+ if (typeof calendarDate === 'string') {
407
+ return calculateDayDifference(new Date(calendarDate), timeZone);
408
+ }
409
+ return calculateDayDifference(calendarDate, timeZone);
410
+ };
275
411
  export const millisecondsToHours = (milliseconds) => {
276
412
  return millisecondsToDate(milliseconds).formattedDate;
277
413
  };
@@ -12,6 +12,7 @@ interface VirtualGroupRowDayProps {
12
12
  getTask: (hash: string, taskId: string) => TaskType | undefined;
13
13
  dailyHours: dayInfoType[];
14
14
  dayOffset: number;
15
+ weekOffset: number;
15
16
  hashScope: "week" | "group" | "day";
16
17
  tasks: TasksStore;
17
18
  }
@@ -55,7 +55,6 @@ import { CalendarPropsType } from "../definitions";
55
55
  * @param {() => React.ReactNode} [props.sumHoursHeadRender] - Custom render function for the sum-of-hours header.
56
56
  * @param {(currentTask: TaskFeildsType) => void} [props.handleClickTask] - Handler function for clicking a task.
57
57
  * @param {(currentGroup: GroupFeildsType) => void} [props.handleClickGroup] - Handler function for clicking a group.
58
- * @param {0|1|2|3|4|5|6} [props.dayOffset] - Offset index for the day column (0 = first day of week, …, 6 = last day).
59
58
  * @param {React.CSSProperties} [props.dayColsStyle] - Additional styles for the day columns.
60
59
  * @param {string} [props.dayColsClassName] - Additional class names for the day columns.
61
60
  * @param {React.CSSProperties} [props.hoursColsStyle] - Additional styles for the hours columns.
@@ -96,7 +96,6 @@ export type DayPropsType = {
96
96
  */
97
97
  export type CalendarPropsType = {
98
98
  drop?: "copy" | "move";
99
- scope?: "day" | "week";
100
99
  /** Offset for the week (e.g., -7 for last week, 0 for current week, 7 for next week). */
101
100
  weekOffset?: number;
102
101
  /** Array of group data to be displayed in the calendar. */
@@ -194,8 +193,6 @@ export type CalendarPropsType = {
194
193
  handleClickGroup?: (currentGroup: GroupFeildsType) => void;
195
194
  /** your timezones */
196
195
  timeZone?: TimeZone;
197
- /** day id */
198
- dayOffset?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
199
196
  /**day columns styles */
200
197
  dayColsStyle?: React.CSSProperties | undefined;
201
198
  /**day columns className*/
@@ -0,0 +1,17 @@
1
+ import { TimeZone } from "../definitions";
2
+ declare function useCalendarDateStateForDay(date: Date, weekOffset: number | undefined, timeZone: TimeZone | undefined, dayOffset: number | undefined): {
3
+ dailyHours: {
4
+ positionDay: number;
5
+ day: Date;
6
+ start: number;
7
+ end: number;
8
+ }[];
9
+ weekDays: {
10
+ day: string;
11
+ dayMonth: string;
12
+ dayYear: number;
13
+ dayOfTheMonth: number;
14
+ }[];
15
+ weekOffset: number;
16
+ };
17
+ export default useCalendarDateStateForDay;
@@ -12,6 +12,6 @@ export * from './components/GroupContainer';
12
12
  export * from './components/VirtualGroupRow';
13
13
  export * from './components/VirtualGroupRowDay';
14
14
  export * from './components/DayContainer';
15
- export * from './hooks/useCalendarDateState';
15
+ export { default as useCalendarDateState } from './hooks/useCalendarDateState';
16
16
  export * from './contexts/CalendarTaskContext';
17
17
  export * from './hooks/useIntersectionObserver';
@@ -9,6 +9,18 @@ export declare function getDayHourly(weekOffset: number, timeZone?: TimeZone): {
9
9
  start: number;
10
10
  end: number;
11
11
  }[];
12
+ /**
13
+ * Get daily hours for all days of a specific month.
14
+ * @param monthOffset - The number of months to offset from current month.
15
+ * @param timeZone - The optional timezone.
16
+ * @returns An array of day objects containing start and end timestamps.
17
+ */
18
+ export declare function getDayHourlyForMonth(monthOffset: number, timeZone?: TimeZone): {
19
+ positionDay: number;
20
+ day: Date;
21
+ start: number;
22
+ end: number;
23
+ }[];
12
24
  export declare function millisecondsToDate(milliseconds: number): {
13
25
  formattedDate: string;
14
26
  dayOfWeek: string;
@@ -23,12 +35,38 @@ export declare function getWeekDays(jump: number, timeZone?: TimeZone): {
23
35
  dayYear: number;
24
36
  dayOfTheMonth: number;
25
37
  }[];
38
+ /**
39
+ * Get days of a specific month with an offset.
40
+ * @param monthOffset - The number of months to offset from current month.
41
+ * @param timeZone - The optional timezone.
42
+ * @returns An array of day metadata for the target month.
43
+ */
44
+ export declare function getMonthDay(monthOffset: number, timeZone?: TimeZone): {
45
+ day: string;
46
+ dayMonth: string;
47
+ dayYear: number;
48
+ dayOfTheMonth: number;
49
+ }[];
26
50
  /**
27
51
  * Calculate the week difference between the selected date and the current date.
28
52
  * @param dateSelectionnee - The selected date.
29
53
  * @returns The week difference in days.
30
54
  */
31
55
  export declare function calculateWeekDifference(dateSelectionnee: Date, timeZone?: TimeZone): number;
56
+ /**
57
+ * Calculate the day difference between the selected date and the current date.
58
+ * @param dateSelectionnee - The selected date.
59
+ * @param timeZone - The optional timezone.
60
+ * @returns The difference in days.
61
+ */
62
+ export declare function calculateDayDifference(dateSelectionnee: Date, timeZone?: TimeZone): number;
63
+ /**
64
+ * Calculate the month difference between the selected date and the current date.
65
+ * @param dateSelectionnee - The selected date.
66
+ * @param timeZone - The optional timezone.
67
+ * @returns The difference in months.
68
+ */
69
+ export declare function calculateMonthDifference(dateSelectionnee: Date, timeZone?: TimeZone): number;
32
70
  /**
33
71
  * Calculate the number of weeks since an arbitrary origin date (January 1, 2022).
34
72
  * @param annee - The year.
@@ -42,10 +80,16 @@ export declare function getNewTaskForDropOrPaste(positionDay: number, dropGroupI
42
80
  newTask: any;
43
81
  } | undefined;
44
82
  export declare function compareWeekOffset(calendarDate: Date, weekOffset: number, taskDate: Date, timeZone?: TimeZone): boolean;
83
+ export declare function compareMonthOffset(monthOffset: number, taskDate: Date, timeZone?: TimeZone): boolean;
84
+ export declare function compareDayOffset(dayOffset: number, taskDate: Date, timeZone?: TimeZone): boolean;
45
85
  export declare const sumHoursByGroups: (groupId: string, tasks: TasksType | any, weekOffset: number, calendarDate: Date, timeZone?: TimeZone) => number;
86
+ export declare const sumHoursByGroupsForMonth: (groupId: string, tasks: TasksType | any, monthOffset: number, timeZone?: TimeZone) => number;
87
+ export declare const sumHoursByGroupsForDay: (groupId: string, tasks: TasksType | any, dayOffset: number, timeZone?: TimeZone) => number;
46
88
  export declare function saveTasksToLocalStorage(tasks: TasksType): void;
47
89
  export declare const updateCalendarDateWithOffset: (offset: number, timeZone?: TimeZone) => Date;
48
90
  export declare const updateOffsetWithDateCalendar: (calendarDate: Date, timeZone?: TimeZone) => number;
91
+ export declare const updateOffsetWithDateCalendarForMonth: (calendarDate: Date, timeZone?: TimeZone) => number;
92
+ export declare const updateOffsetWithDateCalendarForDay: (calendarDate: Date, timeZone?: TimeZone) => number;
49
93
  export declare const millisecondsToHours: (milliseconds: number) => string;
50
94
  export declare const checkDuplicates: (tasks: TasksType, taskStart: number, taskEnd: number, groupId: string) => boolean;
51
95
  export declare const getSavedTasks: () => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-weekly-planning",
3
- "version": "1.0.44",
3
+ "version": "1.0.46",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "exports": {
@@ -67,6 +67,10 @@
67
67
  "./hooks/useIntersectionObserver": {
68
68
  "types": "./dist/types/hooks/useIntersectionObserver.d.ts",
69
69
  "default": "./dist/hooks/useIntersectionObserver.js"
70
+ },
71
+ "./hooks/useCalendarDateState": {
72
+ "types": "./dist/types/hooks/useCalendarDateState.d.ts",
73
+ "default": "./dist/hooks/useCalendarDatState.js"
70
74
  }
71
75
  },
72
76
  "files": [