@olenbetong/synergi-react 5.6.0 → 5.7.0
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/es/chrome/refresh.d.ts +16 -0
- package/es/chrome/refresh.js +65 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/package.json +9 -7
- package/src/chrome/refresh.test.tsx +78 -0
- package/src/chrome/refresh.ts +86 -0
- package/src/index.ts +1 -0
- package/src/test/setup.ts +4 -2
- package/src/vehicles/VehicleInfoDialog/formatting.test.ts +10 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes pull-to-refresh run `handler` instead of reloading the page, while the component is
|
|
3
|
+
* mounted. The indicator spins until the returned promise settles. The latest render's handler is
|
|
4
|
+
* used; pass `null` or `false` to leave the page reload in place (for a page that only sometimes
|
|
5
|
+
* can reload its own data). Nested components win: the one mounted last is the one that runs.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* usePullToRefresh(() => Promise.all([dsTimesheet.refreshDataSource(), dsActivities.refreshDataSource()]));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function usePullToRefresh(handler: (() => unknown | Promise<unknown>) | null | false | undefined): void;
|
|
13
|
+
/** Turns the pull-to-refresh gesture off while the component is mounted (and `disabled` is true). */
|
|
14
|
+
export declare function useDisablePullToRefresh(disabled?: boolean): void;
|
|
15
|
+
/** Runs the page's refresh, as a pull would: the registered handler, or a reload. */
|
|
16
|
+
export declare function refreshPage(): Promise<void>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
const CHROME_READY_EVENT = "ob:chrome-ready";
|
|
3
|
+
function currentRefresh() {
|
|
4
|
+
return globalThis.ob?.chrome?.refresh;
|
|
5
|
+
}
|
|
6
|
+
function hasChrome() {
|
|
7
|
+
return Boolean(globalThis.ob?.chrome);
|
|
8
|
+
}
|
|
9
|
+
/** Calls `use` once the chrome is on the page and returns a function that undoes it. */
|
|
10
|
+
function withRefresh(use) {
|
|
11
|
+
let cleanup;
|
|
12
|
+
let run = () => {
|
|
13
|
+
let refresh = currentRefresh();
|
|
14
|
+
if (refresh)
|
|
15
|
+
cleanup = use(refresh);
|
|
16
|
+
};
|
|
17
|
+
if (hasChrome() || typeof window === "undefined") {
|
|
18
|
+
run();
|
|
19
|
+
return () => cleanup?.();
|
|
20
|
+
}
|
|
21
|
+
window.addEventListener(CHROME_READY_EVENT, run, { once: true });
|
|
22
|
+
return () => {
|
|
23
|
+
window.removeEventListener(CHROME_READY_EVENT, run);
|
|
24
|
+
cleanup?.();
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Makes pull-to-refresh run `handler` instead of reloading the page, while the component is
|
|
29
|
+
* mounted. The indicator spins until the returned promise settles. The latest render's handler is
|
|
30
|
+
* used; pass `null` or `false` to leave the page reload in place (for a page that only sometimes
|
|
31
|
+
* can reload its own data). Nested components win: the one mounted last is the one that runs.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* usePullToRefresh(() => Promise.all([dsTimesheet.refreshDataSource(), dsActivities.refreshDataSource()]));
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function usePullToRefresh(handler) {
|
|
39
|
+
let latest = useRef(handler);
|
|
40
|
+
useLayoutEffect(() => {
|
|
41
|
+
latest.current = handler;
|
|
42
|
+
});
|
|
43
|
+
let active = Boolean(handler);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (!active)
|
|
46
|
+
return;
|
|
47
|
+
return withRefresh((refresh) => refresh.register(() => (latest.current ? latest.current() : undefined)));
|
|
48
|
+
}, [active]);
|
|
49
|
+
}
|
|
50
|
+
/** Turns the pull-to-refresh gesture off while the component is mounted (and `disabled` is true). */
|
|
51
|
+
export function useDisablePullToRefresh(disabled = true) {
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!disabled)
|
|
54
|
+
return;
|
|
55
|
+
return withRefresh((refresh) => refresh.disable());
|
|
56
|
+
}, [disabled]);
|
|
57
|
+
}
|
|
58
|
+
/** Runs the page's refresh, as a pull would: the registered handler, or a reload. */
|
|
59
|
+
export function refreshPage() {
|
|
60
|
+
let refresh = currentRefresh();
|
|
61
|
+
if (refresh)
|
|
62
|
+
return refresh.refresh();
|
|
63
|
+
window.location.reload();
|
|
64
|
+
return Promise.resolve();
|
|
65
|
+
}
|
package/es/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export { MergeProgressDialogDs, type MergeProgressDialogDsProps, } from "./compo
|
|
|
11
11
|
export { type MergeJobOptions, type MergePhase, useMergeJob } from "./components/MergeProgressDialog/useMergeJob.js";
|
|
12
12
|
export { type AssistantTool, type AssistantToolParameters, isAssistantEnabled, openAssistant, useAssistantContext, useAssistantInstructions, useAssistantTool, } from "./chrome/assistant.js";
|
|
13
13
|
export { type OpenReportOptions, openReport } from "./chrome/openReport.js";
|
|
14
|
+
export { refreshPage, useDisablePullToRefresh, usePullToRefresh } from "./chrome/refresh.js";
|
|
14
15
|
export { BannerImage as PageBannerImage, type BannerImageProps, PageBanner, type PageBannerProps, Tab as PageBannerTab, TabList as PageBannerTabList, type TabListProps as PageBannerTabListProps, type TabProps as PageBannerTabProps, Title as PageBannerTitle, type TitleProps as PageBannerTitleProps, } from "./components/PageBanner/index.js";
|
|
15
16
|
export { Portal } from "./components/Portal/index.js";
|
|
16
17
|
export { ToolbarBreadcrumbs, ToolbarDrawerToggle, ToolbarPortal, type BreadcrumbItem, type ToolbarBreadcrumbsProps, type ToolbarDrawerToggleProps, type ToolbarPortalProps, } from "./components/Toolbar/index.js";
|
package/es/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export { MergeProgressDialogDs, } from "./components/MergeProgressDialog/MergePr
|
|
|
13
13
|
export { useMergeJob } from "./components/MergeProgressDialog/useMergeJob.js";
|
|
14
14
|
export { isAssistantEnabled, openAssistant, useAssistantContext, useAssistantInstructions, useAssistantTool, } from "./chrome/assistant.js";
|
|
15
15
|
export { openReport } from "./chrome/openReport.js";
|
|
16
|
+
export { refreshPage, useDisablePullToRefresh, usePullToRefresh } from "./chrome/refresh.js";
|
|
16
17
|
export { BannerImage as PageBannerImage, PageBanner, Tab as PageBannerTab, TabList as PageBannerTabList, Title as PageBannerTitle, } from "./components/PageBanner/index.js";
|
|
17
18
|
// PdfViewer is intentionally not re-exported here. It references the pdf.js worker
|
|
18
19
|
// with `new URL(..., import.meta.url)`, which makes bundlers emit the 1.2 MB worker
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.0",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -58,14 +58,17 @@
|
|
|
58
58
|
"@navikt/aksel-icons": ">=8.16.1",
|
|
59
59
|
"react-router": ">=8.3.0"
|
|
60
60
|
},
|
|
61
|
-
"sideEffects":
|
|
61
|
+
"sideEffects": [
|
|
62
|
+
"**/*.css",
|
|
63
|
+
"./es/elements-ds/index.js"
|
|
64
|
+
],
|
|
62
65
|
"dependencies": {
|
|
63
66
|
"@emotion/styled": "^11.14.1",
|
|
64
67
|
"@mui/icons-material": "9.5.0",
|
|
65
68
|
"@mui/material": "9.5.0",
|
|
66
|
-
"@olenbetong/appframe-core": "^2.16.
|
|
69
|
+
"@olenbetong/appframe-core": "^2.16.2",
|
|
67
70
|
"@olenbetong/appframe-data": "^1.10.3",
|
|
68
|
-
"@olenbetong/appframe-ds": "^1.
|
|
71
|
+
"@olenbetong/appframe-ds": "^1.14.0",
|
|
69
72
|
"@olenbetong/appframe-mui": "^7.0.13",
|
|
70
73
|
"@olenbetong/appframe-react": "^2.2.8",
|
|
71
74
|
"clsx": "^2.1.1",
|
|
@@ -119,11 +122,10 @@
|
|
|
119
122
|
},
|
|
120
123
|
"optionalDependencies": {},
|
|
121
124
|
"scripts": {
|
|
122
|
-
"build": "pnpm run build:tsc && pnpm
|
|
125
|
+
"build": "pnpm run build:tsc && pnpm run build:sass:components && pnpm test && pnpm run build:sass",
|
|
123
126
|
"build:sass": "sass ./src/index.scss ./dist/styles/styles.css --source-map-urls=relative --style compressed",
|
|
124
127
|
"build:sass:components": "node ./scripts/buildSassComponents.mjs",
|
|
125
|
-
"build:tsc
|
|
126
|
-
"build:tsc": "node --no-warnings ../../scripts/clean.ts ./es tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
|
|
128
|
+
"build:tsc": "node --no-warnings ../../scripts/clean.ts tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json && node ./scripts/replaceCss.mjs && node --no-warnings ../../scripts/prune-output.ts ./es ./src",
|
|
127
129
|
"test": "tsc -p tsconfig.test.json && vitest run",
|
|
128
130
|
"test:browser": "vitest run --config vitest.browser.config.ts",
|
|
129
131
|
"test:coverage": "vitest run --coverage"
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { render } from "@testing-library/react";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { useDisablePullToRefresh, usePullToRefresh } from "./refresh.js";
|
|
5
|
+
|
|
6
|
+
function installChrome() {
|
|
7
|
+
let handlers: Array<() => unknown> = [];
|
|
8
|
+
let disabled = 0;
|
|
9
|
+
let refresh = {
|
|
10
|
+
register: vi.fn((handler: () => unknown) => {
|
|
11
|
+
handlers.push(handler);
|
|
12
|
+
return () => handlers.splice(handlers.indexOf(handler), 1);
|
|
13
|
+
}),
|
|
14
|
+
refresh: async () => {
|
|
15
|
+
await handlers[handlers.length - 1]?.();
|
|
16
|
+
},
|
|
17
|
+
disable: vi.fn(() => {
|
|
18
|
+
disabled++;
|
|
19
|
+
return () => disabled--;
|
|
20
|
+
}),
|
|
21
|
+
get enabled() {
|
|
22
|
+
return disabled === 0;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
(globalThis as { ob?: unknown }).ob = { chrome: { refresh } };
|
|
26
|
+
return refresh;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Refreshing({ handler }: { handler: () => unknown }) {
|
|
30
|
+
usePullToRefresh(handler);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function NoPull() {
|
|
35
|
+
useDisablePullToRefresh();
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("usePullToRefresh", () => {
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
delete (globalThis as { ob?: unknown }).ob;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("registers the latest handler while mounted", async () => {
|
|
45
|
+
let refresh = installChrome();
|
|
46
|
+
let first = vi.fn();
|
|
47
|
+
let second = vi.fn();
|
|
48
|
+
let { rerender, unmount } = render(<Refreshing handler={first} />);
|
|
49
|
+
rerender(<Refreshing handler={second} />);
|
|
50
|
+
|
|
51
|
+
await refresh.refresh();
|
|
52
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
53
|
+
expect(first).not.toHaveBeenCalled();
|
|
54
|
+
expect(refresh.register).toHaveBeenCalledTimes(1);
|
|
55
|
+
|
|
56
|
+
unmount();
|
|
57
|
+
await refresh.refresh();
|
|
58
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("waits for the chrome when the app mounts first", async () => {
|
|
62
|
+
let handler = vi.fn();
|
|
63
|
+
render(<Refreshing handler={handler} />);
|
|
64
|
+
|
|
65
|
+
let refresh = installChrome();
|
|
66
|
+
window.dispatchEvent(new CustomEvent("ob:chrome-ready"));
|
|
67
|
+
await refresh.refresh();
|
|
68
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("turns the gesture off while mounted", () => {
|
|
72
|
+
let refresh = installChrome();
|
|
73
|
+
let { unmount } = render(<NoPull />);
|
|
74
|
+
expect(refresh.enabled).toBe(false);
|
|
75
|
+
unmount();
|
|
76
|
+
expect(refresh.enabled).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Pulling down at the top of a page on a 2026 template reloads the page: the site chrome
|
|
5
|
+
* (`@olenbetong/synergi-chrome`) owns the gesture. An app that can reload just its data offers its
|
|
6
|
+
* own refresh instead, and a page with vertical gestures of its own turns the pull off. These hooks
|
|
7
|
+
* forward to `window.ob.chrome.refresh`, waiting for the chrome to mount (`ob:chrome-ready`) when
|
|
8
|
+
* the app starts first. On a page without the chrome they do nothing.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type RefreshApi = {
|
|
12
|
+
register: (handler: () => unknown | Promise<unknown>) => () => void;
|
|
13
|
+
refresh: () => Promise<void>;
|
|
14
|
+
disable: () => () => void;
|
|
15
|
+
readonly enabled: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const CHROME_READY_EVENT = "ob:chrome-ready";
|
|
19
|
+
|
|
20
|
+
function currentRefresh(): RefreshApi | undefined {
|
|
21
|
+
return (globalThis as { ob?: { chrome?: { refresh?: RefreshApi } } }).ob?.chrome?.refresh;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function hasChrome() {
|
|
25
|
+
return Boolean((globalThis as { ob?: { chrome?: unknown } }).ob?.chrome);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Calls `use` once the chrome is on the page and returns a function that undoes it. */
|
|
29
|
+
function withRefresh(use: (refresh: RefreshApi) => void | (() => void)): () => void {
|
|
30
|
+
let cleanup: void | (() => void);
|
|
31
|
+
let run = () => {
|
|
32
|
+
let refresh = currentRefresh();
|
|
33
|
+
if (refresh) cleanup = use(refresh);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
if (hasChrome() || typeof window === "undefined") {
|
|
37
|
+
run();
|
|
38
|
+
return () => cleanup?.();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
window.addEventListener(CHROME_READY_EVENT, run, { once: true });
|
|
42
|
+
return () => {
|
|
43
|
+
window.removeEventListener(CHROME_READY_EVENT, run);
|
|
44
|
+
cleanup?.();
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Makes pull-to-refresh run `handler` instead of reloading the page, while the component is
|
|
50
|
+
* mounted. The indicator spins until the returned promise settles. The latest render's handler is
|
|
51
|
+
* used; pass `null` or `false` to leave the page reload in place (for a page that only sometimes
|
|
52
|
+
* can reload its own data). Nested components win: the one mounted last is the one that runs.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* usePullToRefresh(() => Promise.all([dsTimesheet.refreshDataSource(), dsActivities.refreshDataSource()]));
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function usePullToRefresh(handler: (() => unknown | Promise<unknown>) | null | false | undefined) {
|
|
60
|
+
let latest = useRef(handler);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
latest.current = handler;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
let active = Boolean(handler);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!active) return;
|
|
68
|
+
return withRefresh((refresh) => refresh.register(() => (latest.current ? latest.current() : undefined)));
|
|
69
|
+
}, [active]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Turns the pull-to-refresh gesture off while the component is mounted (and `disabled` is true). */
|
|
73
|
+
export function useDisablePullToRefresh(disabled = true) {
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (!disabled) return;
|
|
76
|
+
return withRefresh((refresh) => refresh.disable());
|
|
77
|
+
}, [disabled]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Runs the page's refresh, as a pull would: the registered handler, or a reload. */
|
|
81
|
+
export function refreshPage(): Promise<void> {
|
|
82
|
+
let refresh = currentRefresh();
|
|
83
|
+
if (refresh) return refresh.refresh();
|
|
84
|
+
window.location.reload();
|
|
85
|
+
return Promise.resolve();
|
|
86
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -58,6 +58,7 @@ export {
|
|
|
58
58
|
useAssistantTool,
|
|
59
59
|
} from "./chrome/assistant.js";
|
|
60
60
|
export { type OpenReportOptions, openReport } from "./chrome/openReport.js";
|
|
61
|
+
export { refreshPage, useDisablePullToRefresh, usePullToRefresh } from "./chrome/refresh.js";
|
|
61
62
|
export {
|
|
62
63
|
BannerImage as PageBannerImage,
|
|
63
64
|
type BannerImageProps,
|
package/src/test/setup.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import "@testing-library/jest-dom/
|
|
1
|
+
import * as matchers from "@testing-library/jest-dom/matchers";
|
|
2
2
|
|
|
3
3
|
import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
|
|
4
4
|
|
|
5
5
|
import { cleanup } from "@testing-library/react";
|
|
6
|
-
import { afterEach } from "vitest";
|
|
6
|
+
import { afterEach, expect } from "vitest";
|
|
7
|
+
|
|
8
|
+
expect.extend(matchers);
|
|
7
9
|
|
|
8
10
|
afterEach(() => {
|
|
9
11
|
cleanup();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
2
2
|
|
|
3
3
|
import { formatMinutes, formatPeriod, fromInputs, getNewPeriod, toDateInput, toTimeInput } from "./formatting.js";
|
|
4
4
|
|
|
@@ -27,6 +27,15 @@ describe("formatMinutes", () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
describe("formatPeriod", () => {
|
|
30
|
+
// The format follows the user's culture, and without one the machine's locale (12-hour on en-US).
|
|
31
|
+
beforeAll(() => {
|
|
32
|
+
vi.stubGlobal("af", { userSession: { culture: "nb-NO" } });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterAll(() => {
|
|
36
|
+
vi.unstubAllGlobals();
|
|
37
|
+
});
|
|
38
|
+
|
|
30
39
|
it("is empty without both ends of the period", () => {
|
|
31
40
|
expect(formatPeriod(null, new Date())).toBe("");
|
|
32
41
|
});
|