@marktiderman/genesis-ui-native 2.6.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.
Files changed (29) hide show
  1. package/dist/{chunk-KZEIF4YI.js → chunk-7HWS2JGX.js} +3 -3
  2. package/dist/{chunk-KZEIF4YI.js.map → chunk-7HWS2JGX.js.map} +1 -1
  3. package/dist/{chunk-3S5XVER4.js → chunk-C6Q5ZAVT.js} +4 -3
  4. package/dist/chunk-C6Q5ZAVT.js.map +1 -0
  5. package/dist/{chunk-VMSIKSU2.js → chunk-ON7WP4T6.js} +4 -4
  6. package/dist/{chunk-VMSIKSU2.js.map → chunk-ON7WP4T6.js.map} +1 -1
  7. package/dist/{chunk-56QLEHHV.js → chunk-PO3MK6IY.js} +3 -3
  8. package/dist/{chunk-56QLEHHV.js.map → chunk-PO3MK6IY.js.map} +1 -1
  9. package/dist/{chunk-6JBNSW4D.js → chunk-SXCG2REG.js} +3 -3
  10. package/dist/{chunk-6JBNSW4D.js.map → chunk-SXCG2REG.js.map} +1 -1
  11. package/dist/components/index.d.ts +7 -2
  12. package/dist/components/index.js +3 -3
  13. package/dist/data/index.d.ts +122 -22
  14. package/dist/data/index.js +440 -25
  15. package/dist/data/index.js.map +1 -1
  16. package/dist/index.js +5 -5
  17. package/dist/layouts/index.js +3 -3
  18. package/package.json +2 -2
  19. package/src/components/__tests__/button-a11y.test.tsx +17 -0
  20. package/src/components/button.tsx +8 -2
  21. package/src/data/resource-view/NativeResourceView.tsx +11 -3
  22. package/src/data/resource-view/NativeResourceWorkspace.tsx +657 -0
  23. package/src/data/resource-view/__tests__/native-resource-view.test.tsx +108 -1
  24. package/src/data/resource-view/__tests__/native-resource-workspace.test.tsx +356 -0
  25. package/src/data/resource-view/display.tsx +6 -3
  26. package/src/data/resource-view/index.ts +11 -0
  27. package/src/data/resource-view/mapping.ts +12 -5
  28. package/src/data/resource-view/use-native-resource-view.ts +7 -5
  29. package/dist/chunk-3S5XVER4.js.map +0 -1
@@ -1,21 +1,23 @@
1
- import { NativeImage, NativeAvatar, NativeCard, NativeCardContent, NativeSeparator, NativeLabel, NativeSwitch, NativeCheckbox, NativeSelect, NativeSelectTrigger, NativeSelectContent, NativeSelectItem, NativeTextarea, NativeAlert, NativeAlertTitle, NativeAlertDescription, NativeSheet, NativeSheetScrollView, NativeInlineAlert, NativeSegmentedControl, NativeDatePicker, NativeTimePicker, NativeSearchBar, NativeSkeleton, NativeCardHeader, NativeCardTitle } from '../chunk-56QLEHHV.js';
1
+ import { NativeImage, NativeAvatar, NativeCard, NativeCardContent, NativeSeparator, NativeLabel, NativeSwitch, NativeCheckbox, NativeSelect, NativeSelectTrigger, NativeSelectContent, NativeSelectItem, NativeTextarea, NativeAlert, NativeAlertTitle, NativeAlertDescription, NativeSheet, NativeSheetScrollView, NativeInlineAlert, NativeSegmentedControl, NativeDatePicker, NativeTimePicker, NativeSearchBar, NativeSkeleton, NativeCardHeader, NativeCardTitle } from '../chunk-PO3MK6IY.js';
2
2
  import '../chunk-7PCWEY6R.js';
3
- import { showInboxActions, NavBarSaveButton } from '../chunk-6JBNSW4D.js';
4
- import { NativeListItem, NativeBadge, NativeText, NativeButton, NativeInput, useGenesisModules, NativeLoadingState, NativeSectionListHeader, NativeEmptyState, NativeRefreshControl } from '../chunk-3S5XVER4.js';
3
+ import { showInboxActions, NavBarSaveButton, composeHeaderOptions } from '../chunk-SXCG2REG.js';
4
+ import { NativeListItem, NativeBadge, NativeText, NativeButton, NativeInput, useGenesisModules, NativeLoadingState, NativeSectionListHeader, NativeEmptyState, NativeRefreshControl, NativeIcon } from '../chunk-C6Q5ZAVT.js';
5
5
  import { useThemeRoles } from '../chunk-Z3AQYVUA.js';
6
6
  import '../chunk-OHZD7KRO.js';
7
7
  import '../chunk-DGUM43GV.js';
8
- import { Platform, View, Pressable, Text, Linking, ScrollView, Alert, SectionList, FlatList } from 'react-native';
8
+ import { Platform, View, Pressable, Text, Linking, ScrollView, Alert, SectionList, FlatList, ActionSheetIOS } from 'react-native';
9
9
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
10
10
  import * as React from 'react';
