@olenbetong/synergi-react 4.1.1 → 4.1.3

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.
Files changed (59) hide show
  1. package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.js +3 -1
  2. package/es/components/ImageEditor/useEdgeDetection.d.ts +17 -2
  3. package/es/components/ImageEditor/useEdgeDetection.js +241 -61
  4. package/es/components/Toolbar/index.js +20 -1
  5. package/package.json +3 -3
  6. package/src/chrome/AppShell/index.tsx +29 -29
  7. package/src/chrome/openReport.ts +34 -34
  8. package/src/colorScheme.ts +81 -81
  9. package/src/components/DateNavigator/index.tsx +186 -186
  10. package/src/components/FilePond.tsx +312 -312
  11. package/src/components/ImageEditor/ImageEditor.tsx +343 -343
  12. package/src/components/ImageEditor/index.ts +9 -9
  13. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  14. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  15. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -403
  16. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  17. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  18. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  19. package/src/components/ImageEditor/useEdgeDetection.ts +319 -127
  20. package/src/components/ImageEditor/useImageHistory.ts +197 -197
  21. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  22. package/src/components/LinkedCardList/index.scss +95 -95
  23. package/src/components/MergeProgressDialog/index.tsx +249 -249
  24. package/src/components/PageBanner/index.tsx +80 -80
  25. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  26. package/src/components/ProgressBar/index.scss +96 -96
  27. package/src/components/SplitContainer/index.tsx +333 -333
  28. package/src/components/Toolbar/index.tsx +232 -212
  29. package/src/designsystemet-web-types.d.ts +24 -24
  30. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  31. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  32. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  33. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  34. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  35. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  36. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  37. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  38. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  39. package/src/elements/ElementLookup.tsx +123 -123
  40. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
  41. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
  42. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
  43. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
  44. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
  45. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
  46. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
  47. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
  48. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  49. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  50. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  51. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  52. package/src/elements/ProjectSelect.tsx +130 -130
  53. package/src/elements/index.ts +15 -15
  54. package/src/filter/index.ts +34 -34
  55. package/src/i18n.ts +13 -13
  56. package/src/index.ts +77 -77
  57. package/src/mui/MuiThemeProvider.tsx +174 -174
  58. package/src/mui/index.ts +1 -1
  59. package/src/test/setup.ts +8 -8
