@mochabug/adapt-react 1.0.0-rc1

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/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # @mochabug/adapt-react
2
+
3
+ React component for Adapt.
4
+
5
+ ```bash
6
+ npm install @mochabug/adapt-react
7
+ ```
8
+
9
+ Requires React 17, 18, or 19.
10
+
11
+ ## Quickstart
12
+
13
+ ```tsx
14
+ import { AdaptAutomation } from '@mochabug/adapt-react';
15
+
16
+ <AdaptAutomation id="auto-123" style={{ height: 600 }} />
17
+ ```
18
+
19
+ If the automation requires authentication:
20
+
21
+ ```tsx
22
+ <AdaptAutomation id="auto-123" authToken="your-token" style={{ height: 600 }} />
23
+ ```
24
+
25
+ ## SSR (Next.js)
26
+
27
+ Keep auth token on server:
28
+
29
+ ```tsx
30
+ // app/page.tsx (Server Component)
31
+ import { startSession } from '@mochabug/adapt-core';
32
+
33
+ export default async function Page() {
34
+ const authToken = await getAuthTokenFromBackend();
35
+ const { token } = await startSession({ id: 'auto-123' }, authToken);
36
+
37
+ return <AutomationClient sessionToken={token} />;
38
+ }
39
+
40
+ // components/AutomationClient.tsx (Client Component)
41
+ 'use client';
42
+ import { AdaptAutomation } from '@mochabug/adapt-react';
43
+
44
+ export function AutomationClient({ sessionToken }) {
45
+ return (
46
+ <AdaptAutomation
47
+ id="auto-123"
48
+ sessionToken={sessionToken}
49
+ style={{ height: 600 }}
50
+ />
51
+ );
52
+ }
53
+ ```
54
+
55
+ ## Session inheritance
56
+
57
+ ```tsx
58
+ // direct
59
+ <AdaptAutomation id="auto-123" inheritToken="token-from-parent" />
60
+
61
+ // from URL hash: example.com#mb_session=xxx
62
+ <AdaptAutomation id="auto-123" inheritFrom={{ hash: 'mb_session' }} />
63
+ ```
64
+
65
+ ## Fork display
66
+
67
+ ```tsx
68
+ // side-by-side
69
+ <AdaptAutomation
70
+ id="auto-123"
71
+ forkDisplayMode="side-by-side"
72
+ sideBySideSplit={60}
73
+ />
74
+
75
+ // dialog
76
+ <AdaptAutomation
77
+ id="auto-123"
78
+ forkDisplayMode="dialog"
79
+ dialogBackdropClose
80
+ />
81
+ ```
82
+
83
+ ## Callbacks
84
+
85
+ ```tsx
86
+ <AdaptAutomation
87
+ id="auto-123"
88
+ onSession={(status, fork) => console.log(status, fork)}
89
+ onOutput={(output) => console.log(output)}
90
+ />
91
+ ```
92
+
93
+ ## Props
94
+
95
+ | Prop | Type |
96
+ |------|------|
97
+ | `id` | `string` (required) |
98
+ | `sessionToken` | `string` |
99
+ | `authToken` | `string` |
100
+ | `inheritToken` | `string` |
101
+ | `inheritFrom` | `{ hash: string } \| { param: string }` |
102
+ | `forkDisplayMode` | `'side-by-side' \| 'dialog'` |
103
+ | `sideBySideSplit` | `number` (0-100) |
104
+ | `dialogBackdropClose` | `boolean` |
105
+ | `onSession` | `(status, fork?) => void` |
106
+ | `onOutput` | `(output) => void` |
107
+ | `className` | `string` |
108
+ | `style` | `CSSProperties` |
109
+
110
+ ## License
111
+
112
+ ISC © mochabug AB
@@ -0,0 +1,71 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useEffect, useRef, forwardRef } from "react";
3
+ import { AdaptWebClient, } from "@mochabug/adapt-web";
4
+ /**
5
+ * React component for embedding Adapt automations.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { AdaptAutomation } from '@mochabug/adapt-react';
10
+ *
11
+ * function App() {
12
+ * return (
13
+ * <AdaptAutomation
14
+ * id="automation-123"
15
+ * style={{ height: '600px' }}
16
+ * onSession={(status) => console.log('Status:', status)}
17
+ * />
18
+ * );
19
+ * }
20
+ * ```
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * // With session inheritance from URL hash
25
+ * <AdaptAutomation
26
+ * id="automation-123"
27
+ * inheritFrom={{ hash: 'mb_session' }}
28
+ * style={{ height: '100vh' }}
29
+ * />
30
+ * ```
31
+ */
32
+ export const AdaptAutomation = forwardRef(function AdaptAutomation({ id, sessionToken, authToken, inheritToken, inheritFrom, forkDisplayMode, sideBySideSplit, dialogBackdropClose, onSession, onOutput, classNames, className, style, }, ref) {
33
+ const clientRef = useRef(null);
34
+ const internalRef = useRef(null);
35
+ const containerIdRef = useRef(`adapt-${Math.random().toString(36).slice(2, 11)}`);
36
+ const containerId = containerIdRef.current;
37
+ // Merge refs
38
+ const setRef = (element) => {
39
+ internalRef.current =
40
+ element;
41
+ if (typeof ref === "function") {
42
+ ref(element);
43
+ }
44
+ else if (ref) {
45
+ ref.current = element;
46
+ }
47
+ };
48
+ useEffect(() => {
49
+ clientRef.current = new AdaptWebClient({
50
+ container: containerId,
51
+ id,
52
+ sessionToken,
53
+ authToken,
54
+ inheritToken,
55
+ inheritFrom,
56
+ forkDisplayMode,
57
+ sideBySideSplit,
58
+ dialogBackdropClose,
59
+ onSession,
60
+ onOutput,
61
+ classNames,
62
+ });
63
+ return () => {
64
+ clientRef.current?.destroy();
65
+ clientRef.current = null;
66
+ };
67
+ // Only recreate client if automation ID changes
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ }, [id]);
70
+ return (_jsx("div", { ref: setRef, id: containerId, className: className, style: style }));
71
+ });
@@ -0,0 +1,41 @@
1
+ import { type AdaptWebClientOptions } from "@mochabug/adapt-web";
2
+ import type { Output, StatusJson } from "@mochabug/adapt-core";
3
+ export type { Output, StatusJson };
4
+ /**
5
+ * Props for the AdaptAutomation React component.
6
+ */
7
+ export interface AdaptAutomationProps extends Pick<AdaptWebClientOptions, "id" | "sessionToken" | "authToken" | "inheritToken" | "inheritFrom" | "forkDisplayMode" | "sideBySideSplit" | "dialogBackdropClose" | "onSession" | "onOutput" | "classNames"> {
8
+ /** CSS class name for the container */
9
+ className?: string;
10
+ /** Inline styles for the container */
11
+ style?: React.CSSProperties;
12
+ }
13
+ /**
14
+ * React component for embedding Adapt automations.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * import { AdaptAutomation } from '@mochabug/adapt-react';
19
+ *
20
+ * function App() {
21
+ * return (
22
+ * <AdaptAutomation
23
+ * id="automation-123"
24
+ * style={{ height: '600px' }}
25
+ * onSession={(status) => console.log('Status:', status)}
26
+ * />
27
+ * );
28
+ * }
29
+ * ```
30
+ *
31
+ * @example
32
+ * ```tsx
33
+ * // With session inheritance from URL hash
34
+ * <AdaptAutomation
35
+ * id="automation-123"
36
+ * inheritFrom={{ hash: 'mb_session' }}
37
+ * style={{ height: '100vh' }}
38
+ * />
39
+ * ```
40
+ */
41
+ export declare const AdaptAutomation: import("react").ForwardRefExoticComponent<AdaptAutomationProps & import("react").RefAttributes<HTMLDivElement>>;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@mochabug/adapt-react",
3
+ "version": "1.0.0-rc1",
4
+ "description": "React component for Adapt automation platform",
5
+ "type": "module",
6
+ "main": "./dist/esm/index.js",
7
+ "types": "./dist/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/types/index.d.ts",
11
+ "import": "./dist/esm/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build:esm": "tsc --project tsconfig.esm.json",
19
+ "build:types": "tsc --project tsconfig.types.json",
20
+ "build": "rm -rf dist && npm run build:esm && npm run build:types",
21
+ "sample": "cd sample && npm run dev"
22
+ },
23
+ "keywords": [
24
+ "adapt",
25
+ "automations",
26
+ "mochabug",
27
+ "react"
28
+ ],
29
+ "author": "mochabug AB",
30
+ "license": "ISC",
31
+ "peerDependencies": {
32
+ "react": "^17.0.0 || ^18.0.0 || ^19.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/react": "^19.2.6",
36
+ "react": "^19.2.0",
37
+ "typescript": "^5.9.3"
38
+ },
39
+ "dependencies": {
40
+ "@mochabug/adapt-web": "^1.0.0-rc22"
41
+ }
42
+ }