@object-ui/react 0.5.0 → 2.0.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 (65) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +13 -0
  3. package/dist/LazyPluginLoader.d.ts +44 -0
  4. package/dist/LazyPluginLoader.js +92 -3
  5. package/dist/components/form/FieldFactory.js +44 -40
  6. package/dist/components/form/FormRenderer.js +45 -3
  7. package/dist/context/ActionContext.d.ts +76 -0
  8. package/dist/context/ActionContext.js +139 -0
  9. package/dist/context/ThemeContext.d.ts +94 -0
  10. package/dist/context/ThemeContext.js +215 -0
  11. package/dist/context/index.d.ts +2 -0
  12. package/dist/context/index.js +2 -0
  13. package/dist/hooks/index.d.ts +5 -0
  14. package/dist/hooks/index.js +5 -0
  15. package/dist/hooks/useActionRunner.d.ts +21 -7
  16. package/dist/hooks/useActionRunner.js +27 -7
  17. package/dist/hooks/useDiscovery.d.ts +78 -0
  18. package/dist/hooks/useDiscovery.js +96 -0
  19. package/dist/hooks/useDynamicApp.d.ts +70 -0
  20. package/dist/hooks/useDynamicApp.js +92 -0
  21. package/dist/hooks/useNavigationOverlay.d.ts +78 -0
  22. package/dist/hooks/useNavigationOverlay.js +112 -0
  23. package/dist/hooks/usePageVariables.d.ts +67 -0
  24. package/dist/hooks/usePageVariables.js +109 -0
  25. package/dist/hooks/useViewData.d.ts +73 -0
  26. package/dist/hooks/useViewData.js +135 -0
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.js +2 -0
  29. package/dist/utils/i18n.d.ts +10 -0
  30. package/dist/utils/i18n.js +12 -0
  31. package/package.json +6 -4
  32. package/src/LazyPluginLoader.tsx +129 -7
  33. package/src/__tests__/LazyPluginLoader.test.tsx +97 -1
  34. package/src/__tests__/ThemeProvider.test.tsx +451 -0
  35. package/src/__tests__/useNavigationOverlay.test.ts +416 -0
  36. package/src/__tests__/usePageVariables.test.tsx +334 -0
  37. package/src/__tests__/useViewData.test.tsx +422 -0
  38. package/src/components/form/FieldFactory.tsx +51 -46
  39. package/src/components/form/FormRenderer.test.tsx +145 -0
  40. package/src/components/form/FormRenderer.tsx +52 -3
  41. package/src/context/ActionContext.tsx +213 -0
  42. package/src/context/ThemeContext.tsx +325 -0
  43. package/src/context/__tests__/ActionContext.test.tsx +174 -0
  44. package/src/context/index.ts +2 -0
  45. package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
  46. package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
  47. package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
  48. package/src/hooks/__tests__/useExpression.test.ts +86 -0
  49. package/src/hooks/index.ts +5 -0
  50. package/src/hooks/useActionRunner.ts +49 -11
  51. package/src/hooks/useDiscovery.ts +141 -0
  52. package/src/hooks/useDynamicApp.ts +142 -0
  53. package/src/hooks/useNavigationOverlay.ts +180 -0
  54. package/src/hooks/usePageVariables.tsx +157 -0
  55. package/src/hooks/useViewData.ts +201 -0
  56. package/src/index.ts +21 -0
  57. package/src/utils/i18n.ts +10 -0
  58. package/tsconfig.json +1 -0
  59. package/tsconfig.tsbuildinfo +1 -1
  60. package/dist/__tests__/LazyPluginLoader.test.d.ts +0 -8
  61. package/dist/__tests__/LazyPluginLoader.test.js +0 -49
  62. package/dist/components/form/FormRenderer.test.d.ts +0 -8
  63. package/dist/components/form/FormRenderer.test.js +0 -534
  64. package/dist/index.test.d.ts +0 -8
  65. package/dist/index.test.js +0 -13
