@marktiderman/genesis-ui-native 2.5.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-YJV5MMZX.js → chunk-7HWS2JGX.js} +7 -741
- package/dist/chunk-7HWS2JGX.js.map +1 -0
- package/dist/{chunk-IDHW2NOP.js → chunk-C6Q5ZAVT.js} +232 -6
- package/dist/chunk-C6Q5ZAVT.js.map +1 -0
- package/dist/{chunk-YI7XVY2D.js → chunk-ON7WP4T6.js} +13 -479
- package/dist/chunk-ON7WP4T6.js.map +1 -0
- package/dist/chunk-PO3MK6IY.js +1231 -0
- package/dist/chunk-PO3MK6IY.js.map +1 -0
- package/dist/chunk-SXCG2REG.js +479 -0
- package/dist/chunk-SXCG2REG.js.map +1 -0
- package/dist/components/index.d.ts +10 -3
- package/dist/components/index.js +3 -4
- package/dist/data/index.d.ts +482 -4
- package/dist/data/index.js +1480 -17
- package/dist/data/index.js.map +1 -1
- package/dist/inbox-page-DZUR-PJ1.d.ts +199 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +5 -5
- package/dist/layouts/index.d.ts +4 -195
- package/dist/layouts/index.js +3 -3
- package/package.json +2 -2
- package/src/components/__tests__/button-a11y.test.tsx +17 -0
- package/src/components/button.tsx +8 -2
- package/src/components/image.tsx +5 -0
- package/src/data/NativeDetailPanel.tsx +8 -1
- package/src/data/index.ts +1 -0
- package/src/data/resource-view/NativeResourceFormSheet.tsx +305 -0
- package/src/data/resource-view/NativeResourceView.tsx +536 -0
- package/src/data/resource-view/NativeResourceWorkspace.tsx +657 -0
- package/src/data/resource-view/__tests__/native-resource-view.test.tsx +977 -0
- package/src/data/resource-view/__tests__/native-resource-workspace.test.tsx +356 -0
- package/src/data/resource-view/display.tsx +259 -0
- package/src/data/resource-view/index.ts +61 -0
- package/src/data/resource-view/mapping.ts +333 -0
- package/src/data/resource-view/messages.ts +121 -0
- package/src/data/resource-view/use-native-resource-view.ts +309 -0
- package/dist/chunk-EFXCJ5AN.js +0 -234
- package/dist/chunk-EFXCJ5AN.js.map +0 -1
- package/dist/chunk-FIITITXO.js +0 -492
- package/dist/chunk-FIITITXO.js.map +0 -1
- package/dist/chunk-IDHW2NOP.js.map +0 -1
- package/dist/chunk-YI7XVY2D.js.map +0 -1
- package/dist/chunk-YJV5MMZX.js.map +0 -1
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
// NativeResourceWorkspace: one record's screen from a genesis-core workspace (defineWorkspace),
|
|
2
|
+
// in native patterns:
|
|
3
|
+
// - the title in the navigation bar and the actions in the header menu, through
|
|
4
|
+
// `getNativeResourceWorkspaceStackOptions` (the host's <Stack.Screen options>), as SettingsPage does;
|
|
5
|
+
// - up to four sections as a segmented control; more as pushed rows (`onOpenSection`);
|
|
6
|
+
// - a list section → an embedded NativeResourceView of the child view, filtered by the row;
|
|
7
|
+
// - a settings section → inset grouped settings rows, saving only its listed keys, a zero-row
|
|
8
|
+
// save shown inline as a refusal;
|
|
9
|
+
// - readiness → a summary chip, then the checks while any is unmet.
|
|
10
|
+
// A section with neither list nor settings is the screen's own, through `slots[id]`.
|
|
11
|
+
|
|
12
|
+
import { useMemo, useRef, useState, type ReactNode } from "react";
|
|
13
|
+
import { ActionSheetIOS, Alert, Platform, Pressable, ScrollView, View } from "react-native";
|
|
14
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
15
|
+
import { DataWriteError, resourceKeys, useCapabilities, useDataProvider, useViewer, type DataRow, type ResourceContract } from "@marktiderman/genesis-core";
|
|
16
|
+
import { readinessChip, type ReadinessLabels } from "@marktiderman/genesis-core/resource";
|
|
17
|
+
import type { Allowed, ResourceWorkspace as Workspace, WorkspaceHeaderAction } from "@marktiderman/genesis-core/resource-view";
|
|
18
|
+
import { NativeBadge } from "../../components/badge";
|
|
19
|
+
import { NativeButton } from "../../components/button";
|
|
20
|
+
import { NativeIcon } from "../../components/icon";
|
|
21
|
+
import { NativeInlineAlert } from "../../components/inline-alert";
|
|
22
|
+
import { NativeInput } from "../../components/input";
|
|
23
|
+
import { NativeListItem } from "../../components/list-item";
|
|
24
|
+
import { NativeSegmentedControl } from "../../components/segmented-control";
|
|
25
|
+
import { NativeSheet } from "../../components/sheet";
|
|
26
|
+
import { NativeSelect, NativeSelectContent, NativeSelectItem, NativeSelectTrigger } from "../../components/select";
|
|
27
|
+
import { NativeSwitch } from "../../components/switch";
|
|
28
|
+
import { NativeText } from "../../components/text";
|
|
29
|
+
import { NativeTextarea } from "../../components/textarea";
|
|
30
|
+
import { useThemeRoles } from "../../hooks/use-theme-roles";
|
|
31
|
+
import { composeHeaderOptions, type ScreenHeaderProps } from "../../layouts/screen-container";
|
|
32
|
+
import { badgeVariant } from "./display";
|
|
33
|
+
import { formFieldsFor, refusalMessage, type NativeFormField } from "./mapping";
|
|
34
|
+
import { fill } from "./messages";
|
|
35
|
+
import { NativeResourceView, type NativeResourceViewProps } from "./NativeResourceView";
|
|
36
|
+
|
|
37
|
+
type Row = Record<string, unknown>;
|
|
38
|
+
type Values = Record<string, unknown>;
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any contract; the row is what matters here
|
|
40
|
+
type AnyWorkspace<T extends Row> = Workspace<ResourceContract<any>, T & DataRow>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The words NativeResourceWorkspace shows. Override any of them with `messages`. The readiness
|
|
44
|
+
* chip's keys (`ready`, `empty`, `pending`, `problems`, `warnings`) are genesis-core/resource's
|
|
45
|
+
* `readinessChip` labels: unset, they use its defaults.
|
|
46
|
+
*/
|
|
47
|
+
export interface NativeResourceWorkspaceMessages extends ReadinessLabels {
|
|
48
|
+
actions: string;
|
|
49
|
+
cancel: string;
|
|
50
|
+
save: string;
|
|
51
|
+
/** `{noun}`: the record's noun. When a save changed no rows. */
|
|
52
|
+
notChanged: string;
|
|
53
|
+
failed: string;
|
|
54
|
+
/** `{name}`: the action. When the provider can't run actions and the screen gave no `onAction`. */
|
|
55
|
+
noRunner: string;
|
|
56
|
+
checks: string;
|
|
57
|
+
checkDone: string;
|
|
58
|
+
checkTodo: string;
|
|
59
|
+
/** The back row of a section opened from the list of sections. */
|
|
60
|
+
allSections: string;
|
|
61
|
+
notANumber: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const defaultNativeResourceWorkspaceMessages: NativeResourceWorkspaceMessages = {
|
|
65
|
+
actions: "Actions",
|
|
66
|
+
cancel: "Cancel",
|
|
67
|
+
save: "Save",
|
|
68
|
+
notChanged: "This {noun} was not changed.",
|
|
69
|
+
failed: "That didn't work. Try again.",
|
|
70
|
+
noRunner: "{name} isn't available here.",
|
|
71
|
+
checks: "Checks",
|
|
72
|
+
checkDone: "Done",
|
|
73
|
+
checkTodo: "To do",
|
|
74
|
+
allSections: "All sections",
|
|
75
|
+
notANumber: "Enter a number.",
|
|
76
|
+
ready: undefined,
|
|
77
|
+
empty: undefined,
|
|
78
|
+
pending: undefined,
|
|
79
|
+
problems: undefined,
|
|
80
|
+
warnings: undefined,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** Up to this many sections show as a segmented control; more are pushed rows. */
|
|
84
|
+
export const SEGMENTED_SECTIONS_MAX = 4;
|
|
85
|
+
|
|
86
|
+
// ── Header: title and menu ────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
export interface NativeResourceWorkspaceHeaderProps<T extends Row = Row> {
|
|
89
|
+
workspace: AnyWorkspace<T>;
|
|
90
|
+
row: T;
|
|
91
|
+
/** Runs an allowed (and confirmed) action. Default: the provider's `runAction`. */
|
|
92
|
+
onAction?: (name: string, row: T) => unknown;
|
|
93
|
+
/** The screen's own rule: `false` hides an action, a sentence refuses it. Only narrows. */
|
|
94
|
+
can?: (action: string, row: T) => Allowed | boolean;
|
|
95
|
+
messages?: Partial<NativeResourceWorkspaceMessages>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Runs header actions: guards explain, confirms ask, one at a time, failures in an alert. */
|
|
99
|
+
function useHeaderActions<T extends Row>({ workspace, row, onAction, can, messages }: NativeResourceWorkspaceHeaderProps<T>) {
|
|
100
|
+
const words = { ...defaultNativeResourceWorkspaceMessages, ...messages };
|
|
101
|
+
const provider = useDataProvider();
|
|
102
|
+
const queryClient = useQueryClient();
|
|
103
|
+
const view = workspace.view;
|
|
104
|
+
// Actions show only where something can run them, and only those the provider serves when it says.
|
|
105
|
+
const capabilities = useCapabilities(view.resource, { enabled: !onAction && !!provider.capabilities });
|
|
106
|
+
const described = useQuery({
|
|
107
|
+
queryKey: ["__describe", view.resource],
|
|
108
|
+
queryFn: () => provider.describe!(view.resource),
|
|
109
|
+
enabled: !onAction && !!provider.describe,
|
|
110
|
+
staleTime: Infinity,
|
|
111
|
+
});
|
|
112
|
+
// The view's action guards read the viewer (ViewerScope's id, scope and roles).
|
|
113
|
+
const viewer = useViewer();
|
|
114
|
+
const actions = workspace.headerFor(row as T & DataRow, {
|
|
115
|
+
viewer,
|
|
116
|
+
runnable: Boolean(onAction) || (Boolean(provider.runAction) && capabilities.data?.actions !== false),
|
|
117
|
+
...(!onAction && described.data?.actions ? { served: described.data.actions.map((a) => a.name) } : {}),
|
|
118
|
+
...(can ? { can: (name: string) => can(name, row) } : {}),
|
|
119
|
+
}).actions;
|
|
120
|
+
// One action at a time: a second press while one runs does nothing.
|
|
121
|
+
const running = useRef(false);
|
|
122
|
+
const [pending, setPending] = useState(false);
|
|
123
|
+
const run = async (a: WorkspaceHeaderAction) => {
|
|
124
|
+
if (running.current) return;
|
|
125
|
+
running.current = true;
|
|
126
|
+
setPending(true);
|
|
127
|
+
try {
|
|
128
|
+
if (onAction) await onAction(a.name, row);
|
|
129
|
+
else if (provider.runAction) await provider.runAction(view.resource, { id: workspace.recordId(row as T & DataRow), action: a.name });
|
|
130
|
+
else throw new DataWriteError("error", fill(words.noRunner, { name: a.label }));
|
|
131
|
+
await queryClient.invalidateQueries({ queryKey: [...resourceKeys(view.resource).all] });
|
|
132
|
+
} catch (error) {
|
|
133
|
+
Alert.alert(a.label, refusalMessage(error, words.failed));
|
|
134
|
+
} finally {
|
|
135
|
+
running.current = false;
|
|
136
|
+
setPending(false);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const press = (a: WorkspaceHeaderAction) => {
|
|
140
|
+
if (running.current) return;
|
|
141
|
+
if (a.allowed !== true) return Alert.alert(a.label, a.allowed);
|
|
142
|
+
if (!a.confirm) return void run(a);
|
|
143
|
+
// Two buttons: within Android's three (React Native docs, Alert).
|
|
144
|
+
Alert.alert(a.confirm, a.description, [
|
|
145
|
+
{ text: words.cancel, style: "cancel" },
|
|
146
|
+
{ text: a.label, style: a.destructive ? "destructive" : "default", onPress: () => void run(a) },
|
|
147
|
+
]);
|
|
148
|
+
};
|
|
149
|
+
return { actions, press, pending, words };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Prefixes accessibility action names, so an action named "activate" never replaces the button's own. */
|
|
153
|
+
const A11Y_PREFIX = "action:";
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The header menu: a button that lists the workspace's actions. On iOS an action sheet
|
|
157
|
+
* (ActionSheetIOS, destructive ones in red); elsewhere a sheet of rows, since Android's alert
|
|
158
|
+
* shows at most three buttons. A refused action stays listed and explains why when pressed.
|
|
159
|
+
*/
|
|
160
|
+
export function NativeResourceWorkspaceMenu<T extends Row = Row>(props: NativeResourceWorkspaceHeaderProps<T> & { testID?: string }) {
|
|
161
|
+
const { testID = "native-resource-workspace-menu" } = props;
|
|
162
|
+
const { actions, press, pending, words } = useHeaderActions(props);
|
|
163
|
+
const [open, setOpen] = useState(false);
|
|
164
|
+
if (actions.length === 0) return null;
|
|
165
|
+
const labelOf = (a: WorkspaceHeaderAction) => (a.allowed === true ? a.label : `${a.label} (${a.allowed})`);
|
|
166
|
+
const show = () => {
|
|
167
|
+
if (Platform.OS === "ios") {
|
|
168
|
+
const destructive = actions.flatMap((a, i) => (a.destructive ? [i] : []));
|
|
169
|
+
ActionSheetIOS.showActionSheetWithOptions(
|
|
170
|
+
{
|
|
171
|
+
title: props.workspace.headerFor(props.row as T & DataRow).title,
|
|
172
|
+
options: [...actions.map(labelOf), words.cancel],
|
|
173
|
+
cancelButtonIndex: actions.length,
|
|
174
|
+
...(destructive.length ? { destructiveButtonIndex: destructive } : {}),
|
|
175
|
+
},
|
|
176
|
+
(index) => {
|
|
177
|
+
const a = actions[index];
|
|
178
|
+
if (a) press(a);
|
|
179
|
+
},
|
|
180
|
+
);
|
|
181
|
+
} else {
|
|
182
|
+
setOpen(true);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
return (
|
|
186
|
+
<>
|
|
187
|
+
<Pressable
|
|
188
|
+
testID={testID}
|
|
189
|
+
accessibilityRole="button"
|
|
190
|
+
accessibilityLabel={words.actions}
|
|
191
|
+
accessibilityState={{ disabled: pending, busy: pending }}
|
|
192
|
+
disabled={pending}
|
|
193
|
+
accessibilityActions={actions.map((a) => ({ name: `${A11Y_PREFIX}${a.name}`, label: a.allowed === true ? a.label : `${a.label}: ${a.allowed}` }))}
|
|
194
|
+
onAccessibilityAction={(event) => {
|
|
195
|
+
const name = event.nativeEvent.actionName;
|
|
196
|
+
const a = name.startsWith(A11Y_PREFIX) ? actions.find((x) => `${A11Y_PREFIX}${x.name}` === name) : undefined;
|
|
197
|
+
if (a) press(a);
|
|
198
|
+
}}
|
|
199
|
+
onPress={show}
|
|
200
|
+
hitSlop={8}
|
|
201
|
+
className="min-h-[44px] min-w-[44px] items-center justify-center"
|
|
202
|
+
>
|
|
203
|
+
<NativeIcon name="more" label={words.actions} />
|
|
204
|
+
</Pressable>
|
|
205
|
+
<NativeSheet open={open} onOpenChange={setOpen} testID={`${testID}-sheet`}>
|
|
206
|
+
<View style={{ paddingVertical: 8 }}>
|
|
207
|
+
{actions.map((a) => (
|
|
208
|
+
<Pressable
|
|
209
|
+
key={a.name}
|
|
210
|
+
testID={`${testID}-${a.name}`}
|
|
211
|
+
accessibilityRole="button"
|
|
212
|
+
accessibilityLabel={a.allowed === true ? a.label : `${a.label}: ${a.allowed}`}
|
|
213
|
+
onPress={() => {
|
|
214
|
+
setOpen(false);
|
|
215
|
+
press(a);
|
|
216
|
+
}}
|
|
217
|
+
className="min-h-[48px] justify-center px-4"
|
|
218
|
+
>
|
|
219
|
+
<NativeText preset="body" className={a.destructive ? "text-destructive" : undefined}>
|
|
220
|
+
{a.label}
|
|
221
|
+
</NativeText>
|
|
222
|
+
{a.allowed !== true ? (
|
|
223
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
224
|
+
{a.allowed}
|
|
225
|
+
</NativeText>
|
|
226
|
+
) : null}
|
|
227
|
+
</Pressable>
|
|
228
|
+
))}
|
|
229
|
+
<Pressable testID={`${testID}-cancel`} accessibilityRole="button" onPress={() => setOpen(false)} className="min-h-[48px] justify-center px-4">
|
|
230
|
+
<NativeText preset="body" className="text-muted-foreground">
|
|
231
|
+
{words.cancel}
|
|
232
|
+
</NativeText>
|
|
233
|
+
</Pressable>
|
|
234
|
+
</View>
|
|
235
|
+
</NativeSheet>
|
|
236
|
+
</>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* `<Stack.Screen options>` for a workspace: the record's title in the navigation bar and the
|
|
242
|
+
* actions in the header menu.
|
|
243
|
+
*
|
|
244
|
+
* ```tsx
|
|
245
|
+
* <Stack.Screen options={getNativeResourceWorkspaceStackOptions({ workspace: brandWorkspace, row: brand })} />
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export function getNativeResourceWorkspaceStackOptions<T extends Row>(
|
|
249
|
+
props: NativeResourceWorkspaceHeaderProps<T> & Omit<ScreenHeaderProps, "headerTitle">,
|
|
250
|
+
): Record<string, unknown> {
|
|
251
|
+
const { workspace, row, onAction, can, messages, ...header } = props;
|
|
252
|
+
const menu = { workspace, row, ...(onAction ? { onAction } : {}), ...(can ? { can } : {}), ...(messages ? { messages } : {}) };
|
|
253
|
+
return composeHeaderOptions({
|
|
254
|
+
...header,
|
|
255
|
+
headerTitle: workspace.headerFor(row as T & DataRow).title,
|
|
256
|
+
headerRight: header.headerRight ?? (workspace.headerActions.length ? () => <NativeResourceWorkspaceMenu {...menu} /> : undefined),
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ── Screen ────────────────────────────────────────────────────────────
|
|
261
|
+
|
|
262
|
+
export interface NativeResourceWorkspaceProps<T extends Row = Row> {
|
|
263
|
+
workspace: AnyWorkspace<T>;
|
|
264
|
+
/** The record, as the screen loaded it. A save refreshes it through the cache. */
|
|
265
|
+
row: T;
|
|
266
|
+
/**
|
|
267
|
+
* The section to show. With up to four sections, the segmented control's value (default the
|
|
268
|
+
* first). With more, the pushed section; without it, the list of sections.
|
|
269
|
+
*/
|
|
270
|
+
section?: string;
|
|
271
|
+
/** The segmented control changed section. */
|
|
272
|
+
onSectionChange?: (id: string) => void;
|
|
273
|
+
/**
|
|
274
|
+
* With more than four sections: push that section's route (the pushed screen passes `section`).
|
|
275
|
+
* Without it, the section opens in place with an "All sections" row to go back.
|
|
276
|
+
*/
|
|
277
|
+
onOpenSection?: (id: string) => void;
|
|
278
|
+
/** After a settings section saved, with the stored row. */
|
|
279
|
+
onSaved?: (row: T) => void;
|
|
280
|
+
/** Above the sections: a switcher between records. */
|
|
281
|
+
switcher?: ReactNode;
|
|
282
|
+
/** The screen's own sections, by id. */
|
|
283
|
+
slots?: Partial<Record<string, ReactNode>>;
|
|
284
|
+
/** More props for a list section's NativeResourceView, by section id. */
|
|
285
|
+
lists?: Partial<Record<string, Partial<Omit<NativeResourceViewProps, "view" | "filter">>>>;
|
|
286
|
+
messages?: Partial<NativeResourceWorkspaceMessages>;
|
|
287
|
+
testID?: string;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* One record's screen from a workspace:
|
|
292
|
+
*
|
|
293
|
+
* ```tsx
|
|
294
|
+
* <Stack.Screen options={getNativeResourceWorkspaceStackOptions({ workspace: brandWorkspace, row: brand })} />
|
|
295
|
+
* <NativeResourceWorkspace workspace={brandWorkspace} row={brand} />
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
export function NativeResourceWorkspace<T extends Row = Row>(props: NativeResourceWorkspaceProps<T>) {
|
|
299
|
+
const { workspace, row, section, onSectionChange, onOpenSection, onSaved, switcher, slots, lists, messages, testID = "native-resource-workspace" } = props;
|
|
300
|
+
const words = { ...defaultNativeResourceWorkspaceMessages, ...messages };
|
|
301
|
+
const colors = useThemeRoles();
|
|
302
|
+
const queryClient = useQueryClient();
|
|
303
|
+
const record = row as T & DataRow;
|
|
304
|
+
const sections = workspace.sections;
|
|
305
|
+
const many = sections.length > SEGMENTED_SECTIONS_MAX;
|
|
306
|
+
const [local, setLocal] = useState<string | undefined>(undefined);
|
|
307
|
+
const activeId = section ?? local ?? (many ? undefined : sections[0]?.id);
|
|
308
|
+
const active = activeId ? workspace.section(activeId) : undefined;
|
|
309
|
+
const readiness = workspace.readinessFor(record);
|
|
310
|
+
const summary = workspace.readinessSummary(record);
|
|
311
|
+
const chip = summary ? readinessChip(summary, words) : null;
|
|
312
|
+
|
|
313
|
+
const choose = (id: string) => {
|
|
314
|
+
if (many && onOpenSection) return onOpenSection(id);
|
|
315
|
+
setLocal(id);
|
|
316
|
+
onSectionChange?.(id);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const top = (
|
|
320
|
+
<View style={{ paddingHorizontal: 16, paddingTop: 8, gap: 12 }}>
|
|
321
|
+
{switcher}
|
|
322
|
+
{chip ? (
|
|
323
|
+
<View style={{ flexDirection: "row" }}>
|
|
324
|
+
<NativeBadge testID={`${testID}-readiness`} variant={badgeVariant(chip.tone)}>
|
|
325
|
+
{chip.label}
|
|
326
|
+
</NativeBadge>
|
|
327
|
+
</View>
|
|
328
|
+
) : null}
|
|
329
|
+
{readiness.some((r) => !r.ok) ? (
|
|
330
|
+
<Group testID={`${testID}-checks`} title={words.checks}>
|
|
331
|
+
{readiness.map((r, i) => (
|
|
332
|
+
<Cell key={r.key} last={i === readiness.length - 1}>
|
|
333
|
+
<NativeListItem
|
|
334
|
+
testID={`${testID}-check-${i}`}
|
|
335
|
+
title={r.title}
|
|
336
|
+
subtitle={r.ok ? undefined : r.reason}
|
|
337
|
+
className="bg-transparent"
|
|
338
|
+
trailing={
|
|
339
|
+
<NativeBadge testID={`${testID}-check-${i}-chip`} variant={badgeVariant(r.tone)}>
|
|
340
|
+
{r.ok ? words.checkDone : words.checkTodo}
|
|
341
|
+
</NativeBadge>
|
|
342
|
+
}
|
|
343
|
+
/>
|
|
344
|
+
</Cell>
|
|
345
|
+
))}
|
|
346
|
+
</Group>
|
|
347
|
+
) : null}
|
|
348
|
+
{!many ? (
|
|
349
|
+
<NativeSegmentedControl
|
|
350
|
+
testID={`${testID}-sections`}
|
|
351
|
+
options={sections.map((s) => ({ value: s.id, label: s.label }))}
|
|
352
|
+
value={activeId ?? ""}
|
|
353
|
+
onChange={choose}
|
|
354
|
+
/>
|
|
355
|
+
) : null}
|
|
356
|
+
</View>
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
let body: ReactNode = null;
|
|
360
|
+
if (many && !active) {
|
|
361
|
+
body = (
|
|
362
|
+
<ScrollView contentContainerStyle={{ padding: 16 }}>
|
|
363
|
+
<Group testID={`${testID}-sections`}>
|
|
364
|
+
{sections.map((s, i) => (
|
|
365
|
+
<Cell key={s.id} last={i === sections.length - 1}>
|
|
366
|
+
<NativeListItem testID={`${testID}-section-${s.id}`} title={s.label} showChevron onPress={() => choose(s.id)} className="bg-transparent" />
|
|
367
|
+
</Cell>
|
|
368
|
+
))}
|
|
369
|
+
</Group>
|
|
370
|
+
</ScrollView>
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
// A section opened in place from the list of sections: a row back to the list.
|
|
374
|
+
const back =
|
|
375
|
+
many && local !== undefined && section === undefined ? (
|
|
376
|
+
<View style={{ paddingHorizontal: 16, paddingTop: 8 }}>
|
|
377
|
+
<Group testID={`${testID}-back`}>
|
|
378
|
+
<Cell last>
|
|
379
|
+
<NativeListItem
|
|
380
|
+
testID={`${testID}-all-sections`}
|
|
381
|
+
title={words.allSections}
|
|
382
|
+
leading={<NativeIcon name="chevron-left" />}
|
|
383
|
+
onPress={() => setLocal(undefined)}
|
|
384
|
+
className="bg-transparent"
|
|
385
|
+
/>
|
|
386
|
+
</Cell>
|
|
387
|
+
</Group>
|
|
388
|
+
</View>
|
|
389
|
+
) : null;
|
|
390
|
+
if (active?.kind === "list") {
|
|
391
|
+
body = (
|
|
392
|
+
<NativeResourceView
|
|
393
|
+
key={active.id}
|
|
394
|
+
view={active.view}
|
|
395
|
+
testID={`${testID}-list`}
|
|
396
|
+
{...lists?.[active.id]}
|
|
397
|
+
title={false}
|
|
398
|
+
filter={workspace.listFilter(active.id, record)}
|
|
399
|
+
/>
|
|
400
|
+
);
|
|
401
|
+
} else if (active?.kind === "settings") {
|
|
402
|
+
body = (
|
|
403
|
+
<ScrollView contentContainerStyle={{ padding: 16 }} keyboardShouldPersistTaps="handled">
|
|
404
|
+
<SettingsGroup
|
|
405
|
+
// A fresh draft per record and section.
|
|
406
|
+
key={`${String(workspace.recordId(record))}:${active.id}`}
|
|
407
|
+
workspace={workspace}
|
|
408
|
+
sectionId={active.id}
|
|
409
|
+
row={record}
|
|
410
|
+
words={words}
|
|
411
|
+
onSaved={(saved) => {
|
|
412
|
+
void queryClient.invalidateQueries({ queryKey: [...resourceKeys(workspace.view.resource).all] });
|
|
413
|
+
onSaved?.(saved as T);
|
|
414
|
+
}}
|
|
415
|
+
testID={`${testID}-settings`}
|
|
416
|
+
/>
|
|
417
|
+
</ScrollView>
|
|
418
|
+
);
|
|
419
|
+
} else if (active) {
|
|
420
|
+
body = slots?.[active.id] ?? null;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return (
|
|
424
|
+
<View testID={testID} style={{ flex: 1, backgroundColor: colors.groupedBackground }}>
|
|
425
|
+
{top}
|
|
426
|
+
{back}
|
|
427
|
+
<View style={{ flex: 1 }}>{body}</View>
|
|
428
|
+
</View>
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// ── Settings ──────────────────────────────────────────────────────────
|
|
433
|
+
|
|
434
|
+
function SettingsGroup({
|
|
435
|
+
workspace,
|
|
436
|
+
sectionId,
|
|
437
|
+
row,
|
|
438
|
+
words,
|
|
439
|
+
onSaved,
|
|
440
|
+
testID,
|
|
441
|
+
}: {
|
|
442
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any contract
|
|
443
|
+
workspace: Workspace<ResourceContract<any>, any>;
|
|
444
|
+
sectionId: string;
|
|
445
|
+
row: DataRow;
|
|
446
|
+
words: NativeResourceWorkspaceMessages;
|
|
447
|
+
onSaved: (row: DataRow) => void;
|
|
448
|
+
testID: string;
|
|
449
|
+
}) {
|
|
450
|
+
const provider = useDataProvider();
|
|
451
|
+
const view = workspace.view;
|
|
452
|
+
// The view's edit-form mapping (the form sheet's), narrowed to this section's keys in their order.
|
|
453
|
+
const fields = useMemo(() => {
|
|
454
|
+
const byKey = new Map(formFieldsFor(view, "edit").map((f) => [f.key, f]));
|
|
455
|
+
return workspace.settingsFields(sectionId).map((f) => byKey.get(f.field.name)).filter((f): f is NativeFormField => !!f && !f.readOnly);
|
|
456
|
+
}, [workspace, view, sectionId]);
|
|
457
|
+
// The view's update guard, for this viewer: when it refuses, the settings are read-only with its reason.
|
|
458
|
+
const viewer = useViewer();
|
|
459
|
+
const locked = workspace.settingsLocked(row, viewer);
|
|
460
|
+
const [draft, setDraft] = useState<Values>(() => Object.fromEntries(fields.map((f) => [f.key, row[f.key]])));
|
|
461
|
+
const [invalid, setInvalid] = useState<Record<string, string>>({});
|
|
462
|
+
const [saving, setSaving] = useState(false);
|
|
463
|
+
const [refusal, setRefusal] = useState<string | null>(null);
|
|
464
|
+
const shown = fields.filter((f) => !f.visible || f.visible(draft));
|
|
465
|
+
const changed: Values = {};
|
|
466
|
+
for (const f of shown) if (draft[f.key] !== row[f.key]) changed[f.key] = draft[f.key];
|
|
467
|
+
const dirty = Object.keys(changed).length > 0 && !shown.some((f) => invalid[f.key]);
|
|
468
|
+
|
|
469
|
+
const set = (key: string, value: unknown, error?: string) => {
|
|
470
|
+
setRefusal(null);
|
|
471
|
+
setInvalid(({ [key]: _old, ...rest }) => (error ? { ...rest, [key]: error } : rest));
|
|
472
|
+
setDraft((d) => ({ ...d, [key]: value }));
|
|
473
|
+
};
|
|
474
|
+
const save = async () => {
|
|
475
|
+
setSaving(true);
|
|
476
|
+
setRefusal(null);
|
|
477
|
+
try {
|
|
478
|
+
// Only the section's listed keys, never anything else the row or draft holds.
|
|
479
|
+
const result = await provider.update(view.resource, { id: workspace.recordId(row), data: workspace.settingsPatch(sectionId, changed) });
|
|
480
|
+
// A write that matched no rows is a refusal, never a silent success.
|
|
481
|
+
if (!result || result.data === null || result.data === undefined) setRefusal(fill(words.notChanged, { noun: view.noun }));
|
|
482
|
+
else onSaved(result.data as DataRow);
|
|
483
|
+
} catch (error) {
|
|
484
|
+
setRefusal(refusalMessage(error, words.failed));
|
|
485
|
+
} finally {
|
|
486
|
+
setSaving(false);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
return (
|
|
491
|
+
<View style={{ gap: 12 }}>
|
|
492
|
+
{refusal ? <NativeInlineAlert testID={`${testID}-refusal`} variant="destructive" description={refusal} /> : null}
|
|
493
|
+
{locked !== null ? <NativeInlineAlert testID={`${testID}-locked`} variant="info" description={locked} /> : null}
|
|
494
|
+
<Group testID={`${testID}-group`} title={workspace.section(sectionId)?.label}>
|
|
495
|
+
{shown.map((f, i) => (
|
|
496
|
+
<Cell key={f.key} last={i === shown.length - 1}>
|
|
497
|
+
<SettingRow
|
|
498
|
+
field={f}
|
|
499
|
+
nullable={Boolean(view.field(f.key)?.nullable)}
|
|
500
|
+
value={draft[f.key]}
|
|
501
|
+
error={invalid[f.key]}
|
|
502
|
+
disabled={locked !== null}
|
|
503
|
+
words={words}
|
|
504
|
+
onChange={(v, error) => set(f.key, v, error)}
|
|
505
|
+
testID={`${testID}-${f.key}`}
|
|
506
|
+
/>
|
|
507
|
+
</Cell>
|
|
508
|
+
))}
|
|
509
|
+
</Group>
|
|
510
|
+
{locked === null ? (
|
|
511
|
+
<NativeButton testID={`${testID}-save`} disabled={!dirty || saving} loading={saving} onPress={() => void save()}>
|
|
512
|
+
{words.save}
|
|
513
|
+
</NativeButton>
|
|
514
|
+
) : null}
|
|
515
|
+
</View>
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* A number as typed: "1.5" or, with a comma as the decimal mark, "1,5". Anything else (an empty
|
|
521
|
+
* string aside) is an error, never NaN.
|
|
522
|
+
*/
|
|
523
|
+
export function parseSettingNumber(text: string): number | null {
|
|
524
|
+
const t = text.trim();
|
|
525
|
+
const normalised = /^-?\d+,\d+$/.test(t) ? t.replace(",", ".") : t;
|
|
526
|
+
const n = normalised === "" ? Number.NaN : Number(normalised);
|
|
527
|
+
return Number.isFinite(n) ? n : null;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function SettingRow({
|
|
531
|
+
field: f,
|
|
532
|
+
nullable,
|
|
533
|
+
value,
|
|
534
|
+
error,
|
|
535
|
+
disabled,
|
|
536
|
+
words,
|
|
537
|
+
onChange,
|
|
538
|
+
testID,
|
|
539
|
+
}: {
|
|
540
|
+
field: NativeFormField;
|
|
541
|
+
nullable: boolean;
|
|
542
|
+
value: unknown;
|
|
543
|
+
error?: string | undefined;
|
|
544
|
+
disabled: boolean;
|
|
545
|
+
words: NativeResourceWorkspaceMessages;
|
|
546
|
+
onChange: (v: unknown, error?: string) => void;
|
|
547
|
+
testID: string;
|
|
548
|
+
}) {
|
|
549
|
+
// An emptied field is null when the column allows it, as on the web.
|
|
550
|
+
const empty = nullable ? null : "";
|
|
551
|
+
const [typed, setTyped] = useState(() => (value == null ? "" : String(value)));
|
|
552
|
+
if (f.kind === "switch") {
|
|
553
|
+
return (
|
|
554
|
+
<NativeListItem
|
|
555
|
+
testID={testID}
|
|
556
|
+
title={f.label}
|
|
557
|
+
className="bg-transparent"
|
|
558
|
+
disabled={disabled}
|
|
559
|
+
trailing={<NativeSwitch testID={`${testID}-input`} aria-label={f.label} checked={value === true} disabled={disabled} onCheckedChange={(v) => onChange(v)} />}
|
|
560
|
+
/>
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
const text = value == null ? "" : String(value);
|
|
564
|
+
let control: ReactNode;
|
|
565
|
+
if (f.kind === "segmented") {
|
|
566
|
+
control = <NativeSegmentedControl testID={`${testID}-input`} options={f.options ?? []} value={text} disabled={disabled} onChange={(v) => onChange(v)} />;
|
|
567
|
+
} else if (f.kind === "select") {
|
|
568
|
+
control = (
|
|
569
|
+
<NativeSelect value={text} onValueChange={(v) => onChange(v)}>
|
|
570
|
+
<NativeSelectTrigger testID={`${testID}-input`} placeholder={f.label} disabled={disabled} />
|
|
571
|
+
<NativeSelectContent>
|
|
572
|
+
{(f.options ?? []).map((o) => (
|
|
573
|
+
<NativeSelectItem key={o.value} value={o.value}>
|
|
574
|
+
{o.label}
|
|
575
|
+
</NativeSelectItem>
|
|
576
|
+
))}
|
|
577
|
+
</NativeSelectContent>
|
|
578
|
+
</NativeSelect>
|
|
579
|
+
);
|
|
580
|
+
} else if (f.kind === "textarea") {
|
|
581
|
+
control = (
|
|
582
|
+
<NativeTextarea
|
|
583
|
+
testID={`${testID}-input`}
|
|
584
|
+
accessibilityLabel={f.label}
|
|
585
|
+
editable={!disabled}
|
|
586
|
+
value={text}
|
|
587
|
+
maxLength={f.max}
|
|
588
|
+
onChangeText={(t) => onChange(t === "" ? empty : t)}
|
|
589
|
+
/>
|
|
590
|
+
);
|
|
591
|
+
} else if (f.kind === "number") {
|
|
592
|
+
control = (
|
|
593
|
+
<NativeInput
|
|
594
|
+
testID={`${testID}-input`}
|
|
595
|
+
accessibilityLabel={f.label}
|
|
596
|
+
editable={!disabled}
|
|
597
|
+
value={typed}
|
|
598
|
+
keyboardType="decimal-pad"
|
|
599
|
+
error={error}
|
|
600
|
+
onChangeText={(t) => {
|
|
601
|
+
setTyped(t);
|
|
602
|
+
if (t.trim() === "") return onChange(empty);
|
|
603
|
+
const n = parseSettingNumber(t);
|
|
604
|
+
return n === null ? onChange(t, words.notANumber) : onChange(n);
|
|
605
|
+
}}
|
|
606
|
+
/>
|
|
607
|
+
);
|
|
608
|
+
} else {
|
|
609
|
+
control = (
|
|
610
|
+
<NativeInput
|
|
611
|
+
testID={`${testID}-input`}
|
|
612
|
+
accessibilityLabel={f.label}
|
|
613
|
+
editable={!disabled}
|
|
614
|
+
value={text}
|
|
615
|
+
maxLength={f.max}
|
|
616
|
+
autoCapitalize={f.kind === "text" ? "sentences" : "none"}
|
|
617
|
+
keyboardType={f.kind === "email" ? "email-address" : f.kind === "url" ? "url" : "default"}
|
|
618
|
+
onChangeText={(t) => onChange(t === "" ? empty : t)}
|
|
619
|
+
/>
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
return (
|
|
623
|
+
<View testID={testID} style={{ paddingHorizontal: 16, paddingVertical: 10, gap: 6, opacity: disabled ? 0.6 : 1 }}>
|
|
624
|
+
<NativeText preset="body-sm" className="font-medium">
|
|
625
|
+
{f.label}
|
|
626
|
+
</NativeText>
|
|
627
|
+
{control}
|
|
628
|
+
</View>
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// ── Inset grouped pieces (SettingsPage's look) ────────────────────────
|
|
633
|
+
|
|
634
|
+
function Group({ title, children, testID }: { title?: string | undefined; children: ReactNode; testID: string }) {
|
|
635
|
+
const colors = useThemeRoles();
|
|
636
|
+
const ios = Platform.OS === "ios";
|
|
637
|
+
return (
|
|
638
|
+
<View testID={testID} style={{ gap: 6 }}>
|
|
639
|
+
{title ? (
|
|
640
|
+
<NativeText preset="caption" accessibilityRole="header" className={ios ? "uppercase text-muted-foreground px-4" : "text-primary px-4"}>
|
|
641
|
+
{title}
|
|
642
|
+
</NativeText>
|
|
643
|
+
) : null}
|
|
644
|
+
<View style={{ backgroundColor: colors.surface, borderRadius: ios ? 10 : 0, overflow: "hidden" }}>{children}</View>
|
|
645
|
+
</View>
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function Cell({ last, children }: { last: boolean; children: ReactNode }) {
|
|
650
|
+
const colors = useThemeRoles();
|
|
651
|
+
return (
|
|
652
|
+
<View>
|
|
653
|
+
{children}
|
|
654
|
+
{!last ? <View style={{ height: 0.5, marginLeft: 16, backgroundColor: colors.separator }} /> : null}
|
|
655
|
+
</View>
|
|
656
|
+
);
|
|
657
|
+
}
|