@ttt-productions/upload-ui 0.7.17 → 0.7.18
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/index.d.ts.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/react/guard/guarded-link.d.ts +0 -27
- package/dist/react/guard/guarded-link.d.ts.map +1 -1
- package/dist/react/guard/guarded-link.js.map +1 -1
- package/dist/react/guard/local-upload-guard-provider.d.ts +0 -81
- package/dist/react/guard/local-upload-guard-provider.d.ts.map +1 -1
- package/dist/react/guard/local-upload-guard-provider.js +3 -60
- package/dist/react/guard/local-upload-guard-provider.js.map +1 -1
- package/dist/react/guard/use-guarded-navigation.d.ts +0 -7
- package/dist/react/guard/use-guarded-navigation.d.ts.map +1 -1
- package/dist/react/guard/use-guarded-navigation.js +0 -28
- package/dist/react/guard/use-guarded-navigation.js.map +1 -1
- package/dist/react/tray/in-flight-uploads-provider.d.ts +1 -126
- package/dist/react/tray/in-flight-uploads-provider.d.ts.map +1 -1
- package/dist/react/tray/in-flight-uploads-provider.js +10 -133
- package/dist/react/tray/in-flight-uploads-provider.js.map +1 -1
- package/dist/react/tray/use-clear-upload-activity.d.ts +0 -25
- package/dist/react/tray/use-clear-upload-activity.d.ts.map +1 -1
- package/dist/react/tray/use-clear-upload-activity.js +0 -18
- package/dist/react/tray/use-clear-upload-activity.js.map +1 -1
- package/dist/react/tray/use-mark-upload-activity-seen.d.ts +0 -4
- package/dist/react/tray/use-mark-upload-activity-seen.d.ts.map +1 -1
- package/dist/react/tray/use-mark-upload-activity-seen.js +0 -21
- package/dist/react/tray/use-mark-upload-activity-seen.js.map +1 -1
- package/dist/react/tray/use-upload-processing.d.ts +0 -10
- package/dist/react/tray/use-upload-processing.d.ts.map +1 -1
- package/dist/react/tray/use-upload-processing.js +0 -18
- package/dist/react/tray/use-upload-processing.js.map +1 -1
- package/dist/react/upload/deferred-upload-form-shell.d.ts +1 -19
- package/dist/react/upload/deferred-upload-form-shell.d.ts.map +1 -1
- package/dist/react/upload/deferred-upload-form-shell.js +0 -1
- package/dist/react/upload/deferred-upload-form-shell.js.map +1 -1
- package/dist/react/upload/use-guarded-upload.d.ts +0 -29
- package/dist/react/upload/use-guarded-upload.d.ts.map +1 -1
- package/dist/react/upload/use-guarded-upload.js +0 -13
- package/dist/react/upload/use-guarded-upload.js.map +1 -1
- package/dist/upload-display.d.ts +0 -14
- package/dist/upload-display.d.ts.map +1 -1
- package/dist/upload-display.js +2 -5
- package/dist/upload-display.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAC;AACpF,YAAY,EACV,iBAAiB,EACjB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["export { formatFileSize, createUploadDisplayFormatters } from './upload-display.js';\nexport type {\n UploadDisplayCopy,\n UploadDisplayConfig,\n UploadDisplayFormatters,\n} from './upload-display.js';\n"]}
|
|
@@ -1,33 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Render-prop link wrapper that guards navigation while an upload or unsaved
|
|
3
|
-
* work is registered. The consumer owns the actual navigator component (Next.js Link,
|
|
4
|
-
* React Router Link, plain anchor, etc.) — this wrapper only injects the
|
|
5
|
-
* onClick interceptor.
|
|
6
|
-
*
|
|
7
|
-
* Why render-prop and not `as={Link}`? Polymorphic `as` props create
|
|
8
|
-
* notoriously bad TypeScript inference and force the wrapper to forward all
|
|
9
|
-
* link-specific props. Render-prop keeps the consumer in full control of the
|
|
10
|
-
* link API, costs one extra wrapper closure, and keeps upload-ui agnostic of
|
|
11
|
-
* Next.js / React Router / etc.
|
|
12
|
-
*
|
|
13
|
-
* Design note: the design spec lists `<GuardedNavigationButton>` as a second
|
|
14
|
-
* component. We deliberately don't ship it — buttons that navigate use
|
|
15
|
-
* `useGuardedNavigation()` directly in one line. A second component would
|
|
16
|
-
* split the same surface for no payoff.
|
|
17
|
-
*
|
|
18
|
-
* Usage:
|
|
19
|
-
*
|
|
20
|
-
* <GuardedLink renderLink={({ onClick, children }) => (
|
|
21
|
-
* <Link href="/your/destination" onClick={onClick}>{children}</Link>
|
|
22
|
-
* )}>
|
|
23
|
-
* View all uploads →
|
|
24
|
-
* </GuardedLink>
|
|
25
|
-
*/
|
|
26
1
|
import type { MouseEvent, ReactNode } from 'react';
|
|
27
2
|
export interface GuardedLinkRenderArgs {
|
|
28
|
-
/** Click handler the consumer MUST attach to its underlying link element. */
|
|
29
3
|
onClick: (event: MouseEvent<HTMLElement>) => void;
|
|
30
|
-
/** The children passed to `<GuardedLink>` — forward them to the link. */
|
|
31
4
|
children: ReactNode;
|
|
32
5
|
}
|
|
33
6
|
export interface GuardedLinkProps {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guarded-link.d.ts","sourceRoot":"","sources":["../../../src/react/guard/guarded-link.tsx"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"guarded-link.d.ts","sourceRoot":"","sources":["../../../src/react/guard/guarded-link.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGnD,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,KAAK,IAAI,CAAC;IAClD,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,SAAS,CAAC;IACpB,UAAU,EAAE,CAAC,IAAI,EAAE,qBAAqB,KAAK,SAAS,CAAC;CACxD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,+BAWlD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guarded-link.js","sourceRoot":"","sources":["../../../src/react/guard/guarded-link.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;
|
|
1
|
+
{"version":3,"file":"guarded-link.js","sourceRoot":"","sources":["../../../src/react/guard/guarded-link.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAGb,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAYvE,MAAM,UAAU,WAAW,CAAC,KAAuB;IACjD,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IACvC,MAAM,EAAE,iBAAiB,EAAE,GAAG,mBAAmB,EAAE,CAAC;IAEpD,MAAM,OAAO,GAAG,CAAC,KAA8B,EAAE,EAAE;QACjD,IAAI,CAAC,iBAAiB,EAAE,EAAE,CAAC;YACzB,KAAK,CAAC,cAAc,EAAE,CAAC;QACzB,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,4BAAG,UAAU,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,GAAI,CAAC;AAClD,CAAC","sourcesContent":["'use client';\n\nimport type { MouseEvent, ReactNode } from 'react';\nimport { useLocalUploadGuard } from './local-upload-guard-provider.js';\n\nexport interface GuardedLinkRenderArgs {\n onClick: (event: MouseEvent<HTMLElement>) => void;\n children: ReactNode;\n}\n\nexport interface GuardedLinkProps {\n children: ReactNode;\n renderLink: (args: GuardedLinkRenderArgs) => ReactNode;\n}\n\nexport function GuardedLink(props: GuardedLinkProps) {\n const { children, renderLink } = props;\n const { confirmNavigation } = useLocalUploadGuard();\n\n const onClick = (event: MouseEvent<HTMLElement>) => {\n if (!confirmNavigation()) {\n event.preventDefault();\n }\n };\n\n return <>{renderLink({ onClick, children })}</>;\n}\n"]}
|
|
@@ -1,110 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The navigation and leave guard: warns before the user leaves while work that leaving would lose
|
|
3
|
-
* is registered — a LOCAL byte upload in flight, or an editor's unsaved input.
|
|
4
|
-
*
|
|
5
|
-
* Uploads: phases `preparing` and `uploading` only. `finalizing` and backend `pendingMedia`
|
|
6
|
-
* processing are safe to leave (the bytes are already in Storage), so they are not registered.
|
|
7
|
-
* Unsaved work: an editor registers while it holds input its user has not saved, and unregisters
|
|
8
|
-
* when the input is saved, discarded, or the editor unmounts.
|
|
9
|
-
*
|
|
10
|
-
* While anything is registered the provider installs ONE `beforeunload` listener (tab close,
|
|
11
|
-
* reload, full-page navigation), and `confirmNavigation()` asks before an in-app navigation. It is
|
|
12
|
-
* the one leave guard: an editor never installs its own `beforeunload` handler (FRONTEND-207).
|
|
13
|
-
*
|
|
14
|
-
* Consumers register with a stable id per piece of work; registering a registered id, or
|
|
15
|
-
* unregistering an absent one, is a no-op. Uploads and unsaved work are counted separately, so the
|
|
16
|
-
* same id may be both.
|
|
17
|
-
*/
|
|
18
1
|
import { type ReactNode } from 'react';
|
|
19
2
|
export interface ConfirmNavigationOptions {
|
|
20
|
-
/**
|
|
21
|
-
* The caller leaves through a FULL-PAGE navigation (`location.assign` /
|
|
22
|
-
* `location.replace`) once this returns true. The provider then lets that
|
|
23
|
-
* navigation's next `beforeunload` pass without the browser's own
|
|
24
|
-
* "Leave site?" prompt, because the user has just answered this one. Soft
|
|
25
|
-
* (in-app router) navigations never pass it: they fire no `beforeunload`.
|
|
26
|
-
*/
|
|
27
3
|
fullPageNavigation?: boolean;
|
|
28
4
|
}
|
|
29
5
|
interface LocalUploadGuardContextValue {
|
|
30
|
-
/** Registered local uploads. */
|
|
31
6
|
activeUploadCount: number;
|
|
32
|
-
/** Registered pieces of unsaved work. */
|
|
33
7
|
unsavedWorkCount: number;
|
|
34
|
-
/** Everything the guard warns about: uploads plus unsaved work. Zero means leaving asks nothing. */
|
|
35
8
|
guardedWorkCount: number;
|
|
36
9
|
registerUpload: (id: string) => void;
|
|
37
10
|
unregisterUpload: (id: string) => void;
|
|
38
11
|
registerUnsavedWork: (id: string) => void;
|
|
39
12
|
unregisterUnsavedWork: (id: string) => void;
|
|
40
|
-
/** Returns true iff an upload or unsaved work is currently registered. Pure read. */
|
|
41
13
|
shouldConfirmNavigation: () => boolean;
|
|
42
|
-
/**
|
|
43
|
-
* Shows a confirm() dialog with the message for what is registered and returns the user's
|
|
44
|
-
* choice. Safe to call when nothing is registered — returns true without prompting.
|
|
45
|
-
*
|
|
46
|
-
* With `{ fullPageNavigation: true }` and a result of true (confirmed, or nothing registered),
|
|
47
|
-
* arms a ONE-SHOT bypass: the next `beforeunload` is not prevented, so a confirmed full-page
|
|
48
|
-
* navigation does not prompt twice. A declined confirm never arms it; the bypass is cleared when
|
|
49
|
-
* it is consumed, when new work (an upload or unsaved work) registers, and on `pageshow` (a
|
|
50
|
-
* back/forward-cache return).
|
|
51
|
-
*/
|
|
52
14
|
confirmNavigation: (options?: ConfirmNavigationOptions) => boolean;
|
|
53
15
|
}
|
|
54
16
|
export interface LocalUploadGuardProviderProps {
|
|
55
|
-
/**
|
|
56
|
-
* Message the browser is given when the user closes the tab, reloads, or leaves the page while
|
|
57
|
-
* work is registered. Modern browsers show their own generic dialog and ignore it; when omitted,
|
|
58
|
-
* the default for what is registered is used.
|
|
59
|
-
*/
|
|
60
17
|
beforeUnloadMessage?: string;
|
|
61
|
-
/** `confirmNavigation()`'s message while an upload is in progress (and no unsaved work). */
|
|
62
18
|
navigationConfirmMessage?: string;
|
|
63
|
-
/** `confirmNavigation()`'s message while unsaved work is registered (and no upload). */
|
|
64
19
|
unsavedWorkConfirmMessage?: string;
|
|
65
|
-
/** `confirmNavigation()`'s message while both an upload and unsaved work are registered. */
|
|
66
20
|
uploadAndUnsavedWorkConfirmMessage?: string;
|
|
67
21
|
children: ReactNode;
|
|
68
22
|
}
|
|
69
23
|
export declare function LocalUploadGuardProvider(props: LocalUploadGuardProviderProps): import("react").JSX.Element;
|
|
70
|
-
/**
|
|
71
|
-
* Low-level accessor for the guard context. Throws if used outside a
|
|
72
|
-
* `LocalUploadGuardProvider`. Most consumers should use `useLocalUploadGuard`
|
|
73
|
-
* (the public re-export below) instead.
|
|
74
|
-
*/
|
|
75
24
|
export declare function useLocalUploadGuardContext(): LocalUploadGuardContextValue;
|
|
76
|
-
/**
|
|
77
|
-
* Optional accessor: returns the guard context, or `null` when rendered
|
|
78
|
-
* outside a `LocalUploadGuardProvider`. For package components (e.g. the
|
|
79
|
-
* chat composer registering an in-flight send) that must degrade gracefully
|
|
80
|
-
* in consumers that don't mount the provider, instead of throwing.
|
|
81
|
-
*/
|
|
82
25
|
export declare function useOptionalLocalUploadGuard(): LocalUploadGuardContextValue | null;
|
|
83
|
-
/**
|
|
84
|
-
* Public hook for the navigation and leave guard.
|
|
85
|
-
*
|
|
86
|
-
* Typical usage in an upload flow:
|
|
87
|
-
*
|
|
88
|
-
* const { registerUpload, unregisterUpload } = useLocalUploadGuard();
|
|
89
|
-
* useEffect(() => {
|
|
90
|
-
* if (phase === 'preparing' || phase === 'uploading') {
|
|
91
|
-
* registerUpload(uploadId);
|
|
92
|
-
* return () => unregisterUpload(uploadId);
|
|
93
|
-
* }
|
|
94
|
-
* }, [phase, uploadId, registerUpload, unregisterUpload]);
|
|
95
|
-
*
|
|
96
|
-
* An editor holding input uses `useUnsavedWorkGuard(hasUnsavedWork)` instead.
|
|
97
|
-
*/
|
|
98
26
|
export declare function useLocalUploadGuard(): LocalUploadGuardContextValue;
|
|
99
|
-
/**
|
|
100
|
-
* Registers the calling editor's unsaved input with the leave guard while `hasUnsavedWork` is
|
|
101
|
-
* true, and unregisters it when that turns false or the editor unmounts. While registered, closing
|
|
102
|
-
* the tab, reloading, a full-page navigation, and every guarded in-app navigation ask first.
|
|
103
|
-
* Throws outside a `LocalUploadGuardProvider`.
|
|
104
|
-
*
|
|
105
|
-
* Returns `release`, which unregisters at once: an editor that saves and then navigates in the
|
|
106
|
-
* same handler calls it after the save, because its dirty flag clears only on the next render.
|
|
107
|
-
*/
|
|
108
27
|
export declare function useUnsavedWorkGuard(hasUnsavedWork: boolean): {
|
|
109
28
|
release: () => void;
|
|
110
29
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-upload-guard-provider.d.ts","sourceRoot":"","sources":["../../../src/react/guard/local-upload-guard-provider.tsx"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"local-upload-guard-provider.d.ts","sourceRoot":"","sources":["../../../src/react/guard/local-upload-guard-provider.tsx"],"names":[],"mappings":"AAEA,OAAO,EASL,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AAEf,MAAM,WAAW,wBAAwB;IACvC,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED,UAAU,4BAA4B;IACpC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,gBAAgB,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IACrC,gBAAgB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC,mBAAmB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1C,qBAAqB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5C,uBAAuB,EAAE,MAAM,OAAO,CAAC;IACvC,iBAAiB,EAAE,CAAC,OAAO,CAAC,EAAE,wBAAwB,KAAK,OAAO,CAAC;CACpE;AAID,MAAM,WAAW,6BAA6B;IAC5C,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,yBAAyB,CAAC,EAAE,MAAM,CAAC;IACnC,kCAAkC,CAAC,EAAE,MAAM,CAAC;IAC5C,QAAQ,EAAE,SAAS,CAAC;CACrB;AAoCD,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,6BAA6B,+BA6G5E;AAED,wBAAgB,0BAA0B,IAAI,4BAA4B,CAMzE;AAED,wBAAgB,2BAA2B,IAAI,4BAA4B,GAAG,IAAI,CAEjF;AAED,wBAAgB,mBAAmB,IAAI,4BAA4B,CAElE;AAED,wBAAgB,mBAAmB,CAAC,cAAc,EAAE,OAAO,GAAG;IAAE,OAAO,EAAE,MAAM,IAAI,CAAA;CAAE,CAUpF"}
|
|
@@ -1,22 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
/**
|
|
4
|
-
* The navigation and leave guard: warns before the user leaves while work that leaving would lose
|
|
5
|
-
* is registered — a LOCAL byte upload in flight, or an editor's unsaved input.
|
|
6
|
-
*
|
|
7
|
-
* Uploads: phases `preparing` and `uploading` only. `finalizing` and backend `pendingMedia`
|
|
8
|
-
* processing are safe to leave (the bytes are already in Storage), so they are not registered.
|
|
9
|
-
* Unsaved work: an editor registers while it holds input its user has not saved, and unregisters
|
|
10
|
-
* when the input is saved, discarded, or the editor unmounts.
|
|
11
|
-
*
|
|
12
|
-
* While anything is registered the provider installs ONE `beforeunload` listener (tab close,
|
|
13
|
-
* reload, full-page navigation), and `confirmNavigation()` asks before an in-app navigation. It is
|
|
14
|
-
* the one leave guard: an editor never installs its own `beforeunload` handler (FRONTEND-207).
|
|
15
|
-
*
|
|
16
|
-
* Consumers register with a stable id per piece of work; registering a registered id, or
|
|
17
|
-
* unregistering an absent one, is a no-op. Uploads and unsaved work are counted separately, so the
|
|
18
|
-
* same id may be both.
|
|
19
|
-
*/
|
|
20
3
|
import { createContext, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, } from 'react';
|
|
21
4
|
const LocalUploadGuardContext = createContext(undefined);
|
|
22
5
|
const DEFAULT_NAVIGATION_CONFIRM_MESSAGE = 'An upload is currently in progress. If you leave this page, the upload will be canceled. Continue?';
|
|
@@ -44,9 +27,7 @@ function useRegistry(onRegister) {
|
|
|
44
27
|
}
|
|
45
28
|
export function LocalUploadGuardProvider(props) {
|
|
46
29
|
const { children, beforeUnloadMessage, navigationConfirmMessage = DEFAULT_NAVIGATION_CONFIRM_MESSAGE, unsavedWorkConfirmMessage = DEFAULT_UNSAVED_WORK_CONFIRM_MESSAGE, uploadAndUnsavedWorkConfirmMessage = DEFAULT_UPLOAD_AND_UNSAVED_WORK_CONFIRM_MESSAGE, } = props;
|
|
47
|
-
// One-shot: the user already confirmed the full-page navigation now unloading.
|
|
48
30
|
const bypassNextUnloadRef = useRef(false);
|
|
49
|
-
// Also run when work registers: work that starts after a confirmation was not covered by it.
|
|
50
31
|
const clearBypass = useCallback(() => {
|
|
51
32
|
bypassNextUnloadRef.current = false;
|
|
52
33
|
}, []);
|
|
@@ -56,7 +37,6 @@ export function LocalUploadGuardProvider(props) {
|
|
|
56
37
|
const unsavedWorkIdsRef = unsavedWork.idsRef;
|
|
57
38
|
const guardedWorkCount = uploads.count + unsavedWork.count;
|
|
58
39
|
const hasGuardedWork = guardedWorkCount > 0;
|
|
59
|
-
// Read at the moment of asking, so a message always matches what is registered right then.
|
|
60
40
|
const confirmMessage = useCallback(() => {
|
|
61
41
|
const uploading = uploadIdsRef.current.size > 0;
|
|
62
42
|
const unsaved = unsavedWorkIdsRef.current.size > 0;
|
|
@@ -79,8 +59,7 @@ export function LocalUploadGuardProvider(props) {
|
|
|
79
59
|
return;
|
|
80
60
|
}
|
|
81
61
|
event.preventDefault();
|
|
82
|
-
// Modern browsers may ignore the custom string and show their own generic confirmation
|
|
83
|
-
// what matters is that leaving is warned, not the exact text.
|
|
62
|
+
// Modern browsers may ignore the custom string and show their own generic confirmation.
|
|
84
63
|
event.returnValue =
|
|
85
64
|
beforeUnloadMessage ??
|
|
86
65
|
(uploadIdsRef.current.size > 0
|
|
@@ -93,8 +72,8 @@ export function LocalUploadGuardProvider(props) {
|
|
|
93
72
|
window.removeEventListener('beforeunload', handleBeforeUnload);
|
|
94
73
|
};
|
|
95
74
|
}, [hasGuardedWork, beforeUnloadMessage, uploadIdsRef]);
|
|
96
|
-
// A back/forward-cache return re-shows this page without reloading it
|
|
97
|
-
//
|
|
75
|
+
// A back/forward-cache return re-shows this page without reloading it, so an armed bypass must not
|
|
76
|
+
// outlive the navigation that left.
|
|
98
77
|
useEffect(() => {
|
|
99
78
|
window.addEventListener('pageshow', clearBypass);
|
|
100
79
|
return () => {
|
|
@@ -103,7 +82,6 @@ export function LocalUploadGuardProvider(props) {
|
|
|
103
82
|
}, [clearBypass]);
|
|
104
83
|
const shouldConfirmNavigation = useCallback(() => uploadIdsRef.current.size > 0 || unsavedWorkIdsRef.current.size > 0, [uploadIdsRef, unsavedWorkIdsRef]);
|
|
105
84
|
const confirmNavigation = useCallback((options) => {
|
|
106
|
-
// SSR / non-browser safety: if `window` isn't available, default to allow.
|
|
107
85
|
if (typeof window === 'undefined')
|
|
108
86
|
return true;
|
|
109
87
|
const proceed = !shouldConfirmNavigation() || window.confirm(confirmMessage());
|
|
@@ -134,11 +112,6 @@ export function LocalUploadGuardProvider(props) {
|
|
|
134
112
|
]);
|
|
135
113
|
return (_jsx(LocalUploadGuardContext.Provider, { value: value, children: children }));
|
|
136
114
|
}
|
|
137
|
-
/**
|
|
138
|
-
* Low-level accessor for the guard context. Throws if used outside a
|
|
139
|
-
* `LocalUploadGuardProvider`. Most consumers should use `useLocalUploadGuard`
|
|
140
|
-
* (the public re-export below) instead.
|
|
141
|
-
*/
|
|
142
115
|
export function useLocalUploadGuardContext() {
|
|
143
116
|
const ctx = useContext(LocalUploadGuardContext);
|
|
144
117
|
if (ctx === undefined) {
|
|
@@ -146,42 +119,12 @@ export function useLocalUploadGuardContext() {
|
|
|
146
119
|
}
|
|
147
120
|
return ctx;
|
|
148
121
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Optional accessor: returns the guard context, or `null` when rendered
|
|
151
|
-
* outside a `LocalUploadGuardProvider`. For package components (e.g. the
|
|
152
|
-
* chat composer registering an in-flight send) that must degrade gracefully
|
|
153
|
-
* in consumers that don't mount the provider, instead of throwing.
|
|
154
|
-
*/
|
|
155
122
|
export function useOptionalLocalUploadGuard() {
|
|
156
123
|
return useContext(LocalUploadGuardContext) ?? null;
|
|
157
124
|
}
|
|
158
|
-
/**
|
|
159
|
-
* Public hook for the navigation and leave guard.
|
|
160
|
-
*
|
|
161
|
-
* Typical usage in an upload flow:
|
|
162
|
-
*
|
|
163
|
-
* const { registerUpload, unregisterUpload } = useLocalUploadGuard();
|
|
164
|
-
* useEffect(() => {
|
|
165
|
-
* if (phase === 'preparing' || phase === 'uploading') {
|
|
166
|
-
* registerUpload(uploadId);
|
|
167
|
-
* return () => unregisterUpload(uploadId);
|
|
168
|
-
* }
|
|
169
|
-
* }, [phase, uploadId, registerUpload, unregisterUpload]);
|
|
170
|
-
*
|
|
171
|
-
* An editor holding input uses `useUnsavedWorkGuard(hasUnsavedWork)` instead.
|
|
172
|
-
*/
|
|
173
125
|
export function useLocalUploadGuard() {
|
|
174
126
|
return useLocalUploadGuardContext();
|
|
175
127
|
}
|
|
176
|
-
/**
|
|
177
|
-
* Registers the calling editor's unsaved input with the leave guard while `hasUnsavedWork` is
|
|
178
|
-
* true, and unregisters it when that turns false or the editor unmounts. While registered, closing
|
|
179
|
-
* the tab, reloading, a full-page navigation, and every guarded in-app navigation ask first.
|
|
180
|
-
* Throws outside a `LocalUploadGuardProvider`.
|
|
181
|
-
*
|
|
182
|
-
* Returns `release`, which unregisters at once: an editor that saves and then navigates in the
|
|
183
|
-
* same handler calls it after the save, because its dirty flag clears only on the next render.
|
|
184
|
-
*/
|
|
185
128
|
export function useUnsavedWorkGuard(hasUnsavedWork) {
|
|
186
129
|
const { registerUnsavedWork, unregisterUnsavedWork } = useLocalUploadGuardContext();
|
|
187
130
|
const id = useId();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-upload-guard-provider.js","sourceRoot":"","sources":["../../../src/react/guard/local-upload-guard-provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EACL,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACT,KAAK,EACL,OAAO,EACP,MAAM,EACN,QAAQ,GAET,MAAM,OAAO,CAAC;AAuCf,MAAM,uBAAuB,GAAG,aAAa,CAA2C,SAAS,CAAC,CAAC;AAkBnG,MAAM,kCAAkC,GACtC,oGAAoG,CAAC;AACvG,MAAM,oCAAoC,GACxC,gFAAgF,CAAC;AACnF,MAAM,+CAA+C,GACnD,qJAAqJ,CAAC;AACxJ,MAAM,oCAAoC,GACxC,kFAAkF,CAAC;AACrF,MAAM,0CAA0C,GAC9C,yEAAyE,CAAC;AAE5E,SAAS,WAAW,CAAC,UAAsB;IACzC,MAAM,MAAM,GAAG,MAAM,CAAc,IAAI,GAAG,EAAE,CAAC,CAAC;IAC9C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEtC,MAAM,QAAQ,GAAG,WAAW,CAC1B,CAAC,EAAU,EAAE,EAAE;QACb,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,OAAO;QACnC,UAAU,EAAE,CAAC;QACb,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACvB,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC,EACD,CAAC,UAAU,CAAC,CACb,CAAC;IAEF,MAAM,UAAU,GAAG,WAAW,CAAC,CAAC,EAAU,EAAE,E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AAC,OAAO,CAAC,CAAC,CAAC;AACjD,CAAC","sourcesContent":["'use client';\n\n/**\n * The navigation and leave guard: warns before the user leaves while work that leaving would lose\n * is registered — a LOCAL byte upload in flight, or an editor's unsaved input.\n *\n * Uploads: phases `preparing` and `uploading` only. `finalizing` and backend `pendingMedia`\n * processing are safe to leave (the bytes are already in Storage), so they are not registered.\n * Unsaved work: an editor registers while it holds input its user has not saved, and unregisters\n * when the input is saved, discarded, or the editor unmounts.\n *\n * While anything is registered the provider installs ONE `beforeunload` listener (tab close,\n * reload, full-page navigation), and `confirmNavigation()` asks before an in-app navigation. It is\n * the one leave guard: an editor never installs its own `beforeunload` handler (FRONTEND-207).\n *\n * Consumers register with a stable id per piece of work; registering a registered id, or\n * unregistering an absent one, is a no-op. Uploads and unsaved work are counted separately, so the\n * same id may be both.\n */\n\nimport {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from 'react';\n\nexport interface ConfirmNavigationOptions {\n /**\n * The caller leaves through a FULL-PAGE navigation (`location.assign` /\n * `location.replace`) once this returns true. The provider then lets that\n * navigation's next `beforeunload` pass without the browser's own\n * \"Leave site?\" prompt, because the user has just answered this one. Soft\n * (in-app router) navigations never pass it: they fire no `beforeunload`.\n */\n fullPageNavigation?: boolean;\n}\n\ninterface LocalUploadGuardContextValue {\n /** Registered local uploads. */\n activeUploadCount: number;\n /** Registered pieces of unsaved work. */\n unsavedWorkCount: number;\n /** Everything the guard warns about: uploads plus unsaved work. Zero means leaving asks nothing. */\n guardedWorkCount: number;\n registerUpload: (id: string) => void;\n unregisterUpload: (id: string) => void;\n registerUnsavedWork: (id: string) => void;\n unregisterUnsavedWork: (id: string) => void;\n /** Returns true iff an upload or unsaved work is currently registered. Pure read. */\n shouldConfirmNavigation: () => boolean;\n /**\n * Shows a confirm() dialog with the message for what is registered and returns the user's\n * choice. Safe to call when nothing is registered — returns true without prompting.\n *\n * With `{ fullPageNavigation: true }` and a result of true (confirmed, or nothing registered),\n * arms a ONE-SHOT bypass: the next `beforeunload` is not prevented, so a confirmed full-page\n * navigation does not prompt twice. A declined confirm never arms it; the bypass is cleared when\n * it is consumed, when new work (an upload or unsaved work) registers, and on `pageshow` (a\n * back/forward-cache return).\n */\n confirmNavigation: (options?: ConfirmNavigationOptions) => boolean;\n}\n\nconst LocalUploadGuardContext = createContext<LocalUploadGuardContextValue | undefined>(undefined);\n\nexport interface LocalUploadGuardProviderProps {\n /**\n * Message the browser is given when the user closes the tab, reloads, or leaves the page while\n * work is registered. Modern browsers show their own generic dialog and ignore it; when omitted,\n * the default for what is registered is used.\n */\n beforeUnloadMessage?: string;\n /** `confirmNavigation()`'s message while an upload is in progress (and no unsaved work). */\n navigationConfirmMessage?: string;\n /** `confirmNavigation()`'s message while unsaved work is registered (and no upload). */\n unsavedWorkConfirmMessage?: string;\n /** `confirmNavigation()`'s message while both an upload and unsaved work are registered. */\n uploadAndUnsavedWorkConfirmMessage?: string;\n children: ReactNode;\n}\n\nconst DEFAULT_NAVIGATION_CONFIRM_MESSAGE =\n 'An upload is currently in progress. If you leave this page, the upload will be canceled. Continue?';\nconst DEFAULT_UNSAVED_WORK_CONFIRM_MESSAGE =\n 'You have unsaved changes. If you leave this page, they will be lost. Continue?';\nconst DEFAULT_UPLOAD_AND_UNSAVED_WORK_CONFIRM_MESSAGE =\n 'An upload is in progress and you have unsaved changes. If you leave this page, the upload will be canceled and your changes will be lost. Continue?';\nconst DEFAULT_UPLOAD_BEFORE_UNLOAD_MESSAGE =\n 'File upload in progress. Are you sure you want to leave and end the file upload?';\nconst DEFAULT_UNSAVED_WORK_BEFORE_UNLOAD_MESSAGE =\n 'You have unsaved changes. Are you sure you want to leave and lose them?';\n\nfunction useRegistry(onRegister: () => void) {\n const idsRef = useRef<Set<string>>(new Set());\n const [count, setCount] = useState(0);\n\n const register = useCallback(\n (id: string) => {\n if (idsRef.current.has(id)) return;\n onRegister();\n idsRef.current.add(id);\n setCount(idsRef.current.size);\n },\n [onRegister],\n );\n\n const unregister = useCallback((id: string) => {\n if (!idsRef.current.has(id)) return;\n idsRef.current.delete(id);\n setCount(idsRef.current.size);\n }, []);\n\n return { idsRef, count, register, unregister };\n}\n\nexport function LocalUploadGuardProvider(props: LocalUploadGuardProviderProps) {\n const {\n children,\n beforeUnloadMessage,\n navigationConfirmMessage = DEFAULT_NAVIGATION_CONFIRM_MESSAGE,\n unsavedWorkConfirmMessage = DEFAULT_UNSAVED_WORK_CONFIRM_MESSAGE,\n uploadAndUnsavedWorkConfirmMessage = DEFAULT_UPLOAD_AND_UNSAVED_WORK_CONFIRM_MESSAGE,\n } = props;\n // One-shot: the user already confirmed the full-page navigation now unloading.\n const bypassNextUnloadRef = useRef(false);\n // Also run when work registers: work that starts after a confirmation was not covered by it.\n const clearBypass = useCallback(() => {\n bypassNextUnloadRef.current = false;\n }, []);\n\n const uploads = useRegistry(clearBypass);\n const unsavedWork = useRegistry(clearBypass);\n const uploadIdsRef = uploads.idsRef;\n const unsavedWorkIdsRef = unsavedWork.idsRef;\n const guardedWorkCount = uploads.count + unsavedWork.count;\n const hasGuardedWork = guardedWorkCount > 0;\n\n // Read at the moment of asking, so a message always matches what is registered right then.\n const confirmMessage = useCallback((): string => {\n const uploading = uploadIdsRef.current.size > 0;\n const unsaved = unsavedWorkIdsRef.current.size > 0;\n if (uploading && unsaved) return uploadAndUnsavedWorkConfirmMessage;\n return uploading ? navigationConfirmMessage : unsavedWorkConfirmMessage;\n }, [\n uploadIdsRef,\n unsavedWorkIdsRef,\n navigationConfirmMessage,\n unsavedWorkConfirmMessage,\n uploadAndUnsavedWorkConfirmMessage,\n ]);\n\n useEffect(() => {\n if (!hasGuardedWork) return;\n\n const handleBeforeUnload = (event: BeforeUnloadEvent) => {\n if (bypassNextUnloadRef.current) {\n bypassNextUnloadRef.current = false;\n return;\n }\n event.preventDefault();\n // Modern browsers may ignore the custom string and show their own generic confirmation;\n // what matters is that leaving is warned, not the exact text.\n event.returnValue =\n beforeUnloadMessage ??\n (uploadIdsRef.current.size > 0\n ? DEFAULT_UPLOAD_BEFORE_UNLOAD_MESSAGE\n : DEFAULT_UNSAVED_WORK_BEFORE_UNLOAD_MESSAGE);\n return event.returnValue;\n };\n\n window.addEventListener('beforeunload', handleBeforeUnload);\n return () => {\n window.removeEventListener('beforeunload', handleBeforeUnload);\n };\n }, [hasGuardedWork, beforeUnloadMessage, uploadIdsRef]);\n\n // A back/forward-cache return re-shows this page without reloading it: an\n // armed bypass from the navigation that left must never outlive that navigation.\n useEffect(() => {\n window.addEventListener('pageshow', clearBypass);\n return () => {\n window.removeEventListener('pageshow', clearBypass);\n };\n }, [clearBypass]);\n\n const shouldConfirmNavigation = useCallback(\n () => uploadIdsRef.current.size > 0 || unsavedWorkIdsRef.current.size > 0,\n [uploadIdsRef, unsavedWorkIdsRef],\n );\n\n const confirmNavigation = useCallback(\n (options?: ConfirmNavigationOptions): boolean => {\n // SSR / non-browser safety: if `window` isn't available, default to allow.\n if (typeof window === 'undefined') return true;\n const proceed = !shouldConfirmNavigation() || window.confirm(confirmMessage());\n if (proceed && options?.fullPageNavigation) bypassNextUnloadRef.current = true;\n return proceed;\n },\n [shouldConfirmNavigation, confirmMessage],\n );\n\n const value = useMemo<LocalUploadGuardContextValue>(\n () => ({\n activeUploadCount: uploads.count,\n unsavedWorkCount: unsavedWork.count,\n guardedWorkCount,\n registerUpload: uploads.register,\n unregisterUpload: uploads.unregister,\n registerUnsavedWork: unsavedWork.register,\n unregisterUnsavedWork: unsavedWork.unregister,\n shouldConfirmNavigation,\n confirmNavigation,\n }),\n [\n uploads.count,\n uploads.register,\n uploads.unregister,\n unsavedWork.count,\n unsavedWork.register,\n unsavedWork.unregister,\n guardedWorkCount,\n shouldConfirmNavigation,\n confirmNavigation,\n ],\n );\n\n return (\n <LocalUploadGuardContext.Provider value={value}>{children}</LocalUploadGuardContext.Provider>\n );\n}\n\n/**\n * Low-level accessor for the guard context. Throws if used outside a\n * `LocalUploadGuardProvider`. Most consumers should use `useLocalUploadGuard`\n * (the public re-export below) instead.\n */\nexport function useLocalUploadGuardContext(): LocalUploadGuardContextValue {\n const ctx = useContext(LocalUploadGuardContext);\n if (ctx === undefined) {\n throw new Error('useLocalUploadGuard must be used within LocalUploadGuardProvider');\n }\n return ctx;\n}\n\n/**\n * Optional accessor: returns the guard context, or `null` when rendered\n * outside a `LocalUploadGuardProvider`. For package components (e.g. the\n * chat composer registering an in-flight send) that must degrade gracefully\n * in consumers that don't mount the provider, instead of throwing.\n */\nexport function useOptionalLocalUploadGuard(): LocalUploadGuardContextValue | null {\n return useContext(LocalUploadGuardContext) ?? null;\n}\n\n/**\n * Public hook for the navigation and leave guard.\n *\n * Typical usage in an upload flow:\n *\n * const { registerUpload, unregisterUpload } = useLocalUploadGuard();\n * useEffect(() => {\n * if (phase === 'preparing' || phase === 'uploading') {\n * registerUpload(uploadId);\n * return () => unregisterUpload(uploadId);\n * }\n * }, [phase, uploadId, registerUpload, unregisterUpload]);\n *\n * An editor holding input uses `useUnsavedWorkGuard(hasUnsavedWork)` instead.\n */\nexport function useLocalUploadGuard(): LocalUploadGuardContextValue {\n return useLocalUploadGuardContext();\n}\n\n/**\n * Registers the calling editor's unsaved input with the leave guard while `hasUnsavedWork` is\n * true, and unregisters it when that turns false or the editor unmounts. While registered, closing\n * the tab, reloading, a full-page navigation, and every guarded in-app navigation ask first.\n * Throws outside a `LocalUploadGuardProvider`.\n *\n * Returns `release`, which unregisters at once: an editor that saves and then navigates in the\n * same handler calls it after the save, because its dirty flag clears only on the next render.\n */\nexport function useUnsavedWorkGuard(hasUnsavedWork: boolean): { release: () => void } {\n const { registerUnsavedWork, unregisterUnsavedWork } = useLocalUploadGuardContext();\n const id = useId();\n useEffect(() => {\n if (!hasUnsavedWork) return;\n registerUnsavedWork(id);\n return () => unregisterUnsavedWork(id);\n }, [hasUnsavedWork, id, registerUnsavedWork, unregisterUnsavedWork]);\n const release = useCallback(() => unregisterUnsavedWork(id), [id, unregisterUnsavedWork]);\n return useMemo(() => ({ release }), [release]);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"local-upload-guard-provider.js","sourceRoot":"","sources":["../../../src/react/guard/local-upload-guard-provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EACL,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACT,KAAK,EACL,OAAO,EACP,MAAM,EACN,QAAQ,GAET,MAAM,OAAO,CAAC;AAkBf,MAAM,uBAAuB,GAAG,aAAa,CAA2C,SAAS,CAAC,CAAC;AAUnG,MAAM,kCAAkC,GACtC,oGAAoG,CAAC;AACvG,MAAM,oCAAoC,GACxC,gFAAgF,CAAC;AACnF,MAAM,+CAA+C,GACnD,qJAAqJ,CAAC;AACxJ,MAAM,oCAAoC,GACxC,kFAAkF,CAAC;AACrF,MAAM,0CAA0C,GAC9C,yEAAyE,CAAC;AAE5E,SAAS,WAAW,CAAC,UAAsB;IACzC,MAAM,MAAM,GAAG,MAAM,CAAc,IAAI,GAAG,EAAE,CAAC,CAAC;IAC9C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEtC,MAAM,QAAQ,GAAG,WAAW,CAC1B,CAAC,EAAU,EAAE,EAAE;QACb,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,OAAO;QACnC,UAAU,EAAE,CAAC;QACb,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACvB,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC,EACD,CAAC,UAAU,CAAC,CACb,CAAC;IAEF,MAAM,UAAU,GAAG,WAAW,CAAC,CAAC,EAAU,EAAE,EAAE;QAC5C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,OAAO;QACpC,MAAM,CAAC,OAAO,CAAC,MAAM,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UAAU,CAAC,uBAAuB,CAAC,CAAC;IAChD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;IACtF,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,UAAU,2BAA2B;IACzC,OAAO,UAAU,CAAC,uBAAuB,CAAC,IAAI,IAAI,CAAC;AACrD,CAAC;AAED,MAAM,UAAU,mBAAmB;IACjC,OAAO,0BAA0B,EAAE,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,cAAuB;IACzD,MAAM,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,GAAG,0BAA0B,EAAE,CAAC;IACpF,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC;IACnB,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,cAAc;YAAE,OAAO;QAC5B,mBAAmB,CAAC,EAAE,CAAC,CAAC;QACxB,OAAO,GAAG,EAAE,CAAC,qBAAqB,CAAC,EAAE,CAAC,CAAC;IACzC,CAAC,EAAE,CAAC,cAAc,EAAE,EAAE,EAAE,mBAAmB,EAAE,qBAAqB,CAAC,CAAC,CAAC;IACrE,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,qBAAqB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAC1F,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACjD,CAAC","sourcesContent":["'use client';\n\nimport {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from 'react';\n\nexport interface ConfirmNavigationOptions {\n fullPageNavigation?: boolean;\n}\n\ninterface LocalUploadGuardContextValue {\n activeUploadCount: number;\n unsavedWorkCount: number;\n guardedWorkCount: number;\n registerUpload: (id: string) => void;\n unregisterUpload: (id: string) => void;\n registerUnsavedWork: (id: string) => void;\n unregisterUnsavedWork: (id: string) => void;\n shouldConfirmNavigation: () => boolean;\n confirmNavigation: (options?: ConfirmNavigationOptions) => boolean;\n}\n\nconst LocalUploadGuardContext = createContext<LocalUploadGuardContextValue | undefined>(undefined);\n\nexport interface LocalUploadGuardProviderProps {\n beforeUnloadMessage?: string;\n navigationConfirmMessage?: string;\n unsavedWorkConfirmMessage?: string;\n uploadAndUnsavedWorkConfirmMessage?: string;\n children: ReactNode;\n}\n\nconst DEFAULT_NAVIGATION_CONFIRM_MESSAGE =\n 'An upload is currently in progress. If you leave this page, the upload will be canceled. Continue?';\nconst DEFAULT_UNSAVED_WORK_CONFIRM_MESSAGE =\n 'You have unsaved changes. If you leave this page, they will be lost. Continue?';\nconst DEFAULT_UPLOAD_AND_UNSAVED_WORK_CONFIRM_MESSAGE =\n 'An upload is in progress and you have unsaved changes. If you leave this page, the upload will be canceled and your changes will be lost. Continue?';\nconst DEFAULT_UPLOAD_BEFORE_UNLOAD_MESSAGE =\n 'File upload in progress. Are you sure you want to leave and end the file upload?';\nconst DEFAULT_UNSAVED_WORK_BEFORE_UNLOAD_MESSAGE =\n 'You have unsaved changes. Are you sure you want to leave and lose them?';\n\nfunction useRegistry(onRegister: () => void) {\n const idsRef = useRef<Set<string>>(new Set());\n const [count, setCount] = useState(0);\n\n const register = useCallback(\n (id: string) => {\n if (idsRef.current.has(id)) return;\n onRegister();\n idsRef.current.add(id);\n setCount(idsRef.current.size);\n },\n [onRegister],\n );\n\n const unregister = useCallback((id: string) => {\n if (!idsRef.current.has(id)) return;\n idsRef.current.delete(id);\n setCount(idsRef.current.size);\n }, []);\n\n return { idsRef, count, register, unregister };\n}\n\nexport function LocalUploadGuardProvider(props: LocalUploadGuardProviderProps) {\n const {\n children,\n beforeUnloadMessage,\n navigationConfirmMessage = DEFAULT_NAVIGATION_CONFIRM_MESSAGE,\n unsavedWorkConfirmMessage = DEFAULT_UNSAVED_WORK_CONFIRM_MESSAGE,\n uploadAndUnsavedWorkConfirmMessage = DEFAULT_UPLOAD_AND_UNSAVED_WORK_CONFIRM_MESSAGE,\n } = props;\n const bypassNextUnloadRef = useRef(false);\n const clearBypass = useCallback(() => {\n bypassNextUnloadRef.current = false;\n }, []);\n\n const uploads = useRegistry(clearBypass);\n const unsavedWork = useRegistry(clearBypass);\n const uploadIdsRef = uploads.idsRef;\n const unsavedWorkIdsRef = unsavedWork.idsRef;\n const guardedWorkCount = uploads.count + unsavedWork.count;\n const hasGuardedWork = guardedWorkCount > 0;\n\n const confirmMessage = useCallback((): string => {\n const uploading = uploadIdsRef.current.size > 0;\n const unsaved = unsavedWorkIdsRef.current.size > 0;\n if (uploading && unsaved) return uploadAndUnsavedWorkConfirmMessage;\n return uploading ? navigationConfirmMessage : unsavedWorkConfirmMessage;\n }, [\n uploadIdsRef,\n unsavedWorkIdsRef,\n navigationConfirmMessage,\n unsavedWorkConfirmMessage,\n uploadAndUnsavedWorkConfirmMessage,\n ]);\n\n useEffect(() => {\n if (!hasGuardedWork) return;\n\n const handleBeforeUnload = (event: BeforeUnloadEvent) => {\n if (bypassNextUnloadRef.current) {\n bypassNextUnloadRef.current = false;\n return;\n }\n event.preventDefault();\n // Modern browsers may ignore the custom string and show their own generic confirmation.\n event.returnValue =\n beforeUnloadMessage ??\n (uploadIdsRef.current.size > 0\n ? DEFAULT_UPLOAD_BEFORE_UNLOAD_MESSAGE\n : DEFAULT_UNSAVED_WORK_BEFORE_UNLOAD_MESSAGE);\n return event.returnValue;\n };\n\n window.addEventListener('beforeunload', handleBeforeUnload);\n return () => {\n window.removeEventListener('beforeunload', handleBeforeUnload);\n };\n }, [hasGuardedWork, beforeUnloadMessage, uploadIdsRef]);\n\n // A back/forward-cache return re-shows this page without reloading it, so an armed bypass must not\n // outlive the navigation that left.\n useEffect(() => {\n window.addEventListener('pageshow', clearBypass);\n return () => {\n window.removeEventListener('pageshow', clearBypass);\n };\n }, [clearBypass]);\n\n const shouldConfirmNavigation = useCallback(\n () => uploadIdsRef.current.size > 0 || unsavedWorkIdsRef.current.size > 0,\n [uploadIdsRef, unsavedWorkIdsRef],\n );\n\n const confirmNavigation = useCallback(\n (options?: ConfirmNavigationOptions): boolean => {\n if (typeof window === 'undefined') return true;\n const proceed = !shouldConfirmNavigation() || window.confirm(confirmMessage());\n if (proceed && options?.fullPageNavigation) bypassNextUnloadRef.current = true;\n return proceed;\n },\n [shouldConfirmNavigation, confirmMessage],\n );\n\n const value = useMemo<LocalUploadGuardContextValue>(\n () => ({\n activeUploadCount: uploads.count,\n unsavedWorkCount: unsavedWork.count,\n guardedWorkCount,\n registerUpload: uploads.register,\n unregisterUpload: uploads.unregister,\n registerUnsavedWork: unsavedWork.register,\n unregisterUnsavedWork: unsavedWork.unregister,\n shouldConfirmNavigation,\n confirmNavigation,\n }),\n [\n uploads.count,\n uploads.register,\n uploads.unregister,\n unsavedWork.count,\n unsavedWork.register,\n unsavedWork.unregister,\n guardedWorkCount,\n shouldConfirmNavigation,\n confirmNavigation,\n ],\n );\n\n return (\n <LocalUploadGuardContext.Provider value={value}>{children}</LocalUploadGuardContext.Provider>\n );\n}\n\nexport function useLocalUploadGuardContext(): LocalUploadGuardContextValue {\n const ctx = useContext(LocalUploadGuardContext);\n if (ctx === undefined) {\n throw new Error('useLocalUploadGuard must be used within LocalUploadGuardProvider');\n }\n return ctx;\n}\n\nexport function useOptionalLocalUploadGuard(): LocalUploadGuardContextValue | null {\n return useContext(LocalUploadGuardContext) ?? null;\n}\n\nexport function useLocalUploadGuard(): LocalUploadGuardContextValue {\n return useLocalUploadGuardContext();\n}\n\nexport function useUnsavedWorkGuard(hasUnsavedWork: boolean): { release: () => void } {\n const { registerUnsavedWork, unregisterUnsavedWork } = useLocalUploadGuardContext();\n const id = useId();\n useEffect(() => {\n if (!hasUnsavedWork) return;\n registerUnsavedWork(id);\n return () => unregisterUnsavedWork(id);\n }, [hasUnsavedWork, id, registerUnsavedWork, unregisterUnsavedWork]);\n const release = useCallback(() => unregisterUnsavedWork(id), [id, unregisterUnsavedWork]);\n return useMemo(() => ({ release }), [release]);\n}\n"]}
|
|
@@ -1,10 +1,3 @@
|
|
|
1
1
|
import { type ConfirmNavigationOptions } from './local-upload-guard-provider.js';
|
|
2
|
-
/**
|
|
3
|
-
* Returns a callable that wraps a navigation action. Calling the returned
|
|
4
|
-
* function with `performNavigation` either invokes it immediately (when
|
|
5
|
-
* nothing is registered) or shows a confirmation prompt; if the user confirms,
|
|
6
|
-
* `performNavigation` is invoked. `options` passes through to
|
|
7
|
-
* `confirmNavigation()`.
|
|
8
|
-
*/
|
|
9
2
|
export declare function useGuardedNavigation(): (performNavigation: () => void, options?: ConfirmNavigationOptions) => void;
|
|
10
3
|
//# sourceMappingURL=use-guarded-navigation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-guarded-navigation.d.ts","sourceRoot":"","sources":["../../../src/react/guard/use-guarded-navigation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-guarded-navigation.d.ts","sourceRoot":"","sources":["../../../src/react/guard/use-guarded-navigation.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,kCAAkC,CAAC;AAE1C,wBAAgB,oBAAoB,IAAI,CACtC,iBAAiB,EAAE,MAAM,IAAI,EAC7B,OAAO,CAAC,EAAE,wBAAwB,KAC/B,IAAI,CAUR"}
|
|
@@ -1,34 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
/**
|
|
3
|
-
* Wraps a navigation action so it prompts the user while an upload or unsaved
|
|
4
|
-
* work is registered. Returns a function that takes the consumer's actual navigation
|
|
5
|
-
* call and either runs it immediately (nothing registered) or shows a confirm()
|
|
6
|
-
* dialog first.
|
|
7
|
-
*
|
|
8
|
-
* Designed to be framework-agnostic — the consumer supplies the navigator
|
|
9
|
-
* (Next.js router.push, React Router navigate, plain location.assign, etc.).
|
|
10
|
-
* A FULL-PAGE navigator (`location.assign` / `location.replace`) passes
|
|
11
|
-
* `{ fullPageNavigation: true }`, so the unload that follows a confirmed
|
|
12
|
-
* navigation does not raise the browser's own "Leave site?" as a second prompt
|
|
13
|
-
* (see `ConfirmNavigationOptions`). Router navigations pass nothing.
|
|
14
|
-
*
|
|
15
|
-
* Typical use inside a button:
|
|
16
|
-
*
|
|
17
|
-
* const guardedNav = useGuardedNavigation();
|
|
18
|
-
* <Button onClick={() => guardedNav(() => router.push('/somewhere'))}>Go</Button>
|
|
19
|
-
* <Button onClick={() => guardedNav(() => location.assign(url), { fullPageNavigation: true })}>Leave</Button>
|
|
20
|
-
*
|
|
21
|
-
* For links, use the `<GuardedLink>` render-prop wrapper.
|
|
22
|
-
*/
|
|
23
2
|
import { useCallback } from 'react';
|
|
24
3
|
import { useLocalUploadGuard, } from './local-upload-guard-provider.js';
|
|
25
|
-
/**
|
|
26
|
-
* Returns a callable that wraps a navigation action. Calling the returned
|
|
27
|
-
* function with `performNavigation` either invokes it immediately (when
|
|
28
|
-
* nothing is registered) or shows a confirmation prompt; if the user confirms,
|
|
29
|
-
* `performNavigation` is invoked. `options` passes through to
|
|
30
|
-
* `confirmNavigation()`.
|
|
31
|
-
*/
|
|
32
4
|
export function useGuardedNavigation() {
|
|
33
5
|
const { confirmNavigation } = useLocalUploadGuard();
|
|
34
6
|
return useCallback((performNavigation, options) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-guarded-navigation.js","sourceRoot":"","sources":["../../../src/react/guard/use-guarded-navigation.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb
|
|
1
|
+
{"version":3,"file":"use-guarded-navigation.js","sourceRoot":"","sources":["../../../src/react/guard/use-guarded-navigation.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EACL,mBAAmB,GAEpB,MAAM,kCAAkC,CAAC;AAE1C,MAAM,UAAU,oBAAoB;IAIlC,MAAM,EAAE,iBAAiB,EAAE,GAAG,mBAAmB,EAAE,CAAC;IACpD,OAAO,WAAW,CAChB,CAAC,iBAA6B,EAAE,OAAkC,EAAE,EAAE;QACpE,IAAI,iBAAiB,CAAC,OAAO,CAAC,EAAE,CAAC;YAC/B,iBAAiB,EAAE,CAAC;QACtB,CAAC;IACH,CAAC,EACD,CAAC,iBAAiB,CAAC,CACpB,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport { useCallback } from 'react';\nimport {\n useLocalUploadGuard,\n type ConfirmNavigationOptions,\n} from './local-upload-guard-provider.js';\n\nexport function useGuardedNavigation(): (\n performNavigation: () => void,\n options?: ConfirmNavigationOptions,\n) => void {\n const { confirmNavigation } = useLocalUploadGuard();\n return useCallback(\n (performNavigation: () => void, options?: ConfirmNavigationOptions) => {\n if (confirmNavigation(options)) {\n performNavigation();\n }\n },\n [confirmNavigation],\n );\n}\n"]}
|