@monkvision/common 4.0.3 → 4.0.4
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/APP_UTILS.md +52 -0
- package/README/HOOKS.md +242 -0
- package/README/INTERNATIONALIZATION.md +89 -0
- package/README/STATE_MANAGEMENT.md +125 -0
- package/README/THEMING.md +70 -0
- package/README/UTILITIES.md +254 -0
- package/lib/PreventExit/hooks.js +0 -1
- package/lib/PreventExit/index.js +0 -1
- package/lib/PreventExit/store.js +0 -1
- package/lib/apps/analytics.js +0 -1
- package/lib/apps/appState.js +0 -1
- package/lib/apps/appStateProvider.js +0 -1
- package/lib/apps/index.js +0 -1
- package/lib/apps/monitoring.js +0 -1
- package/lib/apps/searchParams.js +0 -1
- package/lib/hooks/index.js +0 -1
- package/lib/hooks/useAsyncEffect.js +0 -1
- package/lib/hooks/useAsyncInterval.js +0 -1
- package/lib/hooks/useInteractiveStatus.js +0 -1
- package/lib/hooks/useInterval.js +0 -1
- package/lib/hooks/useLoadingState.js +0 -1
- package/lib/hooks/useObjectMemo.js +0 -1
- package/lib/hooks/useObjectTranslation.js +0 -1
- package/lib/hooks/useQueue.js +0 -1
- package/lib/hooks/useResponsiveStyle.js +0 -1
- package/lib/hooks/useSearchParams.js +0 -1
- package/lib/hooks/useSightLabel.js +0 -1
- package/lib/hooks/useWindowDimensions.js +0 -1
- package/lib/i18n/index.js +0 -1
- package/lib/i18n/translations/image.js +0 -1
- package/lib/i18n/translations/index.js +0 -1
- package/lib/i18n/translations/vehicleParts.js +0 -1
- package/lib/i18n/utils.js +0 -1
- package/lib/index.js +0 -1
- package/lib/state/actions/createdOneImage.js +0 -1
- package/lib/state/actions/gotOneInspection.js +0 -1
- package/lib/state/actions/index.js +0 -1
- package/lib/state/actions/monkAction.js +0 -1
- package/lib/state/actions/resetState.js +0 -1
- package/lib/state/actions/updatedManyTasks.js +0 -1
- package/lib/state/actions/updatedVehicle.js +0 -1
- package/lib/state/context.js +0 -1
- package/lib/state/hooks.js +0 -1
- package/lib/state/index.js +0 -1
- package/lib/state/provider.js +0 -1
- package/lib/state/reducer.js +0 -1
- package/lib/state/state.js +0 -1
- package/lib/theme/context.js +0 -1
- package/lib/theme/default/index.js +0 -1
- package/lib/theme/default/palette.js +0 -1
- package/lib/theme/hooks.js +0 -1
- package/lib/theme/index.js +0 -1
- package/lib/theme/provider.js +0 -1
- package/lib/theme/theme.js +0 -1
- package/lib/utils/array.utils.js +0 -1
- package/lib/utils/browser.utils.js +0 -1
- package/lib/utils/color.utils.js +0 -1
- package/lib/utils/env.utils.js +0 -1
- package/lib/utils/index.js +0 -1
- package/lib/utils/mimetype.utils.js +0 -1
- package/lib/utils/promise.utils.js +0 -1
- package/lib/utils/state.utils.js +0 -1
- package/lib/utils/string.utils.js +0 -1
- package/lib/utils/zlib.utils.js +0 -1
- package/package.json +15 -7
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Application Utils
|
|
2
|
+
This README page is aimed at providing documentation on a specific part of the `@monkvision/common` package : the
|
|
3
|
+
application utilities. You can refer to [this page](README.md) for more general information on the package.
|
|
4
|
+
|
|
5
|
+
This package exports various utilities shared accross the different web and native applications integrating the Monk
|
|
6
|
+
workflow using the MonkJs SDK.
|
|
7
|
+
|
|
8
|
+
# Monk Application State
|
|
9
|
+
Most of the time, applications integrating the Monk SDK will need the same basic parameters to function :
|
|
10
|
+
- An authentication token to make API requests
|
|
11
|
+
- An inspection ID to know which inspection it should capture or visualize the reports of
|
|
12
|
+
- A vehicle type to know which sights or car 360 wireframes to use in the capture screen or in the inspection report
|
|
13
|
+
- A steering wheel position for the vehicles (left or right)
|
|
14
|
+
- A preferred language to use instead of the default English
|
|
15
|
+
|
|
16
|
+
This package export some utilities that can be used to more easily handle the state management and the initial values of
|
|
17
|
+
these app parameters.
|
|
18
|
+
|
|
19
|
+
## MonkAppStateProvider component
|
|
20
|
+
The `MonkAppStateProvider` component is a React context provider that declares the application parameters described
|
|
21
|
+
above. It declares a `MonkAppStateContext` that contains the current authentication token, inspection ID and vehicle
|
|
22
|
+
type and setters functions to update them. Using some configuration props, this context provider also initializes the
|
|
23
|
+
parameters with values that can be fetched from the URL search parameters or the local storage :
|
|
24
|
+
|
|
25
|
+
- If the `fetchFromSearchParams` prop in the app config is set to `true`, during the first render of the component, it
|
|
26
|
+
will look in the URL for search parameters described in the `MonkSearchParams` enum in order to update the app params
|
|
27
|
+
accordingly.
|
|
28
|
+
- Auth tokens need be compressed using ZLib (ex: using the `zlibCompress` utility function available in this
|
|
29
|
+
package) and properly URL-encoded before being passed as a URL param. No Monk app should ever use auth tokens
|
|
30
|
+
obtained from URL search params without compression and encoding.
|
|
31
|
+
- The token fetched from the search params will always be used in priority over the one fetched from the local
|
|
32
|
+
storage.
|
|
33
|
+
- During the first render of the MonkAppStateProvider component, it will look in the local storage for a valid
|
|
34
|
+
authentication token to use.
|
|
35
|
+
- The storage key used to read / write the auth token in the local storage is the same throughout the Monk SDK and
|
|
36
|
+
is defined in the `STORAGE_KEY_AUTH_TOKEN` variable exported by this package.
|
|
37
|
+
- If `fetchFromSearchParams` is also set to `true`, the token fetched from the search params will always be
|
|
38
|
+
used in priority over the one fetched from the local storage.
|
|
39
|
+
|
|
40
|
+
| Prop | Type | Description | Required | Default Value |
|
|
41
|
+
|------------------|------------------|-------------------------------------------------------------------------------------------------------------------------------------|----------|---------------|
|
|
42
|
+
| config | CaptureAppConfig | The current configuration of the application. | ✔️ | |
|
|
43
|
+
| onFetchAuthToken | () => void | Callback called when an authentication token has successfully been fetched from either the local storage, or the URL search params. | | |
|
|
44
|
+
| onFetchLanguage | () => void | Callback called when the language of the app must be updated because it has been specified in the URL params. | | |
|
|
45
|
+
|
|
46
|
+
## useMonkAppState hook
|
|
47
|
+
This hook simply returns the current value of the `MonkAppStateContext` declared by the `MonkAppStateProvider`component.
|
|
48
|
+
It means that it can only be called within the render function of a child of this component. This hook accepts an
|
|
49
|
+
optional parameter called `requireInspection` which specifies if the `authToken` and the `inspectionId` params are
|
|
50
|
+
required. If they are, this hook will guarantee that the return values for these params is not null (even in TypeScript
|
|
51
|
+
typings), but will throw an error if they are.
|
|
52
|
+
|
package/README/HOOKS.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# Hooks
|
|
2
|
+
This README page is aimed at providing documentation on a specific part of the `@monkvision/common` package : the
|
|
3
|
+
React hooks. You can refer to [this page](README.md) for more general information on the package.
|
|
4
|
+
|
|
5
|
+
This package exports various custom hooks used throughout the MonkJs SDK.
|
|
6
|
+
|
|
7
|
+
### useAsyncEffect
|
|
8
|
+
```tsx
|
|
9
|
+
import { useAsyncEffect } from '@monkvision/common';
|
|
10
|
+
|
|
11
|
+
function TestComponent() {
|
|
12
|
+
useAsyncEffect(
|
|
13
|
+
async () => {
|
|
14
|
+
const result = myCustomAsyncFunc();
|
|
15
|
+
return result.value;
|
|
16
|
+
},
|
|
17
|
+
[exampleDependency],
|
|
18
|
+
{
|
|
19
|
+
onResolve: (value) => {
|
|
20
|
+
console.log(value);
|
|
21
|
+
},
|
|
22
|
+
onReject: (err) => {
|
|
23
|
+
console.error(err);
|
|
24
|
+
},
|
|
25
|
+
onComplete: () => {
|
|
26
|
+
console.log('Done.');
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
Custom hook that can be used to run asyncrhonous effects. It is similar to `useEffect` but makes sure to not execute the
|
|
33
|
+
effect handlers if the effect's Promise resolves after the current component as been dismounted.
|
|
34
|
+
|
|
35
|
+
### useAsyncInterval
|
|
36
|
+
```tsx
|
|
37
|
+
import { useAsyncInterval } from '@monkvision/common';
|
|
38
|
+
|
|
39
|
+
function TestComponent() {
|
|
40
|
+
useAsyncInterval(
|
|
41
|
+
myCustomAsyncCallback,
|
|
42
|
+
1000,
|
|
43
|
+
{
|
|
44
|
+
onResolve: (value) => {
|
|
45
|
+
console.log(value);
|
|
46
|
+
},
|
|
47
|
+
onReject: (err) => {
|
|
48
|
+
console.error(err);
|
|
49
|
+
},
|
|
50
|
+
onComplete: () => {
|
|
51
|
+
console.log('Done.');
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
This custom hook creates an interval that calls the provided async callback every `delay` milliseconds if the previous
|
|
58
|
+
call isn't still running. If `delay` is `null` or less than 0, the callback will not be called. The promise handlers
|
|
59
|
+
provided will only be called while the component is still mounted.
|
|
60
|
+
|
|
61
|
+
### useInteractiveStatus
|
|
62
|
+
```tsx
|
|
63
|
+
import { useInteractiveStatus } from '@monkvision/common';
|
|
64
|
+
|
|
65
|
+
function TestComponent() {
|
|
66
|
+
const { status, events } = useInteractiveStatus();
|
|
67
|
+
useEffect(() => console.log('Button status :', status), [status]);
|
|
68
|
+
|
|
69
|
+
return <button {...events}>My Button</button>;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
This hook allows the tracking of the interactive status (hovered, active, disabled...) of a React element. It returns
|
|
73
|
+
the interactive status of the element, as well as a set of MouseEvent listeners used to update the status accordingly.
|
|
74
|
+
|
|
75
|
+
### useInterval
|
|
76
|
+
```tsx
|
|
77
|
+
import { useInterval } from '@monkvision/common';
|
|
78
|
+
|
|
79
|
+
function TestComponent() {
|
|
80
|
+
useInterval(() => console.log('Hello World!'), 1000);
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
This custom hook creates an interval that calls the provided callback every `delay` milliseconds. If `delay` is `null`
|
|
84
|
+
or less than 0, the callback will not be called.
|
|
85
|
+
|
|
86
|
+
### useLangProp
|
|
87
|
+
```tsx
|
|
88
|
+
import { useLangProp } from '@monkvision/common';
|
|
89
|
+
|
|
90
|
+
function TestComponent(props: { lang?: string }) {
|
|
91
|
+
useLangProp(lang);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
Custom hook used internally by the Monk SDK components to handle the `lang` prop tha tcan be passed to them to manage
|
|
95
|
+
the current language displayed by the component.
|
|
96
|
+
|
|
97
|
+
### useLoadingState
|
|
98
|
+
```tsx
|
|
99
|
+
import { useEffect } from 'react';
|
|
100
|
+
import { useLoadingState } from '@monkvision/common';
|
|
101
|
+
|
|
102
|
+
function useCustomApiCall() {
|
|
103
|
+
const loading = useLoadingState();
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
loading.start();
|
|
106
|
+
myApiCall().then(() => loading.onSuccess()).catch((err) => loading.onError(err));
|
|
107
|
+
}, []);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
Custom hook used to create a `LoadingState` object. This object can be used to track the processing of a task in the
|
|
111
|
+
component. For instance, you can use this hook to handle the loading and errors of API calls in your components.
|
|
112
|
+
|
|
113
|
+
### useObjectMemo
|
|
114
|
+
```tsx
|
|
115
|
+
import { useMemo } from 'React';
|
|
116
|
+
import { useObjectMemo } from '@monkvision/common';
|
|
117
|
+
|
|
118
|
+
function TestComponent() {
|
|
119
|
+
// These 2 lines are equivalent
|
|
120
|
+
const foo = useMemo(() => ({ bar, baz }), [bar, baz]);
|
|
121
|
+
const foo = useObjectMemo({ bar, baz });
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
This custom hook is used to have a more handy way of memoizing a record of values.
|
|
125
|
+
|
|
126
|
+
### useObjectTranslation
|
|
127
|
+
```tsx
|
|
128
|
+
import { useObjectTranslation } from '@monkvision/common';
|
|
129
|
+
|
|
130
|
+
const translationObject = { en: 'Hello', fr: 'Salut', de: 'Hallo' };
|
|
131
|
+
|
|
132
|
+
function TestComponent() {
|
|
133
|
+
const { tObj } = useObjectTranslation();
|
|
134
|
+
return <div>{tObj(translationObject)}</div>;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
Custom hook used to get a translation function tObj that translates TranslationObjects.
|
|
138
|
+
|
|
139
|
+
### usePreventExit
|
|
140
|
+
```ts
|
|
141
|
+
import { usePreventExit } from '@monkvision/common';
|
|
142
|
+
|
|
143
|
+
function MyComponent() {
|
|
144
|
+
const { allowRedirect } = usePreventExit(true);
|
|
145
|
+
|
|
146
|
+
const anyEvent = useCallback(() => {
|
|
147
|
+
allowRedirect();
|
|
148
|
+
/** ... */
|
|
149
|
+
}, [allowRedirect])
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
This hook is used to prevent the user from leaving the page by displaying a confirmation dialog when the user tries to
|
|
154
|
+
leave the page.
|
|
155
|
+
|
|
156
|
+
### useQueue
|
|
157
|
+
```tsx
|
|
158
|
+
import { useQueue } from '@monkvision/common';
|
|
159
|
+
|
|
160
|
+
function TestComponent() {
|
|
161
|
+
const queue = useQueue<string>((item) => {
|
|
162
|
+
console.log(item);
|
|
163
|
+
return Promise.resolve();
|
|
164
|
+
});
|
|
165
|
+
...
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
This hook is used to create a processing queue. The `process` function passed as a parameter is an async function
|
|
170
|
+
that is used to process items in the queue. You can find more details on how the queue works by taking a look at the
|
|
171
|
+
TSDoc of the `Queue` interface.
|
|
172
|
+
|
|
173
|
+
### useResponsiveStyle
|
|
174
|
+
```tsx
|
|
175
|
+
import { useResponsiveStyle } from '@monkvision/common';
|
|
176
|
+
import { Styles } from '@monkvision/types';
|
|
177
|
+
|
|
178
|
+
const styles: Styles = {
|
|
179
|
+
div: {
|
|
180
|
+
width: 100,
|
|
181
|
+
height: 100,
|
|
182
|
+
},
|
|
183
|
+
divMobile: {
|
|
184
|
+
__media: { maxWidth: 500 },
|
|
185
|
+
backgroundColor: '#ff0000',
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
function TestComponent() {
|
|
190
|
+
const { responsive } = useResponsiveStyle();
|
|
191
|
+
return <div style={{...styles.div, ...responsive(styles.divMobile)}}>Hello</div>
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
This hook returns takes a `ResponsiveStyleProperties` declarations object (see the definition of this type in the
|
|
195
|
+
`@monkvision/types` package for more details) containing a media query and returns either the CSSProperties contained in
|
|
196
|
+
the type, or `null` if the query conditions are not met. Note that if there are no query, the style will be applied.
|
|
197
|
+
|
|
198
|
+
### useSearchParams
|
|
199
|
+
```tsx
|
|
200
|
+
import { sights } from '@monkvision/sights';
|
|
201
|
+
import { useSearchParams } from '@monkvision/common';
|
|
202
|
+
|
|
203
|
+
function TestComponent() {
|
|
204
|
+
const searchParams = useSearchParams();
|
|
205
|
+
console.log(searchParams.get('myParam'));
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
Custom hook used to fetch search params from the current window URL.
|
|
209
|
+
|
|
210
|
+
### useSightLabel
|
|
211
|
+
```tsx
|
|
212
|
+
import { sights } from '@monkvision/sights';
|
|
213
|
+
import { useSightLabel } from '@monkvision/common';
|
|
214
|
+
|
|
215
|
+
function TestComponent() {
|
|
216
|
+
const { label } = useSightLabel();
|
|
217
|
+
return <div>{label(sights['fesc20-0mJeXBDf'])}</div>;
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
Custom hook used to get the label of a sight with the currently selected language.
|
|
221
|
+
|
|
222
|
+
### useThumbnail
|
|
223
|
+
```tsx
|
|
224
|
+
import { useThumbnail } from '@monkvision/common';
|
|
225
|
+
|
|
226
|
+
function TestComponent() {
|
|
227
|
+
const { getThumbnailUrl } = useThumbnail(thumbnailDomain);
|
|
228
|
+
console.log(getThumbnailUrl(image));
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
Custom hook used to generates a thumbnail URL from a full resolution picture.
|
|
232
|
+
|
|
233
|
+
### useWindowDimensions
|
|
234
|
+
```tsx
|
|
235
|
+
import { useWindowDimensions } from '@monkvision/common';
|
|
236
|
+
|
|
237
|
+
function TestComponent() {
|
|
238
|
+
const { width, height, isPortrait } = useWindowDimensions();
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
This hook returns the current window dimensions in pixels, and a boolean indicating if the window is in portrait mode
|
|
242
|
+
(width < height) or not.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Internationalization
|
|
2
|
+
This README page is aimed at providing documentation on a specific part of the `@monkvision/common` package : the
|
|
3
|
+
internationalization. You can refer to [this page](README.md) for more general information on the package.
|
|
4
|
+
|
|
5
|
+
This package exports utility functions and hooks tools that help you manage the internationalization support of the Monk
|
|
6
|
+
SDK, as well as common translations that can be useful when interacting with the SDK.
|
|
7
|
+
|
|
8
|
+
# react-i18next
|
|
9
|
+
The internationalization in the Monk SDK (and in Monk Webapps) is handled using [i18n](https://www.i18next.com/) and
|
|
10
|
+
[react-i18next](https://react.i18next.com/). This means that installing some of our packages (like
|
|
11
|
+
`@monkvision/inspection-report-web` for instance) will mean automatically adding these packages as dependencies in your
|
|
12
|
+
lockfile.
|
|
13
|
+
|
|
14
|
+
We tend to stay as minimal-dependency as we can when developing our SDK, but we felt like i18n and react-i18next were
|
|
15
|
+
packages common enough that it was not worth going through implementing an abstraction package like we did for
|
|
16
|
+
@monkvision/sentry.
|
|
17
|
+
|
|
18
|
+
Keep in mind : if you do not want internationalization support in your application, you can still use our SDK and
|
|
19
|
+
specify to it which language you want it to use. The following languages are supported by the Monk SDK :
|
|
20
|
+
|
|
21
|
+
- English (default)
|
|
22
|
+
- French
|
|
23
|
+
- German
|
|
24
|
+
- Dutch
|
|
25
|
+
|
|
26
|
+
# i18n Utilities
|
|
27
|
+
## useI18nSync hook
|
|
28
|
+
### Description
|
|
29
|
+
This hook is used mostly by MonkJs packages internally to synchronize their own i18n instance with the language param
|
|
30
|
+
or prop that they are provided.
|
|
31
|
+
|
|
32
|
+
### Example of usage
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import i18n from 'i18next';
|
|
36
|
+
import { useI18nSync } from '@monkvision/common';
|
|
37
|
+
|
|
38
|
+
interface MyComponentProps {
|
|
39
|
+
lang?: string | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function MyComponent(props: MyComponentProps) {
|
|
43
|
+
useI18nSync(props.lang);
|
|
44
|
+
...
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## getLanguage
|
|
49
|
+
```ts
|
|
50
|
+
import i18n from 'i18next';
|
|
51
|
+
import { getLanguage } from '@monkvision/common';
|
|
52
|
+
|
|
53
|
+
console.log(getLanguage(i18n.language));
|
|
54
|
+
```
|
|
55
|
+
This function retrieves the language prefix from a given language string.
|
|
56
|
+
If the prefix is not found in the list of supported languages (monkLanguages in Types package), it returns 'en' as default.
|
|
57
|
+
|
|
58
|
+
# Common Translations
|
|
59
|
+
## Car Parts
|
|
60
|
+
You can import the car parts translations like this :
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { cartPartLabels } from '@monkvision/common';
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The `cartPartLabels` object maps each `VehiclePart` name (enum from `@monkvision/types`) to a `TranslationObject`
|
|
67
|
+
containing a label for each supported language.
|
|
68
|
+
|
|
69
|
+
## Image Status Labels
|
|
70
|
+
You can import the image status labels translations like this :
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { imageStatusLabels } from '@monkvision/common';
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The `imageStatusLabels` object maps each `ImageStatus` (enum from `@monkvision/types`) to an object containing a `title`
|
|
77
|
+
and a `description` property, both of which are TranslationObject`s containing a label for each supported language.
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Compliance Issue Labels
|
|
81
|
+
You can import the compliance issue labels translations like this :
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { complianceIssueLabels } from '@monkvision/common';
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
The `complianceIssueLabels` object maps each `ComplianceIssue` (enum from `@monkvision/types`) to an object containing a
|
|
88
|
+
`title` and a `description` property, both of which are TranslationObject`s containing a label for each supporte
|
|
89
|
+
language.
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# State Management
|
|
2
|
+
This README page is aimed at providing documentation on a specific part of the `@monkvision/common` package : the state
|
|
3
|
+
management. You can refer to [this page](README.md) for more general information on the package.
|
|
4
|
+
|
|
5
|
+
This package exports tools that help you manage the state of MonkJs applications. In Monk projects, the state of an
|
|
6
|
+
inspection and everything that goes with it is represented by a set of different entities. The complete list of entities
|
|
7
|
+
available in what we call the "Monk State" is available in the @monkvision/types package.
|
|
8
|
+
|
|
9
|
+
The "Monk State" (described in this package by the `MonkState` interface) is simply the list of all entities currently
|
|
10
|
+
stored in the state of your application. This package exports tools that help you initialize, access and update the
|
|
11
|
+
Monk state.
|
|
12
|
+
|
|
13
|
+
## MonkProvider component
|
|
14
|
+
This package exports a component called `MonkProvider` which is a context provider that will let you access and use the
|
|
15
|
+
Monk state in your application. We recommend placing this provider at the top of your application :
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { MonkProvider } from '@monkvision/common';
|
|
19
|
+
|
|
20
|
+
function RootComponent() {
|
|
21
|
+
return (
|
|
22
|
+
<MonkProvider>
|
|
23
|
+
...
|
|
24
|
+
</MonkProvider>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
After placing this provider in your code, the Monk state will be accessible using the `useMonkState` hook described
|
|
30
|
+
below. This provider will initialize the Monk state with empty values (no entities) by default, but you can pass an
|
|
31
|
+
optional `initialState` prop to the provider to initialize the satte with custom values.
|
|
32
|
+
|
|
33
|
+
> Important note : If the Monk state context has already been defined previously, this Monk provider component will
|
|
34
|
+
> have no effect. This is because packages of the MonkJs project use this provider internally to set up the state, even
|
|
35
|
+
> if you do not use it in your app.
|
|
36
|
+
|
|
37
|
+
# useMonkState hook
|
|
38
|
+
Once you have initialized the Monk state in your app using the `MonkProvider` component, you can access the state using
|
|
39
|
+
the `useMonkState` hook as follows :
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { useMonkState } from '@monkvision/common';
|
|
43
|
+
|
|
44
|
+
function MyComponent() {
|
|
45
|
+
const { state, dispatch } = useMonkState();
|
|
46
|
+
...
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
This hook behaves similarly to the `useReducer` React hook, and will return two values :
|
|
51
|
+
- `state` : The Monk state object, that updates automatically when an action is dispatched
|
|
52
|
+
- `dispatch` : A dispatch function that will let you dispatch `MonkAction` objects to update the state
|
|
53
|
+
|
|
54
|
+
The list of actions that can be dispatched is described in the *Monk state actions* section below.
|
|
55
|
+
|
|
56
|
+
> Important note : This hook will throw an error if called in a component where the MonkContext has not been initialized
|
|
57
|
+
> previously by a MonkProvider
|
|
58
|
+
|
|
59
|
+
# Monk state actions
|
|
60
|
+
This section enumerates the available actions that can be dispatched in the MonkState.
|
|
61
|
+
|
|
62
|
+
## ResetState Action
|
|
63
|
+
This action can be dispatched in order to completely reset the MonkState and clear all stored entities. This action does
|
|
64
|
+
not need any payload.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { MonkResetStateAction, MonkActionType } from '@monkvision/common';
|
|
68
|
+
|
|
69
|
+
const action: MonkResetStateAction = {
|
|
70
|
+
type: MonkActionType.RESET_STATE,
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## GotOneInspection Action
|
|
75
|
+
This action can be dispatched when details about an inspection have been fetched from the API. The payload of this
|
|
76
|
+
action is actually a partial MonkState containing details about every entity fetched with this inspection.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { MonkResetStateAction, MonkActionType } from '@monkvision/common';
|
|
80
|
+
|
|
81
|
+
const action: Monk = {
|
|
82
|
+
type: MonkActionType.GOT_ONE_INSPECTION,
|
|
83
|
+
payload: {
|
|
84
|
+
inspections: [...],
|
|
85
|
+
images: [...],
|
|
86
|
+
...
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## CreatedOneImage Action
|
|
92
|
+
This action can be dispatched after an image has beencreated and uploaded to the API. The payload of this action should
|
|
93
|
+
contain the details about the image that has been created, as well as the ID of the inspection. You can also start by
|
|
94
|
+
creating a local image (with a custom local ID), and then update this image when the API has returned the actual ID of
|
|
95
|
+
the image. To do so, re-dispatch another time the same action, but with the new Id and the property `localId` in the
|
|
96
|
+
payload containing the previous ID assigned to the image when it was created locally.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { MonkResetStateAction, MonkActionType } from '@monkvision/common';
|
|
100
|
+
|
|
101
|
+
const action: Monk = {
|
|
102
|
+
type: MonkActionType.CREATED_ONE_IMAGE,
|
|
103
|
+
payload: {
|
|
104
|
+
inspectionId: 'e1cb2852-77f3-4fb5-a851-e700cf31a7d1',
|
|
105
|
+
image: {...},
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## UpdatedManyTasks Action
|
|
111
|
+
This action can be dispatched after one or multiple tasks' statuses have been updated. The payload of this action should
|
|
112
|
+
be an array containing the details of the tasks that have been updated.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { MonkResetStateAction, MonkActionType } from '@monkvision/common';
|
|
116
|
+
import { ProgressStatus } from '@monkvision/types';
|
|
117
|
+
|
|
118
|
+
const action: Monk = {
|
|
119
|
+
type: MonkActionType.CREATED_ONE_IMAGE,
|
|
120
|
+
payload: [
|
|
121
|
+
{ id: '2b2ac131-c613-41a9-ac04-d00b942e2290', status: ProgressStatus.IN_PROGRESS },
|
|
122
|
+
{ id: '4097bc0e-02d0-4ed8-9411-b18f0cb922f2', status: ProgressStatus.IN_PROGRESS },
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
```
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Theming
|
|
2
|
+
This README page is aimed at providing documentation on a specific part of the `@monkvision/common` package : the
|
|
3
|
+
theme customization. You can refer to [this page](README.md) for more general information on the package.
|
|
4
|
+
|
|
5
|
+
This package exports tools that help you customize the look and feel of MonkJs applications.
|
|
6
|
+
|
|
7
|
+
# MonkThemeProvider component
|
|
8
|
+
This package exports a component called `MonkThemeProvider` which is a context provider that will let you specify the
|
|
9
|
+
theme you want to apply to the Monk SDK. In order for this to work, you need to place this component above any Monk SDK
|
|
10
|
+
component :
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
import { MonkThemeProvider } from '@monkvision/common';
|
|
14
|
+
import { InspectionCapture } from '@monkvision/inspection-capture-web';
|
|
15
|
+
|
|
16
|
+
function AppComponent() {
|
|
17
|
+
return (
|
|
18
|
+
<MonkThemeProvider palette={...}>
|
|
19
|
+
<InspectionCapture />
|
|
20
|
+
</MonkThemeProvider>
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This component accepts the following props :
|
|
26
|
+
|
|
27
|
+
| Prop | Type | Description | Required | Default Value |
|
|
28
|
+
|---------|----------------------|-------------------------------------|----------|----------------------|
|
|
29
|
+
| palette | Partial<MonkPalette> | The colors to used by the Monk SDK. | | `MonkDefaultPalette` |
|
|
30
|
+
|
|
31
|
+
# useMonkTheme hook
|
|
32
|
+
Inside a `MonkThemeProvider` child component, you can use the `useMonkTheme` hook to access the current Monk theme. If
|
|
33
|
+
this hook is called while the `MonkThemeProvider` has not been defined, the default Monk theme will be returned. This
|
|
34
|
+
hook will return the following values :
|
|
35
|
+
|
|
36
|
+
- A `palette` object containing the current color palette
|
|
37
|
+
- A `utils` object containing various utility functions
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { useMonkTheme } from '@monkvision/common';
|
|
41
|
+
|
|
42
|
+
function MyComponent() {
|
|
43
|
+
const { palette, utils } = useMonkTheme();
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
# Utility functions
|
|
48
|
+
The `utils` property of the Monk theme contains the following utility functions :
|
|
49
|
+
|
|
50
|
+
## getColor
|
|
51
|
+
```typescript
|
|
52
|
+
const { utils } = useMonkTheme();
|
|
53
|
+
const colorValue = utils.getColor(colorProp);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
This function takes a `ColorProp` as an argument and returns the corresponding color value based on the current theme.
|
|
57
|
+
`ColorProp` corresponds to the type of the props that you can give to Monk UI components in order to specify a color to
|
|
58
|
+
them. It can either be directly a CSS color value (hexcode, rgba function as a string, etc.), or the name of the color
|
|
59
|
+
in the palette (for instance `primary-dark`).
|
|
60
|
+
|
|
61
|
+
The `getColor` function will check if the given argument is a color name, and if it is, it will return its correponding
|
|
62
|
+
value in the curerntly used color palette. If the argument given is not a color name, it will be considered as a CSS
|
|
63
|
+
color property and will be return as is.
|
|
64
|
+
|
|
65
|
+
# Available Theme Customization
|
|
66
|
+
## Color Palette
|
|
67
|
+
You can use the `palette` prop in the MonkThemeProvider to customize the colors of the Monk SDK. The list of available
|
|
68
|
+
colors is defined in the `MonkPalette` interface of the `@monkvision/types` package. The palette given to the provider
|
|
69
|
+
can be a partial palette, where not all colors are implemented. In this case, every missing color will be filled with
|
|
70
|
+
the color present in the default Monk palette.
|