@addev-be/ui 2.7.4 → 2.7.5

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 (238) hide show
  1. package/assets/icons/arrow-down-1-9.svg +1 -1
  2. package/assets/icons/arrow-down-a-z.svg +1 -1
  3. package/assets/icons/arrow-up-z-a.svg +1 -1
  4. package/assets/icons/check.svg +1 -1
  5. package/assets/icons/circle-check.svg +1 -1
  6. package/assets/icons/down.svg +1 -1
  7. package/assets/icons/filter-full.svg +1 -1
  8. package/assets/icons/filter.svg +1 -1
  9. package/assets/icons/hashtag.svg +1 -1
  10. package/assets/icons/image-slash.svg +1 -1
  11. package/assets/icons/left.svg +1 -1
  12. package/assets/icons/magnifier.svg +1 -1
  13. package/assets/icons/phone.svg +1 -1
  14. package/assets/icons/right.svg +1 -1
  15. package/assets/icons/sort-calendar-ascending.svg +5 -5
  16. package/assets/icons/spinner-third.svg +1 -1
  17. package/assets/icons/table-columns.svg +1 -1
  18. package/assets/icons/table-footer-slash.svg +4 -4
  19. package/assets/icons/table-footer.svg +3 -3
  20. package/assets/icons/up.svg +1 -1
  21. package/assets/icons/user-tie.svg +1 -1
  22. package/assets/icons/x-bar.svg +3 -3
  23. package/dist/components/data/AdvancedRequestDataGrid/helpers/advancedRequests.js +8 -9
  24. package/dist/components/data/AdvancedRequestDataGrid/helpers/columns.js +72 -21
  25. package/dist/components/data/AdvancedRequestDataGrid/index.js +2 -2
  26. package/dist/components/data/AdvancedRequestDataGrid/types.d.ts +1 -1
  27. package/dist/components/data/SqlRequestDataGrid/SqlRequestForeignListEditableCell.d.ts +1 -2
  28. package/dist/components/data/SqlRequestDataGrid/SqlRequestForeignListEditableCell.js +3 -2
  29. package/dist/services/index.js +7 -17
  30. package/eslint.config.js +3 -3
  31. package/package.json +2 -2
  32. package/src/Icons.tsx +138 -138
  33. package/src/components/auth/LoginForm.tsx +86 -86
  34. package/src/components/auth/LoginPage.tsx +32 -32
  35. package/src/components/auth/PasswordRecoveryForm.tsx +53 -53
  36. package/src/components/auth/PasswordResetForm.tsx +112 -112
  37. package/src/components/auth/styles.ts +14 -14
  38. package/src/components/data/DataGrid/DataGridCell.tsx +81 -81
  39. package/src/components/data/DataGrid/DataGridColumnsModal/helpers.ts +9 -9
  40. package/src/components/data/DataGrid/DataGridColumnsModal/hooks.tsx +59 -59
  41. package/src/components/data/DataGrid/DataGridColumnsModal/index.tsx +182 -182
  42. package/src/components/data/DataGrid/DataGridColumnsModal/styles.ts +104 -104
  43. package/src/components/data/DataGrid/DataGridEditableCell/CheckboxEditableCell.tsx +37 -37
  44. package/src/components/data/DataGrid/DataGridEditableCell/DateEditableCell.tsx +38 -38
  45. package/src/components/data/DataGrid/DataGridEditableCell/NumberEditableCell.tsx +71 -71
  46. package/src/components/data/DataGrid/DataGridEditableCell/TextEditableCell.tsx +37 -37
  47. package/src/components/data/DataGrid/DataGridEditableCell/index.tsx +106 -106
  48. package/src/components/data/DataGrid/DataGridEditableCell/styles.ts +35 -35
  49. package/src/components/data/DataGrid/DataGridEditableCell/types.ts +18 -18
  50. package/src/components/data/DataGrid/DataGridFilterMenu/FilterValuesScroller.tsx +129 -129
  51. package/src/components/data/DataGrid/DataGridFilterMenu/hooks.tsx +81 -81
  52. package/src/components/data/DataGrid/DataGridFilterMenu/index.tsx +370 -370
  53. package/src/components/data/DataGrid/DataGridFilterMenu/styles.ts +97 -97
  54. package/src/components/data/DataGrid/DataGridFooter.tsx +47 -47
  55. package/src/components/data/DataGrid/DataGridHeader.tsx +74 -74
  56. package/src/components/data/DataGrid/DataGridHeaderCell.tsx +112 -112
  57. package/src/components/data/DataGrid/DataGridRowTemplate.tsx +83 -83
  58. package/src/components/data/DataGrid/DataGridToolbar.tsx +134 -134
  59. package/src/components/data/DataGrid/FilterModalContent/index.tsx +137 -137
  60. package/src/components/data/DataGrid/FilterModalContent/styles.ts +22 -22
  61. package/src/components/data/DataGrid/constants.ts +6 -6
  62. package/src/components/data/DataGrid/helpers/columns.tsx +449 -449
  63. package/src/components/data/DataGrid/helpers/filters.ts +287 -287
  64. package/src/components/data/DataGrid/helpers/index.ts +2 -2
  65. package/src/components/data/DataGrid/hooks/index.ts +29 -29
  66. package/src/components/data/DataGrid/hooks/useDataGrid.tsx +383 -383
  67. package/src/components/data/DataGrid/hooks/useDataGridChangedRows.ts +97 -97
  68. package/src/components/data/DataGrid/hooks/useDataGridCopy.ts +174 -174
  69. package/src/components/data/DataGrid/hooks/useDataGridSettings.ts +48 -48
  70. package/src/components/data/DataGrid/hooks/useRefreshModal.tsx +48 -48
  71. package/src/components/data/DataGrid/index.tsx +111 -111
  72. package/src/components/data/DataGrid/styles.ts +434 -434
  73. package/src/components/data/DataGrid/types.ts +380 -380
  74. package/src/components/data/SqlRequestDataGrid/helpers/columns.tsx +526 -526
  75. package/src/components/data/SqlRequestDataGrid/helpers/index.ts +2 -2
  76. package/src/components/data/SqlRequestDataGrid/helpers/rows.ts +24 -24
  77. package/src/components/data/SqlRequestDataGrid/helpers/sqlRequests.ts +17 -17
  78. package/src/components/data/SqlRequestDataGrid/index.tsx +417 -417
  79. package/src/components/data/SqlRequestDataGrid/styles.ts +15 -15
  80. package/src/components/data/SqlRequestDataGrid/types.ts +74 -74
  81. package/src/components/data/SqlRequestForeignList/index.tsx +254 -254
  82. package/src/components/data/SqlRequestForeignList/styles.ts +43 -43
  83. package/src/components/data/SqlRequestForeignList/types.ts +32 -32
  84. package/src/components/data/SqlRequestGrid/filters/FiltersSidebar.tsx +108 -108
  85. package/src/components/data/SqlRequestGrid/filters/styles.ts +88 -88
  86. package/src/components/data/SqlRequestGrid/helpers/index.ts +1 -1
  87. package/src/components/data/SqlRequestGrid/helpers/sqlRequests.ts +16 -16
  88. package/src/components/data/SqlRequestGrid/index.tsx +304 -304
  89. package/src/components/data/SqlRequestGrid/styles.ts +20 -20
  90. package/src/components/data/SqlRequestGrid/types.ts +73 -73
  91. package/src/components/data/VirtualScroller/hooks.ts +71 -71
  92. package/src/components/data/VirtualScroller/index.tsx +89 -89
  93. package/src/components/data/VirtualScroller/styles.ts +57 -57
  94. package/src/components/data/VirtualScroller/types.ts +10 -10
  95. package/src/components/forms/AutoTextArea.tsx +48 -48
  96. package/src/components/forms/BillitIdentifier/index.tsx +78 -78
  97. package/src/components/forms/BillitIdentifier/styles.tsx +43 -43
  98. package/src/components/forms/Button.tsx +132 -132
  99. package/src/components/forms/Form/Checkbox.tsx +12 -12
  100. package/src/components/forms/Form/CustomSelect.tsx +86 -86
  101. package/src/components/forms/Form/FormGroup.tsx +33 -33
  102. package/src/components/forms/Form/Input.tsx +16 -16
  103. package/src/components/forms/Form/Row.tsx +28 -28
  104. package/src/components/forms/Form/Select.tsx +99 -99
  105. package/src/components/forms/Form/TextArea.tsx +17 -17
  106. package/src/components/forms/Form/index.tsx +48 -48
  107. package/src/components/forms/Form/styles.ts +148 -148
  108. package/src/components/forms/IconButton.tsx +61 -61
  109. package/src/components/forms/IndeterminateCheckbox.tsx +46 -46
  110. package/src/components/forms/NumberInput.tsx +53 -53
  111. package/src/components/forms/Select.tsx +34 -34
  112. package/src/components/forms/VerticalLabel.tsx +20 -20
  113. package/src/components/forms/styles.ts +42 -42
  114. package/src/components/layout/Columns.ts +28 -28
  115. package/src/components/layout/Dropdown/index.tsx +113 -113
  116. package/src/components/layout/Dropdown/styles.ts +53 -53
  117. package/src/components/layout/Flexbox.ts +21 -21
  118. package/src/components/layout/Grid/index.tsx +8 -8
  119. package/src/components/layout/Grid/styles.ts +34 -34
  120. package/src/components/layout/Loading/index.tsx +29 -29
  121. package/src/components/layout/Loading/styles.ts +29 -29
  122. package/src/components/layout/Masonry/index.tsx +29 -29
  123. package/src/components/layout/Masonry/styles.ts +20 -20
  124. package/src/components/layout/Modal/index.tsx +51 -51
  125. package/src/components/layout/Modal/styles.ts +125 -125
  126. package/src/components/search/HighlightedText.tsx +41 -41
  127. package/src/components/search/QuickSearchBar.tsx +102 -102
  128. package/src/components/search/QuickSearchResults.tsx +86 -86
  129. package/src/components/search/styles.ts +96 -96
  130. package/src/components/search/types.ts +29 -29
  131. package/src/components/ui/Avatar/index.tsx +54 -54
  132. package/src/components/ui/Card/index.tsx +16 -16
  133. package/src/components/ui/Card/styles.ts +41 -41
  134. package/src/components/ui/ContextMenu/index.tsx +79 -79
  135. package/src/components/ui/ContextMenu/styles.ts +119 -119
  136. package/src/components/ui/Ellipsis.tsx +33 -33
  137. package/src/components/ui/Label.tsx +93 -93
  138. package/src/components/ui/Message/index.tsx +57 -57
  139. package/src/components/ui/Message/styles.ts +44 -44
  140. package/src/components/ui/TabsView/TabsList.tsx +49 -49
  141. package/src/components/ui/TabsView/TabsView.tsx +42 -42
  142. package/src/components/ui/TabsView/styles.ts +84 -84
  143. package/src/components/ui/TabsView/types.ts +15 -15
  144. package/src/config/index.ts +10 -10
  145. package/src/helpers/components.ts +9 -9
  146. package/src/helpers/dates.ts +17 -17
  147. package/src/helpers/getScrollbarSize.ts +14 -14
  148. package/src/helpers/numbers.ts +63 -63
  149. package/src/helpers/responsive.ts +83 -83
  150. package/src/helpers/styled/space.ts +114 -114
  151. package/src/helpers/styled/typography.ts +25 -25
  152. package/src/helpers/text.ts +13 -13
  153. package/src/helpers/types.ts +9 -9
  154. package/src/hooks/useContainerMediaQuery.ts +7 -7
  155. package/src/hooks/useElementSize.ts +24 -24
  156. package/src/hooks/useMediaQuery.ts +9 -9
  157. package/src/hooks/useMediaQueryForWidth.ts +35 -35
  158. package/src/hooks/useMutableState.test.ts +410 -410
  159. package/src/hooks/useMutableState.ts +39 -39
  160. package/src/hooks/useShowArchived.ts +28 -28
  161. package/src/hooks/useWindowSize.ts +20 -20
  162. package/src/index.ts +103 -103
  163. package/src/providers/AuthenticationProvider/helpers.ts +3 -3
  164. package/src/providers/AuthenticationProvider/index.tsx +303 -303
  165. package/src/providers/LoadingProvider/index.tsx +47 -47
  166. package/src/providers/PortalsProvider/index.tsx +54 -54
  167. package/src/providers/PortalsProvider/styles.ts +31 -31
  168. package/src/providers/SettingsProvider/index.tsx +70 -70
  169. package/src/providers/ThemeProvider/defaultTheme.ts +471 -471
  170. package/src/providers/ThemeProvider/helpers.ts +84 -84
  171. package/src/providers/ThemeProvider/index.ts +73 -73
  172. package/src/providers/ThemeProvider/types.ts +134 -134
  173. package/src/providers/ToastProvider/index.tsx +93 -93
  174. package/src/providers/TrackingProvider/index.tsx +71 -71
  175. package/src/providers/UiProviders/index.tsx +76 -76
  176. package/src/providers/UiProviders/styles.ts +10 -10
  177. package/src/providers/hooks.ts +14 -14
  178. package/src/services/HttpService.ts +92 -92
  179. package/src/services/WebSocketService.ts +155 -155
  180. package/src/services/advancedRequests.ts +102 -102
  181. package/src/services/base.ts +23 -23
  182. package/src/services/globalSearch.ts +32 -32
  183. package/src/services/hooks.ts +92 -92
  184. package/src/services/requests/auth.ts +44 -44
  185. package/src/services/requests/generic.ts +62 -62
  186. package/src/services/requests/printing.ts +12 -12
  187. package/src/services/requests/tracking.ts +12 -12
  188. package/src/services/requests/userProfiles.ts +35 -35
  189. package/src/services/requests/users.ts +28 -28
  190. package/src/services/smartQueries.ts +122 -122
  191. package/src/services/sqlRequests.ts +119 -119
  192. package/src/services/types/auth.ts +98 -98
  193. package/src/services/types/base.ts +10 -10
  194. package/src/services/types/generic.ts +82 -82
  195. package/src/services/types/printing.ts +10 -10
  196. package/src/services/types/tracking.ts +29 -29
  197. package/src/services/types/userProfiles.ts +79 -79
  198. package/src/services/types/users.ts +74 -74
  199. package/src/services/updateSqlRequests.ts +32 -32
  200. package/src/styles/animations.scss +30 -30
  201. package/src/styles/index.scss +42 -42
  202. package/src/types.ts +8 -8
  203. package/src/typings.d.ts +2 -2
  204. package/tsconfig.json +18 -18
  205. package/dist/components/data/DataGrid/AdvancedRequestDataGrid.d.ts +0 -10
  206. package/dist/components/data/DataGrid/AdvancedRequestDataGrid.js +0 -173
  207. package/dist/components/data/DataGrid/FilterValuesScroller.d.ts +0 -13
  208. package/dist/components/data/DataGrid/FilterValuesScroller.js +0 -73
  209. package/dist/components/data/DataGrid/VirtualScroller.d.ts +0 -11
  210. package/dist/components/data/DataGrid/VirtualScroller.js +0 -41
  211. package/dist/components/data/DataGrid/helpers/advancedRequests.d.ts +0 -12
  212. package/dist/components/data/DataGrid/helpers/advancedRequests.js +0 -53
  213. package/dist/components/data/DataGrid/helpers.d.ts +0 -28
  214. package/dist/components/data/DataGrid/helpers.js +0 -436
  215. package/dist/components/data/SmartQueryDataGrid/helpers/columns.d.ts +0 -20
  216. package/dist/components/data/SmartQueryDataGrid/helpers/columns.js +0 -160
  217. package/dist/components/data/SmartQueryDataGrid/helpers/hooks.d.ts +0 -5
  218. package/dist/components/data/SmartQueryDataGrid/helpers/hooks.js +0 -41
  219. package/dist/components/data/SmartQueryDataGrid/helpers/index.d.ts +0 -2
  220. package/dist/components/data/SmartQueryDataGrid/helpers/index.js +0 -18
  221. package/dist/components/data/SmartQueryDataGrid/helpers/smartQueries.d.ts +0 -3
  222. package/dist/components/data/SmartQueryDataGrid/helpers/smartQueries.js +0 -15
  223. package/dist/components/data/SmartQueryDataGrid/hooks.d.ts +0 -9
  224. package/dist/components/data/SmartQueryDataGrid/hooks.js +0 -63
  225. package/dist/components/data/SmartQueryDataGrid/index.d.ts +0 -12
  226. package/dist/components/data/SmartQueryDataGrid/index.js +0 -242
  227. package/dist/components/data/SmartQueryDataGrid/types.d.ts +0 -22
  228. package/dist/components/data/SmartQueryDataGrid/types.js +0 -2
  229. package/dist/components/forms/Form/InputWithLabel.d.ts +0 -2
  230. package/dist/components/forms/Form/InputWithLabel.js +0 -10
  231. package/dist/components/forms/Form/types.d.ts +0 -7
  232. package/dist/components/forms/Form/types.js +0 -2
  233. package/dist/config/types.d.ts +0 -11
  234. package/dist/config/types.js +0 -2
  235. package/dist/helpers/react.d.ts +0 -2
  236. package/dist/helpers/react.js +0 -8
  237. package/dist/services/requests/userPermissions.d.ts +0 -4
  238. package/dist/services/requests/userPermissions.js +0 -20