@@ -7,3 +7,5 @@
7
7
  */
8
8
 
9
9
  export * from './SchemaRendererContext';
10
+ export * from './ActionContext';
11
+ export * from './ThemeContext';
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Tests for useActionRunner hook
3
+ */
4
+
5
+ import { describe, it, expect, vi } from 'vitest';
6
+ import { renderHook, waitFor, act } from '@testing-library/react';
7
+ import { useActionRunner } from '../useActionRunner';
8
+
9
+ describe('useActionRunner', () => {
10
+ it('returns initial state with loading=false, error=null, result=null', () => {
11
+ const { result } = renderHook(() => useActionRunner());
12
+
13
+ expect(result.current.loading).toBe(false);
14
+ expect(result.current.error).toBeNull();
15
+ expect(result.current.result).toBeNull();
16
+ });
17
+
18
+ it('exposes runner, execute, and updateContext', () => {
19
+ const { result } = renderHook(() => useActionRunner());
20
+
21
+ expect(result.current.runner).toBeDefined();
22
+ expect(typeof result.current.execute).toBe('function');
23
+ expect(typeof result.current.updateContext).toBe('function');
24
+ });
25
+
26
+ it('handles options with context key format', () => {
27
+ const { result } = renderHook(() =>
28
+ useActionRunner({ context: { data: { name: 'test' } } }),
29
+ );
30
+
31
+ expect(result.current.runner).toBeDefined();
32
+ expect(result.current.loading).toBe(false);
33
+ });
34
+
35
+ it('handles plain context object format (backwards compat)', () => {
36
+ const { result } = renderHook(() =>
37
+ useActionRunner({ data: { name: 'test' } }),
38
+ );
39
+
40
+ expect(result.current.runner).toBeDefined();
41
+ expect(result.current.loading).toBe(false);
42
+ });
43
+
44
+ it('sets loading=true during execution and loading=false after', async () => {
45
+ let resolveAction: () => void;
46
+ const pendingAction = new Promise<void>((resolve) => {
47
+ resolveAction = resolve;
48
+ });
49
+
50
+ const { result } = renderHook(() => useActionRunner());
51
+
52
+ let executePromise: Promise<unknown>;
53
+ act(() => {
54
+ executePromise = result.current.execute({
55
+ onClick: () => pendingAction,
56
+ });
57
+ });
58
+
59
+ await waitFor(() => {
60
+ expect(result.current.loading).toBe(true);
61
+ });
62
+
63
+ await act(async () => {
64
+ resolveAction!();
65
+ await executePromise;
66
+ });
67
+
68
+ expect(result.current.loading).toBe(false);
69
+ });
70
+
71
+ it('sets result on successful action execution', async () => {
72
+ const onNavigate = vi.fn();
73
+ const { result } = renderHook(() =>
74
+ useActionRunner({ context: {}, onNavigate }),
75
+ );
76
+
77
+ await act(async () => {
78
+ await result.current.execute({
79
+ type: 'navigation',
80
+ navigate: { to: '/dashboard' },
81
+ });
82
+ });
83
+
84
+ expect(result.current.result).toEqual({ success: true });
85
+ expect(result.current.error).toBeNull();
86
+ expect(result.current.loading).toBe(false);
87
+ expect(onNavigate).toHaveBeenCalledWith(
88
+ '/dashboard',
89
+ expect.objectContaining({ external: false }),
90
+ );
91
+ });
92
+
93
+ it('sets error on failed action execution (catch path)', async () => {
94
+ const { result } = renderHook(() => useActionRunner());
95
+
96
+ await act(async () => {
97
+ await result.current.execute({
98
+ onClick: () => {
99
+ throw new Error('Something went wrong');
100
+ },
101
+ });
102
+ });
103
+
104
+ expect(result.current.error).toBe('Something went wrong');
105
+ expect(result.current.result).toEqual({
106
+ success: false,
107
+ error: 'Something went wrong',
108
+ });
109
+ expect(result.current.loading).toBe(false);
110
+ });
111
+
112
+ it('updateContext calls runner.updateContext', () => {
113
+ const { result } = renderHook(() => useActionRunner());
114
+
115
+ const spy = vi.spyOn(result.current.runner, 'updateContext');
116
+
117
+ act(() => {
118
+ result.current.updateContext({ data: { id: 42 } });
119
+ });
120
+
121
+ expect(spy).toHaveBeenCalledWith({ data: { id: 42 } });
122
+ });
123
+
124
+ it('accepts handler options (onToast, onNavigate, etc.)', async () => {
125
+ const onToast = vi.fn();
126
+ const onNavigate = vi.fn();
127
+
128
+ const { result } = renderHook(() =>
129
+ useActionRunner({
130
+ context: {},
131
+ onToast,
132
+ onNavigate,
133
+ }),
134
+ );
135
+
136
+ await act(async () => {
137
+ await result.current.execute({
138
+ type: 'navigation',
139
+ navigate: { to: '/settings' },
140
+ successMessage: 'Navigated!',
141
+ });
142
+ });
143
+
144
+ expect(onNavigate).toHaveBeenCalledWith(
145
+ '/settings',
146
+ expect.objectContaining({ external: false }),
147
+ );
148
+ expect(onToast).toHaveBeenCalledWith(
149
+ 'Navigated!',
150
+ expect.objectContaining({ type: 'success' }),
151
+ );
152
+ });
153
+ });
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Tests for useDiscovery hook
3
+ */
4
+
5
+ import React from 'react';
6
+ import { describe, it, expect, vi } from 'vitest';
7
+ import { renderHook, waitFor } from '@testing-library/react';
8
+ import { useDiscovery } from '../useDiscovery';
9
+ import { SchemaRendererContext } from '../../context/SchemaRendererContext';
10
+
11
+ function createWrapper(dataSource: any) {
12
+ return ({ children }: { children: React.ReactNode }) =>
13
+ React.createElement(
14
+ SchemaRendererContext.Provider,
15
+ { value: { dataSource } },
16
+ children,
17
+ );
18
+ }
19
+
20
+ describe('useDiscovery', () => {
21
+ it('returns isLoading false and null discovery when no context is provided', async () => {
22
+ const { result } = renderHook(() => useDiscovery());
23
+
24
+ await waitFor(() => {
25
+ expect(result.current.isLoading).toBe(false);
26
+ });
27
+
28
+ expect(result.current.discovery).toBeNull();
29
+ expect(result.current.error).toBeNull();
30
+ });
31
+
32
+ it('returns isLoading false and null discovery when dataSource has no getDiscovery method', async () => {
33
+ const dataSource = { someOtherMethod: vi.fn() };
34
+
35
+ const { result } = renderHook(() => useDiscovery(), {
36
+ wrapper: createWrapper(dataSource),
37
+ });
38
+
39
+ await waitFor(() => {
40
+ expect(result.current.isLoading).toBe(false);
41
+ });
42
+
43
+ expect(result.current.discovery).toBeNull();
44
+ expect(result.current.error).toBeNull();
45
+ });
46
+
47
+ it('successfully fetches discovery data from dataSource.getDiscovery()', async () => {
48
+ const discoveryData = {
49
+ name: 'test-server',
50
+ version: '1.0.0',
51
+ services: {
52
+ auth: { enabled: true, status: 'available' as const },
53
+ data: { enabled: true, status: 'available' as const },
54
+ },
55
+ };
56
+
57
+ const dataSource = {
58
+ getDiscovery: vi.fn().mockResolvedValue(discoveryData),
59
+ };
60
+
61
+ const { result } = renderHook(() => useDiscovery(), {
62
+ wrapper: createWrapper(dataSource),
63
+ });
64
+
65
+ expect(result.current.isLoading).toBe(true);
66
+
67
+ await waitFor(() => {
68
+ expect(result.current.isLoading).toBe(false);
69
+ });
70
+
71
+ expect(result.current.discovery).toEqual(discoveryData);
72
+ expect(result.current.error).toBeNull();
73
+ expect(dataSource.getDiscovery).toHaveBeenCalled();
74
+ });
75
+
76
+ it('handles errors from getDiscovery gracefully', async () => {
77
+ const dataSource = {
78
+ getDiscovery: vi.fn().mockRejectedValue(new Error('Network error')),
79
+ };
80
+
81
+ const { result } = renderHook(() => useDiscovery(), {
82
+ wrapper: createWrapper(dataSource),
83
+ });
84
+
85
+ await waitFor(() => {
86
+ expect(result.current.isLoading).toBe(false);
87
+ });
88
+
89
+ expect(result.current.discovery).toBeNull();
90
+ expect(result.current.error?.message).toBe('Network error');
91
+ });
92
+
93
+ it('isAuthEnabled defaults to true when no discovery', async () => {
94
+ const { result } = renderHook(() => useDiscovery());
95
+
96
+ await waitFor(() => {
97
+ expect(result.current.isLoading).toBe(false);
98
+ });
99
+
100
+ expect(result.current.isAuthEnabled).toBe(true);
101
+ });
102
+
103
+ it('isAuthEnabled reflects the auth service enabled state', async () => {
104
+ const discoveryData = {
105
+ services: {
106
+ auth: { enabled: false },
107
+ },
108
+ };
109
+
110
+ const dataSource = {
111
+ getDiscovery: vi.fn().mockResolvedValue(discoveryData),
112
+ };
113
+
114
+ const { result } = renderHook(() => useDiscovery(), {
115
+ wrapper: createWrapper(dataSource),
116
+ });
117
+
118
+ await waitFor(() => {
119
+ expect(result.current.isLoading).toBe(false);
120
+ });
121
+
122
+ expect(result.current.isAuthEnabled).toBe(false);
123
+ });
124
+
125
+ it('cleans up on unmount (cancelled flag)', async () => {
126
+ let resolveDiscovery: (value: any) => void;
127
+ const discoveryPromise = new Promise((resolve) => {
128
+ resolveDiscovery = resolve;
129
+ });
130
+
131
+ const dataSource = {
132
+ getDiscovery: vi.fn().mockReturnValue(discoveryPromise),
133
+ };
134
+
135
+ const { result, unmount } = renderHook(() => useDiscovery(), {
136
+ wrapper: createWrapper(dataSource),
137
+ });
138
+
139
+ expect(result.current.isLoading).toBe(true);
140
+
141
+ unmount();
142
+
143
+ // Resolve after unmount — state should not update
144
+ resolveDiscovery!({ name: 'late-response' });
145
+
146
+ // Discovery should remain null since the component was unmounted
147
+ expect(result.current.discovery).toBeNull();
148
+ });
149
+ });
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Tests for useDynamicApp hook
3
+ */
4
+
5
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
6
+ import { renderHook, waitFor, act } from '@testing-library/react';
7
+ import { useDynamicApp } from '../useDynamicApp';
8
+
9
+ describe('useDynamicApp', () => {
10
+ const staticConfig = {
11
+ name: 'crm',
12
+ label: 'CRM App',
13
+ objects: ['contact', 'account'],
14
+ };
15
+
16
+ it('returns static config when no adapter is provided', () => {
17
+ const { result } = renderHook(() =>
18
+ useDynamicApp({ appId: 'crm', staticConfig }),
19
+ );
20
+
21
+ expect(result.current.config).toEqual(staticConfig);
22
+ expect(result.current.isLoading).toBe(false);
23
+ expect(result.current.isServerConfig).toBe(false);
24
+ });
25
+
26
+ it('returns null config when neither adapter nor static config provided', () => {
27
+ const { result } = renderHook(() =>
28
+ useDynamicApp({ appId: 'crm' }),
29
+ );
30
+
31
+ expect(result.current.config).toBeNull();
32
+ expect(result.current.isLoading).toBe(false);
33
+ });
34
+
35
+ it('loads config from server adapter', async () => {
36
+ const serverConfig = { name: 'crm', label: 'CRM (Server)', objects: ['contact'] };
37
+ const adapter = {
38
+ getApp: vi.fn().mockResolvedValue(serverConfig),
39
+ };
40
+
41
+ const { result } = renderHook(() =>
42
+ useDynamicApp({ appId: 'crm', staticConfig, adapter }),
43
+ );
44
+
45
+ // Initially loading
46
+ expect(result.current.isLoading).toBe(true);
47
+
48
+ await waitFor(() => {
49
+ expect(result.current.isLoading).toBe(false);
50
+ });
51
+
52
+ expect(result.current.config).toEqual(serverConfig);
53
+ expect(result.current.isServerConfig).toBe(true);
54
+ expect(adapter.getApp).toHaveBeenCalledWith('crm');
55
+ });
56
+
57
+ it('falls back to static config when server returns null', async () => {
58
+ const adapter = {
59
+ getApp: vi.fn().mockResolvedValue(null),
60
+ };
61
+
62
+ const { result } = renderHook(() =>
63
+ useDynamicApp({ appId: 'crm', staticConfig, adapter }),
64
+ );
65
+
66
+ await waitFor(() => {
67
+ expect(result.current.isLoading).toBe(false);
68
+ });
69
+
70
+ expect(result.current.config).toEqual(staticConfig);
71
+ expect(result.current.isServerConfig).toBe(false);
72
+ });
73
+
74
+ it('falls back to static config on server error', async () => {
75
+ const adapter = {
76
+ getApp: vi.fn().mockRejectedValue(new Error('Network error')),
77
+ };
78
+
79
+ const { result } = renderHook(() =>
80
+ useDynamicApp({ appId: 'crm', staticConfig, adapter }),
81
+ );
82
+
83
+ await waitFor(() => {
84
+ expect(result.current.isLoading).toBe(false);
85
+ });
86
+
87
+ expect(result.current.config).toEqual(staticConfig);
88
+ expect(result.current.isServerConfig).toBe(false);
89
+ expect(result.current.error?.message).toBe('Network error');
90
+ });
91
+
92
+ it('skips loading when enabled is false', () => {
93
+ const adapter = {
94
+ getApp: vi.fn(),
95
+ };
96
+
97
+ const { result } = renderHook(() =>
98
+ useDynamicApp({ appId: 'crm', staticConfig, adapter, enabled: false }),
99
+ );
100
+
101
+ expect(result.current.config).toEqual(staticConfig);
102
+ expect(result.current.isLoading).toBe(false);
103
+ expect(adapter.getApp).not.toHaveBeenCalled();
104
+ });
105
+
106
+ it('refresh invalidates cache and re-fetches', async () => {
107
+ let callCount = 0;
108
+ const adapter = {
109
+ getApp: vi.fn().mockImplementation(async () => {
110
+ callCount++;
111
+ return { name: 'crm', version: callCount };
112
+ }),
113
+ invalidateCache: vi.fn(),
114
+ };
115
+
116
+ const { result } = renderHook(() =>
117
+ useDynamicApp({ appId: 'crm', adapter }),
118
+ );
119
+
120
+ await waitFor(() => {
121
+ expect(result.current.isLoading).toBe(false);
122
+ });
123
+
124
+ expect((result.current.config as any).version).toBe(1);
125
+
126
+ await act(async () => {
127
+ await result.current.refresh();
128
+ });
129
+
130
+ expect(adapter.invalidateCache).toHaveBeenCalledWith('app:crm');
131
+ expect((result.current.config as any).version).toBe(2);
132
+ });
133
+
134
+ it('reloads when appId changes', async () => {
135
+ const adapter = {
136
+ getApp: vi.fn().mockImplementation(async (appId: string) => {
137
+ return { name: appId };
138
+ }),
139
+ };
140
+
141
+ const { result, rerender } = renderHook(
142
+ ({ appId }: { appId: string }) =>
143
+ useDynamicApp({ appId, adapter }),
144
+ { initialProps: { appId: 'crm' } },
145
+ );
146
+
147
+ await waitFor(() => {
148
+ expect(result.current.isLoading).toBe(false);
149
+ });
150
+
151
+ expect((result.current.config as any).name).toBe('crm');
152
+
153
+ rerender({ appId: 'erp' });
154
+
155
+ await waitFor(() => {
156
+ expect((result.current.config as any).name).toBe('erp');
157
+ });
158
+
159
+ expect(adapter.getApp).toHaveBeenCalledWith('erp');
160
+ });
161
+ });
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Tests for useExpression and useCondition hooks
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest';
6
+ import { renderHook } from '@testing-library/react';
7
+ import { useExpression, useCondition } from '../useExpression';
8
+
9
+ describe('useExpression', () => {
10
+ it('returns string value directly for non-expression strings', () => {
11
+ const { result } = renderHook(() => useExpression('hello'));
12
+
13
+ expect(result.current).toBe('hello');
14
+ });
15
+
16
+ it('returns number value directly', () => {
17
+ const { result } = renderHook(() => useExpression(42));
18
+
19
+ expect(result.current).toBe(42);
20
+ });
21
+
22
+ it('returns boolean value directly', () => {
23
+ const { result } = renderHook(() => useExpression(true));
24
+
25
+ expect(result.current).toBe(true);
26
+ });
27
+
28
+ it('returns null for null expressions', () => {
29
+ const { result } = renderHook(() => useExpression(null));
30
+
31
+ expect(result.current).toBeNull();
32
+ });
33
+
34
+ it('returns undefined for undefined expressions', () => {
35
+ const { result } = renderHook(() => useExpression(undefined));
36
+
37
+ expect(result.current).toBeUndefined();
38
+ });
39
+
40
+ it('evaluates expressions with ${...} syntax', () => {
41
+ const context = { data: { name: 'John' } };
42
+ const { result } = renderHook(() =>
43
+ useExpression('${data.name}', context),
44
+ );
45
+
46
+ expect(result.current).toBe('John');
47
+ });
48
+
49
+ it('evaluates expressions with context data', () => {
50
+ const context = { data: { age: 25 } };
51
+ const { result } = renderHook(() =>
52
+ useExpression('${data.age > 18}', context),
53
+ );
54
+
55
+ expect(result.current).toBe(true);
56
+ });
57
+ });
58
+
59
+ describe('useCondition', () => {
60
+ it('returns true for boolean true', () => {
61
+ const { result } = renderHook(() => useCondition(true));
62
+
63
+ expect(result.current).toBe(true);
64
+ });
65
+
66
+ it('returns false for boolean false', () => {
67
+ const { result } = renderHook(() => useCondition(false));
68
+
69
+ expect(result.current).toBe(false);
70
+ });
71
+
72
+ it('returns true for undefined', () => {
73
+ const { result } = renderHook(() => useCondition(undefined));
74
+
75
+ expect(result.current).toBe(true);
76
+ });
77
+
78
+ it('evaluates string conditions with context', () => {
79
+ const context = { data: { status: 'active' } };
80
+ const { result } = renderHook(() =>
81
+ useCondition('${data.status === "active"}', context),
82
+ );
83
+
84
+ expect(result.current).toBe(true);
85
+ });
86
+ });
@@ -8,4 +8,9 @@
8
8
 
