@wise/dynamic-flow-client-internal 0.0.0-experimental-20240319142919

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,243 @@
1
+ # Dynamic Flow Web Client for Wise
2
+
3
+ This is the Wise Dynamic Flow web client. It provides a simple way to integrate a Dynamic Flow into your Wise web app.
4
+
5
+ ⚡ Access the latest deployed version of the [Dynamic Flow Playground](https://main--641de2ace0e1566a6a450fad.chromatic.com).
6
+
7
+ ## Integration
8
+
9
+ 1. Install `@wise/dynamic-flow-client-internal`.
10
+
11
+ ```
12
+ # yarn
13
+ yarn add @wise/dynamic-flow-client-internal
14
+
15
+ # npm
16
+ npm install @wise/dynamic-flow-client-internal
17
+
18
+ # pnpm
19
+ pnpm install @wise/dynamic-flow-client-internal
20
+ ```
21
+
22
+ 2. Install required peer dependencies (if not already installed). Please refer to setup instructions for `@transferwise/components` and `@transferwise/neptune-css` if installing up for the first time.
23
+
24
+ ```
25
+ # yarn
26
+ yarn add prop-types react react-dom react-intl
27
+ yarn add @transferwise/components @transferwise/formatting @transferwise/icons @transferwise/neptune-css
28
+
29
+ # npm
30
+ npm install prop-types react react-dom react-intl
31
+ npm install @transferwise/components @transferwise/formatting @transferwise/icons @transferwise/neptune-css
32
+
33
+ # pnpm
34
+ pnpm install prop-types react react-dom react-intl
35
+ pnpm install @transferwise/components @transferwise/formatting @transferwise/icons @transferwise/neptune-css
36
+ ```
37
+
38
+ **Note:** Keep in mind that some of these dependencies have their own peer dependencies. Don't forget to install those, for instance: `@transferwise/components` needs `@wise/art` and `@wise/components-theming`.
39
+
40
+ ```js
41
+ // Should be imported once in your application
42
+ import '@wise/dynamic-flow-client-internal/build/main.css';
43
+ ```
44
+
45
+ The `DynamicFlow` component must be wraped in a Neptune `Provider` to support localisation, a `ThemeProvider` to provide theming, and a `SnackbarProvider` to ensure snackbars display correctly. Translations should be imported from both components and dynamic flows, merged, and passed to the `Provider` component (as below).
46
+
47
+ ### For CRAB apps, use the `getLocalisedMessages(...)` function for translations
48
+
49
+ ```js
50
+ import {
51
+ Provider,
52
+ SnackbarProvider,
53
+ translations as componentTranslations,
54
+ } from '@transferwise/components';
55
+ import { getLocalisedMessages } from '@transferwise/crab/client';
56
+ import {
57
+ DynamicFlow,
58
+ translations as dynamicFlowsTranslations,
59
+ } from '@wise/dynamic-flow-client-internal';
60
+
61
+ const messages = getLocalisedMessages(locale, [componentTranslations, dynamicFlowsTranslations])
62
+
63
+ return (
64
+ <Provider i18n={{ locale, messages }}>
65
+ <ThemeProvider theme={theme} screenMode={screenMode}>
66
+ <SnackbarProvider>
67
+ <DynamicFlow {...props} />
68
+ </SnackbarProvider>
69
+ </ThemeProvider>
70
+ </Provider>
71
+ );
72
+ ```
73
+
74
+ ### For non-CRAB apps
75
+
76
+ You'll need to merge the '@transferwise/components' translations with the '@wise/dynamic-flow-client' translations.
77
+
78
+ ```js
79
+ import {
80
+ Provider,
81
+ SnackbarProvider,
82
+ translations as componentTranslations,
83
+ } from '@transferwise/components';
84
+ import {
85
+ DynamicFlow,
86
+ translations as dynamicFlowsTranslations,
87
+ } from '@wise/dynamic-flow-client-internal';
88
+
89
+ // create your messages object
90
+ const messages: Record<string, string> = {
91
+ ...(componentTranslations[lang] || componentTranslations[lang.replace('-', '_')] || componentTranslations[lang.substring(0, 2)] || {}),
92
+ ...(translations[lang] || translations[lang.replace('-', '_')] || translations[lang.substring(0, 2)] || {}),
93
+ }
94
+
95
+ return (
96
+ <Provider i18n={{ locale, messages }}>
97
+ <ThemeProvider theme={theme} screenMode={screenMode}>
98
+ <SnackbarProvider>
99
+ <DynamicFlow {...props} />
100
+ </SnackbarProvider>
101
+ </ThemeProvider>
102
+ </Provider>
103
+ );
104
+ ```
105
+
106
+ ## Configuring your Flow
107
+
108
+ DF can be initialised with `initialAction` (recommended) or with an `initialStep`.
109
+
110
+ ```tsx
111
+ <DynamicFlow
112
+ initialAction={{ method: 'GET', url: '/my-amazing-new-flow' }}
113
+ customFetch={(...args) => fetch(...args)}
114
+ onCompletion={(result) => {
115
+ console.log('Flow exited with', result);
116
+ }}
117
+ onError={(error, statusCode) => {
118
+ console.error('Flow Error:', error, 'status code', statusCode);
119
+ }}
120
+ />
121
+ ```
122
+
123
+ ### When to use `initialStep` instead of `initialAction`
124
+
125
+ In some cases you may want to obtain the initial step yourself, and then pass it to DF component. In these cases you don't provide a `initialAction` since the next steps will result from interactions in the provided `initialStep`:
126
+
127
+ ```tsx
128
+ <DynamicFlow
129
+ initialStep={someInitialStep}
130
+ customFetch={...}
131
+ onCompletion={...}
132
+ onError={...}
133
+ />
134
+ ```
135
+
136
+ ### The `customFetch`
137
+
138
+ You probably want to pass a custom fetch function. This would allow you to add additional request headers to each network request and possibly prefix a base URL to relative paths.
139
+
140
+ You can take advantage of the `makeCustomFetch` utility function. This function takes a `baseUrl` and `additionalHeaders` arguments. The `baseUrl` will be prefixed to any relative request URLs. Absolute URLs will not be altered. The `additionalHeaders` parameter can be used to add any request headers you need in all requests, such as `{ 'X-Access-Token': 'Tr4n5f3rw153' }`:
141
+
142
+ ```tsx
143
+ import { makeCustomFetch, DynamicFlow } from '@wise/dynamic-flow-client-internal';
144
+
145
+ const customFetch = makeCustomFetch('/gateway', {
146
+ 'Accept-Language': currentLanguage,
147
+ 'X-Access-Token': 'Tr4n5f3rw153',
148
+ 'X-Visual-Context': 'personal::light'
149
+ });
150
+
151
+ ...
152
+
153
+ <DynamicFlow
154
+ initialAction={{ method: 'GET', url: '/my-flow' }}
155
+ customFetch={customFetch}
156
+ onCompletion={...}
157
+ onError={...}
158
+ />
159
+ ```
160
+
161
+ #### Writing your own `customFetch` function
162
+
163
+ If you want to write your own `customFetch` (or if you're writing mocks), it's important that you return proper `Response` objects, and that you **do not throw**. Errors should result in a response with an error status code and potentially a body with an error message. For example:
164
+
165
+ ```tsx
166
+ const mockCustomFetch = (input, init) => {
167
+ switch (input) {
168
+ case '/standard':
169
+ return Promise.resolve(new Response(init.body));
170
+ case '/exit':
171
+ return Promise.resolve(new Response(init.body, { headers: { 'x-df-exit': true } }));
172
+ case '/error':
173
+ default:
174
+ return Promise.resolve(new Response('An error has occurred.', { status: 500 }));
175
+ }
176
+ };
177
+ ```
178
+
179
+ Also, please make sure your mocks return a new `Response` instace every time. This is because responses are mutated when we parse their body, and they cannot be parsed a second time.
180
+
181
+ ```ts
182
+ const initialResponse = new Response(JSON.stringify(initialStep));
183
+ // ❌ wrong - the same instance is returned on each request
184
+ const mockCustomFetch = (input, init) => Promise.resolve(initialResponse);
185
+ ```
186
+
187
+ ```ts
188
+ // ✅ correct - a new instance is returned on each request
189
+ const mockCustomFetch = (input, init) => Promise.resolve(new Response(JSON.stringify(initialStep)));
190
+ ```
191
+
192
+ ### Telemetry
193
+
194
+ The `DynamicFlow` component accepts two optional props: `onEvent` and `onLog` which can be used to track and log.
195
+
196
+ In the example below we send tracking events to Mixpanel and logging events to Rollbar.
197
+
198
+ ```tsx
199
+ <DynamicFlow
200
+ onEvent={(event, props) => mixpanel.track(event, props)}
201
+ onLog={(level, message, extra) => Rollbar[level](message, extra)}
202
+ />
203
+ ```
204
+
205
+ Alternatively, you can log to the browser console:
206
+
207
+ ```ts
208
+ onEvent={(event, props) => console.log(event, props)}
209
+ onLog={(level, message, extra) => {
210
+ const levelToConsole = {
211
+ critical: console.error,
212
+ error: console.error,
213
+ warning: console.warn,
214
+ info: console.info,
215
+ debug: console.log,
216
+ } as const;
217
+ levelToConsole[level](message, extra);
218
+ }}
219
+ ```
220
+
221
+ ### Loader configuration
222
+
223
+ By default, DF will display a loading animation (The `Loader` component from Neptune) when the first step is loading. It will not display it during refresh-on-change or while submitting forms.
224
+
225
+ You can change the defaults by passing a `loaderConfig` prop:
226
+
227
+ ```ts
228
+ type LoaderConfig = {
229
+ size?: `xs | sm | md | lg | xl`;
230
+ initial?: boolean;
231
+ submission?: boolean;
232
+ };
233
+ ```
234
+
235
+ | property | type | notes | default |
236
+ | ------------ | ------- | ------------------------------------------------------------------------------ | ------- |
237
+ | `size` | string | The size of the Loader component. | `xl` |
238
+ | `initial` | boolean | Whether or not to display the Loader component while loading the initial step. | true |
239
+ | `submission` | boolean | Whether or not to display the Loader component during form submissions. | false |
240
+
241
+ ## Contributing
242
+
243
+ We love contributions! Check out `CONTRIBUTING.md` for more information.
@@ -0,0 +1,19 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { DynamicFlowRevamp, DynamicFlow as DynamicFlowLegacy, } from '@wise/dynamic-flow-client';
14
+ export function DynamicFlow(props) {
15
+ var _a = props.customFetch, customFetch = _a === void 0 ? globalThis.fetch : _a, features = props.features;
16
+ var coreProps = __assign(__assign({}, props), { httpClient: customFetch });
17
+ var revampEnabled = features === null || features === void 0 ? void 0 : features.find(function (feature) { return feature.featureName === 'dynamic-flow-revamp'; });
18
+ return revampEnabled ? (_jsx(DynamicFlowRevamp, __assign({}, coreProps))) : (_jsx(DynamicFlowLegacy, __assign({}, coreProps)));
19
+ }
package/build/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { makeHttpClient } from '@wise/dynamic-flow-client';
2
+ export { translations, JsonSchemaForm, isValidSchema } from '@wise/dynamic-flow-client';
3
+ export { DynamicFlow } from './dynamicFlow/DynamicFlow';
4
+ export var makeCustomFetch = makeHttpClient;
package/build/main.css ADDED
@@ -0,0 +1,263 @@
1
+ .tw-form-control textarea {
2
+ resize: vertical;
3
+ }
4
+ .camera-capture {
5
+ background: #000;
6
+ height: 100%;
7
+ left: 0;
8
+ position: fixed;
9
+ top: 0;
10
+ width: 100%;
11
+ z-index: 1030;
12
+ }
13
+ .camera-capture video {
14
+ display: block;
15
+ height: 100%;
16
+ left: 0;
17
+ object-fit: cover;
18
+ position: absolute;
19
+ top: 0;
20
+ width: 100%;
21
+ z-index: 0;
22
+ }
23
+ .camera-capture .bottom-bar {
24
+ bottom: 0;
25
+ display: flex;
26
+ flex-direction: column;
27
+ left: 0;
28
+ position: absolute;
29
+ width: 100%;
30
+ }
31
+ .camera-capture .camera-capture-btn {
32
+ align-self: center;
33
+ background-color: transparent;
34
+ background-image: none;
35
+ border: 6px solid #f8f9fa;
36
+ border-radius: 50%;
37
+ cursor: pointer;
38
+ display: inline-block;
39
+ height: 76px;
40
+ position: relative;
41
+ touch-action: manipulation;
42
+ transition: all 0.15s ease-in-out;
43
+ user-select: none;
44
+ vertical-align: middle;
45
+ width: 76px;
46
+ }
47
+ .camera-capture .camera-capture-btn:hover {
48
+ border-color: #cbd3da;
49
+ }
50
+ .camera-capture .camera-capture-btn:hover .camera-capture-btn-inner {
51
+ background-color: #cbd3da;
52
+ }
53
+ .camera-capture .camera-capture-btn:active {
54
+ border-color: #9fadba;
55
+ }
56
+ .camera-capture .camera-capture-btn:active .camera-capture-btn-inner {
57
+ background-color: #9fadba;
58
+ }
59
+ .camera-capture .camera-capture-btn:focus,
60
+ .camera-capture .camera-capture-btn:focus-visible {
61
+ outline: 0;
62
+ }
63
+ .camera-capture .camera-capture-btn .camera-capture-btn-inner {
64
+ background-color: #f8f9fa;
65
+ border-radius: 50%;
66
+ display: inline-block;
67
+ height: 60px;
68
+ margin: 2px;
69
+ transition: all 0.15s ease-in-out;
70
+ width: 60px;
71
+ }
72
+ .camera-capture svg {
73
+ display: block;
74
+ height: 100%;
75
+ left: 0;
76
+ object-fit: cover;
77
+ position: absolute;
78
+ top: 0;
79
+ width: 100%;
80
+ z-index: 0;
81
+ }
82
+ .camera-capture svg .camera-capture-text-and-image-container {
83
+ align-items: center;
84
+ display: flex;
85
+ flex-direction: column;
86
+ gap: 8px;
87
+ justify-content: center;
88
+ overflow: hidden;
89
+ position: absolute;
90
+ }
91
+ .camera-capture svg .camera-capture-instructions,
92
+ .camera-capture svg .camera-capture-title {
93
+ color: #fff;
94
+ display: block;
95
+ text-align: center;
96
+ }
97
+ .camera-capture svg .camera-capture-title {
98
+ overflow: hidden;
99
+ text-overflow: ellipsis;
100
+ white-space: normal;
101
+ }
102
+ .camera-capture svg .camera-capture-img {
103
+ display: block;
104
+ filter: invert(100%);
105
+ height: 32px;
106
+ position: relative;
107
+ width: 32px;
108
+ }
109
+ .camera-capture svg foreignObject {
110
+ overflow: visible;
111
+ }
112
+ .camera-capture .review-image {
113
+ display: block;
114
+ height: 100%;
115
+ left: 0;
116
+ object-fit: cover;
117
+ position: absolute;
118
+ top: 0;
119
+ width: 100%;
120
+ z-index: 0;
121
+ }
122
+ .df-back-btn .tw-avatar__content {
123
+ cursor: pointer;
124
+ background-color: var(--color-background-neutral);
125
+ }
126
+ .df-back-btn:hover .tw-avatar__content {
127
+ background-color: var(--color-background-neutral-hover);
128
+ }
129
+ .df-back-btn:active .tw-avatar__content {
130
+ background-color: var(--color-background-neutral-active);
131
+ }
132
+ .orientation-lock-overlay {
133
+ display: none;
134
+ position: fixed;
135
+ inset: 0;
136
+ background-color: var(--color-dark);
137
+ color: var(--color-white);
138
+ font-size: var(--font-size-16);
139
+ font-weight: var(--font-weight-semi-bold);
140
+ flex-direction: column;
141
+ justify-content: center;
142
+ align-items: center;
143
+ z-index: 1000;
144
+ padding: 24px;
145
+ }
146
+ @media screen and (orientation: landscape) and (height <= 767px) {
147
+ .orientation-lock-overlay {
148
+ display: flex;
149
+ }
150
+ }
151
+ .df-image {
152
+ display: flex;
153
+ justify-content: center;
154
+ align-items: center;
155
+ }
156
+ .df-image img {
157
+ height: auto;
158
+ width: 100%;
159
+ }
160
+ /* narrow screens */
161
+ .df-image.xs img {
162
+ width: 50px;
163
+ }
164
+ .df-image.sm img {
165
+ width: 100px;
166
+ }
167
+ .df-image.md img {
168
+ width: 200px;
169
+ }
170
+ .df-image.lg img {
171
+ width: 300px;
172
+ }
173
+ /* wide screens */
174
+ @media (width >= 576px) {
175
+ .df-image.xs img {
176
+ width: 100px;
177
+ }
178
+
179
+ .df-image.sm img {
180
+ width: 200px;
181
+ }
182
+
183
+ .df-image.md img {
184
+ width: 300px;
185
+ }
186
+
187
+ .df-image.lg img {
188
+ width: 500px;
189
+ }
190
+
191
+ .df-image.xl img {
192
+ max-width: 600px;
193
+ }
194
+ }
195
+ /* We should move to CSS modules when we move this to the Wise layer */
196
+ .df-box-renderer-border {
197
+ border: 1px solid var(--color-border-neutral);
198
+ border-radius: var(--radius-small);
199
+ padding: var(--size-16);
200
+ }
201
+ .df-box-renderer-fixed-width {
202
+ display: flex;
203
+ justify-content: center;
204
+ }
205
+ .df-box-renderer-width-xs,
206
+ .df-box-renderer-width-sm,
207
+ .df-box-renderer-width-md,
208
+ .df-box-renderer-width-lg {
209
+ width: 100%;
210
+ }
211
+ @media screen and (width >= 768px) {
212
+ .df-box-renderer-width-xs {
213
+ width: 33.33%;
214
+ }
215
+
216
+ .df-box-renderer-width-sm {
217
+ width: 50%;
218
+ }
219
+
220
+ .df-box-renderer-width-md {
221
+ width: 66.66%;
222
+ }
223
+
224
+ .df-box-renderer-width-lg {
225
+ width: 83.33%;
226
+ }
227
+ }
228
+ @media screen and (width >= 990px) {
229
+ .df-box-renderer-width-xs {
230
+ width: 25%;
231
+ }
232
+
233
+ .df-box-renderer-width-sm {
234
+ width: 33.33%;
235
+ }
236
+
237
+ .df-box-renderer-width-md {
238
+ width: 50%;
239
+ }
240
+
241
+ .df-box-renderer-width-lg {
242
+ width: 66.66%;
243
+ }
244
+ }
245
+ .df-columns-renderer-container {
246
+ display: flex;
247
+ gap: var(--size-16);
248
+ flex-direction: column;
249
+ }
250
+ .df-columns-renderer-column,
251
+ .df-columns-renderer-bias-start .df-columns-renderer-column:nth-child(2),
252
+ .df-columns-renderer-bias-end .df-columns-renderer-column:nth-child(1) {
253
+ flex: 1;
254
+ }
255
+ .df-columns-renderer-bias-start .df-columns-renderer-column:nth-child(1),
256
+ .df-columns-renderer-bias-end .df-columns-renderer-column:nth-child(2) {
257
+ flex: 2;
258
+ }
259
+ @media (width >= 768px) {
260
+ .df-columns-renderer-container {
261
+ flex-direction: row;
262
+ }
263
+ }