@@ -1,303 +1,303 @@
1
- import {
2
- FC,
3
- PropsWithChildren,
4
- createContext,
5
- useCallback,
6
- useEffect,
7
- useMemo,
8
- useState,
9
- } from 'react';
10
- import {
11
- useAuthenticateRequestHandler,
12
- useCheckRecoveryKeyRequestHandler,
13
- useLoginRequestHandler,
14
- useLogoutRequestHandler,
15
- useResetPasswordRequestHandler,
16
- useSendRecoveryKeyRequestHandler,
17
- } from '../../services/requests/auth';
18
-
19
- import { UserDTO } from '../../services/types/users';
20
- import { WebSocketService } from '../../services/WebSocketService';
21
- import { basePermissions } from './helpers';
22
-
23
- export type AuthenticationContextProps = {
24
- status?: number;
25
- token?: string;
26
- user?: UserDTO;
27
- isAuthenticated: boolean;
28
- authenticate: () => void;
29
- login: (username: string, password: string) => Promise<boolean>;
30
- logout: () => Promise<boolean>;
31
- sendRecoveryKey: (email: string) => Promise<number>;
32
- checkRecoveryKey: (key: string) => Promise<number>;
33
- resetPassword: (key: string, password: string) => Promise<number>;
34
- permissions: Record<string, string>;
35
- hasPermission: (permission: string) => boolean;
36
- setToken: (token?: string) => void;
37
- };
38
-
39
- export const AuthenticationContext = createContext<AuthenticationContextProps>({
40
- status: undefined,
41
- authenticate: () => {},
42
- isAuthenticated: false,
43
- login: async () => false,
44
- logout: async () => false,
45
- sendRecoveryKey: async () => -1,
46
- checkRecoveryKey: async () => -1,
47
- resetPassword: async () => -1,
48
- permissions: {},
49
- hasPermission: () => false,
50
- setToken: () => {},
51
- });
52
-
53
- export type AuthenticationProviderProps = PropsWithChildren<{
54
- permissions?: Record<string, string>;
55
- loginComponent?: FC;
56
- requestHandlerName?: string;
57
- }>;
58
-
59
- export const AuthenticationProvider: FC<AuthenticationProviderProps> = ({
60
- permissions,
61
- loginComponent: LoginComponent,
62
- children,
63
- requestHandlerName,
64
- }) => {
65
- const allPermissions = useMemo(
66
- () => ({ ...basePermissions, ...permissions }),
67
- [permissions]
68
- );
69
- const [isLoading, setIsLoading] = useState<boolean>(false);
70
- const [, setWebSocketStatus] = useState<boolean | undefined>(false);
71
- const [token, setToken] = useState<string | undefined>(
72
- sessionStorage.getItem('authToken') || undefined
73
- );
74
- const [user, setUser] = useState<UserDTO | undefined>(undefined);
75
- const [status, setStatus] = useState<number | undefined>(undefined);
76
-
77
- const sendAuthenticateRequest =
78
- useAuthenticateRequestHandler(requestHandlerName);
79
- const sendLoginRequest = useLoginRequestHandler();
80
- const sendLogoutRequest = useLogoutRequestHandler();
81
-
82
- const sendSendRecoveryKeyRequest = useSendRecoveryKeyRequestHandler();
83
- const sendCheckRecoveryKeyRequest = useCheckRecoveryKeyRequestHandler();
84
- const sendResetPasswordRequest = useResetPasswordRequestHandler();
85
-
86
- const authenticate = useCallback(
87
- (force = false) => {
88
- if (token && (!user || force) && !isLoading) {
89
- setIsLoading(true);
90
- sendAuthenticateRequest({ token }).then(
91
- (response) => {
92
- setIsLoading(false);
93
- setStatus(response.status);
94
- if (response.status === 0 && response.user) {
95
- setUser(response.user);
96
- } else if (response.status > 0) {
97
- setToken(undefined);
98
- setUser(undefined);
99
- }
100
- },
101
- () => {
102
- setIsLoading(false);
103
- setToken(undefined);
104
- setUser(undefined);
105
- }
106
- );
107
- }
108
- },
109
- [isLoading, sendAuthenticateRequest, token, user]
110
- );
111
-
112
- useEffect(() => {
113
- if (token) {
114
- sessionStorage.setItem('authToken', token);
115
- authenticate();
116
- } else {
117
- sessionStorage.removeItem('authToken');
118
- }
119
- }, [authenticate, token]);
120
-
121
- const [networkStatus, setNetworkStatus] = useState<boolean | undefined>(
122
- false
123
- );
124
-
125
- const login = useCallback(
126
- async (username: string, password: string) => {
127
- if (isLoading) {
128
- throw new Error('Already loading');
129
- }
130
-
131
- setIsLoading(true);
132
- return sendLoginRequest({ username, password })
133
- .then((response) => {
134
- setIsLoading(false);
135
- if (response.status === 0 && response.user) {
136
- setToken(response.token);
137
- setUser(response.user);
138
- return true;
139
- }
140
- throw new Error(`Login failed with status ${response.status}`);
141
- })
142
- .catch(() => {
143
- setIsLoading(false);
144
- setToken(undefined);
145
- setUser(undefined);
146
- return false;
147
- });
148
- },
149
- [isLoading, sendLoginRequest]
150
- );
151
-
152
- const logout = useCallback(
153
- async () =>
154
- sendLogoutRequest({})
155
- .then((response) => {
156
- return response.status === 0;
157
- })
158
- .finally(() => {
159
- setToken(undefined);
160
- setUser(undefined);
161
- setStatus(undefined);
162
- }),
163
- [sendLogoutRequest]
164
- );
165
-
166
- const sendRecoveryKey = useCallback(
167
- async (email: string) =>
168
- sendSendRecoveryKeyRequest({ email }).then((response) => {
169
- return response.status;
170
- }),
171
- [sendSendRecoveryKeyRequest]
172
- );
173
-
174
- const resetPassword = useCallback(
175
- async (key: string, password: string) =>
176
- sendResetPasswordRequest({ key, password }).then((response) => {
177
- return response.status;
178
- }),
179
- [sendResetPasswordRequest]
180
- );
181
-
182
- const checkRecoveryKey = useCallback(
183
- async (key: string) =>
184
- sendCheckRecoveryKeyRequest({ key }).then((response) => {
185
- return response.status;
186
- }),
187
- [sendCheckRecoveryKeyRequest]
188
- );
189
-
190
- const hasPermission = useCallback(
191
- (permission: string) =>
192
- permission in allPermissions &&
193
- !!allPermissions[permission] &&
194
- (user?.isAdmin ||
195
- user?.profile?.isAdmin ||
196
- (user?.permissions?.includes(permission) ?? false) ||
197
- (user?.profile?.permissions?.includes(permission) ?? false)),
198
- [
199
- allPermissions,
200
- user?.isAdmin,
201
- user?.permissions,
202
- user?.profile?.isAdmin,
203
- user?.profile?.permissions,
204
- ]
205
- );
206
-
207
- const webSocketStatusChanged = useCallback(
208
- (status: boolean | undefined) => {
209
- if (!networkStatus && status) {
210
- authenticate(true);
211
- }
212
- setNetworkStatus(status);
213
- },
214
- [authenticate, networkStatus]
215
- );
216
-
217
- const webSocketErrorReceived = useCallback(
218
- ({ status }: { status: number; message: string }) => {
219
- // Handle logout when NotAuthenticated error is received
220
- if (status === -4) {
221
- logout();
222
- }
223
- },
224
- [logout]
225
- );
226
-
227
- useEffect(() => {
228
- const subscription = WebSocketService.getInstance().status$.subscribe(
229
- webSocketStatusChanged
230
- );
231
- return () => {
232
- subscription.unsubscribe();
233
- };
234
- }, [webSocketStatusChanged]);
235
-
236
- useEffect(() => {
237
- const subscription = WebSocketService.getInstance().error$.subscribe(
238
- webSocketErrorReceived
239
- );
240
- return () => {
241
- subscription.unsubscribe();
242
- };
243
- }, [webSocketErrorReceived]);
244
-
245
- const contextValue = useMemo(
246
- () => ({
247
- status,
248
- token,
249
- user,
250
- isAuthenticated: !!user,
251
- authenticate,
252
- login,
253
- logout,
254
- sendRecoveryKey,
255
- checkRecoveryKey,
256
- resetPassword,
257
- permissions: allPermissions,
258
- hasPermission,
259
- setToken,
260
- }),
261
- [
262
- status,
263
- token,
264
- user,
265
- authenticate,
266
- login,
267
- logout,
268
- sendRecoveryKey,
269
- checkRecoveryKey,
270
- resetPassword,
271
- allPermissions,
272
- hasPermission,
273
- ]
274
- );
275
-
276
- useEffect(() => {
277
- const subscription = WebSocketService.getInstance()?.status$.subscribe(
278
- (newStatus) => {
279
- setWebSocketStatus((previousStatus) => {
280
- if (!previousStatus && newStatus) {
281
- authenticate(true);
282
- }
283
- return newStatus;
284
- });
285
- }
286
- );
287
- return () => subscription?.unsubscribe();
288
- }, [authenticate]);
289
-
290
- return (
291
- <AuthenticationContext.Provider value={contextValue}>
292
- {!user ? (
293
- LoginComponent ? (
294
- <LoginComponent />
295
- ) : (
296
- <div>Not authenticated</div>
297
- )
298
- ) : (
299
- children
300
- )}
301
- </AuthenticationContext.Provider>
302
- );
303
- };
1
+ import {
2
+ FC,
3
+ PropsWithChildren,
4
+ createContext,
5
+ useCallback,
6
+ useEffect,
7
+ useMemo,
8
+ useState,
9
+ } from 'react';
10
+ import {
11
+ useAuthenticateRequestHandler,
12
+ useCheckRecoveryKeyRequestHandler,
13
+ useLoginRequestHandler,
14
+ useLogoutRequestHandler,
15
+ useResetPasswordRequestHandler,
16
+ useSendRecoveryKeyRequestHandler,
17
+ } from '../../services/requests/auth';
18
+
19
+ import { UserDTO } from '../../services/types/users';
20
+ import { WebSocketService } from '../../services/WebSocketService';
21
+ import { basePermissions } from './helpers';
22
+
23
+ export type AuthenticationContextProps = {
24
+ status?: number;
25
+ token?: string;
26
+ user?: UserDTO;
27
+ isAuthenticated: boolean;
28
+ authenticate: () => void;
29
+ login: (username: string, password: string) => Promise<boolean>;
30
+ logout: () => Promise<boolean>;
31
+ sendRecoveryKey: (email: string) => Promise<number>;
32
+ checkRecoveryKey: (key: string) => Promise<number>;
33
+ resetPassword: (key: string, password: string) => Promise<number>;
34
+ permissions: Record<string, string>;
35
+ hasPermission: (permission: string) => boolean;
36
+ setToken: (token?: string) => void;
37
+ };
38
+
39
+ export const AuthenticationContext = createContext<AuthenticationContextProps>({
40
+ status: undefined,
41
+ authenticate: () => {},
42
+ isAuthenticated: false,
43
+ login: async () => false,
44
+ logout: async () => false,
45
+ sendRecoveryKey: async () => -1,
46
+ checkRecoveryKey: async () => -1,
47
+ resetPassword: async () => -1,
48
+ permissions: {},
49
+ hasPermission: () => false,
50
+ setToken: () => {},
51
+ });
52
+
53
+ export type AuthenticationProviderProps = PropsWithChildren<{
54
+ permissions?: Record<string, string>;
55
+ loginComponent?: FC;
56
+ requestHandlerName?: string;
57
+ }>;
58
+
59
+ export const AuthenticationProvider: FC<AuthenticationProviderProps> = ({
60
+ permissions,
61
+ loginComponent: LoginComponent,
62
+ children,
63
+ requestHandlerName,
64
+ }) => {
65
+ const allPermissions = useMemo(
66
+ () => ({ ...basePermissions, ...permissions }),
67
+ [permissions]
68
+ );
69
+ const [isLoading, setIsLoading] = useState<boolean>(false);
70
+ const [, setWebSocketStatus] = useState<boolean | undefined>(false);
71
+ const [token, setToken] = useState<string | undefined>(
72
+ sessionStorage.getItem('authToken') || undefined
73
+ );
74
+ const [user, setUser] = useState<UserDTO | undefined>(undefined);
75
+ const [status, setStatus] = useState<number | undefined>(undefined);
76
+
77
+ const sendAuthenticateRequest =
78
+ useAuthenticateRequestHandler(requestHandlerName);
79
+ const sendLoginRequest = useLoginRequestHandler();
80
+ const sendLogoutRequest = useLogoutRequestHandler();
81
+
82
+ const sendSendRecoveryKeyRequest = useSendRecoveryKeyRequestHandler();
83
+ const sendCheckRecoveryKeyRequest = useCheckRecoveryKeyRequestHandler();
84
+ const sendResetPasswordRequest = useResetPasswordRequestHandler();
85
+
86
+ const authenticate = useCallback(
87
+ (force = false) => {
88
+ if (token && (!user || force) && !isLoading) {
89
+ setIsLoading(true);
90
+ sendAuthenticateRequest({ token }).then(
91
+ (response) => {
92
+ setIsLoading(false);
93
+ setStatus(response.status);
94
+ if (response.status === 0 && response.user) {
95
+ setUser(response.user);
96
+ } else if (response.status > 0) {
97
+ setToken(undefined);
98
+ setUser(undefined);
99
+ }
100
+ },
101
+ () => {
102
+ setIsLoading(false);
103
+ setToken(undefined);
104
+ setUser(undefined);
105
+ }
106
+ );
107
+ }
108
+ },
109
+ [isLoading, sendAuthenticateRequest, token, user]
110
+ );
111
+
112
+ useEffect(() => {
113
+ if (token) {
114
+ sessionStorage.setItem('authToken', token);
115
+ authenticate();
116
+ } else {
117
+ sessionStorage.removeItem('authToken');
118
+ }
119
+ }, [authenticate, token]);
120
+
121
+ const [networkStatus, setNetworkStatus] = useState<boolean | undefined>(
122
+ false
123
+ );
124
+
125
+ const login = useCallback(
126
+ async (username: string, password: string) => {
127
+ if (isLoading) {
128
+ throw new Error('Already loading');
129
+ }
130
+
131
+ setIsLoading(true);
132
+ return sendLoginRequest({ username, password })
133
+ .then((response) => {
134
+ setIsLoading(false);
135
+ if (response.status === 0 && response.user) {
136
+ setToken(response.token);
137
+ setUser(response.user);
138
+ return true;
139
+ }
140
+ throw new Error(`Login failed with status ${response.status}`);
141
+ })
142
+ .catch(() => {
143
+ setIsLoading(false);
144
+ setToken(undefined);
145
+ setUser(undefined);
146
+ return false;
147
+ });
148
+ },
149
+ [isLoading, sendLoginRequest]
150
+ );
151
+
152
+ const logout = useCallback(
153
+ async () =>
154
+ sendLogoutRequest({})
155
+ .then((response) => {
156
+ return response.status === 0;
157
+ })
158
+ .finally(() => {
159
+ setToken(undefined);
160
+ setUser(undefined);
161
+ setStatus(undefined);
162
+ }),
163
+ [sendLogoutRequest]
164
+ );
165
+
166
+ const sendRecoveryKey = useCallback(
167
+ async (email: string) =>
168
+ sendSendRecoveryKeyRequest({ email }).then((response) => {
169
+ return response.status;
170
+ }),
171
+ [sendSendRecoveryKeyRequest]
172
+ );
173
+
174
+ const resetPassword = useCallback(
175
+ async (key: string, password: string) =>
176
+ sendResetPasswordRequest({ key, password }).then((response) => {
177
+ return response.status;
178
+ }),
179
+ [sendResetPasswordRequest]
180
+ );
181
+
182
+ const checkRecoveryKey = useCallback(
183
+ async (key: string) =>
184
+ sendCheckRecoveryKeyRequest({ key }).then((response) => {
185
+ return response.status;
186
+ }),
187
+ [sendCheckRecoveryKeyRequest]
188
+ );
189
+
190
+ const hasPermission = useCallback(
191
+ (permission: string) =>
192
+ permission in allPermissions &&
193
+ !!allPermissions[permission] &&
194
+ (user?.isAdmin ||
195
+ user?.profile?.isAdmin ||
196
+ (user?.permissions?.includes(permission) ?? false) ||
197
+ (user?.profile?.permissions?.includes(permission) ?? false)),
198
+ [
199
+ allPermissions,
200
+ user?.isAdmin,
201
+ user?.permissions,
202
+ user?.profile?.isAdmin,
203
+ user?.profile?.permissions,
204
+ ]
205
+ );
206
+
207
+ const webSocketStatusChanged = useCallback(
208
+ (status: boolean | undefined) => {
209
+ if (!networkStatus && status) {
210
+ authenticate(true);
211
+ }
212
+ setNetworkStatus(status);
213
+ },
214
+ [authenticate, networkStatus]
215
+ );
216
+
217
+ const webSocketErrorReceived = useCallback(
218
+ ({ status }: { status: number; message: string }) => {
219
+ // Handle logout when NotAuthenticated error is received
220
+ if (status === -4) {
221
+ logout();
222
+ }
223
+ },
224
+ [logout]
225
+ );
226
+
227
+ useEffect(() => {
228
+ const subscription = WebSocketService.getInstance().status$.subscribe(
229
+ webSocketStatusChanged
230
+ );
231
+ return () => {
232
+ subscription.unsubscribe();
233
+ };
234
+ }, [webSocketStatusChanged]);
235
+
236
+ useEffect(() => {
237
+ const subscription = WebSocketService.getInstance().error$.subscribe(
238
+ webSocketErrorReceived
239
+ );
240
+ return () => {
241
+ subscription.unsubscribe();
242
+ };
243
+ }, [webSocketErrorReceived]);
244
+
245
+ const contextValue = useMemo(
246
+ () => ({
247
+ status,
248
+ token,
249
+ user,
250
+ isAuthenticated: !!user,
251
+ authenticate,
252
+ login,
253
+ logout,
254
+ sendRecoveryKey,
255
+ checkRecoveryKey,
256
+ resetPassword,
257
+ permissions: allPermissions,
258
+ hasPermission,
259
+ setToken,
260
+ }),
261
+ [
262
+ status,
263
+ token,
264
+ user,
265
+ authenticate,
266
+ login,
267
+ logout,
268
+ sendRecoveryKey,
269
+ checkRecoveryKey,
270
+ resetPassword,
271
+ allPermissions,
272
+ hasPermission,
273
+ ]
274
+ );
275
+
276
+ useEffect(() => {
277
+ const subscription = WebSocketService.getInstance()?.status$.subscribe(
278
+ (newStatus) => {
279
+ setWebSocketStatus((previousStatus) => {
280
+ if (!previousStatus && newStatus) {
281
+ authenticate(true);
282
+ }
283
+ return newStatus;
284
+ });
285
+ }
286
+ );
287
+ return () => subscription?.unsubscribe();
288
+ }, [authenticate]);
289
+
290
+ return (
291
+ <AuthenticationContext.Provider value={contextValue}>
292
+ {!user ? (
293
+ LoginComponent ? (
294
+ <LoginComponent />
295
+ ) : (
296
+ <div>Not authenticated</div>
297
+ )
298
+ ) : (
299
+ children
300
+ )}
301
+ </AuthenticationContext.Provider>
302
+ );
303
+ };