9
9
  export * from './useExpression';
10
10
  export * from './useActionRunner';
11
+ export * from './useNavigationOverlay';
12
+ export * from './usePageVariables';
13
+ export * from './useViewData';
14
+ export * from './useDynamicApp';
15
+ export * from './useDiscovery';
11
16
 
@@ -7,36 +7,74 @@
7
7
  */
8
8
 
9
9
  import { useState, useCallback, useMemo } from 'react';
10
- import { ActionRunner, type ActionContext, type ActionResult } from '@object-ui/core';
10
+ import {
11
+ ActionRunner,
12
+ type ActionContext,
13
+ type ActionDef,
14
+ type ActionResult,
15
+ type ConfirmationHandler,
16
+ type ToastHandler,
17
+ type ModalHandler,
18
+ type NavigationHandler,
19
+ } from '@object-ui/core';
20
+
21
+ export interface UseActionRunnerOptions {
22
+ context?: ActionContext;
23
+ /** Custom confirmation handler — wire to Shadcn AlertDialog or similar */
24
+ onConfirm?: ConfirmationHandler;
25
+ /** Custom toast handler — wire to Sonner or similar */
26
+ onToast?: ToastHandler;
27
+ /** Custom modal handler — render modals in response to modal actions */
28
+ onModal?: ModalHandler;
29
+ /** Custom navigation handler — SPA-aware routing */
30
+ onNavigate?: NavigationHandler;
31
+ }
11
32
 