11
11
  import { createContext, memo, useState, useCallback, useMemo, useEffect, useContext, useRef } from 'react';
12
12
  import { SafeAreaView } from 'react-native-safe-area-context';
13
- import { titleCase, useResourceForm } from '@marktiderman/genesis-core';
13
+ import { titleCase, useResourceForm, resourceKeys, useDataProvider, useViewer as useViewer$1, encodeRecordKey, useCapabilities as useCapabilities$1, DataWriteError } from '@marktiderman/genesis-core';
14
14
  import { Controller, useForm } from 'react-hook-form';
15
- import { useResource, useInfiniteResource, useCapabilities, useOptionalViewerScope, useOne } from '@marktiderman/genesis-core/hooks';
15
+ import { useResource, useViewer, useInfiniteResource, useCapabilities, useOptionalViewerScope, useOne } from '@marktiderman/genesis-core/hooks';
16
16
  import { personTimeZone, isTimeZone, wallClock } from '@marktiderman/genesis-core/dates';
17
17
  import { isSafeUrl } from '@marktiderman/genesis-core/urls';
18
18
  import { rowPicture } from '@marktiderman/genesis-core/resource-view';
19
+ import { useQueryClient, useQuery } from '@tanstack/react-query';
20
+ import { readinessChip } from '@marktiderman/genesis-core/resource';
19
21
 
