@flipdish/ui-library 0.58.0 → 0.58.1
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/dist/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.cjs +43 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.cjs.map +1 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.d.ts +14 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.d.ts.map +1 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.js +41 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.js.map +1 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.cjs +57 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.cjs.map +1 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.d.ts +16 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.d.ts.map +1 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.js +55 -0
- package/dist/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.js.map +1 -0
- package/dist/providers/UnsavedChangesGuardProvider/index.cjs +7 -61
- package/dist/providers/UnsavedChangesGuardProvider/index.cjs.map +1 -1
- package/dist/providers/UnsavedChangesGuardProvider/index.d.ts.map +1 -1
- package/dist/providers/UnsavedChangesGuardProvider/index.js +8 -62
- package/dist/providers/UnsavedChangesGuardProvider/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var unsavedChangesGuard_constants = require('../unsavedChangesGuard.constants.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Covers the one navigation a host makes after the user agrees to leave, so that move is not held or prompted again.
|
|
8
|
+
* The cover ends when it is taken, when the path or the set of unsaved forms changes, or after a second if the host never moves.
|
|
9
|
+
*
|
|
10
|
+
* @param pathname The current path. A change of it ends the cover; a change of search or hash does not.
|
|
11
|
+
* @param dirtyIds The ids of the forms that are still unsaved. A new set ends the cover.
|
|
12
|
+
* @returns `takeHostApproval`, which reports whether this move is the approved one and ends the cover, and
|
|
13
|
+
* `noteHostAnswer`, which starts the cover when the user agrees to leave.
|
|
14
|
+
*/
|
|
15
|
+
const useHostApproval = (pathname, dirtyIds) => {
|
|
16
|
+
// Set when the user lets a host navigate, so the navigation they just approved is not held or prompted a second time.
|
|
17
|
+
const isHostApproved = React.useRef(false);
|
|
18
|
+
const hostApprovalTimer = React.useRef(undefined);
|
|
19
|
+
const endHostApproval = React.useCallback(() => {
|
|
20
|
+
isHostApproved.current = false;
|
|
21
|
+
clearTimeout(hostApprovalTimer.current);
|
|
22
|
+
}, []);
|
|
23
|
+
/** Whether the host's approved navigation is the one happening now; the approval covers one navigation only. */
|
|
24
|
+
const takeHostApproval = React.useCallback(() => {
|
|
25
|
+
const isApproved = isHostApproved.current;
|
|
26
|
+
endHostApproval();
|
|
27
|
+
return isApproved;
|
|
28
|
+
}, [endHostApproval]);
|
|
29
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: a new path or a new set of unsaved forms ends the host's approval
|
|
30
|
+
React.useEffect(() => endHostApproval, [pathname, dirtyIds, endHostApproval]);
|
|
31
|
+
const noteHostAnswer = React.useCallback((leave) => {
|
|
32
|
+
endHostApproval();
|
|
33
|
+
if (leave) {
|
|
34
|
+
isHostApproved.current = true;
|
|
35
|
+
// A host that then does not navigate (another app said stay) must not leave the form unguarded.
|
|
36
|
+
hostApprovalTimer.current = setTimeout(endHostApproval, unsavedChangesGuard_constants.HOST_APPROVAL_MS);
|
|
37
|
+
}
|
|
38
|
+
}, [endHostApproval]);
|
|
39
|
+
return { takeHostApproval, noteHostAnswer };
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
exports.useHostApproval = useHostApproval;
|
|
43
|
+
//# sourceMappingURL=useHostApproval.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHostApproval.cjs","sources":["../../../../src/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.ts"],"sourcesContent":[null],"names":["useRef","useCallback","useEffect","HOST_APPROVAL_MS"],"mappings":";;;;;AAKA;;;;;;;;AAQG;MACU,eAAe,GAAG,CAAC,QAAgB,EAAE,QAA2B,KAAI;;AAE/E,IAAA,MAAM,cAAc,GAAGA,YAAM,CAAC,KAAK,CAAC;AACpC,IAAA,MAAM,iBAAiB,GAAGA,YAAM,CAAgC,SAAS,CAAC;AAE1E,IAAA,MAAM,eAAe,GAAGC,iBAAW,CAAC,MAAK;AACvC,QAAA,cAAc,CAAC,OAAO,GAAG,KAAK;AAC9B,QAAA,YAAY,CAAC,iBAAiB,CAAC,OAAO,CAAC;IACzC,CAAC,EAAE,EAAE,CAAC;;AAEN,IAAA,MAAM,gBAAgB,GAAGA,iBAAW,CAAC,MAAK;AACxC,QAAA,MAAM,UAAU,GAAG,cAAc,CAAC,OAAO;AACzC,QAAA,eAAe,EAAE;AACjB,QAAA,OAAO,UAAU;AACnB,IAAA,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC;;AAGrB,IAAAC,eAAS,CAAC,MAAM,eAAe,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAC;AAEvE,IAAA,MAAM,cAAc,GAAGD,iBAAW,CAChC,CAAC,KAAc,KAAI;AACjB,QAAA,eAAe,EAAE;QACjB,IAAI,KAAK,EAAE;AACT,YAAA,cAAc,CAAC,OAAO,GAAG,IAAI;;YAE7B,iBAAiB,CAAC,OAAO,GAAG,UAAU,CAAC,eAAe,EAAEE,8CAAgB,CAAC;QAC3E;AACF,IAAA,CAAC,EACD,CAAC,eAAe,CAAC,CAClB;AAED,IAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE;AAC7C;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Covers the one navigation a host makes after the user agrees to leave, so that move is not held or prompted again.
|
|
3
|
+
* The cover ends when it is taken, when the path or the set of unsaved forms changes, or after a second if the host never moves.
|
|
4
|
+
*
|
|
5
|
+
* @param pathname The current path. A change of it ends the cover; a change of search or hash does not.
|
|
6
|
+
* @param dirtyIds The ids of the forms that are still unsaved. A new set ends the cover.
|
|
7
|
+
* @returns `takeHostApproval`, which reports whether this move is the approved one and ends the cover, and
|
|
8
|
+
* `noteHostAnswer`, which starts the cover when the user agrees to leave.
|
|
9
|
+
*/
|
|
10
|
+
export declare const useHostApproval: (pathname: string, dirtyIds: readonly string[]) => {
|
|
11
|
+
takeHostApproval: () => boolean;
|
|
12
|
+
noteHostAnswer: (leave: boolean) => void;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=useHostApproval.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHostApproval.d.ts","sourceRoot":"","sources":["../../../../src/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,EAAE,UAAU,SAAS,MAAM,EAAE;;4BAoBjE,OAAO;CAYlB,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useRef, useCallback, useEffect } from 'react';
|
|
2
|
+
import { HOST_APPROVAL_MS } from '../unsavedChangesGuard.constants.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Covers the one navigation a host makes after the user agrees to leave, so that move is not held or prompted again.
|
|
6
|
+
* The cover ends when it is taken, when the path or the set of unsaved forms changes, or after a second if the host never moves.
|
|
7
|
+
*
|
|
8
|
+
* @param pathname The current path. A change of it ends the cover; a change of search or hash does not.
|
|
9
|
+
* @param dirtyIds The ids of the forms that are still unsaved. A new set ends the cover.
|
|
10
|
+
* @returns `takeHostApproval`, which reports whether this move is the approved one and ends the cover, and
|
|
11
|
+
* `noteHostAnswer`, which starts the cover when the user agrees to leave.
|
|
12
|
+
*/
|
|
13
|
+
const useHostApproval = (pathname, dirtyIds) => {
|
|
14
|
+
// Set when the user lets a host navigate, so the navigation they just approved is not held or prompted a second time.
|
|
15
|
+
const isHostApproved = useRef(false);
|
|
16
|
+
const hostApprovalTimer = useRef(undefined);
|
|
17
|
+
const endHostApproval = useCallback(() => {
|
|
18
|
+
isHostApproved.current = false;
|
|
19
|
+
clearTimeout(hostApprovalTimer.current);
|
|
20
|
+
}, []);
|
|
21
|
+
/** Whether the host's approved navigation is the one happening now; the approval covers one navigation only. */
|
|
22
|
+
const takeHostApproval = useCallback(() => {
|
|
23
|
+
const isApproved = isHostApproved.current;
|
|
24
|
+
endHostApproval();
|
|
25
|
+
return isApproved;
|
|
26
|
+
}, [endHostApproval]);
|
|
27
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: a new path or a new set of unsaved forms ends the host's approval
|
|
28
|
+
useEffect(() => endHostApproval, [pathname, dirtyIds, endHostApproval]);
|
|
29
|
+
const noteHostAnswer = useCallback((leave) => {
|
|
30
|
+
endHostApproval();
|
|
31
|
+
if (leave) {
|
|
32
|
+
isHostApproved.current = true;
|
|
33
|
+
// A host that then does not navigate (another app said stay) must not leave the form unguarded.
|
|
34
|
+
hostApprovalTimer.current = setTimeout(endHostApproval, HOST_APPROVAL_MS);
|
|
35
|
+
}
|
|
36
|
+
}, [endHostApproval]);
|
|
37
|
+
return { takeHostApproval, noteHostAnswer };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { useHostApproval };
|
|
41
|
+
//# sourceMappingURL=useHostApproval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHostApproval.js","sources":["../../../../src/providers/UnsavedChangesGuardProvider/hooks/useHostApproval.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAKA;;;;;;;;AAQG;MACU,eAAe,GAAG,CAAC,QAAgB,EAAE,QAA2B,KAAI;;AAE/E,IAAA,MAAM,cAAc,GAAG,MAAM,CAAC,KAAK,CAAC;AACpC,IAAA,MAAM,iBAAiB,GAAG,MAAM,CAAgC,SAAS,CAAC;AAE1E,IAAA,MAAM,eAAe,GAAG,WAAW,CAAC,MAAK;AACvC,QAAA,cAAc,CAAC,OAAO,GAAG,KAAK;AAC9B,QAAA,YAAY,CAAC,iBAAiB,CAAC,OAAO,CAAC;IACzC,CAAC,EAAE,EAAE,CAAC;;AAEN,IAAA,MAAM,gBAAgB,GAAG,WAAW,CAAC,MAAK;AACxC,QAAA,MAAM,UAAU,GAAG,cAAc,CAAC,OAAO;AACzC,QAAA,eAAe,EAAE;AACjB,QAAA,OAAO,UAAU;AACnB,IAAA,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC;;AAGrB,IAAA,SAAS,CAAC,MAAM,eAAe,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAC;AAEvE,IAAA,MAAM,cAAc,GAAG,WAAW,CAChC,CAAC,KAAc,KAAI;AACjB,QAAA,eAAe,EAAE;QACjB,IAAI,KAAK,EAAE;AACT,YAAA,cAAc,CAAC,OAAO,GAAG,IAAI;;YAE7B,iBAAiB,CAAC,OAAO,GAAG,UAAU,CAAC,eAAe,EAAE,gBAAgB,CAAC;QAC3E;AACF,IAAA,CAAC,EACD,CAAC,eAAe,CAAC,CAClB;AAED,IAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE;AAC7C;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var unsavedChangesGuard_utils = require('../utilities/unsavedChangesGuard.utils.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Registers with the host's navigation bridge while there are unsaved changes, and keeps the answer the host is waiting on.
|
|
8
|
+
* A second ask replaces the first, which is told to stay so the host is never left waiting.
|
|
9
|
+
*
|
|
10
|
+
* @param hasUnsavedChanges Whether any form beneath the provider is still dirty. The bridge is registered only while it is.
|
|
11
|
+
* @param basePath The app's router base path, handed to the host with the registration.
|
|
12
|
+
* @returns The host's current ask, and a way to drop it once the provider has answered.
|
|
13
|
+
*/
|
|
14
|
+
const useHostGuardRegistration = (hasUnsavedChanges, basePath) => {
|
|
15
|
+
const [answerHost, setAnswerHost] = React.useState(null);
|
|
16
|
+
const clearAnswerHost = React.useCallback(() => setAnswerHost(null), []);
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
const guard = hasUnsavedChanges ? unsavedChangesGuard_utils.hostGuard() : undefined;
|
|
19
|
+
if (!guard)
|
|
20
|
+
return;
|
|
21
|
+
let pending = null;
|
|
22
|
+
let isRegistered = true;
|
|
23
|
+
const canLeave = () => new Promise((resolve) => {
|
|
24
|
+
// A host holding a registration that has since gone has nothing left to wait for.
|
|
25
|
+
if (!isRegistered)
|
|
26
|
+
return resolve(true);
|
|
27
|
+
// A second request replaces the first, which is answered "stay" so the host is never left waiting.
|
|
28
|
+
pending?.(false);
|
|
29
|
+
pending = resolve;
|
|
30
|
+
setAnswerHost(() => resolve);
|
|
31
|
+
});
|
|
32
|
+
let unregister;
|
|
33
|
+
try {
|
|
34
|
+
unregister = guard.register({ basePath, canLeave });
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// A bridge that throws is treated as no bridge: this runs outside the app's error boundary, so it would take the page down.
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
return () => {
|
|
41
|
+
isRegistered = false;
|
|
42
|
+
try {
|
|
43
|
+
unregister();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// Ignored, as a `register` that throws is: this cleanup runs outside the app's error boundary too.
|
|
47
|
+
}
|
|
48
|
+
// Nothing is left to lose once the changes are saved or the form is gone, so a waiting host may go.
|
|
49
|
+
pending?.(true);
|
|
50
|
+
setAnswerHost(null);
|
|
51
|
+
};
|
|
52
|
+
}, [hasUnsavedChanges, basePath]);
|
|
53
|
+
return { answerHost, clearAnswerHost };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
exports.useHostGuardRegistration = useHostGuardRegistration;
|
|
57
|
+
//# sourceMappingURL=useHostGuardRegistration.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHostGuardRegistration.cjs","sources":["../../../../src/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.ts"],"sourcesContent":[null],"names":["useState","useCallback","useEffect","hostGuard"],"mappings":";;;;;AAQA;;;;;;;AAOG;MACU,wBAAwB,GAAG,CAAC,iBAA0B,EAAE,QAAgB,KAAI;IACvF,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAGA,cAAQ,CAAoB,IAAI,CAAC;AACrE,IAAA,MAAM,eAAe,GAAGC,iBAAW,CAAC,MAAM,aAAa,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;IAElEC,eAAS,CAAC,MAAK;AACb,QAAA,MAAM,KAAK,GAAG,iBAAiB,GAAGC,mCAAS,EAAE,GAAG,SAAS;AACzD,QAAA,IAAI,CAAC,KAAK;YAAE;QACZ,IAAI,OAAO,GAAsB,IAAI;QACrC,IAAI,YAAY,GAAG,IAAI;QACvB,MAAM,QAAQ,GAAG,MACf,IAAI,OAAO,CAAU,CAAC,OAAO,KAAI;;AAE/B,YAAA,IAAI,CAAC,YAAY;AAAE,gBAAA,OAAO,OAAO,CAAC,IAAI,CAAC;;AAEvC,YAAA,OAAO,GAAG,KAAK,CAAC;YAChB,OAAO,GAAG,OAAO;AACjB,YAAA,aAAa,CAAC,MAAM,OAAO,CAAC;AAC9B,QAAA,CAAC,CAAC;AACJ,QAAA,IAAI,UAAsB;AAC1B,QAAA,IAAI;YACF,UAAU,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;QACrD;AAAE,QAAA,MAAM;;YAEN;QACF;AACA,QAAA,OAAO,MAAK;YACV,YAAY,GAAG,KAAK;AACpB,YAAA,IAAI;AACF,gBAAA,UAAU,EAAE;YACd;AAAE,YAAA,MAAM;;YAER;;AAEA,YAAA,OAAO,GAAG,IAAI,CAAC;YACf,aAAa,CAAC,IAAI,CAAC;AACrB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAC;AAEjC,IAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE;AACxC;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Resolves a host's `canLeave`: `true` lets the host navigate. */
|
|
2
|
+
type HostAnswer = (leave: boolean) => void;
|
|
3
|
+
/**
|
|
4
|
+
* Registers with the host's navigation bridge while there are unsaved changes, and keeps the answer the host is waiting on.
|
|
5
|
+
* A second ask replaces the first, which is told to stay so the host is never left waiting.
|
|
6
|
+
*
|
|
7
|
+
* @param hasUnsavedChanges Whether any form beneath the provider is still dirty. The bridge is registered only while it is.
|
|
8
|
+
* @param basePath The app's router base path, handed to the host with the registration.
|
|
9
|
+
* @returns The host's current ask, and a way to drop it once the provider has answered.
|
|
10
|
+
*/
|
|
11
|
+
export declare const useHostGuardRegistration: (hasUnsavedChanges: boolean, basePath: string) => {
|
|
12
|
+
answerHost: HostAnswer | null;
|
|
13
|
+
clearAnswerHost: () => void;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=useHostGuardRegistration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHostGuardRegistration.d.ts","sourceRoot":"","sources":["../../../../src/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.ts"],"names":[],"mappings":"AAKA,mEAAmE;AACnE,KAAK,UAAU,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;AAE3C;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,GAAI,mBAAmB,OAAO,EAAE,UAAU,MAAM;;;CAuCpF,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { hostGuard } from '../utilities/unsavedChangesGuard.utils.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Registers with the host's navigation bridge while there are unsaved changes, and keeps the answer the host is waiting on.
|
|
6
|
+
* A second ask replaces the first, which is told to stay so the host is never left waiting.
|
|
7
|
+
*
|
|
8
|
+
* @param hasUnsavedChanges Whether any form beneath the provider is still dirty. The bridge is registered only while it is.
|
|
9
|
+
* @param basePath The app's router base path, handed to the host with the registration.
|
|
10
|
+
* @returns The host's current ask, and a way to drop it once the provider has answered.
|
|
11
|
+
*/
|
|
12
|
+
const useHostGuardRegistration = (hasUnsavedChanges, basePath) => {
|
|
13
|
+
const [answerHost, setAnswerHost] = useState(null);
|
|
14
|
+
const clearAnswerHost = useCallback(() => setAnswerHost(null), []);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const guard = hasUnsavedChanges ? hostGuard() : undefined;
|
|
17
|
+
if (!guard)
|
|
18
|
+
return;
|
|
19
|
+
let pending = null;
|
|
20
|
+
let isRegistered = true;
|
|
21
|
+
const canLeave = () => new Promise((resolve) => {
|
|
22
|
+
// A host holding a registration that has since gone has nothing left to wait for.
|
|
23
|
+
if (!isRegistered)
|
|
24
|
+
return resolve(true);
|
|
25
|
+
// A second request replaces the first, which is answered "stay" so the host is never left waiting.
|
|
26
|
+
pending?.(false);
|
|
27
|
+
pending = resolve;
|
|
28
|
+
setAnswerHost(() => resolve);
|
|
29
|
+
});
|
|
30
|
+
let unregister;
|
|
31
|
+
try {
|
|
32
|
+
unregister = guard.register({ basePath, canLeave });
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// A bridge that throws is treated as no bridge: this runs outside the app's error boundary, so it would take the page down.
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
return () => {
|
|
39
|
+
isRegistered = false;
|
|
40
|
+
try {
|
|
41
|
+
unregister();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Ignored, as a `register` that throws is: this cleanup runs outside the app's error boundary too.
|
|
45
|
+
}
|
|
46
|
+
// Nothing is left to lose once the changes are saved or the form is gone, so a waiting host may go.
|
|
47
|
+
pending?.(true);
|
|
48
|
+
setAnswerHost(null);
|
|
49
|
+
};
|
|
50
|
+
}, [hasUnsavedChanges, basePath]);
|
|
51
|
+
return { answerHost, clearAnswerHost };
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export { useHostGuardRegistration };
|
|
55
|
+
//# sourceMappingURL=useHostGuardRegistration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHostGuardRegistration.js","sources":["../../../../src/providers/UnsavedChangesGuardProvider/hooks/useHostGuardRegistration.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAQA;;;;;;;AAOG;MACU,wBAAwB,GAAG,CAAC,iBAA0B,EAAE,QAAgB,KAAI;IACvF,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAoB,IAAI,CAAC;AACrE,IAAA,MAAM,eAAe,GAAG,WAAW,CAAC,MAAM,aAAa,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;IAElE,SAAS,CAAC,MAAK;AACb,QAAA,MAAM,KAAK,GAAG,iBAAiB,GAAG,SAAS,EAAE,GAAG,SAAS;AACzD,QAAA,IAAI,CAAC,KAAK;YAAE;QACZ,IAAI,OAAO,GAAsB,IAAI;QACrC,IAAI,YAAY,GAAG,IAAI;QACvB,MAAM,QAAQ,GAAG,MACf,IAAI,OAAO,CAAU,CAAC,OAAO,KAAI;;AAE/B,YAAA,IAAI,CAAC,YAAY;AAAE,gBAAA,OAAO,OAAO,CAAC,IAAI,CAAC;;AAEvC,YAAA,OAAO,GAAG,KAAK,CAAC;YAChB,OAAO,GAAG,OAAO;AACjB,YAAA,aAAa,CAAC,MAAM,OAAO,CAAC;AAC9B,QAAA,CAAC,CAAC;AACJ,QAAA,IAAI,UAAsB;AAC1B,QAAA,IAAI;YACF,UAAU,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;QACrD;AAAE,QAAA,MAAM;;YAEN;QACF;AACA,QAAA,OAAO,MAAK;YACV,YAAY,GAAG,KAAK;AACpB,YAAA,IAAI;AACF,gBAAA,UAAU,EAAE;YACd;AAAE,YAAA,MAAM;;YAER;;AAEA,YAAA,OAAO,GAAG,IAAI,CAAC;YACf,aAAa,CAAC,IAAI,CAAC;AACrB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAC;AAEjC,IAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE;AACxC;;"}
|
|
@@ -4,7 +4,8 @@ var jsxRuntime = require('react/jsx-runtime');
|
|
|
4
4
|
var React = require('react');
|
|
5
5
|
var reactRouter = require('react-router');
|
|
6
6
|
var UnsavedChangesGuard_context = require('../../hooks/useUnsavedChangesGuard/providers/UnsavedChangesGuard.context.cjs');
|
|
7
|
-
var
|
|
7
|
+
var useHostApproval = require('./hooks/useHostApproval.cjs');
|
|
8
|
+
var useHostGuardRegistration = require('./hooks/useHostGuardRegistration.cjs');
|
|
8
9
|
var unsavedChangesGuard_utils = require('./utilities/unsavedChangesGuard.utils.cjs');
|
|
9
10
|
|
|
10
11
|
/** Registers the router's blocker. Rendered only while it is needed: react-router warns on every host navigation while one exists. */
|
|
@@ -39,20 +40,7 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
39
40
|
const [blocker, setBlocker] = React.useState(null);
|
|
40
41
|
// The blocker last answered. The copy in state can outlive the navigation it held, and react-router throws on a second answer.
|
|
41
42
|
const settledBlocker = React.useRef(null);
|
|
42
|
-
const
|
|
43
|
-
// Set when the user lets a host navigate, so the navigation they just approved is not held or prompted a second time.
|
|
44
|
-
const isHostApproved = React.useRef(false);
|
|
45
|
-
const hostApprovalTimer = React.useRef(undefined);
|
|
46
|
-
const endHostApproval = React.useCallback(() => {
|
|
47
|
-
isHostApproved.current = false;
|
|
48
|
-
clearTimeout(hostApprovalTimer.current);
|
|
49
|
-
}, []);
|
|
50
|
-
/** Whether the host's approved navigation is the one happening now; the approval covers one navigation only. */
|
|
51
|
-
const takeHostApproval = React.useCallback(() => {
|
|
52
|
-
const isApproved = isHostApproved.current;
|
|
53
|
-
endHostApproval();
|
|
54
|
-
return isApproved;
|
|
55
|
-
}, [endHostApproval]);
|
|
43
|
+
const { takeHostApproval, noteHostAnswer } = useHostApproval.useHostApproval(pathname, dirtyIds);
|
|
56
44
|
// A change of search params or hash keeps the page, and the form on it, so only a change of path is held.
|
|
57
45
|
const shouldBlock = React.useCallback(({ currentLocation, nextLocation, historyAction }) => {
|
|
58
46
|
if (dirtyCount.current === 0 || currentLocation.pathname === nextLocation.pathname)
|
|
@@ -94,8 +82,6 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
94
82
|
window.addEventListener('popstate', noteHostMove, true);
|
|
95
83
|
return () => window.removeEventListener('popstate', noteHostMove, true);
|
|
96
84
|
}, []);
|
|
97
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: a new path or a new set of unsaved forms ends the host's approval
|
|
98
|
-
React.useEffect(() => endHostApproval, [pathname, dirtyIds, endHostApproval]);
|
|
99
85
|
React.useEffect(() => {
|
|
100
86
|
if (!hasUnsavedChanges)
|
|
101
87
|
return;
|
|
@@ -109,42 +95,7 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
109
95
|
window.addEventListener('beforeunload', warn);
|
|
110
96
|
return () => window.removeEventListener('beforeunload', warn);
|
|
111
97
|
}, [hasUnsavedChanges, takeHostApproval]);
|
|
112
|
-
|
|
113
|
-
const guard = hasUnsavedChanges ? unsavedChangesGuard_utils.hostGuard() : undefined;
|
|
114
|
-
if (!guard)
|
|
115
|
-
return;
|
|
116
|
-
let pending = null;
|
|
117
|
-
let isRegistered = true;
|
|
118
|
-
const canLeave = () => new Promise((resolve) => {
|
|
119
|
-
// A host holding a registration that has since gone has nothing left to wait for.
|
|
120
|
-
if (!isRegistered)
|
|
121
|
-
return resolve(true);
|
|
122
|
-
// A second request replaces the first, which is answered "stay" so the host is never left waiting.
|
|
123
|
-
pending?.(false);
|
|
124
|
-
pending = resolve;
|
|
125
|
-
setAnswerHost(() => resolve);
|
|
126
|
-
});
|
|
127
|
-
let unregister;
|
|
128
|
-
try {
|
|
129
|
-
unregister = guard.register({ basePath, canLeave });
|
|
130
|
-
}
|
|
131
|
-
catch {
|
|
132
|
-
// A bridge that throws is treated as no bridge: this runs outside the app's error boundary, so it would take the page down.
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
return () => {
|
|
136
|
-
isRegistered = false;
|
|
137
|
-
try {
|
|
138
|
-
unregister();
|
|
139
|
-
}
|
|
140
|
-
catch {
|
|
141
|
-
// Ignored, as a `register` that throws is: this cleanup runs outside the app's error boundary too.
|
|
142
|
-
}
|
|
143
|
-
// Nothing is left to lose once the changes are saved or the form is gone, so a waiting host may go.
|
|
144
|
-
pending?.(true);
|
|
145
|
-
setAnswerHost(null);
|
|
146
|
-
};
|
|
147
|
-
}, [hasUnsavedChanges, basePath]);
|
|
98
|
+
const { answerHost, clearAnswerHost } = useHostGuardRegistration.useHostGuardRegistration(hasUnsavedChanges, basePath);
|
|
148
99
|
/** Answers a held navigation, once: lets it go ahead, or puts the user back where they were. */
|
|
149
100
|
const settle = React.useCallback((held, leave) => {
|
|
150
101
|
if (held?.state !== 'blocked' || settledBlocker.current === held)
|
|
@@ -171,16 +122,11 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
171
122
|
const answer = React.useCallback((leave) => {
|
|
172
123
|
settle(blocker, leave);
|
|
173
124
|
if (answerHost) {
|
|
174
|
-
|
|
175
|
-
if (leave) {
|
|
176
|
-
isHostApproved.current = true;
|
|
177
|
-
// A host that then does not navigate (another app said stay) must not leave the form unguarded.
|
|
178
|
-
hostApprovalTimer.current = setTimeout(endHostApproval, unsavedChangesGuard_constants.HOST_APPROVAL_MS);
|
|
179
|
-
}
|
|
125
|
+
noteHostAnswer(leave);
|
|
180
126
|
answerHost(leave);
|
|
181
|
-
|
|
127
|
+
clearAnswerHost();
|
|
182
128
|
}
|
|
183
|
-
}, [blocker, answerHost,
|
|
129
|
+
}, [blocker, answerHost, noteHostAnswer, clearAnswerHost, settle]);
|
|
184
130
|
const value = React.useMemo(() => ({ register, promptOwner: dirtyIds[0], isPromptOpen: blocker?.state === 'blocked' || answerHost !== null, answer }), [register, dirtyIds, blocker?.state, answerHost, answer]);
|
|
185
131
|
// Kept until a held navigation has finished, so one let go by a save is not dropped with the blocker.
|
|
186
132
|
const isBlockerNeeded = hasUnsavedChanges || (blocker !== null && blocker.state !== 'unblocked');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../src/providers/UnsavedChangesGuardProvider/index.tsx"],"sourcesContent":[null],"names":["useBlocker","useEffect","useContext","UNSAFE_DataRouterContext","useState","useRef","useHref","useLocation","useCallback","browserEntry","isInside","hasLeftFrom","canRewind","historyPosition","historyIndex","watchLeftPosition","
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../src/providers/UnsavedChangesGuardProvider/index.tsx"],"sourcesContent":[null],"names":["useBlocker","useEffect","useContext","UNSAFE_DataRouterContext","useState","useRef","useHref","useLocation","useHostApproval","useCallback","browserEntry","isInside","hasLeftFrom","canRewind","historyPosition","historyIndex","watchLeftPosition","useHostGuardRegistration","useMemo","_jsxs","UnsavedChangesGuardContext","_jsx"],"mappings":";;;;;;;;;;AA0BA;AACA,MAAM,cAAc,GAAG,CAAC,EAAE,WAAW,EAAE,QAAQ,EAAuB,KAAI;AACxE,IAAA,MAAM,OAAO,GAAGA,sBAAU,CAAC,WAAW,CAAC;AACvC,IAAAC,eAAS,CAAC,MAAM,QAAQ,CAAC,OAAO,CAAC,EAAE,CA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AAG,iBAAiB,KAAK,OAAO,KAAK,IAAI,IAAI,OAAO,CAAC,KAAK,KAAK,WAAW,CAAC;IAEhG,QACEC,eAAA,CAACC,sDAA0B,EAAA,EAAC,KAAK,EAAE,KAAK,EAAA,QAAA,EAAA,CACrC,eAAe,IAAIC,cAAA,CAAC,cAAc,IAAC,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAA,CAAI,EACrF,QAAQ,CAAA,EAAA,CACkB;AAEjC;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/UnsavedChangesGuardProvider/index.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,iBAAiB,EAAiE,MAAM,OAAO,CAAC;AAiB9G,YAAY,EAAE,iBAAiB,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/UnsavedChangesGuardProvider/index.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,iBAAiB,EAAiE,MAAM,OAAO,CAAC;AAiB9G,YAAY,EAAE,iBAAiB,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAepG;;;;;GAKG;AACH,eAAO,MAAM,2BAA2B,GAAI,cAAc,iBAAiB,gCAkI1E,CAAC"}
|
|
@@ -2,8 +2,9 @@ import { jsxs, jsx } from 'react/jsx-runtime';
|
|
|
2
2
|
import { useContext, useState, useRef, useCallback, useEffect, useMemo } from 'react';
|
|
3
3
|
import { UNSAFE_DataRouterContext, useHref, useLocation, useBlocker } from 'react-router';
|
|
4
4
|
import { UnsavedChangesGuardContext } from '../../hooks/useUnsavedChangesGuard/providers/UnsavedChangesGuard.context.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { useHostApproval } from './hooks/useHostApproval.js';
|
|
6
|
+
import { useHostGuardRegistration } from './hooks/useHostGuardRegistration.js';
|
|
7
|
+
import { browserEntry, isInside, hasLeftFrom, canRewind, historyPosition, historyIndex, watchLeftPosition } from './utilities/unsavedChangesGuard.utils.js';
|
|
7
8
|
|
|
8
9
|
/** Registers the router's blocker. Rendered only while it is needed: react-router warns on every host navigation while one exists. */
|
|
9
10
|
const NavigationHold = ({ shouldBlock, onChange }) => {
|
|
@@ -37,20 +38,7 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
37
38
|
const [blocker, setBlocker] = useState(null);
|
|
38
39
|
// The blocker last answered. The copy in state can outlive the navigation it held, and react-router throws on a second answer.
|
|
39
40
|
const settledBlocker = useRef(null);
|
|
40
|
-
const
|
|
41
|
-
// Set when the user lets a host navigate, so the navigation they just approved is not held or prompted a second time.
|
|
42
|
-
const isHostApproved = useRef(false);
|
|
43
|
-
const hostApprovalTimer = useRef(undefined);
|
|
44
|
-
const endHostApproval = useCallback(() => {
|
|
45
|
-
isHostApproved.current = false;
|
|
46
|
-
clearTimeout(hostApprovalTimer.current);
|
|
47
|
-
}, []);
|
|
48
|
-
/** Whether the host's approved navigation is the one happening now; the approval covers one navigation only. */
|
|
49
|
-
const takeHostApproval = useCallback(() => {
|
|
50
|
-
const isApproved = isHostApproved.current;
|
|
51
|
-
endHostApproval();
|
|
52
|
-
return isApproved;
|
|
53
|
-
}, [endHostApproval]);
|
|
41
|
+
const { takeHostApproval, noteHostAnswer } = useHostApproval(pathname, dirtyIds);
|
|
54
42
|
// A change of search params or hash keeps the page, and the form on it, so only a change of path is held.
|
|
55
43
|
const shouldBlock = useCallback(({ currentLocation, nextLocation, historyAction }) => {
|
|
56
44
|
if (dirtyCount.current === 0 || currentLocation.pathname === nextLocation.pathname)
|
|
@@ -92,8 +80,6 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
92
80
|
window.addEventListener('popstate', noteHostMove, true);
|
|
93
81
|
return () => window.removeEventListener('popstate', noteHostMove, true);
|
|
94
82
|
}, []);
|
|
95
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: a new path or a new set of unsaved forms ends the host's approval
|
|
96
|
-
useEffect(() => endHostApproval, [pathname, dirtyIds, endHostApproval]);
|
|
97
83
|
useEffect(() => {
|
|
98
84
|
if (!hasUnsavedChanges)
|
|
99
85
|
return;
|
|
@@ -107,42 +93,7 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
107
93
|
window.addEventListener('beforeunload', warn);
|
|
108
94
|
return () => window.removeEventListener('beforeunload', warn);
|
|
109
95
|
}, [hasUnsavedChanges, takeHostApproval]);
|
|
110
|
-
|
|
111
|
-
const guard = hasUnsavedChanges ? hostGuard() : undefined;
|
|
112
|
-
if (!guard)
|
|
113
|
-
return;
|
|
114
|
-
let pending = null;
|
|
115
|
-
let isRegistered = true;
|
|
116
|
-
const canLeave = () => new Promise((resolve) => {
|
|
117
|
-
// A host holding a registration that has since gone has nothing left to wait for.
|
|
118
|
-
if (!isRegistered)
|
|
119
|
-
return resolve(true);
|
|
120
|
-
// A second request replaces the first, which is answered "stay" so the host is never left waiting.
|
|
121
|
-
pending?.(false);
|
|
122
|
-
pending = resolve;
|
|
123
|
-
setAnswerHost(() => resolve);
|
|
124
|
-
});
|
|
125
|
-
let unregister;
|
|
126
|
-
try {
|
|
127
|
-
unregister = guard.register({ basePath, canLeave });
|
|
128
|
-
}
|
|
129
|
-
catch {
|
|
130
|
-
// A bridge that throws is treated as no bridge: this runs outside the app's error boundary, so it would take the page down.
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
return () => {
|
|
134
|
-
isRegistered = false;
|
|
135
|
-
try {
|
|
136
|
-
unregister();
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
// Ignored, as a `register` that throws is: this cleanup runs outside the app's error boundary too.
|
|
140
|
-
}
|
|
141
|
-
// Nothing is left to lose once the changes are saved or the form is gone, so a waiting host may go.
|
|
142
|
-
pending?.(true);
|
|
143
|
-
setAnswerHost(null);
|
|
144
|
-
};
|
|
145
|
-
}, [hasUnsavedChanges, basePath]);
|
|
96
|
+
const { answerHost, clearAnswerHost } = useHostGuardRegistration(hasUnsavedChanges, basePath);
|
|
146
97
|
/** Answers a held navigation, once: lets it go ahead, or puts the user back where they were. */
|
|
147
98
|
const settle = useCallback((held, leave) => {
|
|
148
99
|
if (held?.state !== 'blocked' || settledBlocker.current === held)
|
|
@@ -169,16 +120,11 @@ const UnsavedChangesGuardProvider = ({ children }) => {
|
|
|
169
120
|
const answer = useCallback((leave) => {
|
|
170
121
|
settle(blocker, leave);
|
|
171
122
|
if (answerHost) {
|
|
172
|
-
|
|
173
|
-
if (leave) {
|
|
174
|
-
isHostApproved.current = true;
|
|
175
|
-
// A host that then does not navigate (another app said stay) must not leave the form unguarded.
|
|
176
|
-
hostApprovalTimer.current = setTimeout(endHostApproval, HOST_APPROVAL_MS);
|
|
177
|
-
}
|
|
123
|
+
noteHostAnswer(leave);
|
|
178
124
|
answerHost(leave);
|
|
179
|
-
|
|
125
|
+
clearAnswerHost();
|
|
180
126
|
}
|
|
181
|
-
}, [blocker, answerHost,
|
|
127
|
+
}, [blocker, answerHost, noteHostAnswer, clearAnswerHost, settle]);
|
|
182
128
|
const value = useMemo(() => ({ register, promptOwner: dirtyIds[0], isPromptOpen: blocker?.state === 'blocked' || answerHost !== null, answer }), [register, dirtyIds, blocker?.state, answerHost, answer]);
|
|
183
129
|
// Kept until a held navigation has finished, so one let go by a save is not dropped with the blocker.
|
|
184
130
|
const isBlockerNeeded = hasUnsavedChanges || (blocker !== null && blocker.state !== 'unblocked');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/providers/UnsavedChangesGuardProvider/index.tsx"],"sourcesContent":[null],"names":["_jsxs","_jsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/providers/UnsavedChangesGuardProvider/index.tsx"],"sourcesContent":[null],"names":["_jsxs","_jsx"],"mappings":";;;;;;;;AA0BA;AACA,MAAM,cAAc,GAAG,CAAC,EAA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