@multiplatform.one/components 7.85.0 → 7.86.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/dist/cjs/Avatar/index.cjs +17 -1
- package/dist/cjs/Avatar/index.native.js +17 -1
- package/dist/cjs/Avatar/index.native.js.map +1 -1
- package/dist/cjs/Dialog.cjs +86 -3
- package/dist/cjs/Dialog.native.js +94 -3
- package/dist/cjs/Dialog.native.js.map +1 -1
- package/dist/cjs/feedback/ConfirmDialog.cjs +15 -2
- package/dist/cjs/feedback/ConfirmDialog.native.js +16 -2
- package/dist/cjs/feedback/ConfirmDialog.native.js.map +1 -1
- package/dist/cjs/feedback/index.cjs +1 -0
- package/dist/cjs/feedback/index.native.js +1 -0
- package/dist/cjs/feedback/index.native.js.map +1 -1
- package/dist/cjs/layouts/AsyncBoundary.cjs +70 -21
- package/dist/cjs/layouts/AsyncBoundary.native.js +72 -21
- package/dist/cjs/layouts/AsyncBoundary.native.js.map +1 -1
- package/dist/cjs/layouts/asyncFailure.cjs +104 -0
- package/dist/cjs/layouts/asyncFailure.native.js +133 -0
- package/dist/cjs/layouts/asyncFailure.native.js.map +1 -0
- package/dist/cjs/layouts/index.cjs +4 -1
- package/dist/cjs/layouts/index.native.js +3 -0
- package/dist/cjs/layouts/index.native.js.map +1 -1
- package/dist/cjs/tamagui.cjs +1 -0
- package/dist/cjs/tamagui.native.js +1 -0
- package/dist/cjs/tamagui.native.js.map +1 -1
- package/dist/esm/Avatar/index.mjs +16 -2
- package/dist/esm/Avatar/index.mjs.map +1 -1
- package/dist/esm/Avatar/index.native.js +16 -2
- package/dist/esm/Avatar/index.native.js.map +1 -1
- package/dist/esm/Dialog.mjs +88 -6
- package/dist/esm/Dialog.mjs.map +1 -1
- package/dist/esm/Dialog.native.js +96 -6
- package/dist/esm/Dialog.native.js.map +1 -1
- package/dist/esm/feedback/ConfirmDialog.mjs +14 -2
- package/dist/esm/feedback/ConfirmDialog.mjs.map +1 -1
- package/dist/esm/feedback/ConfirmDialog.native.js +15 -2
- package/dist/esm/feedback/ConfirmDialog.native.js.map +1 -1
- package/dist/esm/feedback/index.mjs +2 -2
- package/dist/esm/feedback/index.mjs.map +1 -1
- package/dist/esm/feedback/index.native.js +2 -2
- package/dist/esm/feedback/index.native.js.map +1 -1
- package/dist/esm/layouts/AsyncBoundary.mjs +70 -21
- package/dist/esm/layouts/AsyncBoundary.mjs.map +1 -1
- package/dist/esm/layouts/AsyncBoundary.native.js +72 -21
- package/dist/esm/layouts/AsyncBoundary.native.js.map +1 -1
- package/dist/esm/layouts/asyncFailure.mjs +78 -0
- package/dist/esm/layouts/asyncFailure.mjs.map +1 -0
- package/dist/esm/layouts/asyncFailure.native.js +104 -0
- package/dist/esm/layouts/asyncFailure.native.js.map +1 -0
- package/dist/esm/layouts/index.mjs +2 -1
- package/dist/esm/layouts/index.mjs.map +1 -1
- package/dist/esm/layouts/index.native.js +2 -1
- package/dist/esm/layouts/index.native.js.map +1 -1
- package/dist/esm/tamagui.mjs +2 -2
- package/dist/esm/tamagui.mjs.map +1 -1
- package/dist/esm/tamagui.native.js +2 -2
- package/dist/esm/tamagui.native.js.map +1 -1
- package/dist/jsx/Avatar/index.mjs +16 -2
- package/dist/jsx/Avatar/index.mjs.map +1 -1
- package/dist/jsx/Avatar/index.native.js +17 -1
- package/dist/jsx/Avatar/index.native.js.map +1 -1
- package/dist/jsx/Dialog.mjs +88 -6
- package/dist/jsx/Dialog.mjs.map +1 -1
- package/dist/jsx/Dialog.native.js +94 -3
- package/dist/jsx/Dialog.native.js.map +1 -1
- package/dist/jsx/feedback/ConfirmDialog.mjs +14 -2
- package/dist/jsx/feedback/ConfirmDialog.mjs.map +1 -1
- package/dist/jsx/feedback/ConfirmDialog.native.js +16 -2
- package/dist/jsx/feedback/ConfirmDialog.native.js.map +1 -1
- package/dist/jsx/feedback/index.mjs +2 -2
- package/dist/jsx/feedback/index.mjs.map +1 -1
- package/dist/jsx/feedback/index.native.js +1 -0
- package/dist/jsx/feedback/index.native.js.map +1 -1
- package/dist/jsx/layouts/AsyncBoundary.mjs +70 -21
- package/dist/jsx/layouts/AsyncBoundary.mjs.map +1 -1
- package/dist/jsx/layouts/AsyncBoundary.native.js +72 -21
- package/dist/jsx/layouts/AsyncBoundary.native.js.map +1 -1
- package/dist/jsx/layouts/asyncFailure.mjs +78 -0
- package/dist/jsx/layouts/asyncFailure.mjs.map +1 -0
- package/dist/jsx/layouts/asyncFailure.native.js +133 -0
- package/dist/jsx/layouts/asyncFailure.native.js.map +1 -0
- package/dist/jsx/layouts/index.mjs +2 -1
- package/dist/jsx/layouts/index.mjs.map +1 -1
- package/dist/jsx/layouts/index.native.js +3 -0
- package/dist/jsx/layouts/index.native.js.map +1 -1
- package/dist/jsx/tamagui.mjs +2 -2
- package/dist/jsx/tamagui.mjs.map +1 -1
- package/dist/jsx/tamagui.native.js +1 -0
- package/dist/jsx/tamagui.native.js.map +1 -1
- package/package.json +9 -9
- package/src/Avatar/Avatar.spec.tsx +62 -1
- package/src/Avatar/index.tsx +39 -2
- package/src/Dialog.phoneSheet.spec.tsx +186 -0
- package/src/Dialog.tsx +143 -5
- package/src/feedback/ConfirmDialog.tsx +26 -2
- package/src/feedback/index.ts +1 -1
- package/src/layouts/AsyncBoundary.reason.spec.tsx +161 -0
- package/src/layouts/AsyncBoundary.stories.tsx +26 -0
- package/src/layouts/AsyncBoundary.tsx +107 -25
- package/src/layouts/asyncFailure.ts +110 -0
- package/src/layouts/index.tsx +7 -0
- package/src/layouts/pageTitleOneLine.spec.tsx +26 -0
- package/src/overlayStack.native-press.spec.tsx +34 -0
- package/src/surfaces.spec.tsx +42 -0
- package/src/tamagui.ts +8 -2
- package/types/Avatar/index.d.ts +11 -0
- package/types/Avatar/index.d.ts.map +1 -1
- package/types/Dialog.d.ts +262 -2
- package/types/Dialog.d.ts.map +1 -1
- package/types/feedback/ConfirmDialog.d.ts +13 -1
- package/types/feedback/ConfirmDialog.d.ts.map +1 -1
- package/types/feedback/index.d.ts +1 -1
- package/types/feedback/index.d.ts.map +1 -1
- package/types/layouts/AsyncBoundary.d.ts +16 -1
- package/types/layouts/AsyncBoundary.d.ts.map +1 -1
- package/types/layouts/asyncFailure.d.ts +21 -0
- package/types/layouts/asyncFailure.d.ts.map +1 -0
- package/types/layouts/index.d.ts +1 -0
- package/types/layouts/index.d.ts.map +1 -1
- package/types/tamagui.d.ts +2 -2
- package/types/tamagui.d.ts.map +1 -1
package/src/Dialog.tsx
CHANGED
|
@@ -16,17 +16,51 @@
|
|
|
16
16
|
* Their Portal binds the overlay stack (FZ-7869): a dialog opened from a sheet
|
|
17
17
|
* or another dialog sits one `$zIndex` step above it, and on native it would
|
|
18
18
|
* otherwise reach the root host with no zIndex and mount under the sheet.
|
|
19
|
-
*
|
|
19
|
+
*
|
|
20
|
+
* LC-269 PHONE-DIALOG-AS-SHEET: at the overlay pivot and below (web 640, and
|
|
21
|
+
* every native window) the house Dialog is a content dialog, so its Portal
|
|
22
|
+
* presents in SheetModal: the bottom sheet on a phone, the width-capped panel
|
|
23
|
+
* at an iPad's regular width (FZ-8116). Desktop keeps the centred dialog.
|
|
24
|
+
* AlertDialog is the alert-shaped one and stays centred everywhere, as
|
|
25
|
+
* UIAlertController does. A dialog opened inside another overlay stays
|
|
26
|
+
* centred (LC-164: no sheet on a sheet), and `phonePresentation="dialog"`
|
|
27
|
+
* ejects. No tamagui Adapt: its content portal does not survive SheetModal's
|
|
28
|
+
* native window (a nested PortalProvider), so the Portal itself swaps hosts
|
|
29
|
+
* and the house DialogContent renders as the sheet's column and the house
|
|
30
|
+
* DialogOverlay as nothing (`DialogSheetFaceProvider`, theme). The raw
|
|
31
|
+
* tamagui Content and Overlay parts stay tamagui's.
|
|
20
32
|
*/
|
|
21
|
-
import {
|
|
22
|
-
|
|
33
|
+
import {
|
|
34
|
+
OverlayStackProvider,
|
|
35
|
+
overlayStackStep,
|
|
36
|
+
useOverlayParentZIndex,
|
|
37
|
+
useOverlayZIndex,
|
|
38
|
+
useViewportGtSm,
|
|
39
|
+
withOverlayStack,
|
|
40
|
+
} from "@multiplatform.one/forms";
|
|
41
|
+
import { DialogSheetFaceProvider } from "@multiplatform.one/theme";
|
|
42
|
+
import {
|
|
43
|
+
createContext,
|
|
44
|
+
forwardRef,
|
|
45
|
+
isValidElement,
|
|
46
|
+
useCallback,
|
|
47
|
+
useContext,
|
|
48
|
+
useImperativeHandle,
|
|
49
|
+
useRef,
|
|
50
|
+
useState,
|
|
51
|
+
type ComponentType,
|
|
52
|
+
type ReactNode,
|
|
53
|
+
} from "react";
|
|
23
54
|
import {
|
|
24
55
|
AlertDialog as TamaguiAlertDialog,
|
|
25
56
|
Dialog as TamaguiDialog,
|
|
57
|
+
SheetControllerContext,
|
|
26
58
|
withStaticProperties,
|
|
59
|
+
type DialogProps as TamaguiDialogProps,
|
|
27
60
|
type GetProps,
|
|
28
61
|
} from "tamagui";
|
|
29
62
|
import { useTranslation } from "./shared/i18n";
|
|
63
|
+
import { SheetModal } from "./SheetModal";
|
|
30
64
|
|
|
31
65
|
type Labelled = {
|
|
32
66
|
"aria-label"?: unknown;
|
|
@@ -75,9 +109,113 @@ function namedByContent<C extends ComponentType<any>>(Part: C, displayName: stri
|
|
|
75
109
|
return Close;
|
|
76
110
|
}
|
|
77
111
|
|
|
78
|
-
|
|
112
|
+
/**
|
|
113
|
+
* How a house Dialog presents at phone width (LC-269). `"sheet"`, the
|
|
114
|
+
* default, is a content dialog: SheetModal at the overlay pivot and below.
|
|
115
|
+
* `"dialog"` ejects and keeps the centred dialog at every width.
|
|
116
|
+
*/
|
|
117
|
+
export type DialogPhonePresentation = "sheet" | "dialog";
|
|
118
|
+
|
|
119
|
+
export type DialogProps = TamaguiDialogProps & {
|
|
120
|
+
phonePresentation?: DialogPhonePresentation;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
type DialogHandle = { open: (open: boolean) => void };
|
|
124
|
+
|
|
125
|
+
/** True where a house Dialog with this presentation draws as a sheet (LC-269). */
|
|
126
|
+
export function useDialogPresentsAsSheet(presentation: DialogPhonePresentation = "sheet"): boolean {
|
|
127
|
+
const wide = useViewportGtSm();
|
|
128
|
+
const nested = useOverlayParentZIndex() !== undefined;
|
|
129
|
+
return presentation === "sheet" && !wide && !nested;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type SheetPresentation = { open: boolean; dismiss: (open: boolean) => void };
|
|
133
|
+
|
|
134
|
+
const DialogSheetContext = createContext<SheetPresentation | null>(null);
|
|
135
|
+
|
|
136
|
+
const RootDialog = TamaguiDialog as unknown as ComponentType<
|
|
137
|
+
TamaguiDialogProps & { ref?: unknown }
|
|
138
|
+
>;
|
|
139
|
+
|
|
140
|
+
const DialogRoot = forwardRef<DialogHandle, DialogProps>(function Dialog(
|
|
141
|
+
{ phonePresentation = "sheet", children, open, defaultOpen, onOpenChange, ...props },
|
|
142
|
+
ref,
|
|
143
|
+
) {
|
|
144
|
+
const asSheet = useDialogPresentsAsSheet(phonePresentation);
|
|
145
|
+
const tamaguiRef = useRef<DialogHandle | null>(null);
|
|
146
|
+
useImperativeHandle(ref, () => ({ open: (next) => tamaguiRef.current?.open(next) }), []);
|
|
147
|
+
const [ownOpen, setOwnOpen] = useState(defaultOpen ?? false);
|
|
148
|
+
const isOpen = open ?? ownOpen;
|
|
149
|
+
// The web sheet reports one close from its Sheet and can report another
|
|
150
|
+
// from Escape; only the first may close it, or a consumer's onOpenChange
|
|
151
|
+
// (ConfirmDialog's onCancel) fires twice.
|
|
152
|
+
const openRef = useRef(isOpen);
|
|
153
|
+
openRef.current = isOpen;
|
|
154
|
+
const handleOpenChange = useCallback(
|
|
155
|
+
(next: boolean) => {
|
|
156
|
+
openRef.current = next;
|
|
157
|
+
setOwnOpen(next);
|
|
158
|
+
onOpenChange?.(next);
|
|
159
|
+
},
|
|
160
|
+
[onOpenChange],
|
|
161
|
+
);
|
|
162
|
+
const dismiss = useCallback((next: boolean) => {
|
|
163
|
+
if (next || !openRef.current) return;
|
|
164
|
+
tamaguiRef.current?.open(false);
|
|
165
|
+
}, []);
|
|
166
|
+
return (
|
|
167
|
+
<RootDialog
|
|
168
|
+
ref={tamaguiRef}
|
|
169
|
+
open={open}
|
|
170
|
+
defaultOpen={defaultOpen}
|
|
171
|
+
onOpenChange={handleOpenChange}
|
|
172
|
+
{...props}
|
|
173
|
+
>
|
|
174
|
+
<DialogSheetContext.Provider value={asSheet ? { open: isOpen, dismiss } : null}>
|
|
175
|
+
{children}
|
|
176
|
+
</DialogSheetContext.Provider>
|
|
177
|
+
</RootDialog>
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const StackedPortal = withOverlayStack(TamaguiDialog.Portal, "dropdown", "DialogPortal");
|
|
182
|
+
|
|
183
|
+
function DialogPortal(props: GetProps<typeof TamaguiDialog.Portal>) {
|
|
184
|
+
const sheet = useContext(DialogSheetContext);
|
|
185
|
+
const dialogZIndex = useOverlayZIndex("dropdown");
|
|
186
|
+
const [opened, setOpened] = useState(false);
|
|
187
|
+
if (sheet?.open && !opened) setOpened(true);
|
|
188
|
+
if (!sheet) return <StackedPortal {...props} />;
|
|
189
|
+
// A closed Sheet still mounts its frame off screen; a dialog that never
|
|
190
|
+
// opened mounts nothing, as tamagui's closed Dialog does.
|
|
191
|
+
if (!opened) return null;
|
|
192
|
+
// The sheet keeps the dialog's rung: SheetModal binds one step above the
|
|
193
|
+
// z it is opened from, so it lands on the dialog tier, covers a page sheet
|
|
194
|
+
// as a dialog does, and what it opens stacks one step above it (FZ-7869).
|
|
195
|
+
// tamagui's Dialog hands its subtree a hidden sheet controller while it is
|
|
196
|
+
// open and not adapted, which renders every Sheet under it as nothing; the
|
|
197
|
+
// null controller gives SheetModal (and any sheet in the content) its own.
|
|
198
|
+
return (
|
|
199
|
+
<SheetControllerContext.Provider value={null}>
|
|
200
|
+
<OverlayStackProvider zIndex={dialogZIndex - overlayStackStep()}>
|
|
201
|
+
<SheetModal open={sheet.open} onOpenChange={sheet.dismiss}>
|
|
202
|
+
<DialogSheetFaceProvider value>{props.children}</DialogSheetFaceProvider>
|
|
203
|
+
</SheetModal>
|
|
204
|
+
</OverlayStackProvider>
|
|
205
|
+
</SheetControllerContext.Provider>
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
DialogPortal.displayName = "DialogPortal";
|
|
209
|
+
|
|
210
|
+
export const Dialog = withStaticProperties(DialogRoot, {
|
|
211
|
+
Trigger: TamaguiDialog.Trigger,
|
|
212
|
+
Portal: DialogPortal as unknown as typeof TamaguiDialog.Portal,
|
|
213
|
+
Overlay: TamaguiDialog.Overlay,
|
|
214
|
+
Content: TamaguiDialog.Content,
|
|
215
|
+
Title: TamaguiDialog.Title,
|
|
216
|
+
Description: TamaguiDialog.Description,
|
|
79
217
|
Close: namedByContent(TamaguiDialog.Close, "DialogClose"),
|
|
80
|
-
|
|
218
|
+
FocusScope: TamaguiDialog.FocusScope,
|
|
81
219
|
});
|
|
82
220
|
|
|
83
221
|
export const AlertDialog: typeof TamaguiAlertDialog = withStaticProperties(TamaguiAlertDialog, {
|
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
import { useCallback, useEffect, useRef, type ReactNode } from "react";
|
|
29
29
|
import { useTranslation } from "../shared/i18n";
|
|
30
30
|
import { Paragraph, XStack, YStack, isWeb, type TamaguiElement } from "tamagui";
|
|
31
|
-
import { Dialog } from "../Dialog";
|
|
31
|
+
import { Dialog, type DialogPhonePresentation } from "../Dialog";
|
|
32
32
|
import { countActionsInTree } from "../shared/actionScan";
|
|
33
33
|
import { useReturnFocusOnClose } from "../shared/useReturnFocusOnClose";
|
|
34
34
|
import { useKeyboardInset } from "../shared/useKeyboardInset";
|
|
@@ -125,6 +125,24 @@ export interface ConfirmDialogProps {
|
|
|
125
125
|
* `dialog-too-many-actions` DEV warn.
|
|
126
126
|
*/
|
|
127
127
|
allowManyActions?: boolean;
|
|
128
|
+
/**
|
|
129
|
+
* LC-269. A confirm with a title, at most one line of `body` and its two
|
|
130
|
+
* actions is alert-shaped and stays a centred alert at every width, as
|
|
131
|
+
* UIAlertController does. One whose `children` hold content (a form, a
|
|
132
|
+
* picker, a list, a search) is a content dialog and presents as a sheet at
|
|
133
|
+
* phone width. Pass `"dialog"` to keep the centred dialog, `"sheet"` to
|
|
134
|
+
* force the sheet.
|
|
135
|
+
*/
|
|
136
|
+
phonePresentation?: DialogPhonePresentation;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** LC-269: content in `children` (not a string `body`) makes a content dialog. */
|
|
140
|
+
export function confirmDialogPresentation({
|
|
141
|
+
body,
|
|
142
|
+
children,
|
|
143
|
+
}: Pick<ConfirmDialogProps, "body" | "children">): DialogPhonePresentation {
|
|
144
|
+
if (body != null || children == null || typeof children === "boolean") return "dialog";
|
|
145
|
+
return typeof children === "string" || typeof children === "number" ? "dialog" : "sheet";
|
|
128
146
|
}
|
|
129
147
|
|
|
130
148
|
export function ConfirmDialog({
|
|
@@ -143,6 +161,7 @@ export function ConfirmDialog({
|
|
|
143
161
|
confirmDisabledReason,
|
|
144
162
|
cancelDisabled = false,
|
|
145
163
|
allowManyActions = false,
|
|
164
|
+
phonePresentation,
|
|
146
165
|
}: ConfirmDialogProps) {
|
|
147
166
|
const { t } = useTranslation();
|
|
148
167
|
const { knobProps } = useResolvedKnobs();
|
|
@@ -217,7 +236,12 @@ export function ConfirmDialog({
|
|
|
217
236
|
}, [open]);
|
|
218
237
|
|
|
219
238
|
return (
|
|
220
|
-
<Dialog
|
|
239
|
+
<Dialog
|
|
240
|
+
modal
|
|
241
|
+
open={open}
|
|
242
|
+
onOpenChange={handleOpenChange}
|
|
243
|
+
phonePresentation={phonePresentation ?? confirmDialogPresentation({ body, children })}
|
|
244
|
+
>
|
|
221
245
|
<Dialog.Portal paddingBottom={keyboardInset || undefined}>
|
|
222
246
|
<DialogOverlay key="overlay" />
|
|
223
247
|
<DialogContent
|
package/src/feedback/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export {
|
|
|
10
10
|
} from "./route";
|
|
11
11
|
export { notify } from "./notify";
|
|
12
12
|
export { NotifyHost, NotifyRegion, type NotifyHostProps } from "./NotifyHost";
|
|
13
|
-
export { ConfirmDialog, type ConfirmDialogProps } from "./ConfirmDialog";
|
|
13
|
+
export { ConfirmDialog, confirmDialogPresentation, type ConfirmDialogProps } from "./ConfirmDialog";
|
|
14
14
|
export {
|
|
15
15
|
dismissFeedback,
|
|
16
16
|
dismissFeedbackByRegion,
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { cleanup, fireEvent, screen } from "@testing-library/react";
|
|
6
|
+
import { renderWithProviders } from "@multiplatform.one/test-utils";
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
8
|
+
import { Text } from "tamagui";
|
|
9
|
+
import { AsyncBoundary } from "./AsyncBoundary";
|
|
10
|
+
import { asyncFailure } from "./asyncFailure";
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
cleanup();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("asyncFailure copy from a reason (FZ-8045, LC-204)", () => {
|
|
17
|
+
it("names the subject, the cause and the next step", () => {
|
|
18
|
+
expect(asyncFailure({ subject: "Races", reason: "query_timeout", retry: true })).toEqual({
|
|
19
|
+
title: "Races didn't load",
|
|
20
|
+
description: "The server took too long to answer. Tap Retry to try again.",
|
|
21
|
+
retry: true,
|
|
22
|
+
refusal: false,
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("tells a live view it retries by itself", () => {
|
|
27
|
+
expect(
|
|
28
|
+
asyncFailure({ subject: "Races", reason: "source_busy", retry: true, live: true })
|
|
29
|
+
.description,
|
|
30
|
+
).toBe("The server is busy. It will retry; tap Retry to try now.");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("states there is no action when the view has no Retry", () => {
|
|
34
|
+
expect(asyncFailure({ subject: "Races", reason: "source_down" }).description).toBe(
|
|
35
|
+
"The server is down. Try again later.",
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("never lets a bare code stand as the description", () => {
|
|
40
|
+
for (const reason of ["click_house_timeout", "weird_code_42", "failed"]) {
|
|
41
|
+
const copy = asyncFailure({ subject: "Races", reason, retry: true });
|
|
42
|
+
expect(copy.description).not.toContain(reason);
|
|
43
|
+
expect(copy.description).toMatch(/\. Tap Retry to try again\.$/);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it.each([
|
|
48
|
+
["query_too_large", "The response is too large to load."],
|
|
49
|
+
["credentials_refused", "The server turned down its login."],
|
|
50
|
+
["session_expired", "Your session ended."],
|
|
51
|
+
])("drops Retry for %s, which retrying can't fix", (reason, cause) => {
|
|
52
|
+
const copy = asyncFailure({ subject: "Races", reason, retry: true });
|
|
53
|
+
expect(copy.retry).toBe(false);
|
|
54
|
+
expect(copy.description.startsWith(cause)).toBe(true);
|
|
55
|
+
expect(copy.description).not.toContain("Tap Retry");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("routes a permission refusal to a neutral state", () => {
|
|
59
|
+
expect(
|
|
60
|
+
asyncFailure({ subject: "Approvals", reason: "permission_denied", retry: true }),
|
|
61
|
+
).toEqual({
|
|
62
|
+
title: "Approvals isn't open to this account",
|
|
63
|
+
description: "Ask an admin for access.",
|
|
64
|
+
retry: false,
|
|
65
|
+
refusal: true,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("lets an app add its own reason words over the defaults", () => {
|
|
70
|
+
const copy = asyncFailure({
|
|
71
|
+
subject: "Races",
|
|
72
|
+
reason: "source_unreachable",
|
|
73
|
+
retry: true,
|
|
74
|
+
reasons: { source_unreachable: { cause: "Pelt can't reach its database." } },
|
|
75
|
+
});
|
|
76
|
+
expect(copy.description).toBe("Pelt can't reach its database. Tap Retry to try again.");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("AsyncBoundary reason (FZ-8045, LC-204)", () => {
|
|
81
|
+
it("renders the reason's title and line, never the raw code", () => {
|
|
82
|
+
renderWithProviders(
|
|
83
|
+
<AsyncBoundary error reason="click_house_timeout" subject="Races" onRetry={() => {}} />,
|
|
84
|
+
);
|
|
85
|
+
expect(screen.getByText("Races didn't load")).toBeInTheDocument();
|
|
86
|
+
expect(
|
|
87
|
+
screen.getByText("The server took too long to answer. Tap Retry to try again."),
|
|
88
|
+
).toBeInTheDocument();
|
|
89
|
+
expect(screen.queryByText(/click_house_timeout/)).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("reads a code-shaped error string as its reason", () => {
|
|
93
|
+
renderWithProviders(<AsyncBoundary error="click_house_timeout" onRetry={() => {}} />);
|
|
94
|
+
expect(screen.queryByText("click_house_timeout")).toBeNull();
|
|
95
|
+
expect(
|
|
96
|
+
screen.getByText("The server took too long to answer. Tap Retry to try again."),
|
|
97
|
+
).toBeInTheDocument();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("keeps a one-word message that names no reason as the description", () => {
|
|
101
|
+
renderWithProviders(<AsyncBoundary error="boom" />);
|
|
102
|
+
expect(screen.getByText("boom")).toBeInTheDocument();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("keeps a sentence error as the server's own words", () => {
|
|
106
|
+
renderWithProviders(<AsyncBoundary error="The report needs a date range." />);
|
|
107
|
+
expect(screen.getByText("The report needs a date range.")).toBeInTheDocument();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("renders no Retry for a non-retryable reason", () => {
|
|
111
|
+
const onRetry = vi.fn();
|
|
112
|
+
renderWithProviders(
|
|
113
|
+
<AsyncBoundary error reason="credentials_refused" subject="Races" onRetry={onRetry} />,
|
|
114
|
+
);
|
|
115
|
+
expect(screen.getByText("Races didn't load")).toBeInTheDocument();
|
|
116
|
+
expect(screen.queryByText("Retry")).toBeNull();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("renders a permission refusal as a neutral EmptyState with no Retry (LC-165)", () => {
|
|
120
|
+
renderWithProviders(
|
|
121
|
+
<AsyncBoundary error reason="forbidden" subject="Approvals" onRetry={() => {}} />,
|
|
122
|
+
);
|
|
123
|
+
expect(screen.getByText("Approvals isn't open to this account")).toBeInTheDocument();
|
|
124
|
+
expect(document.querySelector('[data-async-state="refused"]')).toBeTruthy();
|
|
125
|
+
expect(document.querySelector('[data-empty-intent="neutral"]')).toBeTruthy();
|
|
126
|
+
expect(document.querySelector('[data-empty-intent="error"]')).toBeNull();
|
|
127
|
+
expect(screen.queryByText("Retry")).toBeNull();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("keeps Retry on a retryable reason over stale data", () => {
|
|
131
|
+
const onRetry = vi.fn();
|
|
132
|
+
const { rerender } = renderWithProviders(
|
|
133
|
+
<AsyncBoundary>
|
|
134
|
+
<Text>Ready rows</Text>
|
|
135
|
+
</AsyncBoundary>,
|
|
136
|
+
);
|
|
137
|
+
rerender(
|
|
138
|
+
<AsyncBoundary error reason="source_busy" subject="Races" onRetry={onRetry}>
|
|
139
|
+
<Text>Ready rows</Text>
|
|
140
|
+
</AsyncBoundary>,
|
|
141
|
+
);
|
|
142
|
+
expect(screen.getByText("Ready rows")).toBeInTheDocument();
|
|
143
|
+
expect(screen.getByText("Races didn't load")).toBeInTheDocument();
|
|
144
|
+
fireEvent.click(screen.getByText("Retry"));
|
|
145
|
+
expect(onRetry).toHaveBeenCalledTimes(1);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("lets explicit errorTitle and errorDescription win", () => {
|
|
149
|
+
renderWithProviders(
|
|
150
|
+
<AsyncBoundary
|
|
151
|
+
error
|
|
152
|
+
reason="query_timeout"
|
|
153
|
+
subject="Races"
|
|
154
|
+
errorTitle="Custom title"
|
|
155
|
+
errorDescription="Custom line."
|
|
156
|
+
/>,
|
|
157
|
+
);
|
|
158
|
+
expect(screen.getByText("Custom title")).toBeInTheDocument();
|
|
159
|
+
expect(screen.getByText("Custom line.")).toBeInTheDocument();
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -185,3 +185,29 @@ export const KeepPreviousOnRefetch: Story = {
|
|
|
185
185
|
);
|
|
186
186
|
},
|
|
187
187
|
};
|
|
188
|
+
|
|
189
|
+
/** A failure reason becomes a title plus the cause and next step; refusals go neutral with no Retry (FZ-8045, LC-204, LC-165). */
|
|
190
|
+
export const FailureReasons: Story = {
|
|
191
|
+
render: () => (
|
|
192
|
+
<YStack gap="$4" padding="$3">
|
|
193
|
+
{(
|
|
194
|
+
[
|
|
195
|
+
["query_timeout", false],
|
|
196
|
+
["source_busy", true],
|
|
197
|
+
["query_too_large", false],
|
|
198
|
+
["session_expired", false],
|
|
199
|
+
["permission_denied", false],
|
|
200
|
+
] as const
|
|
201
|
+
).map(([reason, live]) => (
|
|
202
|
+
<AsyncBoundary
|
|
203
|
+
key={reason}
|
|
204
|
+
error={reason}
|
|
205
|
+
subject="Races"
|
|
206
|
+
live={live}
|
|
207
|
+
size="$3"
|
|
208
|
+
onRetry={action("retry")}
|
|
209
|
+
/>
|
|
210
|
+
))}
|
|
211
|
+
</YStack>
|
|
212
|
+
),
|
|
213
|
+
};
|
|
@@ -20,6 +20,7 @@ import { type SizeTokens, XStack, YStack, type YStackProps } from "tamagui";
|
|
|
20
20
|
import { Alert } from "../Alert";
|
|
21
21
|
import { Skeleton } from "../Skeleton";
|
|
22
22
|
import { Text } from "../Text";
|
|
23
|
+
import { asyncFailure, isReasonCode, type AsyncFailureReason } from "./asyncFailure";
|
|
23
24
|
import { EmptyState } from "./page";
|
|
24
25
|
|
|
25
26
|
// ---------------------------------------------------------------------------
|
|
@@ -106,6 +107,20 @@ export interface AsyncBoundaryProps extends Omit<YStackProps, "children"> {
|
|
|
106
107
|
emptyDescription?: string;
|
|
107
108
|
/** Default error description (overrides Error.message when set). */
|
|
108
109
|
errorDescription?: string;
|
|
110
|
+
/**
|
|
111
|
+
* Failure reason code from the server (`query_timeout`, `forbidden`, ...).
|
|
112
|
+
* The boundary turns it into a title plus one line naming the cause and
|
|
113
|
+
* the next step, drops Retry when retrying can't help, and routes a
|
|
114
|
+
* permission refusal to a neutral state (LC-204, LC-165). A code-shaped
|
|
115
|
+
* `error` string is read as the reason.
|
|
116
|
+
*/
|
|
117
|
+
reason?: string | null;
|
|
118
|
+
/** What failed to load, for the reason copy: "Races" → "Races didn't load". */
|
|
119
|
+
subject?: string;
|
|
120
|
+
/** The view refetches on its own, so the copy says it will retry. */
|
|
121
|
+
live?: boolean;
|
|
122
|
+
/** App reason words, merged over the MPO defaults. */
|
|
123
|
+
reasons?: Record<string, AsyncFailureReason>;
|
|
109
124
|
/**
|
|
110
125
|
* Size token for the default empty / error chrome (Tamagui's own `size`).
|
|
111
126
|
* Below `$true` it draws inline, for table and column use.
|
|
@@ -157,7 +172,60 @@ export function resolveAsyncStatus(opts: {
|
|
|
157
172
|
return "data";
|
|
158
173
|
}
|
|
159
174
|
|
|
175
|
+
interface ErrorCopy {
|
|
176
|
+
title: string;
|
|
177
|
+
description?: string;
|
|
178
|
+
retry: boolean;
|
|
179
|
+
refusal: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface ReasonCopyOptions {
|
|
183
|
+
reason?: string | null;
|
|
184
|
+
subject?: string;
|
|
185
|
+
live?: boolean;
|
|
186
|
+
reasons?: Record<string, AsyncFailureReason>;
|
|
187
|
+
canRetry: boolean;
|
|
188
|
+
errorTitle?: string;
|
|
189
|
+
errorDescription?: string;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function errorMessage(error: AsyncBoundaryProps["error"]): string | undefined {
|
|
193
|
+
if (typeof error === "string") return error;
|
|
194
|
+
if (error instanceof Error) return error.message;
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
160
198
|
function errorCopy(
|
|
199
|
+
error: AsyncBoundaryProps["error"],
|
|
200
|
+
errorTitle: string,
|
|
201
|
+
errorDescription: string | undefined,
|
|
202
|
+
opts: ReasonCopyOptions,
|
|
203
|
+
): ErrorCopy {
|
|
204
|
+
const message = errorMessage(error);
|
|
205
|
+
const reason = opts.reason ?? (isReasonCode(message) ? message : undefined);
|
|
206
|
+
if (reason) {
|
|
207
|
+
const failure = asyncFailure({
|
|
208
|
+
subject: opts.subject,
|
|
209
|
+
reason,
|
|
210
|
+
retry: opts.canRetry,
|
|
211
|
+
live: opts.live,
|
|
212
|
+
reasons: opts.reasons,
|
|
213
|
+
});
|
|
214
|
+
return {
|
|
215
|
+
title: opts.errorTitle ?? failure.title,
|
|
216
|
+
description: errorDescription ?? failure.description,
|
|
217
|
+
retry: failure.retry,
|
|
218
|
+
refusal: failure.refusal,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
...plainErrorCopy(error, errorTitle, errorDescription),
|
|
223
|
+
retry: opts.canRetry,
|
|
224
|
+
refusal: false,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function plainErrorCopy(
|
|
161
229
|
error: AsyncBoundaryProps["error"],
|
|
162
230
|
errorTitle: string,
|
|
163
231
|
errorDescription?: string,
|
|
@@ -425,24 +493,29 @@ function defaultEmptyBody({
|
|
|
425
493
|
}
|
|
426
494
|
|
|
427
495
|
function defaultErrorBody({
|
|
428
|
-
|
|
429
|
-
errorTitle,
|
|
430
|
-
errorDescription,
|
|
496
|
+
copy,
|
|
431
497
|
onRetry,
|
|
432
498
|
retryLabel,
|
|
433
499
|
size,
|
|
434
500
|
small,
|
|
435
501
|
}: {
|
|
436
|
-
|
|
437
|
-
errorTitle: string;
|
|
438
|
-
errorDescription?: string;
|
|
502
|
+
copy: ErrorCopy;
|
|
439
503
|
onRetry?: () => void;
|
|
440
504
|
retryLabel: string;
|
|
441
505
|
size?: SizeTokens;
|
|
442
506
|
small: boolean;
|
|
443
507
|
}): ReactNode {
|
|
444
|
-
if (
|
|
445
|
-
|
|
508
|
+
if (copy.refusal) {
|
|
509
|
+
return (
|
|
510
|
+
<EmptyState
|
|
511
|
+
size={size}
|
|
512
|
+
intent="neutral"
|
|
513
|
+
data-async-state="refused"
|
|
514
|
+
title={copy.title}
|
|
515
|
+
description={copy.description}
|
|
516
|
+
/>
|
|
517
|
+
);
|
|
518
|
+
}
|
|
446
519
|
return (
|
|
447
520
|
<EmptyState
|
|
448
521
|
size={size}
|
|
@@ -452,7 +525,7 @@ function defaultErrorBody({
|
|
|
452
525
|
description={copy.description}
|
|
453
526
|
icon={<WarningCircleIcon size={small ? 24 : 32} />}
|
|
454
527
|
action={
|
|
455
|
-
onRetry ? (
|
|
528
|
+
onRetry && copy.retry ? (
|
|
456
529
|
<Button size={small ? "$3" : "$4"} onPress={onRetry}>
|
|
457
530
|
<Text fontWeight="400">{retryLabel}</Text>
|
|
458
531
|
</Button>
|
|
@@ -506,10 +579,14 @@ export function AsyncBoundary({
|
|
|
506
579
|
children,
|
|
507
580
|
layout = "generic",
|
|
508
581
|
onRetry,
|
|
509
|
-
errorTitle
|
|
582
|
+
errorTitle,
|
|
510
583
|
emptyTitle = "No items found",
|
|
511
584
|
emptyDescription,
|
|
512
585
|
errorDescription,
|
|
586
|
+
reason,
|
|
587
|
+
subject,
|
|
588
|
+
live,
|
|
589
|
+
reasons,
|
|
513
590
|
chrome,
|
|
514
591
|
keepPrevious = true,
|
|
515
592
|
size,
|
|
@@ -523,6 +600,18 @@ export function AsyncBoundary({
|
|
|
523
600
|
// MPO-48 / R7: Retry is a §6.1 label — weight 400 on the TEXT NODE via
|
|
524
601
|
// house Text. Alert's actionLabel hardcodes 600; the action slot does not.
|
|
525
602
|
const retryLabel = t("Retry");
|
|
603
|
+
const copy =
|
|
604
|
+
status === "error" && !isReactNodeSlot(error)
|
|
605
|
+
? errorCopy(error, errorTitle ?? "Couldn't load data", errorDescription, {
|
|
606
|
+
reason,
|
|
607
|
+
subject,
|
|
608
|
+
live,
|
|
609
|
+
reasons,
|
|
610
|
+
canRetry: !!onRetry,
|
|
611
|
+
errorTitle,
|
|
612
|
+
errorDescription,
|
|
613
|
+
})
|
|
614
|
+
: null;
|
|
526
615
|
|
|
527
616
|
const emptyBody = defaultEmptyBody({ empty, emptyTitle, emptyDescription, size });
|
|
528
617
|
const settledRef = useRef<{ kind: "data" | "empty"; body: ReactNode } | null>(null);
|
|
@@ -559,14 +648,13 @@ export function AsyncBoundary({
|
|
|
559
648
|
showChrome = true;
|
|
560
649
|
if (isReactNodeSlot(error)) {
|
|
561
650
|
notice = error;
|
|
562
|
-
} else {
|
|
563
|
-
const copy = errorCopy(error, errorTitle, errorDescription);
|
|
651
|
+
} else if (copy) {
|
|
564
652
|
notice = (
|
|
565
653
|
<Alert
|
|
566
|
-
intent="error"
|
|
654
|
+
intent={copy.refusal ? "info" : "error"}
|
|
567
655
|
title={copy.title}
|
|
568
656
|
action={
|
|
569
|
-
onRetry ? (
|
|
657
|
+
onRetry && copy.retry ? (
|
|
570
658
|
<Text
|
|
571
659
|
fontWeight="400"
|
|
572
660
|
color="$color12"
|
|
@@ -587,17 +675,11 @@ export function AsyncBoundary({
|
|
|
587
675
|
}
|
|
588
676
|
body = content ?? settled.body;
|
|
589
677
|
} else if (status === "error") {
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
errorDescription,
|
|
596
|
-
onRetry,
|
|
597
|
-
retryLabel,
|
|
598
|
-
size,
|
|
599
|
-
small,
|
|
600
|
-
});
|
|
678
|
+
if (isReactNodeSlot(error)) {
|
|
679
|
+
body = error;
|
|
680
|
+
} else if (copy) {
|
|
681
|
+
body = defaultErrorBody({ copy, onRetry, retryLabel, size, small });
|
|
682
|
+
}
|
|
601
683
|
} else if (refetching && settled) {
|
|
602
684
|
pending = true;
|
|
603
685
|
displayStatus = settled.kind;
|