12
33
  /**
13
- * Hook for executing actions with loading state
34
+ * Hook for executing actions with loading state, typed with ActionDef.
14
35
  *
15
36
  * @example
16
37
  * ```tsx
17
- * const { execute, loading, error, result } = useActionRunner({ data: formData });
38
+ * const { execute, loading, error, result } = useActionRunner({
39
+ * context: { data: formData },
40
+ * onToast: (msg, opts) => toast[opts?.type || 'info'](msg),
41
+ * });
18
42
  *
19
43
  * const handleClick = () => {
20
44
  * execute({
21
- * type: 'action',
45
+ * type: 'api',
22
46
  * api: '/api/save',
23
- * successMessage: 'Saved successfully!'
47
+ * successMessage: 'Saved successfully!',
24
48
  * });
25
49
  * };
26
50
  * ```
27
51
  */
28
- export function useActionRunner(context: ActionContext = {}) {
52
+ export function useActionRunner(
53
+ optionsOrContext: UseActionRunnerOptions | ActionContext = {},
54
+ ) {
55
+ // Backwards compat: if no 'context' key, treat entire object as context
56
+ const options: UseActionRunnerOptions = 'context' in optionsOrContext
57
+ ? optionsOrContext as UseActionRunnerOptions
58
+ : { context: optionsOrContext as ActionContext };
59
+
60
+ const { context = {}, onConfirm, onToast, onModal, onNavigate } = options;
61
+
29
62
  const [loading, setLoading] = useState(false);
30
63
  const [error, setError] = useState<string | null>(null);
31
64
  const [result, setResult] = useState<ActionResult | null>(null);
32
65
 
33
- const runner = useMemo(
34
- () => new ActionRunner(context),
35
- [JSON.stringify(context)]
36
- );
66
+ const runner = useMemo(() => {
67
+ const r = new ActionRunner(context);
68
+ if (onConfirm) r.setConfirmHandler(onConfirm);
69
+ if (onToast) r.setToastHandler(onToast);
70
+ if (onModal) r.setModalHandler(onModal);
71
+ if (onNavigate) r.setNavigationHandler(onNavigate);
72
+ return r;
73
+ // eslint-disable-next-line react-hooks/exhaustive-deps
74
+ }, [JSON.stringify(context)]);
37
75
 
38
76
  const execute = useCallback(
39
- async (action: any): Promise<ActionResult> => {
77
+ async (action: ActionDef): Promise<ActionResult> => {
40
78
  setLoading(true);
41
79
  setError(null);
42
80
  setResult(null);