@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,536 @@
|
|
|
1
|
+
// NativeResourceView: a native list screen from a genesis-core view, in one line
|
|
2
|
+
// (docs/RESOURCE-VIEW.md, "Native"). It reads the same view as the web's ResourceView:
|
|
3
|
+
// - `view.summary(row)` → NativeListItem (title, subtitle, a chip); `view.columns()` → the peek;
|
|
4
|
+
// - `definition.groupBy` → SectionList sections (sorted on the server first), inset grouped on iOS;
|
|
5
|
+
// - `view.can()` → swipe actions (InboxPage's shape: long press and accessibility actions,
|
|
6
|
+
// the `swipeable` slot for the gesture), every refusal with its reason;
|
|
7
|
+
// - `view.form(mode)` + `view.writable(mode)` → the form sheet, over the row fetched by id,
|
|
8
|
+
// sending only the writable fields the person changed.
|
|
9
|
+
// Paging is by cursor on `onEndReached`, search is debounced 300 ms, a delete is optimistic and
|
|
10
|
+
// comes back if the server refuses, and a refusal shows inline in plain words.
|
|
11
|
+
//
|
|
12
|
+
// List performance follows React Native's "Optimizing FlatList configuration" (SectionList is a
|
|
13
|
+
// VirtualizedList and a PureComponent): a memoised row component, a stable `renderItem` and
|
|
14
|
+
// `keyExtractor`, and `extraData` only for what rows read besides their item.
|
|
15
|
+
|
|
16
|
+
import { memo, useCallback, useMemo, useRef, useState, type ComponentType, type ReactNode } from "react";
|
|
17
|
+
import { Alert, Platform, Pressable, SectionList, View } from "react-native";
|
|
18
|
+
import { SafeAreaView } from "react-native-safe-area-context";
|
|
19
|
+
import { encodeRecordKey, type BaseRecord, type DataRow } from "@marktiderman/genesis-core";
|
|
20
|
+
import { isTimeZone, personTimeZone } from "@marktiderman/genesis-core/dates";
|
|
21
|
+
import { useOne, useOptionalViewerScope } from "@marktiderman/genesis-core/hooks";
|
|
22
|
+
import type { RowPicture } from "@marktiderman/genesis-core/resource-view";
|
|
23
|
+
import { NativeAvatar } from "../../components/avatar";
|
|
24
|
+
import { NativeBadge } from "../../components/badge";
|
|
25
|
+
import { NativeImage } from "../../components/image";
|
|
26
|
+
import { NativeButton } from "../../components/button";
|
|
27
|
+
import { NativeEmptyState } from "../../components/empty-state";
|
|
28
|
+
import { NativeInlineAlert } from "../../components/inline-alert";
|
|
29
|
+
import { NativeListItem } from "../../components/list-item";
|
|
30
|
+
import { NativeLoadingState } from "../../components/loading-state";
|
|
31
|
+
import { NativeRefreshControl } from "../../components/refresh-control";
|
|
32
|
+
import { NativeSearchBar } from "../../components/search-bar";
|
|
33
|
+
import { NativeSectionListHeader } from "../../components/section-list";
|
|
34
|
+
import { NativeSheet } from "../../components/sheet";
|
|
35
|
+
import { NativeText } from "../../components/text";
|
|
36
|
+
import { useThemeRoles } from "../../hooks/use-theme-roles";
|
|
37
|
+
import { showInboxActions, type InboxPageSlotMap } from "../../layouts/inbox-page";
|
|
38
|
+
import { NativeDetailPanel } from "../NativeDetailPanel";
|
|
39
|
+
import {
|
|
40
|
+
badgeVariant,
|
|
41
|
+
DisplayTimeZonesProvider,
|
|
42
|
+
useDisplayCell,
|
|
43
|
+
useNativeResourceViewMessages,
|
|
44
|
+
type DisplayTimeZones,
|
|
45
|
+
} from "./display";
|
|
46
|
+
import type { AnyResourceView, NativeResourceRowItem, NativeResourceSection, NativeResourceSwipeAction } from "./mapping";
|
|
47
|
+
import { fill } from "./messages";
|
|
48
|
+
import { NativeResourceFormSheet } from "./NativeResourceFormSheet";
|
|
49
|
+
import {
|
|
50
|
+
useNativeResourceView,
|
|
51
|
+
type NativeResourceViewOptions,
|
|
52
|
+
type UseNativeResourceViewReturn,
|
|
53
|
+
} from "./use-native-resource-view";
|
|
54
|
+
|
|
55
|
+
type Row = Record<string, unknown>;
|
|
56
|
+
|
|
57
|
+
/** The gesture wrapper around a row, the same props as InboxPage's `swipeable` slot. */
|
|
58
|
+
export type NativeResourceSwipeableProps = Omit<InboxPageSlotMap["swipeable"], "actions"> & {
|
|
59
|
+
actions: NativeResourceSwipeAction[];
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export interface NativeResourceViewProps<T extends Row = Row> extends NativeResourceViewOptions<T> {
|
|
63
|
+
view: AnyResourceView<T>;
|
|
64
|
+
/** The heading above the list. Default the view's title; `false` when the navigation bar shows it. */
|
|
65
|
+
title?: string | false;
|
|
66
|
+
subtitle?: string;
|
|
67
|
+
/**
|
|
68
|
+
* A row was pressed: push its detail route (`router.push(\`/brands/${row.id}\`)`), so deep links
|
|
69
|
+
* and notification taps land on the same screen. Without it, a press opens a peek sheet.
|
|
70
|
+
*/
|
|
71
|
+
onRowPress?: (row: T) => void;
|
|
72
|
+
/** Wraps each row that has actions in a swipe gesture (e.g. gesture-handler's ReanimatedSwipeable). */
|
|
73
|
+
swipeable?: ComponentType<NativeResourceSwipeableProps>;
|
|
74
|
+
/** Above the list, under the search (a filter, a call to action). */
|
|
75
|
+
header?: ReactNode;
|
|
76
|
+
/** Replaces the empty state. */
|
|
77
|
+
emptyState?: ReactNode;
|
|
78
|
+
/** Hide the search box. Shown by default when the view has something to search. */
|
|
79
|
+
searchable?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* The practice's IANA zone, for columns with `timeZone: "practice"`. Timestamps otherwise read
|
|
82
|
+
* on the viewer's clock (ViewerScope's zone, else the device's).
|
|
83
|
+
*/
|
|
84
|
+
practiceTimeZone?: string;
|
|
85
|
+
testID?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** UIKit's inset grouped margin on a phone, in points (as InboxPage and SettingsPage). */
|
|
89
|
+
const IOS_GROUP_MARGIN = 16;
|
|
90
|
+
/** The group's corner radius: computed once, the OS version doesn't change while the app runs. */
|
|
91
|
+
const GROUP_RADIUS = Number.parseFloat(String(Platform.Version)) >= 26 ? 26 : 10;
|
|
92
|
+
const IOS = Platform.OS === "ios";
|
|
93
|
+
|
|
94
|
+
// Rows are keyed by the view's key fields (`keyFields`), so composite keys never collide.
|
|
95
|
+
const recordKey = (view: AnyResourceView<Row>, row: Row) => {
|
|
96
|
+
try {
|
|
97
|
+
return String(encodeRecordKey(view.contract, row as DataRow));
|
|
98
|
+
} catch {
|
|
99
|
+
return String((row as DataRow).id);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A native list screen from a view:
|
|
105
|
+
*
|
|
106
|
+
* ```tsx
|
|
107
|
+
* <NativeResourceView view={brandsView} onRowPress={(b) => router.push(`/brands/${b.id}`)} />
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function NativeResourceView<T extends Row = Row>(props: NativeResourceViewProps<T>) {
|
|
111
|
+
const scope = useOptionalViewerScope();
|
|
112
|
+
const viewer = scope?.timeZone;
|
|
113
|
+
const { practiceTimeZone } = props;
|
|
114
|
+
const zones = useMemo<DisplayTimeZones>(() => {
|
|
115
|
+
const own = personTimeZone({ timeZone: viewer });
|
|
116
|
+
return { viewer: own, practice: practiceTimeZone && isTimeZone(practiceTimeZone) ? practiceTimeZone : own };
|
|
117
|
+
}, [viewer, practiceTimeZone]);
|
|
118
|
+
return (
|
|
119
|
+
<DisplayTimeZonesProvider value={zones}>
|
|
120
|
+
<Screen {...props} />
|
|
121
|
+
</DisplayTimeZonesProvider>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface RowProps<T extends Row> {
|
|
126
|
+
row: T;
|
|
127
|
+
first: boolean;
|
|
128
|
+
last: boolean;
|
|
129
|
+
item: UseNativeResourceViewReturn<T>["item"];
|
|
130
|
+
actions: UseNativeResourceViewReturn<T>["actions"];
|
|
131
|
+
handlers: Parameters<UseNativeResourceViewReturn<T>["actions"]>[1];
|
|
132
|
+
onPress: (row: T) => void;
|
|
133
|
+
chevron: boolean;
|
|
134
|
+
Swipeable: ComponentType<NativeResourceSwipeableProps> | undefined;
|
|
135
|
+
cancelLabel: string;
|
|
136
|
+
surface: string;
|
|
137
|
+
separator: string;
|
|
138
|
+
testID: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A row picture's size, in points (Mail's and Contacts' list avatars). */
|
|
142
|
+
const PICTURE_SIZE = 40;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* NativeListItem's `leading` from `rowPicture(summary)`: the image (decorative unless it has
|
|
146
|
+
* `alt`), with its initials if it fails to load; initials alone; or nothing.
|
|
147
|
+
*/
|
|
148
|
+
function RowLeading({ picture, testID }: { picture: RowPicture; testID: string }) {
|
|
149
|
+
// Which src failed, not a flag: a recycled row for another record starts fresh.
|
|
150
|
+
const [failed, setFailed] = useState<string | null>(null);
|
|
151
|
+
if (picture.kind === "image" && failed !== picture.src) {
|
|
152
|
+
return (
|
|
153
|
+
<NativeImage
|
|
154
|
+
testID={`${testID}-image`}
|
|
155
|
+
src={picture.src}
|
|
156
|
+
{...(picture.alt ? { alt: picture.alt } : {})}
|
|
157
|
+
width={PICTURE_SIZE}
|
|
158
|
+
height={PICTURE_SIZE}
|
|
159
|
+
className="rounded-full"
|
|
160
|
+
onError={() => setFailed(picture.src)}
|
|
161
|
+
/>
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const initials = picture.kind === "image" ? picture.fallback : picture.text;
|
|
165
|
+
if (!initials) return null;
|
|
166
|
+
return (
|
|
167
|
+
// The row's label already names it; the letters would only be read twice.
|
|
168
|
+
<View accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
|
|
169
|
+
<NativeAvatar testID={`${testID}-initials`} fallback={initials} size={PICTURE_SIZE} />
|
|
170
|
+
</View>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** One row. Memoised: it redraws only when its row or what it reads changes, not per keystroke. */
|
|
175
|
+
const ResourceRow = memo(function ResourceRow<T extends Row>({
|
|
176
|
+
row,
|
|
177
|
+
first,
|
|
178
|
+
last,
|
|
179
|
+
item: itemFor,
|
|
180
|
+
actions: actionsFor,
|
|
181
|
+
handlers,
|
|
182
|
+
onPress,
|
|
183
|
+
chevron,
|
|
184
|
+
Swipeable,
|
|
185
|
+
cancelLabel,
|
|
186
|
+
surface,
|
|
187
|
+
separator,
|
|
188
|
+
testID,
|
|
189
|
+
}: RowProps<T>) {
|
|
190
|
+
const item: NativeResourceRowItem = itemFor(row);
|
|
191
|
+
const actions = actionsFor(row, handlers);
|
|
192
|
+
const chip = item.chips[0];
|
|
193
|
+
const picture = item.picture;
|
|
194
|
+
const rowTestID = `${testID}-row-${item.id}`;
|
|
195
|
+
const content = (
|
|
196
|
+
<View
|
|
197
|
+
testID={`${rowTestID}-cell`}
|
|
198
|
+
style={[
|
|
199
|
+
{ backgroundColor: surface },
|
|
200
|
+
IOS
|
|
201
|
+
? {
|
|
202
|
+
marginHorizontal: IOS_GROUP_MARGIN,
|
|
203
|
+
borderTopLeftRadius: first ? GROUP_RADIUS : 0,
|
|
204
|
+
borderTopRightRadius: first ? GROUP_RADIUS : 0,
|
|
205
|
+
borderBottomLeftRadius: last ? GROUP_RADIUS : 0,
|
|
206
|
+
borderBottomRightRadius: last ? GROUP_RADIUS : 0,
|
|
207
|
+
overflow: "hidden",
|
|
208
|
+
}
|
|
209
|
+
: null,
|
|
210
|
+
]}
|
|
211
|
+
>
|
|
212
|
+
<Pressable
|
|
213
|
+
testID={rowTestID}
|
|
214
|
+
accessibilityRole="button"
|
|
215
|
+
accessibilityLabel={[picture?.kind === "image" ? picture.alt : null, item.title, item.subtitle, chip?.label].filter(Boolean).join(", ")}
|
|
216
|
+
accessibilityActions={actions.map((a) => ({ name: a.id, label: a.reason ? `${a.label}: ${a.reason}` : a.label }))}
|
|
217
|
+
onAccessibilityAction={(event) => actions.find((a) => a.id === event.nativeEvent.actionName)?.onPress()}
|
|
218
|
+
onPress={() => onPress(row)}
|
|
219
|
+
onLongPress={actions.length ? () => showInboxActions(item.title, actions, cancelLabel) : undefined}
|
|
220
|
+
>
|
|
221
|
+
<NativeListItem
|
|
222
|
+
testID={`${rowTestID}-item`}
|
|
223
|
+
title={item.title}
|
|
224
|
+
subtitle={item.subtitle}
|
|
225
|
+
leading={picture ? <RowLeading picture={picture} testID={rowTestID} /> : undefined}
|
|
226
|
+
className="bg-transparent"
|
|
227
|
+
trailing={
|
|
228
|
+
chip ? (
|
|
229
|
+
<NativeBadge testID={`${rowTestID}-chip`} variant={badgeVariant(chip.tone)}>
|
|
230
|
+
{chip.label}
|
|
231
|
+
</NativeBadge>
|
|
232
|
+
) : undefined
|
|
233
|
+
}
|
|
234
|
+
showChevron={chevron}
|
|
235
|
+
/>
|
|
236
|
+
</Pressable>
|
|
237
|
+
{!last ? <View style={{ height: 0.5, marginLeft: 16, backgroundColor: separator }} /> : null}
|
|
238
|
+
</View>
|
|
239
|
+
);
|
|
240
|
+
return actions.length && Swipeable ? (
|
|
241
|
+
<Swipeable actions={actions} testID={`${rowTestID}-swipe`}>
|
|
242
|
+
{content}
|
|
243
|
+
</Swipeable>
|
|
244
|
+
) : (
|
|
245
|
+
content
|
|
246
|
+
);
|
|
247
|
+
}) as <T extends Row>(props: RowProps<T>) => ReactNode;
|
|
248
|
+
|
|
249
|
+
function Screen<T extends Row = Row>(props: NativeResourceViewProps<T>) {
|
|
250
|
+
const {
|
|
251
|
+
view,
|
|
252
|
+
title,
|
|
253
|
+
subtitle,
|
|
254
|
+
onRowPress,
|
|
255
|
+
swipeable: Swipeable,
|
|
256
|
+
header,
|
|
257
|
+
emptyState,
|
|
258
|
+
searchable,
|
|
259
|
+
practiceTimeZone: _zone,
|
|
260
|
+
testID = "native-resource-view",
|
|
261
|
+
...options
|
|
262
|
+
} = props;
|
|
263
|
+
void _zone;
|
|
264
|
+
const state = useNativeResourceView<T>(view, options);
|
|
265
|
+
const keyExtractor = useCallback((row: T) => recordKey(view as AnyResourceView<Row>, row), [view]);
|
|
266
|
+
const messages = useNativeResourceViewMessages();
|
|
267
|
+
const colors = useThemeRoles();
|
|
268
|
+
const cell = useDisplayCell();
|
|
269
|
+
|
|
270
|
+
const [peekId, setPeekId] = useState<{ row: T } | null>(null);
|
|
271
|
+
const [sheet, setSheet] = useState<{ mode: "create" | "edit"; row?: T } | null>(null);
|
|
272
|
+
|
|
273
|
+
const refused = useCallback((label: string, reason: string) => Alert.alert(label, reason), []);
|
|
274
|
+
const { remove, softDelete, item: itemFor, actions: actionsFor } = state;
|
|
275
|
+
const confirmDelete = useCallback(
|
|
276
|
+
(row: T) => {
|
|
277
|
+
const name = itemFor(row).title;
|
|
278
|
+
const words = softDelete ? messages.bin : messages.delete;
|
|
279
|
+
Alert.alert(fill(words.confirmTitle, { name }), words.confirmBody, [
|
|
280
|
+
{ text: messages.common.cancel, style: "cancel" },
|
|
281
|
+
{
|
|
282
|
+
text: words.action,
|
|
283
|
+
style: "destructive",
|
|
284
|
+
onPress: () => {
|
|
285
|
+
setPeekId(null);
|
|
286
|
+
remove(row);
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
]);
|
|
290
|
+
},
|
|
291
|
+
[itemFor, softDelete, messages, remove],
|
|
292
|
+
);
|
|
293
|
+
const edit = useCallback((row: T) => {
|
|
294
|
+
setPeekId(null);
|
|
295
|
+
setSheet({ mode: "edit", row });
|
|
296
|
+
}, []);
|
|
297
|
+
const handlers = useMemo(() => ({ onEdit: edit, onDelete: confirmDelete, onRefused: refused }), [edit, confirmDelete, refused]);
|
|
298
|
+
|
|
299
|
+
// The screen's handler through a ref, so an inline `onRowPress` doesn't redraw every row.
|
|
300
|
+
const rowPress = useRef(onRowPress);
|
|
301
|
+
rowPress.current = onRowPress;
|
|
302
|
+
const press = useCallback((row: T) => (rowPress.current ? rowPress.current(row) : setPeekId({ row })), []);
|
|
303
|
+
const chevron = !!onRowPress;
|
|
304
|
+
|
|
305
|
+
const renderItem = useCallback(
|
|
306
|
+
({ item: row, index, section }: { item: T; index: number; section: NativeResourceSection<T> }) => (
|
|
307
|
+
<ResourceRow<T>
|
|
308
|
+
row={row}
|
|
309
|
+
first={index === 0}
|
|
310
|
+
last={index === section.data.length - 1}
|
|
311
|
+
item={itemFor}
|
|
312
|
+
actions={actionsFor}
|
|
313
|
+
handlers={handlers}
|
|
314
|
+
onPress={press}
|
|
315
|
+
chevron={chevron}
|
|
316
|
+
Swipeable={Swipeable}
|
|
317
|
+
cancelLabel={messages.common.cancel}
|
|
318
|
+
surface={colors.surface as string}
|
|
319
|
+
separator={colors.separator as string}
|
|
320
|
+
testID={testID}
|
|
321
|
+
/>
|
|
322
|
+
),
|
|
323
|
+
[itemFor, actionsFor, handlers, press, chevron, Swipeable, messages, colors.surface, colors.separator, testID],
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
// Rows are counted only as far as they're loaded, so the header shows the title alone.
|
|
327
|
+
const renderSectionHeader = useCallback(
|
|
328
|
+
({ section }: { section: NativeResourceSection<T> }) =>
|
|
329
|
+
section.title ? (
|
|
330
|
+
<NativeSectionListHeader
|
|
331
|
+
testID={`${testID}-section-${section.key || "none"}`}
|
|
332
|
+
title={section.title}
|
|
333
|
+
className="border-b-0 bg-transparent pt-4"
|
|
334
|
+
/>
|
|
335
|
+
) : null,
|
|
336
|
+
[testID],
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
const create = () => {
|
|
340
|
+
if (state.canCreate === true) setSheet({ mode: "create" });
|
|
341
|
+
else if (typeof state.canCreate === "string") refused(fill(messages.common.create, { noun: view.noun }), state.canCreate);
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const showSearch = searchable ?? !!(options.searchFields?.length || view.labelField);
|
|
345
|
+
const heading = title === false ? null : (title ?? view.title);
|
|
346
|
+
const hasRows = state.rows.length > 0;
|
|
347
|
+
|
|
348
|
+
const listHeader = (
|
|
349
|
+
<View style={{ paddingHorizontal: 16, paddingTop: 8, paddingBottom: 8, gap: 12 }}>
|
|
350
|
+
{heading || state.canCreate !== false ? (
|
|
351
|
+
<View style={{ flexDirection: "row", alignItems: "center", justifyContent: "space-between" }}>
|
|
352
|
+
<View style={{ flex: 1 }}>
|
|
353
|
+
{heading ? (
|
|
354
|
+
<NativeText preset="h2" accessibilityRole="header">
|
|
355
|
+
{heading}
|
|
356
|
+
</NativeText>
|
|
357
|
+
) : null}
|
|
358
|
+
{(subtitle ?? view.definition.copy?.subtitle) ? (
|
|
359
|
+
<NativeText preset="body-sm" className="text-muted-foreground mt-0.5">
|
|
360
|
+
{subtitle ?? view.definition.copy?.subtitle}
|
|
361
|
+
</NativeText>
|
|
362
|
+
) : null}
|
|
363
|
+
</View>
|
|
364
|
+
{state.canCreate !== false ? (
|
|
365
|
+
<NativeButton size="sm" testID={`${testID}-create`} onPress={create}>
|
|
366
|
+
{view.definition.copy?.create ?? fill(messages.common.create, { noun: view.noun })}
|
|
367
|
+
</NativeButton>
|
|
368
|
+
) : null}
|
|
369
|
+
</View>
|
|
370
|
+
) : null}
|
|
371
|
+
{showSearch ? (
|
|
372
|
+
<NativeSearchBar
|
|
373
|
+
testID={`${testID}-search`}
|
|
374
|
+
value={state.search}
|
|
375
|
+
onChange={state.setSearch}
|
|
376
|
+
placeholder={fill(messages.common.search, { nounPlural: view.nounPlural })}
|
|
377
|
+
/>
|
|
378
|
+
) : null}
|
|
379
|
+
{state.error && hasRows ? (
|
|
380
|
+
// A refetch or the next page failed: keep the rows, say so above them.
|
|
381
|
+
<View testID={`${testID}-read-error`} style={{ gap: 8 }}>
|
|
382
|
+
<NativeInlineAlert variant="destructive" description={state.error} />
|
|
383
|
+
<NativeButton variant="outline" size="sm" onPress={() => void state.refresh()}>
|
|
384
|
+
{messages.list.retry}
|
|
385
|
+
</NativeButton>
|
|
386
|
+
</View>
|
|
387
|
+
) : null}
|
|
388
|
+
{state.refusal ? (
|
|
389
|
+
<NativeInlineAlert
|
|
390
|
+
testID={`${testID}-refusal`}
|
|
391
|
+
variant="destructive"
|
|
392
|
+
description={state.refusal}
|
|
393
|
+
onDismiss={state.dismissRefusal}
|
|
394
|
+
/>
|
|
395
|
+
) : null}
|
|
396
|
+
{header}
|
|
397
|
+
{state.loadingPrevious ? (
|
|
398
|
+
<NativeLoadingState fullScreen={false} size="small" label={messages.list.loadingMore} testID={`${testID}-previous`} />
|
|
399
|
+
) : null}
|
|
400
|
+
</View>
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
const empty = state.loading ? (
|
|
404
|
+
<NativeLoadingState fullScreen={false} testID={`${testID}-loading`} />
|
|
405
|
+
) : state.error ? (
|
|
406
|
+
<NativeEmptyState
|
|
407
|
+
testID={`${testID}-error`}
|
|
408
|
+
variant="error"
|
|
409
|
+
title={state.error}
|
|
410
|
+
primaryAction={
|
|
411
|
+
<NativeButton variant="outline" size="sm" onPress={() => void state.refresh()}>
|
|
412
|
+
{messages.list.retry}
|
|
413
|
+
</NativeButton>
|
|
414
|
+
}
|
|
415
|
+
/>
|
|
416
|
+
) : (
|
|
417
|
+
(emptyState ??
|
|
418
|
+
(state.search ? (
|
|
419
|
+
<NativeEmptyState testID={`${testID}-empty`} variant="no-results" title={messages.list.noResultsTitle} body={messages.list.noResultsBody} />
|
|
420
|
+
) : (
|
|
421
|
+
<NativeEmptyState
|
|
422
|
+
testID={`${testID}-empty`}
|
|
423
|
+
title={view.definition.copy?.empty?.title ?? fill(messages.list.emptyTitle, { nounPlural: view.nounPlural })}
|
|
424
|
+
body={view.definition.copy?.empty?.body ?? messages.list.emptyBody}
|
|
425
|
+
/>
|
|
426
|
+
)))
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
// The peek and the edit sheet read the row by id: the list's select may be narrower.
|
|
430
|
+
const peekRow = useOne<T & BaseRecord>(view.resource, peekId ? ((peekId.row as DataRow).id as string) : null);
|
|
431
|
+
const editRow = useOne<T & BaseRecord>(view.resource, sheet?.mode === "edit" ? ((sheet.row as DataRow).id as string) : null);
|
|
432
|
+
const peek = peekId ? ((peekRow.data as T | undefined) ?? peekId.row) : null;
|
|
433
|
+
|
|
434
|
+
// Peek: the view's full column list, drawn like the cells (so `when` hides there too), never
|
|
435
|
+
// every key of the row: sensitive values and columns the view leaves out stay out. As the web's.
|
|
436
|
+
const peekFields = useMemo(
|
|
437
|
+
() =>
|
|
438
|
+
view.columns().map((column) => ({
|
|
439
|
+
key: column.key,
|
|
440
|
+
label: column.label,
|
|
441
|
+
render: (_value: unknown, row: Row) => cell(column as never, row),
|
|
442
|
+
})),
|
|
443
|
+
[view, cell],
|
|
444
|
+
);
|
|
445
|
+
const { formFields } = state;
|
|
446
|
+
const createFields = useMemo(() => formFields("create"), [formFields]);
|
|
447
|
+
const editFields = useMemo(() => formFields("edit"), [formFields]);
|
|
448
|
+
const peekActions = peek ? actionsFor(peek, handlers) : [];
|
|
449
|
+
const editLoading = sheet?.mode === "edit" && editRow.data === undefined && !editRow.isError;
|
|
450
|
+
|
|
451
|
+
return (
|
|
452
|
+
<SafeAreaView testID={testID} edges={["left", "right", "bottom"]} style={{ flex: 1, backgroundColor: colors.groupedBackground }}>
|
|
453
|
+
<SectionList<T, NativeResourceSection<T>>
|
|
454
|
+
testID={`${testID}-list`}
|
|
455
|
+
sections={state.sections}
|
|
456
|
+
keyExtractor={keyExtractor}
|
|
457
|
+
renderItem={renderItem}
|
|
458
|
+
renderSectionHeader={renderSectionHeader}
|
|
459
|
+
// Rows read their item and actions besides `sections`; they change only with the view,
|
|
460
|
+
// the bin capability or the words, never per keystroke.
|
|
461
|
+
extraData={actionsFor}
|
|
462
|
+
stickySectionHeadersEnabled={false}
|
|
463
|
+
contentInsetAdjustmentBehavior="automatic"
|
|
464
|
+
keyboardShouldPersistTaps="handled"
|
|
465
|
+
ListHeaderComponent={listHeader}
|
|
466
|
+
ListEmptyComponent={empty}
|
|
467
|
+
ListFooterComponent={
|
|
468
|
+
state.loadingMore ? (
|
|
469
|
+
<NativeLoadingState fullScreen={false} size="small" label={messages.list.loadingMore} testID={`${testID}-more`} />
|
|
470
|
+
) : null
|
|
471
|
+
}
|
|
472
|
+
contentContainerStyle={{ flexGrow: 1, paddingBottom: 32 }}
|
|
473
|
+
onEndReached={state.hasMore ? state.loadMore : undefined}
|
|
474
|
+
onEndReachedThreshold={0.5}
|
|
475
|
+
// maxPages drops pages from the front; reaching the top loads them back, and the rows on
|
|
476
|
+
// screen keep their place while they're added above (VirtualizedList's onStartReached and
|
|
477
|
+
// ScrollView's maintainVisibleContentPosition, both inherited by SectionList).
|
|
478
|
+
onStartReached={state.hasPrevious ? state.loadPrevious : undefined}
|
|
479
|
+
onStartReachedThreshold={0.5}
|
|
480
|
+
maintainVisibleContentPosition={{ minIndexForVisible: 0 }}
|
|
481
|
+
refreshControl={<NativeRefreshControl testID={`${testID}-refresh`} refreshing={state.refreshing} onRefresh={() => void state.refresh()} />}
|
|
482
|
+
/>
|
|
483
|
+
|
|
484
|
+
<NativeSheet open={peek !== null} onOpenChange={(open) => (open ? undefined : setPeekId(null))} testID={`${testID}-peek`}>
|
|
485
|
+
{peek ? (
|
|
486
|
+
<NativeDetailPanel<Row>
|
|
487
|
+
item={peek}
|
|
488
|
+
title={itemFor(peek).title}
|
|
489
|
+
fields={peekFields}
|
|
490
|
+
footer={
|
|
491
|
+
peekActions.length ? (
|
|
492
|
+
<View testID={`${testID}-peek-actions`} style={{ gap: 12 }}>
|
|
493
|
+
{peekActions.map((action) => (
|
|
494
|
+
<View key={action.id} style={{ gap: 4 }}>
|
|
495
|
+
<NativeButton
|
|
496
|
+
testID={`${testID}-peek-${action.id}`}
|
|
497
|
+
variant={action.destructive ? "destructive" : "outline"}
|
|
498
|
+
// A refused action stays, dimmed, and pressing it says why.
|
|
499
|
+
className={action.reason ? "opacity-50" : undefined}
|
|
500
|
+
onPress={action.onPress}
|
|
501
|
+
>
|
|
502
|
+
{action.label}
|
|
503
|
+
</NativeButton>
|
|
504
|
+
{action.reason ? (
|
|
505
|
+
<NativeText testID={`${testID}-peek-${action.id}-reason`} preset="caption" className="text-muted-foreground">
|
|
506
|
+
{action.reason}
|
|
507
|
+
</NativeText>
|
|
508
|
+
) : null}
|
|
509
|
+
</View>
|
|
510
|
+
))}
|
|
511
|
+
</View>
|
|
512
|
+
) : null
|
|
513
|
+
}
|
|
514
|
+
/>
|
|
515
|
+
) : null}
|
|
516
|
+
</NativeSheet>
|
|
517
|
+
|
|
518
|
+
<NativeResourceFormSheet
|
|
519
|
+
open={sheet !== null}
|
|
520
|
+
mode={sheet?.mode ?? "create"}
|
|
521
|
+
title={fill(sheet?.mode === "edit" ? messages.common.editTitle : messages.common.create, { noun: view.noun })}
|
|
522
|
+
fields={sheet?.mode === "edit" ? editFields : createFields}
|
|
523
|
+
initial={sheet?.mode === "edit" ? ((editRow.data as Row | undefined) ?? (editRow.isError ? sheet.row : undefined)) : undefined}
|
|
524
|
+
loading={editLoading}
|
|
525
|
+
recordKey={sheet?.row ? recordKey(view as AnyResourceView<Row>, sheet.row) : "new"}
|
|
526
|
+
onCancel={() => setSheet(null)}
|
|
527
|
+
onSave={async (values, dirty) => {
|
|
528
|
+
const result = await state.save(sheet?.mode ?? "create", values, sheet?.row, dirty);
|
|
529
|
+
if (result.ok) setSheet(null);
|
|
530
|
+
return result;
|
|
531
|
+
}}
|
|
532
|
+
testID={`${testID}-form`}
|
|
533
|
+
/>
|
|
534
|
+
</SafeAreaView>
|
|
535
|
+
);
|
|
536
|
+
}
|