20
22
  function NativeEmptyState2({
21
23
  message = "No items found",
@@ -673,7 +675,7 @@ function NativeResourcePage({
673
675
  setSearchLocal("");
674
676
  setSelectedStatuses([]);
675
677
  }, []);
676
- const keyExtractor2 = useCallback(
678
+ const keyExtractor = useCallback(
677
679
  (item) => String(item.id),
678
680
  []
679
681
  );
@@ -744,7 +746,7 @@ function NativeResourcePage({
744
746
  {
745
747
  testID: "native-resource-page-list",
746
748
  data: items,
747
- keyExtractor: keyExtractor2,
749
+ keyExtractor,
748
750
  renderItem: renderFlatListItem,
749
751
  ListHeaderComponent,
750
752
  ListEmptyComponent,
@@ -759,7 +761,7 @@ function NativeResourcePage({
759
761
  {
760
762
  testID: "native-resource-page-list",
761
763
  data: items,
762
- keyExtractor: keyExtractor2,
764
+ keyExtractor,
763
765
  renderItem: renderFlatListItem,
764
766
  ListHeaderComponent,
765
767
  ListEmptyComponent,
@@ -989,14 +991,15 @@ function useDisplayCell() {
989
991
  const renderers = useContext(DisplayRenderersContext);
990
992
  const messages = useNativeResourceViewMessages();
991
993
  const zones = useDisplayTimeZones();
994
+ const viewer = useViewer();
992
995
  return useMemo(
993
996
  () => (column, row) => {
994
- if (column.when && !column.when(row)) return /* @__PURE__ */ jsx(NativeText, { preset: "body", children: messages.common.empty });
997
+ if (column.when && !column.when(row, viewer)) return /* @__PURE__ */ jsx(NativeText, { preset: "body", children: messages.common.empty });
995
998
  const value = column.value(row);
996
999
  if (isEmptyValue(value)) return /* @__PURE__ */ jsx(NativeText, { preset: "body", children: messages.common.empty });
997
1000
  return renderers[column.display](value, { row, column, messages, zones });
998
1001
  },
999
- [renderers, messages, zones]
1002
+ [renderers, messages, zones, viewer]
1000
1003
  );
1001
1004
  }
1002
1005
  function nativePicture(summary) {
@@ -1034,15 +1037,15 @@ function sectionsFor(view, rows, messages) {
1034
1037
  }
1035
1038
  return [...groups.values()];
1036
1039
  }
1037
- function allowed(view, action, row, screen) {
1040
+ function allowed(view, action, row, screen, viewer) {
1038
1041
  const own = screen?.(action, row);
1039
1042
  if (own === false) return false;
1040
1043
  if (typeof own === "string") return own;
1041
- const guard = action === "create" ? view.can("create") : row ? view.can(action, row) : true;
1044
+ const guard = action === "create" ? view.can("create", viewer) : row ? view.can(action, row, viewer) : true;
1042
1045
  return guard;
1043
1046
  }
1044
1047
  function swipeActionsFor(view, row, options) {
1045
- const { messages, softDelete, editable, screen, onEdit, onDelete, onRefused } = options;
1048
+ const { messages, softDelete, editable, screen, viewer, onEdit, onDelete, onRefused } = options;
1046
1049
  const actions = [];
1047
1050
  const add = (id, label, icon, right, run, destructive = false) => {
1048
1051
  if (right === false) return;
@@ -1056,12 +1059,12 @@ function swipeActionsFor(view, row, options) {
1056
1059
  onPress: reason ? () => onRefused(label, reason) : run
1057
1060
  });
1058
1061
  };
1059
- if (editable) add("edit", messages.common.edit, "edit", allowed(view, "update", row, screen), () => onEdit(row));
1062
+ if (editable) add("edit", messages.common.edit, "edit", allowed(view, "update", row, screen, viewer), () => onEdit(row));
1060
1063
  add(
1061
1064
  "delete",
1062
1065
  softDelete ? messages.bin.action : messages.delete.action,
1063
1066
  "trash",
1064
- allowed(view, "delete", row, screen),
1067
+ allowed(view, "delete", row, screen, viewer),
1065
1068
  () => onDelete(row),
1066
1069
  true
1067
1070
  );
@@ -1180,7 +1183,7 @@ function NativeResourceFormSheet({
1180
1183
  fields,
1181
1184
  initial,
1182
1185
  loading = false,
1183
- recordKey,
1186
+ recordKey: recordKey2,
1184
1187
  onCancel,
1185
1188
  onSave,
1186
1189
  testID = "native-resource-form"
@@ -1199,7 +1202,7 @@ function NativeResourceFormSheet({
1199
1202
  form.reset(start.current);
1200
1203
  setRefusal(null);
1201
1204
  }
1202
- }, [open, loading, recordKey, form]);
1205
+ }, [open, loading, recordKey2, form]);
1203
1206
  const values = form.watch();
1204
1207
  const sections = useMemo(() => {
1205
1208
  const out = [];
@@ -1431,9 +1434,10 @@ function useNativeResourceView(view, options = {}) {
1431
1434
  [view, createDefaults]
1432
1435
  );
1433
1436
  const editable = useMemo(() => formFieldsFor(view, "edit").some((f) => !f.readOnly), [view]);
1437
+ const viewer = useViewer();
1434
1438
  const canCreate = useMemo(
1435
- () => formFieldsFor(view, "create").length ? allowed(view, "create", void 0, can) : false,
1436
- [view, can]
1439
+ () => formFieldsFor(view, "create").length ? allowed(view, "create", void 0, can, viewer) : false,
1440
+ [view, can, viewer]
1437
1441
  );
1438
1442
  const writers = useRef(pessimistic);
1439
1443
  writers.current = pessimistic;
@@ -1468,8 +1472,8 @@ function useNativeResourceView(view, options = {}) {
1468
1472
  }, []);
1469
1473
  const item = useCallback((row) => rowItem(view, row), [view]);
1470
1474
  const actions = useCallback(
1471
- (row, handlers) => swipeActionsFor(view, row, { messages, softDelete, editable, screen: can, ...handlers }),
1472
- [view, messages, softDelete, editable, can]
1475
+ (row, handlers) => swipeActionsFor(view, row, { messages, softDelete, editable, screen: can, viewer, ...handlers }),
1476
+ [view, messages, softDelete, editable, can, viewer]
1473
1477
  );
1474
1478
  const sections = useMemo(() => sectionsFor(view, pages.rows, messages), [view, pages.rows, messages]);
1475
1479
  const { query } = pages;
@@ -1513,7 +1517,13 @@ function useNativeResourceView(view, options = {}) {
1513
1517
  var IOS_GROUP_MARGIN = 16;
1514
1518
  var GROUP_RADIUS = Number.parseFloat(String(Platform.Version)) >= 26 ? 26 : 10;
1515
1519
  var IOS = Platform.OS === "ios";
1516
- var keyExtractor = (row) => String(row.id);
1520
+ var recordKey = (view, row) => {
1521
+ try {
1522
+ return String(encodeRecordKey(view.contract, row));
1523
+ } catch {
1524
+ return String(row.id);
1525
+ }
1526
+ };
1517
1527
  function NativeResourceView(props) {
1518
1528
  const scope = useOptionalViewerScope();
1519
1529
  const viewer = scope?.timeZone;
@@ -1629,6 +1639,7 @@ function Screen(props) {
1629
1639
  ...options
1630
1640
  } = props;
1631
1641
  const state = useNativeResourceView(view, options);
1642
+ const keyExtractor = useCallback((row) => recordKey(view, row), [view]);
1632
1643
  const messages = useNativeResourceViewMessages();
1633
1644
  const colors = useThemeRoles();
1634
1645
  const cell = useDisplayCell();
@@ -1825,7 +1836,7 @@ function Screen(props) {
1825
1836
  fields: sheet?.mode === "edit" ? editFields : createFields,
1826
1837
  initial: sheet?.mode === "edit" ? editRow.data ?? (editRow.isError ? sheet.row : void 0) : void 0,
1827
1838
  loading: editLoading,
1828
- recordKey: sheet?.row ? String(sheet.row.id) : "new",
1839
+ recordKey: sheet?.row ? recordKey(view, sheet.row) : "new",
1829
1840
  onCancel: () => setSheet(null),
1830
1841
  onSave: async (values, dirty) => {
1831
1842
  const result = await state.save(sheet?.mode ?? "create", values, sheet?.row, dirty);
@@ -1837,7 +1848,411 @@ function Screen(props) {
1837
1848
  )
1838
1849
  ] });
1839
1850
  }
1851
+ var defaultNativeResourceWorkspaceMessages = {
1852
+ actions: "Actions",
1853
+ cancel: "Cancel",
1854
+ save: "Save",
1855
+ notChanged: "This {noun} was not changed.",
1856
+ failed: "That didn't work. Try again.",
1857
+ noRunner: "{name} isn't available here.",
1858
+ checks: "Checks",
1859
+ checkDone: "Done",
1860
+ checkTodo: "To do",
1861
+ allSections: "All sections",
1862
+ notANumber: "Enter a number.",
1863
+ ready: void 0,
1864
+ empty: void 0,
1865
+ pending: void 0,
1866
+ problems: void 0,
1867
+ warnings: void 0
1868
+ };
1869
+ var SEGMENTED_SECTIONS_MAX = 4;
1870
+ function useHeaderActions({ workspace, row, onAction, can, messages }) {
1871
+ const words = { ...defaultNativeResourceWorkspaceMessages, ...messages };
1872
+ const provider = useDataProvider();
1873
+ const queryClient = useQueryClient();
1874
+ const view = workspace.view;
1875
+ const capabilities = useCapabilities$1(view.resource, { enabled: !onAction && !!provider.capabilities });
1876
+ const described = useQuery({
1877
+ queryKey: ["__describe", view.resource],
1878
+ queryFn: () => provider.describe(view.resource),
1879
+ enabled: !onAction && !!provider.describe,
1880
+ staleTime: Infinity
1881
+ });
1882
+ const viewer = useViewer$1();
1883
+ const actions = workspace.headerFor(row, {
1884
+ viewer,
1885
+ runnable: Boolean(onAction) || Boolean(provider.runAction) && capabilities.data?.actions !== false,
1886
+ ...!onAction && described.data?.actions ? { served: described.data.actions.map((a) => a.name) } : {},
1887
+ ...can ? { can: (name) => can(name, row) } : {}
1888
+ }).actions;
1889
+ const running = useRef(false);
1890
+ const [pending, setPending] = useState(false);
1891
+ const run = async (a) => {
1892
+ if (running.current) return;
1893
+ running.current = true;
1894
+ setPending(true);
1895
+ try {
1896
+ if (onAction) await onAction(a.name, row);
1897
+ else if (provider.runAction) await provider.runAction(view.resource, { id: workspace.recordId(row), action: a.name });
1898
+ else throw new DataWriteError("error", fill(words.noRunner, { name: a.label }));
1899
+ await queryClient.invalidateQueries({ queryKey: [...resourceKeys(view.resource).all] });
1900
+ } catch (error) {
1901
+ Alert.alert(a.label, refusalMessage(error, words.failed));
1902
+ } finally {
1903
+ running.current = false;
1904
+ setPending(false);
1905
+ }
1906
+ };
1907
+ const press = (a) => {
1908
+ if (running.current) return;
1909
+ if (a.allowed !== true) return Alert.alert(a.label, a.allowed);
1910
+ if (!a.confirm) return void run(a);
1911
+ Alert.alert(a.confirm, a.description, [
1912
+ { text: words.cancel, style: "cancel" },
1913
+ { text: a.label, style: a.destructive ? "destructive" : "default", onPress: () => void run(a) }
1914
+ ]);
1915
+ };
1916
+ return { actions, press, pending, words };
1917
+ }
1918
+ var A11Y_PREFIX = "action:";
1919
+ function NativeResourceWorkspaceMenu(props) {
1920
+ const { testID = "native-resource-workspace-menu" } = props;
1921
+ const { actions, press, pending, words } = useHeaderActions(props);
1922
+ const [open, setOpen] = useState(false);
1923
+ if (actions.length === 0) return null;
1924
+ const labelOf = (a) => a.allowed === true ? a.label : `${a.label} (${a.allowed})`;
1925
+ const show = () => {
1926
+ if (Platform.OS === "ios") {
1927
+ const destructive = actions.flatMap((a, i) => a.destructive ? [i] : []);
1928
+ ActionSheetIOS.showActionSheetWithOptions(
1929
+ {
1930
+ title: props.workspace.headerFor(props.row).title,
1931
+ options: [...actions.map(labelOf), words.cancel],
1932
+ cancelButtonIndex: actions.length,
1933
+ ...destructive.length ? { destructiveButtonIndex: destructive } : {}
1934
+ },
1935
+ (index) => {
1936
+ const a = actions[index];
1937
+ if (a) press(a);
1938
+ }
1939
+ );
1940
+ } else {
1941
+ setOpen(true);
1942
+ }
1943
+ };
1944
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1945
+ /* @__PURE__ */ jsx(
1946
+ Pressable,
1947
+ {
1948
+ testID,
1949
+ accessibilityRole: "button",
1950
+ accessibilityLabel: words.actions,
1951
+ accessibilityState: { disabled: pending, busy: pending },
1952
+ disabled: pending,
1953
+ accessibilityActions: actions.map((a) => ({ name: `${A11Y_PREFIX}${a.name}`, label: a.allowed === true ? a.label : `${a.label}: ${a.allowed}` })),
1954
+ onAccessibilityAction: (event) => {
1955
+ const name = event.nativeEvent.actionName;
1956
+ const a = name.startsWith(A11Y_PREFIX) ? actions.find((x) => `${A11Y_PREFIX}${x.name}` === name) : void 0;
1957
+ if (a) press(a);
1958
+ },
1959
+ onPress: show,
1960
+ hitSlop: 8,
1961
+ className: "min-h-[44px] min-w-[44px] items-center justify-center",
1962
+ children: /* @__PURE__ */ jsx(NativeIcon, { name: "more", label: words.actions })
1963
+ }
1964
+ ),
1965
+ /* @__PURE__ */ jsx(NativeSheet, { open, onOpenChange: setOpen, testID: `${testID}-sheet`, children: /* @__PURE__ */ jsxs(View, { style: { paddingVertical: 8 }, children: [
1966
+ actions.map((a) => /* @__PURE__ */ jsxs(
1967
+ Pressable,
1968
+ {
1969
+ testID: `${testID}-${a.name}`,
1970
+ accessibilityRole: "button",
1971
+ accessibilityLabel: a.allowed === true ? a.label : `${a.label}: ${a.allowed}`,
1972
+ onPress: () => {
1973
+ setOpen(false);
1974
+ press(a);
1975
+ },
1976
+ className: "min-h-[48px] justify-center px-4",
1977
+ children: [
1978
+ /* @__PURE__ */ jsx(NativeText, { preset: "body", className: a.destructive ? "text-destructive" : void 0, children: a.label }),
1979
+ a.allowed !== true ? /* @__PURE__ */ jsx(NativeText, { preset: "caption", className: "text-muted-foreground", children: a.allowed }) : null
1980
+ ]
1981
+ },
1982
+ a.name
1983
+ )),
1984
+ /* @__PURE__ */ jsx(Pressable, { testID: `${testID}-cancel`, accessibilityRole: "button", onPress: () => setOpen(false), className: "min-h-[48px] justify-center px-4", children: /* @__PURE__ */ jsx(NativeText, { preset: "body", className: "text-muted-foreground", children: words.cancel }) })
1985
+ ] }) })
1986
+ ] });
1987
+ }
1988
+ function getNativeResourceWorkspaceStackOptions(props) {
1989
+ const { workspace, row, onAction, can, messages, ...header } = props;
1990
+ const menu = { workspace, row, ...onAction ? { onAction } : {}, ...can ? { can } : {}, ...messages ? { messages } : {} };
1991
+ return composeHeaderOptions({
1992
+ ...header,
1993
+ headerTitle: workspace.headerFor(row).title,
1994
+ headerRight: header.headerRight ?? (workspace.headerActions.length ? () => /* @__PURE__ */ jsx(NativeResourceWorkspaceMenu, { ...menu }) : void 0)
1995
+ });
1996
+ }
1997
+ function NativeResourceWorkspace(props) {
1998
+ const { workspace, row, section, onSectionChange, onOpenSection, onSaved, switcher, slots, lists, messages, testID = "native-resource-workspace" } = props;
1999
+ const words = { ...defaultNativeResourceWorkspaceMessages, ...messages };
2000
+ const colors = useThemeRoles();
2001
+ const queryClient = useQueryClient();
2002
+ const record = row;
2003
+ const sections = workspace.sections;
2004
+ const many = sections.length > SEGMENTED_SECTIONS_MAX;
2005
+ const [local, setLocal] = useState(void 0);
2006
+ const activeId = section ?? local ?? (many ? void 0 : sections[0]?.id);
2007
+ const active = activeId ? workspace.section(activeId) : void 0;
2008
+ const readiness = workspace.readinessFor(record);
2009
+ const summary = workspace.readinessSummary(record);
2010
+ const chip = summary ? readinessChip(summary, words) : null;
2011
+ const choose = (id) => {
2012
+ if (many && onOpenSection) return onOpenSection(id);
2013
+ setLocal(id);
2014
+ onSectionChange?.(id);
2015
+ };
2016
+ const top = /* @__PURE__ */ jsxs(View, { style: { paddingHorizontal: 16, paddingTop: 8, gap: 12 }, children: [
2017
+ switcher,
2018
+ chip ? /* @__PURE__ */ jsx(View, { style: { flexDirection: "row" }, children: /* @__PURE__ */ jsx(NativeBadge, { testID: `${testID}-readiness`, variant: badgeVariant(chip.tone), children: chip.label }) }) : null,
2019
+ readiness.some((r) => !r.ok) ? /* @__PURE__ */ jsx(Group, { testID: `${testID}-checks`, title: words.checks, children: readiness.map((r, i) => /* @__PURE__ */ jsx(Cell, { last: i === readiness.length - 1, children: /* @__PURE__ */ jsx(
2020
+ NativeListItem,
2021
+ {
2022
+ testID: `${testID}-check-${i}`,
2023
+ title: r.title,
2024
+ subtitle: r.ok ? void 0 : r.reason,
2025
+ className: "bg-transparent",
2026
+ trailing: /* @__PURE__ */ jsx(NativeBadge, { testID: `${testID}-check-${i}-chip`, variant: badgeVariant(r.tone), children: r.ok ? words.checkDone : words.checkTodo })
2027
+ }
2028
+ ) }, r.key)) }) : null,
2029
+ !many ? /* @__PURE__ */ jsx(
2030
+ NativeSegmentedControl,
2031
+ {
2032
+ testID: `${testID}-sections`,
2033
+ options: sections.map((s) => ({ value: s.id, label: s.label })),
2034
+ value: activeId ?? "",
2035
+ onChange: choose
2036
+ }
2037
+ ) : null
2038
+ ] });
2039
+ let body = null;
2040
+ if (many && !active) {
2041
+ body = /* @__PURE__ */ jsx(ScrollView, { contentContainerStyle: { padding: 16 }, children: /* @__PURE__ */ jsx(Group, { testID: `${testID}-sections`, children: sections.map((s, i) => /* @__PURE__ */ jsx(Cell, { last: i === sections.length - 1, children: /* @__PURE__ */ jsx(NativeListItem, { testID: `${testID}-section-${s.id}`, title: s.label, showChevron: true, onPress: () => choose(s.id), className: "bg-transparent" }) }, s.id)) }) });
2042
+ }
2043
+ const back = many && local !== void 0 && section === void 0 ? /* @__PURE__ */ jsx(View, { style: { paddingHorizontal: 16, paddingTop: 8 }, children: /* @__PURE__ */ jsx(Group, { testID: `${testID}-back`, children: /* @__PURE__ */ jsx(Cell, { last: true, children: /* @__PURE__ */ jsx(
2044
+ NativeListItem,
2045
+ {
2046
+ testID: `${testID}-all-sections`,
2047
+ title: words.allSections,
2048
+ leading: /* @__PURE__ */ jsx(NativeIcon, { name: "chevron-left" }),
2049
+ onPress: () => setLocal(void 0),
2050
+ className: "bg-transparent"
2051
+ }
2052
+ ) }) }) }) : null;
2053
+ if (active?.kind === "list") {
2054
+ body = /* @__PURE__ */ jsx(
2055
+ NativeResourceView,
2056
+ {
2057
+ view: active.view,
2058
+ testID: `${testID}-list`,
2059
+ ...lists?.[active.id],
2060
+ title: false,
2061
+ filter: workspace.listFilter(active.id, record)
2062
+ },
2063
+ active.id
2064
+ );
2065
+ } else if (active?.kind === "settings") {
2066
+ body = /* @__PURE__ */ jsx(ScrollView, { contentContainerStyle: { padding: 16 }, keyboardShouldPersistTaps: "handled", children: /* @__PURE__ */ jsx(
2067
+ SettingsGroup,
2068
+ {
2069
+ workspace,
2070
+ sectionId: active.id,
2071
+ row: record,
2072
+ words,
2073
+ onSaved: (saved) => {
2074
+ void queryClient.invalidateQueries({ queryKey: [...resourceKeys(workspace.view.resource).all] });
2075
+ onSaved?.(saved);
2076
+ },
2077
+ testID: `${testID}-settings`
2078
+ },
2079
+ `${String(workspace.recordId(record))}:${active.id}`
2080
+ ) });
2081
+ } else if (active) {
2082
+ body = slots?.[active.id] ?? null;
2083
+ }
2084
+ return /* @__PURE__ */ jsxs(View, { testID, style: { flex: 1, backgroundColor: colors.groupedBackground }, children: [
2085
+ top,
2086
+ back,
2087
+ /* @__PURE__ */ jsx(View, { style: { flex: 1 }, children: body })
2088
+ ] });
2089
+ }
2090
+ function SettingsGroup({
2091
+ workspace,
2092
+ sectionId,
2093
+ row,
2094
+ words,
2095
+ onSaved,
2096
+ testID
2097
+ }) {
2098
+ const provider = useDataProvider();
2099
+ const view = workspace.view;
2100
+ const fields = useMemo(() => {
2101
+ const byKey = new Map(formFieldsFor(view, "edit").map((f) => [f.key, f]));
2102
+ return workspace.settingsFields(sectionId).map((f) => byKey.get(f.field.name)).filter((f) => !!f && !f.readOnly);
2103
+ }, [workspace, view, sectionId]);
2104
+ const viewer = useViewer$1();
2105
+ const locked = workspace.settingsLocked(row, viewer);
2106
+ const [draft, setDraft] = useState(() => Object.fromEntries(fields.map((f) => [f.key, row[f.key]])));
2107
+ const [invalid, setInvalid] = useState({});
2108
+ const [saving, setSaving] = useState(false);
2109
+ const [refusal, setRefusal] = useState(null);
2110
+ const shown = fields.filter((f) => !f.visible || f.visible(draft));
2111
+ const changed = {};
2112
+ for (const f of shown) if (draft[f.key] !== row[f.key]) changed[f.key] = draft[f.key];
2113
+ const dirty = Object.keys(changed).length > 0 && !shown.some((f) => invalid[f.key]);
2114
+ const set = (key, value, error) => {
2115
+ setRefusal(null);
2116
+ setInvalid(({ [key]: _old, ...rest }) => error ? { ...rest, [key]: error } : rest);
2117
+ setDraft((d) => ({ ...d, [key]: value }));
2118
+ };
2119
+ const save = async () => {
2120
+ setSaving(true);
2121
+ setRefusal(null);
2122
+ try {
2123
+ const result = await provider.update(view.resource, { id: workspace.recordId(row), data: workspace.settingsPatch(sectionId, changed) });
2124
+ if (!result || result.data === null || result.data === void 0) setRefusal(fill(words.notChanged, { noun: view.noun }));
2125
+ else onSaved(result.data);
2126
+ } catch (error) {
2127
+ setRefusal(refusalMessage(error, words.failed));
2128
+ } finally {
2129
+ setSaving(false);
2130
+ }
2131
+ };
2132
+ return /* @__PURE__ */ jsxs(View, { style: { gap: 12 }, children: [
2133
+ refusal ? /* @__PURE__ */ jsx(NativeInlineAlert, { testID: `${testID}-refusal`, variant: "destructive", description: refusal }) : null,
2134
+ locked !== null ? /* @__PURE__ */ jsx(NativeInlineAlert, { testID: `${testID}-locked`, variant: "info", description: locked }) : null,
2135
+ /* @__PURE__ */ jsx(Group, { testID: `${testID}-group`, title: workspace.section(sectionId)?.label, children: shown.map((f, i) => /* @__PURE__ */ jsx(Cell, { last: i === shown.length - 1, children: /* @__PURE__ */ jsx(
2136
+ SettingRow,
2137
+ {
2138
+ field: f,
2139
+ nullable: Boolean(view.field(f.key)?.nullable),
2140
+ value: draft[f.key],
2141
+ error: invalid[f.key],
2142
+ disabled: locked !== null,
2143
+ words,
2144
+ onChange: (v, error) => set(f.key, v, error),
2145
+ testID: `${testID}-${f.key}`
2146
+ }
2147
+ ) }, f.key)) }),
2148
+ locked === null ? /* @__PURE__ */ jsx(NativeButton, { testID: `${testID}-save`, disabled: !dirty || saving, loading: saving, onPress: () => void save(), children: words.save }) : null
2149
+ ] });
2150
+ }
2151
+ function parseSettingNumber(text2) {
2152
+ const t = text2.trim();
2153
+ const normalised = /^-?\d+,\d+$/.test(t) ? t.replace(",", ".") : t;
2154
+ const n = normalised === "" ? Number.NaN : Number(normalised);
2155
+ return Number.isFinite(n) ? n : null;
2156
+ }
2157
+ function SettingRow({
2158
+ field: f,
2159
+ nullable,
2160
+ value,
2161
+ error,
2162
+ disabled,
2163
+ words,
2164
+ onChange,
2165
+ testID
2166
+ }) {
2167
+ const empty = nullable ? null : "";
2168
+ const [typed, setTyped] = useState(() => value == null ? "" : String(value));
2169
+ if (f.kind === "switch") {
2170
+ return /* @__PURE__ */ jsx(
2171
+ NativeListItem,
2172
+ {
2173
+ testID,
2174
+ title: f.label,
2175
+ className: "bg-transparent",
2176
+ disabled,
2177
+ trailing: /* @__PURE__ */ jsx(NativeSwitch, { testID: `${testID}-input`, "aria-label": f.label, checked: value === true, disabled, onCheckedChange: (v) => onChange(v) })
2178
+ }
2179
+ );
2180
+ }
2181
+ const text2 = value == null ? "" : String(value);
2182
+ let control;
2183
+ if (f.kind === "segmented") {
2184
+ control = /* @__PURE__ */ jsx(NativeSegmentedControl, { testID: `${testID}-input`, options: f.options ?? [], value: text2, disabled, onChange: (v) => onChange(v) });
2185
+ } else if (f.kind === "select") {
2186
+ control = /* @__PURE__ */ jsxs(NativeSelect, { value: text2, onValueChange: (v) => onChange(v), children: [
2187
+ /* @__PURE__ */ jsx(NativeSelectTrigger, { testID: `${testID}-input`, placeholder: f.label, disabled }),
2188
+ /* @__PURE__ */ jsx(NativeSelectContent, { children: (f.options ?? []).map((o) => /* @__PURE__ */ jsx(NativeSelectItem, { value: o.value, children: o.label }, o.value)) })
2189
+ ] });
2190
+ } else if (f.kind === "textarea") {
2191
+ control = /* @__PURE__ */ jsx(
2192
+ NativeTextarea,
2193
+ {
2194
+ testID: `${testID}-input`,
2195
+ accessibilityLabel: f.label,
2196
+ editable: !disabled,
2197
+ value: text2,
2198
+ maxLength: f.max,
2199
+ onChangeText: (t) => onChange(t === "" ? empty : t)
2200
+ }
2201
+ );
2202
+ } else if (f.kind === "number") {
2203
+ control = /* @__PURE__ */ jsx(
2204
+ NativeInput,
2205
+ {
2206
+ testID: `${testID}-input`,
2207
+ accessibilityLabel: f.label,
2208
+ editable: !disabled,
2209
+ value: typed,
2210
+ keyboardType: "decimal-pad",
2211
+ error,
2212
+ onChangeText: (t) => {
2213
+ setTyped(t);
2214
+ if (t.trim() === "") return onChange(empty);
2215
+ const n = parseSettingNumber(t);
2216
+ return n === null ? onChange(t, words.notANumber) : onChange(n);
2217
+ }
2218
+ }
2219
+ );
2220
+ } else {
2221
+ control = /* @__PURE__ */ jsx(
2222
+ NativeInput,
2223
+ {
2224
+ testID: `${testID}-input`,
2225
+ accessibilityLabel: f.label,
2226
+ editable: !disabled,
2227
+ value: text2,
2228
+ maxLength: f.max,
2229
+ autoCapitalize: f.kind === "text" ? "sentences" : "none",
2230
+ keyboardType: f.kind === "email" ? "email-address" : f.kind === "url" ? "url" : "default",
2231
+ onChangeText: (t) => onChange(t === "" ? empty : t)
2232
+ }
2233
+ );
2234
+ }
2235
+ return /* @__PURE__ */ jsxs(View, { testID, style: { paddingHorizontal: 16, paddingVertical: 10, gap: 6, opacity: disabled ? 0.6 : 1 }, children: [
2236
+ /* @__PURE__ */ jsx(NativeText, { preset: "body-sm", className: "font-medium", children: f.label }),
2237
+ control
2238
+ ] });
2239
+ }
2240
+ function Group({ title, children, testID }) {
2241
+ const colors = useThemeRoles();
2242
+ const ios = Platform.OS === "ios";
2243
+ return /* @__PURE__ */ jsxs(View, { testID, style: { gap: 6 }, children: [
2244
+ title ? /* @__PURE__ */ jsx(NativeText, { preset: "caption", accessibilityRole: "header", className: ios ? "uppercase text-muted-foreground px-4" : "text-primary px-4", children: title }) : null,
2245
+ /* @__PURE__ */ jsx(View, { style: { backgroundColor: colors.surface, borderRadius: ios ? 10 : 0, overflow: "hidden" }, children })
2246
+ ] });
2247
+ }
2248
+ function Cell({ last, children }) {
2249
+ const colors = useThemeRoles();
2250
+ return /* @__PURE__ */ jsxs(View, { children: [
2251
+ children,
2252
+ !last ? /* @__PURE__ */ jsx(View, { style: { height: 0.5, marginLeft: 16, backgroundColor: colors.separator } }) : null
2253
+ ] });
2254
+ }
1840
2255
 
1841
- export { DEFAULT_MAX_PAGES, DisplayRenderersProvider, DisplayTimeZonesProvider, NativeDataFilters, NativeDataPageShell, NativeDetailPanel, NativeEmptyState2 as NativeEmptyState, NativeResourceForm, NativeResourceFormFieldRenderer, NativeResourceFormSheet, NativeResourcePage, NativeResourceView, NativeResourceViewMessagesProvider, SEARCH_DEBOUNCE_MS, allowed, dateToDay, dayToDate, defaultNativeResourceViewMessages, displayText, formFieldsFor, formatDay, formatInstant, nativeDisplayRenderers, openableUrl, resolveZone, rowItem, screenValues, sectionsFor, serverSort, swipeActionsFor, useDisplayCell, useDisplayTimeZones, useNativeResourceView, useNativeResourceViewMessages, validateValues, writableValues };
2256
+ export { DEFAULT_MAX_PAGES, DisplayRenderersProvider, DisplayTimeZonesProvider, NativeDataFilters, NativeDataPageShell, NativeDetailPanel, NativeEmptyState2 as NativeEmptyState, NativeResourceForm, NativeResourceFormFieldRenderer, NativeResourceFormSheet, NativeResourcePage, NativeResourceView, NativeResourceViewMessagesProvider, NativeResourceWorkspace, NativeResourceWorkspaceMenu, SEARCH_DEBOUNCE_MS, SEGMENTED_SECTIONS_MAX, allowed, dateToDay, dayToDate, defaultNativeResourceViewMessages, defaultNativeResourceWorkspaceMessages, displayText, formFieldsFor, formatDay, formatInstant, getNativeResourceWorkspaceStackOptions, nativeDisplayRenderers, openableUrl, parseSettingNumber, resolveZone, rowItem, screenValues, sectionsFor, serverSort, swipeActionsFor, useDisplayCell, useDisplayTimeZones, useNativeResourceView, useNativeResourceViewMessages, validateValues, writableValues };
1842
2257
  //# sourceMappingURL=index.js.map
1843
2258
  //# sourceMappingURL=index.js.map