@@ -1,81 +1,81 @@
1
- import { useCallback, useEffect, useState } from "react";
2
-
3
- /**
4
- * Color scheme preference (system / light / dark).
5
- *
6
- * The preference lives in the `ob-color-scheme` cookie so the article template
7
- * can read it server side and render `data-color-scheme` on `<html>` and
8
- * `<body>` before the first paint. The site chrome bundle
9
- * (`@olenbetong/synergi-chrome`) owns the same cookie for the toolbar user menu
10
- * and app menu toggles, so all of them write the cookie, apply the attributes,
11
- * and dispatch `COLOR_SCHEME_CHANGE_EVENT` to keep every toggle on the page in sync.
12
- */
13
-
14
- export type ColorSchemePreference = "auto" | "light" | "dark";
15
-
16
- const COOKIE_NAME = "ob-color-scheme";
17
- const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
18
- const DEFAULT_PREFERENCE: ColorSchemePreference = "auto";
19
- const PREFERENCES: ColorSchemePreference[] = ["auto", "light", "dark"];
20
-
21
- /** Dispatched on `window` whenever the preference changes. */
22
- export const COLOR_SCHEME_CHANGE_EVENT = "ob:colorschemechange";
23
-
24
- function isColorSchemePreference(value: unknown): value is ColorSchemePreference {
25
- return typeof value === "string" && PREFERENCES.includes(value as ColorSchemePreference);
26
- }
27
-
28
- export function getColorSchemePreference(): ColorSchemePreference {
29
- if (typeof document === "undefined") return DEFAULT_PREFERENCE;
30
-
31
- let match = document.cookie.match(new RegExp(`(?:^|;\\s*)${COOKIE_NAME}=([^;]*)`));
32
- let value = match ? decodeURIComponent(match[1]) : null;
33
-
34
- return isColorSchemePreference(value) ? value : DEFAULT_PREFERENCE;
35
- }
36
-
37
- export function setColorSchemePreference(preference: ColorSchemePreference) {
38
- if (typeof document === "undefined" || !isColorSchemePreference(preference)) return;
39
-
40
- let secure = window.location.protocol === "https:" ? ";secure" : "";
41
- document.cookie = `${COOKIE_NAME}=${preference};path=/;max-age=${COOKIE_MAX_AGE};samesite=lax${secure}`;
42
-
43
- /*
44
- * Both elements are updated: `<html>` so the browser applies `color-scheme`
45
- * to scrollbars and native controls, `<body>` because Designsystemet declares
46
- * the light tokens on `:root` as well (`:root, [data-color-scheme="light"]`).
47
- */
48
- document.documentElement.dataset.colorScheme = preference;
49
- if (document.body) document.body.dataset.colorScheme = preference;
50
-
51
- window.dispatchEvent(new CustomEvent(COLOR_SCHEME_CHANGE_EVENT, { detail: preference }));
52
- }
53
-
54
- /** Reads the stored preference and keeps it in sync with other toggles on the page. */
55
- export function useColorSchemePreference(): [ColorSchemePreference, (value: ColorSchemePreference) => void] {
56
- let [preference, setPreference] = useState<ColorSchemePreference>(() => getColorSchemePreference());
57
-
58
- useEffect(() => {
59
- function update() {
60
- setPreference(getColorSchemePreference());
61
- }
62
-
63
- update();
64
-
65
- window.addEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
66
- // Cookies fire no change event, so also pick up changes made in another tab.
67
- document.addEventListener("visibilitychange", update);
68
-
69
- return () => {
70
- window.removeEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
71
- document.removeEventListener("visibilitychange", update);
72
- };
73
- }, []);
74
-
75
- let setValue = useCallback((value: ColorSchemePreference) => {
76
- setColorSchemePreference(value);
77
- setPreference(value);
78
- }, []);
79
-
80
- return [preference, setValue];
81
- }
1
+ import { useCallback, useEffect, useState } from "react";
2
+
3
+ /**
4
+ * Color scheme preference (system / light / dark).
5
+ *
6
+ * The preference lives in the `ob-color-scheme` cookie so the article template
7
+ * can read it server side and render `data-color-scheme` on `<html>` and
8
+ * `<body>` before the first paint. The site chrome bundle
9
+ * (`@olenbetong/synergi-chrome`) owns the same cookie for the toolbar user menu
10
+ * and app menu toggles, so all of them write the cookie, apply the attributes,
11
+ * and dispatch `COLOR_SCHEME_CHANGE_EVENT` to keep every toggle on the page in sync.
12
+ */
13
+
14
+ export type ColorSchemePreference = "auto" | "light" | "dark";
15
+
16
+ const COOKIE_NAME = "ob-color-scheme";
17
+ const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
18
+ const DEFAULT_PREFERENCE: ColorSchemePreference = "auto";
19
+ const PREFERENCES: ColorSchemePreference[] = ["auto", "light", "dark"];
20
+
21
+ /** Dispatched on `window` whenever the preference changes. */
22
+ export const COLOR_SCHEME_CHANGE_EVENT = "ob:colorschemechange";
23
+
24
+ function isColorSchemePreference(value: unknown): value is ColorSchemePreference {
25
+ return typeof value === "string" && PREFERENCES.includes(value as ColorSchemePreference);
26
+ }
27
+
28
+ export function getColorSchemePreference(): ColorSchemePreference {
29
+ if (typeof document === "undefined") return DEFAULT_PREFERENCE;
30
+
31
+ let match = document.cookie.match(new RegExp(`(?:^|;\\s*)${COOKIE_NAME}=([^;]*)`));
32
+ let value = match ? decodeURIComponent(match[1]) : null;
33
+
34
+ return isColorSchemePreference(value) ? value : DEFAULT_PREFERENCE;
35
+ }
36
+
37
+ export function setColorSchemePreference(preference: ColorSchemePreference) {
38
+ if (typeof document === "undefined" || !isColorSchemePreference(preference)) return;
39
+
40
+ let secure = window.location.protocol === "https:" ? ";secure" : "";
41
+ document.cookie = `${COOKIE_NAME}=${preference};path=/;max-age=${COOKIE_MAX_AGE};samesite=lax${secure}`;
42
+
43
+ /*
44
+ * Both elements are updated: `<html>` so the browser applies `color-scheme`
45
+ * to scrollbars and native controls, `<body>` because Designsystemet declares
46
+ * the light tokens on `:root` as well (`:root, [data-color-scheme="light"]`).
47
+ */
48
+ document.documentElement.dataset.colorScheme = preference;
49
+ if (document.body) document.body.dataset.colorScheme = preference;
50
+
51
+ window.dispatchEvent(new CustomEvent(COLOR_SCHEME_CHANGE_EVENT, { detail: preference }));
52
+ }
53
+
54
+ /** Reads the stored preference and keeps it in sync with other toggles on the page. */
55
+ export function useColorSchemePreference(): [ColorSchemePreference, (value: ColorSchemePreference) => void] {
56
+ let [preference, setPreference] = useState<ColorSchemePreference>(() => getColorSchemePreference());
57
+
58
+ useEffect(() => {
59
+ function update() {
60
+ setPreference(getColorSchemePreference());
61
+ }
62
+
63
+ update();
64
+
65
+ window.addEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
66
+ // Cookies fire no change event, so also pick up changes made in another tab.
67
+ document.addEventListener("visibilitychange", update);
68
+
69
+ return () => {
70
+ window.removeEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
71
+ document.removeEventListener("visibilitychange", update);
72
+ };
73
+ }, []);
74
+
75
+ let setValue = useCallback((value: ColorSchemePreference) => {
76
+ setColorSchemePreference(value);
77
+ setPreference(value);
78
+ }, []);
79
+
80
+ return [preference, setValue];
81
+ }
@@ -1,186 +1,186 @@
1
- import "./index.css";
2
-
3
- import clsx from "clsx";
4
- import { useEffect, useRef, useState } from "react";
5
- import { getLocalizedString } from "../../i18n.js";
6
-
7
- function getUiCulture() {
8
- return af?.userSession?.culture ?? "default";
9
- }
10
-
11
- function Arrow({
12
- direction,
13
- ...rest
14
- }: { direction: "left" | "right" } & React.ButtonHTMLAttributes<HTMLButtonElement>) {
15
- return (
16
- <button
17
- className={clsx(
18
- "ObDateNavigator-arrow btn-reset",
19
- direction === "left" ? "ObDateNavigator-arrowLeft" : "ObDateNavigator-arrowRight",
20
- )}
21
- aria-label={direction === "left" ? getLocalizedString("Previous day") : getLocalizedString("Next day")}
22
- {...rest}
23
- >
24
- <span className="ObDateNavigator-icon">
25
- {direction === "left" ? (
26
- <svg aria-hidden="true" viewBox="0 0 320 512">
27
- <path
28
- fill="currentColor"
29
- d="M34.52 239.03L228.87 44.69c9.37-9.37 24.57-9.37 33.94 0l22.67 22.67c9.36 9.36 9.37 24.52.04 33.9L131.49 256l154.02 154.75c9.34 9.38 9.32 24.54-.04 33.9l-22.67 22.67c-9.37 9.37-24.57 9.37-33.94 0L34.52 272.97c-9.37-9.37-9.37-24.57 0-33.94z"
30
- />
31
- </svg>
32
- ) : (
33
- <svg aria-hidden="true" viewBox="0 0 320 512">
34
- <path
35
- fill="currentColor"
36
- d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"
37
- />
38
- </svg>
39
- )}
40
- </span>
41
- </button>
42
- );
43
- }
44
-
45
- const formatter = new Intl.DateTimeFormat(getUiCulture(), {
46
- weekday: "short",
47
- day: "numeric",
48
- month: "short",
49
- year: "numeric",
50
- timeZone: "utc",
51
- });
52
-
53
- const oneDay = 24 * 60 * 60 * 1000;
54
-
55
- export type DateNavigatorProps = {
56
- date?: Date | string | number;
57
- defaultDate?: Date | string | number; // For default uncontrolled value
58
- disableTodayButton?: boolean;
59
- onChange?: (date: Date) => void;
60
- minDate?: Date | string | number;
61
- maxDate?: Date | string | number;
62
- };
63
-
64
- function sanitizeDate(date: Date | string | number): Date {
65
- if (typeof date === "string" || typeof date === "number") {
66
- let parsedDate = new Date(date);
67
- if (Number.isNaN(parsedDate.getTime())) {
68
- throw new Error(`Invalid date value: ${date}`);
69
- }
70
- return parsedDate;
71
- } else if (date instanceof Date) {
72
- if (Number.isNaN(date.getTime())) {
73
- throw new Error(`Invalid date object: ${date}`);
74
- }
75
- return new Date(date.getTime()); // Return a copy of the date
76
- } else {
77
- throw new Error(`Unsupported date type: ${typeof date}`);
78
- }
79
- }
80
-
81
- export function DateNavigator({
82
- date: dateProp,
83
- defaultDate,
84
- disableTodayButton,
85
- onChange,
86
- minDate: minDateProp,
87
- maxDate: maxDateProp,
88
- }: DateNavigatorProps) {
89
- let [internalDate, setInternalDate] = useState(() => (defaultDate ? sanitizeDate(defaultDate) : new Date()));
90
- let date = dateProp ? sanitizeDate(dateProp) : internalDate;
91
- let dateUtc = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
92
- let today = new Date();
93
- let todayUtc = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate());
94
- let liveRef = useRef<HTMLDivElement>(null);
95
-
96
- let text = (() => {
97
- let base = formatter.format(new Date(dateUtc));
98
- if (todayUtc === dateUtc) return `${getLocalizedString("Today")}, ${base}`;
99
- if (todayUtc === dateUtc + oneDay) return `${getLocalizedString("Yesterday")}, ${base}`;
100
- if (todayUtc === dateUtc - oneDay) return `${getLocalizedString("Tomorrow")}, ${base}`;
101
- return base;
102
- })();
103
-
104
- let minDate = minDateProp && sanitizeDate(minDateProp);
105
- let maxDate = maxDateProp && sanitizeDate(maxDateProp);
106
- let disablePrev = !!minDate && dateUtc <= Date.UTC(minDate.getFullYear(), minDate.getMonth(), minDate.getDate());
107
- let disableNext = !!maxDate && dateUtc >= Date.UTC(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate());
108
-
109
- function updateDate(newDate: Date) {
110
- if (minDate && newDate < minDate) {
111
- newDate = new Date(minDate); // Ensure we don't go below minDate
112
- } else if (maxDate && newDate > maxDate) {
113
- newDate = new Date(maxDate); // Ensure we don't go above maxDate
114
- }
115
-
116
- onChange?.(newDate);
117
- setInternalDate(newDate);
118
- }
119
-
120
- function jumpToToday() {
121
- updateDate(new Date());
122
- }
123
-
124
- useEffect(() => {
125
- if (liveRef.current) {
126
- liveRef.current.textContent = text;
127
- }
128
- }, [text]);
129
-
130
- function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
131
- if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
132
- if (event.defaultPrevented) return;
133
- if (event.altKey || event.metaKey) return; // Ignore if alt or meta key is pressed
134
-
135
- if (disablePrev && event.key === "ArrowLeft") return;
136
- if (disableNext && event.key === "ArrowRight") return;
137
-
138
- if (event.ctrlKey && event.shiftKey) {
139
- // If both ctrl and shift are pressed, navigate by 1 year
140
- let newDate = new Date(dateUtc);
141
- if (event.key === "ArrowLeft") {
142
- newDate.setFullYear(newDate.getFullYear() - 1);
143
- } else if (event.key === "ArrowRight") {
144
- newDate.setFullYear(newDate.getFullYear() + 1);
145
- }
146
- updateDate(newDate);
147
- return;
148
- }
149
-
150
- if (event.ctrlKey) {
151
- // Navigate by month
152
- let newDate = new Date(dateUtc);
153
- if (event.key === "ArrowLeft") {
154
- newDate.setMonth(newDate.getMonth() - 1);
155
- } else if (event.key === "ArrowRight") {
156
- newDate.setMonth(newDate.getMonth() + 1);
157
- }
158
- updateDate(newDate);
159
- return;
160
- }
161
-
162
- let modifier = event.shiftKey ? 7 : 1;
163
- if (event.key === "ArrowLeft") {
164
- updateDate(new Date(dateUtc - oneDay * modifier));
165
- } else if (event.key === "ArrowRight") {
166
- updateDate(new Date(dateUtc + oneDay * modifier));
167
- }
168
- }
169
-
170
- return (
171
- // oxlint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- I hate this component
172
- // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- legacy component
173
- // oxlint-disable-next-line jsx-a11y/role-supports-aria-props -- legacy component
174
- <div className="ObDateNavigator-root" onKeyDown={handleKeyDown} tabIndex={0} aria-label="Date navigator">
175
- <Arrow direction="left" onClick={() => updateDate(new Date(dateUtc - oneDay))} disabled={disablePrev} />
176
- <div className="ObDateNavigator-date">{text}</div>
177
- <Arrow direction="right" onClick={() => updateDate(new Date(dateUtc + oneDay))} disabled={disableNext} />
178
- {!disableTodayButton && (
179
- <button type="button" className="ObDateNavigator-today btn-reset" onClick={jumpToToday}>
180
- {getLocalizedString("Today")}
181
- </button>
182
- )}
183
- <div aria-live="polite" ref={liveRef} style={{ position: "absolute", left: "-9999px" }} />
184
- </div>
185
- );
186
- }
1
+ import "./index.css";
2
+
3
+ import clsx from "clsx";
4
+ import { useEffect, useRef, useState } from "react";
5
+ import { getLocalizedString } from "../../i18n.js";
6
+
7
+ function getUiCulture() {
8
+ return af?.userSession?.culture ?? "default";
9
+ }
10
+
11
+ function Arrow({
12
+ direction,
13
+ ...rest
14
+ }: { direction: "left" | "right" } & React.ButtonHTMLAttributes<HTMLButtonElement>) {
15
+ return (
16
+ <button
17
+ className={clsx(
18
+ "ObDateNavigator-arrow btn-reset",
19
+ direction === "left" ? "ObDateNavigator-arrowLeft" : "ObDateNavigator-arrowRight",
20
+ )}
21
+ aria-label={direction === "left" ? getLocalizedString("Previous day") : getLocalizedString("Next day")}
22
+ {...rest}
23
+ >
24
+ <span className="ObDateNavigator-icon">
25
+ {direction === "left" ? (
26
+ <svg aria-hidden="true" viewBox="0 0 320 512">
27
+ <path
28
+ fill="currentColor"
29
+ d="M34.52 239.03L228.87 44.69c9.37-9.37 24.57-9.37 33.94 0l22.67 22.67c9.36 9.36 9.37 24.52.04 33.9L131.49 256l154.02 154.75c9.34 9.38 9.32 24.54-.04 33.9l-22.67 22.67c-9.37 9.37-24.57 9.37-33.94 0L34.52 272.97c-9.37-9.37-9.37-24.57 0-33.94z"
30
+ />
31
+ </svg>
32
+ ) : (
33
+ <svg aria-hidden="true" viewBox="0 0 320 512">
34
+ <path
35
+ fill="currentColor"
36
+ d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"
37
+ />
38
+ </svg>
39
+ )}
40
+ </span>
41
+ </button>
42
+ );
43
+ }
44
+
45
+ const formatter = new Intl.DateTimeFormat(getUiCulture(), {
46
+ weekday: "short",
47
+ day: "numeric",
48
+ month: "short",
49
+ year: "numeric",
50
+ timeZone: "utc",
51
+ });
52
+
53
+ const oneDay = 24 * 60 * 60 * 1000;
54
+
55
+ export type DateNavigatorProps = {
56
+ date?: Date | string | number;
57
+ defaultDate?: Date | string | number; // For default uncontrolled value
58
+ disableTodayButton?: boolean;
59
+ onChange?: (date: Date) => void;
60
+ minDate?: Date | string | number;
61
+ maxDate?: Date | string | number;
62
+ };
63
+
64
+ function sanitizeDate(date: Date | string | number): Date {
65
+ if (typeof date === "string" || typeof date === "number") {
66
+ let parsedDate = new Date(date);
67
+ if (Number.isNaN(parsedDate.getTime())) {
68
+ throw new Error(`Invalid date value: ${date}`);
69
+ }
70
+ return parsedDate;
71
+ } else if (date instanceof Date) {
72
+ if (Number.isNaN(date.getTime())) {
73
+ throw new Error(`Invalid date object: ${date}`);
74
+ }
75
+ return new Date(date.getTime()); // Return a copy of the date
76
+ } else {
77
+ throw new Error(`Unsupported date type: ${typeof date}`);
78
+ }
79
+ }
80
+
81
+ export function DateNavigator({
82
+ date: dateProp,
83
+ defaultDate,
84
+ disableTodayButton,
85
+ onChange,
86
+ minDate: minDateProp,
87
+ maxDate: maxDateProp,
88
+ }: DateNavigatorProps) {
89
+ let [internalDate, setInternalDate] = useState(() => (defaultDate ? sanitizeDate(defaultDate) : new Date()));
90
+ let date = dateProp ? sanitizeDate(dateProp) : internalDate;
91
+ let dateUtc = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
92
+ let today = new Date();
93
+ let todayUtc = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate());
94
+ let liveRef = useRef<HTMLDivElement>(null);
95
+
96
+ let text = (() => {
97
+ let base = formatter.format(new Date(dateUtc));
98
+ if (todayUtc === dateUtc) return `${getLocalizedString("Today")}, ${base}`;
99
+ if (todayUtc === dateUtc + oneDay) return `${getLocalizedString("Yesterday")}, ${base}`;
100
+ if (todayUtc === dateUtc - oneDay) return `${getLocalizedString("Tomorrow")}, ${base}`;
101
+ return base;
102
+ })();
103
+
104
+ let minDate = minDateProp && sanitizeDate(minDateProp);
105
+ let maxDate = maxDateProp && sanitizeDate(maxDateProp);
106
+ let disablePrev = !!minDate && dateUtc <= Date.UTC(minDate.getFullYear(), minDate.getMonth(), minDate.getDate());
107
+ let disableNext = !!maxDate && dateUtc >= Date.UTC(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate());
108
+
109
+ function updateDate(newDate: Date) {
110
+ if (minDate && newDate < minDate) {
111
+ newDate = new Date(minDate); // Ensure we don't go below minDate
112
+ } else if (maxDate && newDate > maxDate) {
113
+ newDate = new Date(maxDate); // Ensure we don't go above maxDate
114
+ }
115
+
116
+ onChange?.(newDate);
117
+ setInternalDate(newDate);
118
+ }
119
+
120
+ function jumpToToday() {
121
+ updateDate(new Date());
122
+ }
123
+
124
+ useEffect(() => {
125
+ if (liveRef.current) {
126
+ liveRef.current.textContent = text;
127
+ }
128
+ }, [text]);
129
+
130
+ function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
131
+ if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
132
+ if (event.defaultPrevented) return;
133
+ if (event.altKey || event.metaKey) return; // Ignore if alt or meta key is pressed
134
+
135
+ if (disablePrev && event.key === "ArrowLeft") return;
136
+ if (disableNext && event.key === "ArrowRight") return;
137
+
138
+ if (event.ctrlKey && event.shiftKey) {
139
+ // If both ctrl and shift are pressed, navigate by 1 year
140
+ let newDate = new Date(dateUtc);
141
+ if (event.key === "ArrowLeft") {
142
+ newDate.setFullYear(newDate.getFullYear() - 1);
143
+ } else if (event.key === "ArrowRight") {
144
+ newDate.setFullYear(newDate.getFullYear() + 1);
145
+ }
146
+ updateDate(newDate);
147
+ return;
148
+ }
149
+
150
+ if (event.ctrlKey) {
151
+ // Navigate by month
152
+ let newDate = new Date(dateUtc);
153
+ if (event.key === "ArrowLeft") {
154
+ newDate.setMonth(newDate.getMonth() - 1);
155
+ } else if (event.key === "ArrowRight") {
156
+ newDate.setMonth(newDate.getMonth() + 1);
157
+ }
158
+ updateDate(newDate);
159
+ return;
160
+ }
161
+
162
+ let modifier = event.shiftKey ? 7 : 1;
163
+ if (event.key === "ArrowLeft") {
164
+ updateDate(new Date(dateUtc - oneDay * modifier));
165
+ } else if (event.key === "ArrowRight") {
166
+ updateDate(new Date(dateUtc + oneDay * modifier));
167
+ }
168
+ }
169
+
170
+ return (
171
+ // oxlint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- I hate this component
172
+ // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- legacy component
173
+ // oxlint-disable-next-line jsx-a11y/role-supports-aria-props -- legacy component
174
+ <div className="ObDateNavigator-root" onKeyDown={handleKeyDown} tabIndex={0} aria-label="Date navigator">
175
+ <Arrow direction="left" onClick={() => updateDate(new Date(dateUtc - oneDay))} disabled={disablePrev} />
176
+ <div className="ObDateNavigator-date">{text}</div>
177
+ <Arrow direction="right" onClick={() => updateDate(new Date(dateUtc + oneDay))} disabled={disableNext} />
178
+ {!disableTodayButton && (
179
+ <button type="button" className="ObDateNavigator-today btn-reset" onClick={jumpToToday}>
180
+ {getLocalizedString("Today")}
181
+ </button>
182
+ )}
183
+ <div aria-live="polite" ref={liveRef} style={{ position: "absolute", left: "-9999px" }} />
184
+ </div>
185
+ );
186
+ }