@fctc/widget-logic 1.3.0 → 1.3.2

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/dist/config.d.mts +7 -0
  2. package/dist/config.d.ts +7 -0
  3. package/dist/config.js +24 -0
  4. package/dist/config.mjs +2 -0
  5. package/dist/constants.d.mts +1 -0
  6. package/dist/constants.d.ts +1 -0
  7. package/dist/constants.js +24 -0
  8. package/dist/constants.mjs +2 -0
  9. package/dist/environment.d.mts +1 -0
  10. package/dist/environment.d.ts +1 -0
  11. package/dist/environment.js +24 -0
  12. package/dist/environment.mjs +2 -0
  13. package/dist/hooks.d.mts +970 -0
  14. package/dist/hooks.d.ts +970 -0
  15. package/dist/hooks.js +734 -0
  16. package/dist/hooks.mjs +720 -0
  17. package/dist/icons.d.mts +9 -0
  18. package/dist/icons.d.ts +9 -0
  19. package/dist/icons.js +139 -0
  20. package/dist/icons.mjs +110 -0
  21. package/dist/index.d.mts +314 -5
  22. package/dist/index.d.ts +314 -5
  23. package/dist/index.js +7216 -231
  24. package/dist/index.mjs +7340 -221
  25. package/dist/model.d.mts +1 -0
  26. package/dist/model.d.ts +1 -0
  27. package/dist/model.js +24 -0
  28. package/dist/model.mjs +2 -0
  29. package/dist/provider.d.mts +1 -0
  30. package/dist/provider.d.ts +1 -0
  31. package/dist/provider.js +24 -0
  32. package/dist/provider.mjs +2 -0
  33. package/dist/services.d.mts +1 -0
  34. package/dist/services.d.ts +1 -0
  35. package/dist/services.js +24 -0
  36. package/dist/services.mjs +2 -0
  37. package/dist/store.d.mts +1 -0
  38. package/dist/store.d.ts +1 -0
  39. package/dist/store.js +24 -0
  40. package/dist/store.mjs +2 -0
  41. package/dist/types.d.mts +31 -0
  42. package/dist/types.d.ts +31 -0
  43. package/dist/types.js +24 -0
  44. package/dist/types.mjs +2 -0
  45. package/dist/utils.d.mts +38 -0
  46. package/dist/utils.d.ts +38 -0
  47. package/dist/utils.js +282 -0
  48. package/dist/utils.mjs +242 -0
  49. package/package.json +70 -22
  50. package/dist/action.d.mts +0 -68
  51. package/dist/action.d.ts +0 -68
  52. package/dist/action.js +0 -152
  53. package/dist/action.mjs +0 -122
  54. package/dist/common.d.mts +0 -13
  55. package/dist/common.d.ts +0 -13
  56. package/dist/common.js +0 -60
  57. package/dist/common.mjs +0 -33
  58. package/dist/form.d.mts +0 -41
  59. package/dist/form.d.ts +0 -41
  60. package/dist/form.js +0 -116
  61. package/dist/form.mjs +0 -87
  62. package/dist/table.d.mts +0 -33
  63. package/dist/table.d.ts +0 -33
  64. package/dist/table.js +0 -118
  65. package/dist/table.mjs +0 -91
package/dist/hooks.js ADDED
@@ -0,0 +1,734 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/hooks.ts
22
+ var hooks_exports = {};
23
+ __export(hooks_exports, {
24
+ AppProvider: () => AppProvider,
25
+ useAppProvider: () => useAppProvider,
26
+ useAuth: () => useAuth,
27
+ useCallAction: () => useCallAction,
28
+ useClickOutside: () => useClickOutside,
29
+ useConfig: () => useConfig,
30
+ useDebounce: () => useDebounce,
31
+ useDetail: () => useDetail,
32
+ useListData: () => useListData,
33
+ useMenu: () => useMenu,
34
+ useProfile: () => useProfile,
35
+ useUser: () => useUser,
36
+ useViewV2: () => useViewV2
37
+ });
38
+ module.exports = __toCommonJS(hooks_exports);
39
+
40
+ // src/hooks/core/use-call-action.ts
41
+ var import_interface_logic = require("@fctc/interface-logic");
42
+ var import_react = require("react");
43
+ var useCallAction = () => {
44
+ const queryLoadAction = (0, import_interface_logic.useLoadAction)();
45
+ const queryRunAction = (0, import_interface_logic.useRunAction)();
46
+ const [data, setData] = (0, import_react.useState)(void 0);
47
+ const callAction = async ({
48
+ aid,
49
+ actionType = "ir.actions.act_window"
50
+ }) => {
51
+ const context = (0, import_interface_logic.getEnv)().context;
52
+ let res = void 0;
53
+ if (actionType === "ir.actions.act_window") {
54
+ res = await queryLoadAction.mutateAsync({
55
+ idAction: aid,
56
+ context
57
+ });
58
+ } else if (actionType === "ir.actions.server") {
59
+ res = await queryRunAction.mutateAsync({
60
+ idAction: aid,
61
+ context
62
+ });
63
+ }
64
+ setData(res);
65
+ return res;
66
+ };
67
+ return [data, callAction];
68
+ };
69
+
70
+ // src/hooks/core/use-config.ts
71
+ var import_interface_logic2 = require("@fctc/interface-logic");
72
+ var import_react2 = require("react");
73
+ var useConfig = ({ localStorageUtils, sessionStorageUtils }) => {
74
+ const dispatch = (0, import_interface_logic2.useAppDispatch)();
75
+ const envConfig = (0, import_react2.useMemo)(() => {
76
+ return {
77
+ mode: "development",
78
+ baseUrl: "https://api.vitrust.app/c2/api/v2",
79
+ config: {
80
+ grantType: "password",
81
+ clientId: "C52foVQSMpnNOcAP2CBIIkupOSfxUarF8nlOPfXM",
82
+ clientSecret: "rColINr4a9QBFQPqQB8YU1XfBjqzwerDMJGBxsFK"
83
+ }
84
+ };
85
+ }, []);
86
+ const config = (0, import_react2.useMemo)(() => {
87
+ return {
88
+ VITE_SIDEBAR_TYPE: "grid/sidebar",
89
+ VITE_APP_DOMAIN: "https://api.vitrust.app/c2/",
90
+ VITE_IS_EDU: true,
91
+ VITE_LOGO_WHITE_LOGIN: "https://static.vitrust.app/vitrust/3a/3a1301f614dea6ee19ebf99b68f57e3fd46011d2.png",
92
+ VITE_LOGO_BLACK_LOGIN: "https://static.vitrust.app/vitrust/32/3223918780da7a439f916faac9abf0bfe98dfa07.png",
93
+ VITE_BACKGROUND_SIDEBAR: "linear-gradient(178deg, rgb(1, 106, 13) -0.89%, rgb(4, 179, 66) 99.46%",
94
+ VITE_BANNER: "https://static.vitrust.app/vitrust/5d/5d20cab0627182b4ed5cba4ee42c58b98b663e5b.svg",
95
+ VITE_BG_BUTTON: "#008F3C",
96
+ VITE_BACKGROUND_PAGE: "#F9FAFB"
97
+ };
98
+ }, []);
99
+ (0, import_react2.useEffect)(() => {
100
+ try {
101
+ const env = (0, import_interface_logic2.getEnv)();
102
+ env.setupEnv({
103
+ baseUrl: envConfig.baseUrl,
104
+ port: 3e3,
105
+ config: {
106
+ grantType: envConfig.config.grantType,
107
+ clientId: envConfig.config.clientId,
108
+ clientSecret: envConfig.config.clientSecret
109
+ },
110
+ db: "preschool",
111
+ localStorageUtils: localStorageUtils(),
112
+ sessionStorageUtils: sessionStorageUtils()
113
+ });
114
+ dispatch((0, import_interface_logic2.setEnvFile)(config));
115
+ } catch (error) {
116
+ console.error("Error loading env or config:", error);
117
+ }
118
+ }, [dispatch, envConfig, config]);
119
+ return { envConfig, config };
120
+ };
121
+
122
+ // src/hooks/core/use-detail.ts
123
+ var import_interface_logic3 = require("@fctc/interface-logic");
124
+ var import_react_query = require("@tanstack/react-query");
125
+ var import_react3 = require("react");
126
+ var useDetail = (accessToken, sub) => {
127
+ const dispatch = (0, import_interface_logic3.useAppDispatch)();
128
+ const fetchGetDetail = (0, import_interface_logic3.useGetDetail)();
129
+ const userDetailQuery = (0, import_react_query.useQuery)({
130
+ queryKey: ["userDetailQuery", sub && accessToken],
131
+ queryFn: () => {
132
+ return fetchGetDetail.mutateAsync({
133
+ model: "res.users",
134
+ ids: [sub],
135
+ specification: { image_256: {} }
136
+ });
137
+ },
138
+ enabled: !!sub && !!accessToken
139
+ });
140
+ (0, import_react3.useEffect)(() => {
141
+ if (userDetailQuery.data) {
142
+ const userPicture = userDetailQuery.data;
143
+ dispatch(
144
+ (0, import_interface_logic3.setProfile)({ ...userPicture, image: userPicture?.[0]?.image_256 })
145
+ );
146
+ }
147
+ }, [userDetailQuery.data, dispatch]);
148
+ return userDetailQuery;
149
+ };
150
+
151
+ // src/hooks/core/use-list-data.ts
152
+ var import_react5 = require("react");
153
+ var import_interface_logic4 = require("@fctc/interface-logic");
154
+
155
+ // src/utils/function.ts
156
+ var import_react4 = require("react");
157
+ var getDateRange = (currentDate, unit) => {
158
+ const date = new Date(currentDate);
159
+ let dateStart, dateEnd;
160
+ function formatDate(d) {
161
+ return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0") + " " + String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0") + ":" + String(d.getSeconds()).padStart(2, "0");
162
+ }
163
+ switch (unit) {
164
+ case "month":
165
+ dateStart = new Date(
166
+ date.getFullYear(),
167
+ date.getMonth() + 1,
168
+ date.getDate(),
169
+ 23,
170
+ 59,
171
+ 59
172
+ );
173
+ dateStart.setHours(dateStart.getHours() - 7);
174
+ dateEnd = new Date(date.getFullYear(), date.getMonth(), 0, 0, 0, 0);
175
+ dateEnd.setHours(dateEnd.getHours() - 7);
176
+ break;
177
+ case "day":
178
+ dateStart = new Date(
179
+ date.getFullYear(),
180
+ date.getMonth(),
181
+ date.getDate(),
182
+ 23,
183
+ 59,
184
+ 59
185
+ );
186
+ dateStart.setHours(dateStart.getHours() - 7);
187
+ dateEnd = new Date(
188
+ date.getFullYear(),
189
+ date.getMonth(),
190
+ date.getDate(),
191
+ 0,
192
+ 0,
193
+ 0
194
+ );
195
+ dateEnd.setHours(dateEnd.getHours() - 7);
196
+ break;
197
+ case "week":
198
+ const dayOfWeek = date.getDay();
199
+ const daysToMonday = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
200
+ const daysToSunday = dayOfWeek === 0 ? 0 : 7 - dayOfWeek;
201
+ dateStart = new Date(
202
+ date.getFullYear(),
203
+ date.getMonth(),
204
+ date.getDate() + daysToSunday,
205
+ 23,
206
+ 59,
207
+ 59
208
+ );
209
+ dateStart.setHours(dateStart.getHours() - 7);
210
+ dateEnd = new Date(
211
+ date.getFullYear(),
212
+ date.getMonth(),
213
+ date.getDate() + daysToMonday,
214
+ 0,
215
+ 0,
216
+ 0
217
+ );
218
+ dateEnd.setHours(dateEnd.getHours() - 7);
219
+ break;
220
+ case "year":
221
+ dateStart = new Date(date.getFullYear(), 11, 31, 23, 59, 59);
222
+ dateStart.setHours(dateStart.getHours() - 7);
223
+ dateEnd = new Date(date.getFullYear() - 1, 11, 31, 0, 0, 0);
224
+ dateEnd.setHours(dateEnd.getHours() - 7);
225
+ break;
226
+ default:
227
+ throw new Error(
228
+ "\u0110\u01A1n v\u1ECB kh\xF4ng h\u1EE3p l\u1EC7. Ch\u1EC9 ch\u1EA5p nh\u1EADn: week, day, month, year"
229
+ );
230
+ }
231
+ return [
232
+ ["date_start", "<=", formatDate(dateStart)],
233
+ ["date_end", ">=", formatDate(dateEnd)]
234
+ ];
235
+ };
236
+ var convertFieldsToArray = (fields) => {
237
+ const defaultFields = ["display_name", "date_start", "date_end"];
238
+ if (!fields || !Array.isArray(fields)) {
239
+ return defaultFields;
240
+ }
241
+ const inputFields = fields.filter((field) => field && field.type_co === "field").map((field) => field.name);
242
+ return [...defaultFields, ...inputFields];
243
+ };
244
+ function combineContexts(contexts) {
245
+ if (contexts.some((context) => !context)) {
246
+ return void 0;
247
+ } else {
248
+ const res = contexts.reduce((acc, context) => {
249
+ return { ...acc, ...context };
250
+ }, {});
251
+ return res;
252
+ }
253
+ }
254
+ function useAsyncState(initialValue = [true, null]) {
255
+ return (0, import_react4.useReducer)(
256
+ (_state, action = null) => [false, action],
257
+ initialValue
258
+ );
259
+ }
260
+ async function setStorageItemAsync(key, value) {
261
+ try {
262
+ if (value === null) {
263
+ localStorage.removeItem(key);
264
+ } else {
265
+ localStorage.setItem(key, value);
266
+ }
267
+ } catch (e) {
268
+ console.error("Local storage is unavailable:", e);
269
+ }
270
+ }
271
+ function useStorageState(key) {
272
+ const [state, setState] = useAsyncState();
273
+ (0, import_react4.useEffect)(() => {
274
+ try {
275
+ const storedValue = localStorage.getItem(key);
276
+ setState(storedValue);
277
+ } catch (e) {
278
+ console.error("Local storage is unavailable:", e);
279
+ }
280
+ }, [key]);
281
+ const setValue = (0, import_react4.useCallback)(
282
+ (value) => {
283
+ setState(value);
284
+ setStorageItemAsync(key, value);
285
+ },
286
+ [key]
287
+ );
288
+ return [state, setValue];
289
+ }
290
+
291
+ // src/hooks/core/use-list-data.ts
292
+ var useListData = ({
293
+ action,
294
+ context,
295
+ viewResponse
296
+ }) => {
297
+ const { groupByDomain } = (0, import_interface_logic4.useAppSelector)(import_interface_logic4.selectSearch);
298
+ const initModel = (0, import_interface_logic4.useModel)();
299
+ const [type, setType] = (0, import_react5.useState)("list");
300
+ const [mode, setMode] = (0, import_react5.useState)("month");
301
+ const [currentDate, setCurrentDate] = (0, import_react5.useState)(/* @__PURE__ */ new Date());
302
+ const { pageLimit, page, order } = (0, import_interface_logic4.useAppSelector)(import_interface_logic4.selectList);
303
+ const listDataProps = (0, import_react5.useMemo)(() => {
304
+ const actData = action?.result;
305
+ if (!viewResponse || !actData || !context) {
306
+ return null;
307
+ }
308
+ const specification = initModel.initModel({
309
+ name: String(actData.res_model),
310
+ view: viewResponse || {},
311
+ actContext: context,
312
+ fields: type === "kanban" ? viewResponse?.views?.kanban?.fields : type === "calendar" ? viewResponse?.views?.calendar?.fields : viewResponse?.views?.list?.fields
313
+ }).getSpecification();
314
+ const domain = type === "calendar" ? getDateRange(currentDate, mode) : actData?.domain ? Array.isArray(actData?.domain) ? [...actData?.domain] : (0, import_interface_logic4.evalJSONDomain)(actData?.domain, context) : [];
315
+ const limit = type === "calendar" ? 2500 : pageLimit;
316
+ const offset = page * pageLimit;
317
+ const fields = type === "calendar" ? convertFieldsToArray(viewResponse?.views?.calendar?.fields) || [] : typeof groupByDomain === "object" ? groupByDomain?.fields : void 0;
318
+ const groupby = typeof groupByDomain === "object" ? [groupByDomain?.contexts?.[0]?.group_by] : [];
319
+ const sort = order ? order : viewResponse?.views?.list?.default_order ? (0, import_interface_logic4.formatSortingString)(viewResponse?.views?.list?.default_order) : "";
320
+ return {
321
+ model: actData.res_model,
322
+ specification,
323
+ domain,
324
+ limit,
325
+ offset,
326
+ fields,
327
+ groupby,
328
+ context,
329
+ sort,
330
+ type
331
+ };
332
+ }, [
333
+ action?.result,
334
+ context,
335
+ currentDate,
336
+ groupByDomain,
337
+ initModel,
338
+ mode,
339
+ order,
340
+ page,
341
+ pageLimit,
342
+ type,
343
+ viewResponse
344
+ ]);
345
+ const list = (0, import_interface_logic4.useGetListData)(
346
+ listDataProps,
347
+ [listDataProps],
348
+ !!listDataProps
349
+ );
350
+ return {
351
+ ...list,
352
+ state: {
353
+ type,
354
+ setType,
355
+ mode,
356
+ setMode,
357
+ currentDate,
358
+ setCurrentDate
359
+ }
360
+ };
361
+ };
362
+
363
+ // src/hooks/core/use-menu.ts
364
+ var import_react6 = require("react");
365
+ var import_interface_logic5 = require("@fctc/interface-logic");
366
+
367
+ // src/utils/constants.ts
368
+ var languages = [
369
+ { id: "vi_VN", name: "VIE" },
370
+ { id: "en_US", name: "ENG" }
371
+ ];
372
+ var API_APP_URL = {
373
+ baseUrl: "https://api.vitrust.app",
374
+ c2: "https://api.vitrust.app/c2",
375
+ apiV2: "https://api.vitrust.app/c2/api/v2"
376
+ };
377
+
378
+ // src/hooks/core/use-menu.ts
379
+ var useMenu = ({ context }) => {
380
+ const menuData = (0, import_interface_logic5.useGetMenu)(context, !!context);
381
+ const [menuid, setMenuId] = (0, import_react6.useState)(void 0);
382
+ const [action, setAction] = useCallAction();
383
+ const configedIconData = (0, import_react6.useMemo)(() => {
384
+ const data = menuData.data;
385
+ return data?.map((item) => {
386
+ return {
387
+ ...item,
388
+ child_id: item?.child_id?.map((child) => {
389
+ return {
390
+ ...child,
391
+ url_icon: API_APP_URL.c2 + "/" + child.url_icon
392
+ };
393
+ }) ?? [],
394
+ url_icon: API_APP_URL.c2 + "/" + item.url_icon
395
+ };
396
+ });
397
+ }, [menuData.data]);
398
+ const handleChangeMenu = async ({
399
+ menu,
400
+ subMenu
401
+ }) => {
402
+ const aid = subMenu?.action?.id?.id;
403
+ const actionType = subMenu?.action?.type;
404
+ await setAction({
405
+ aid: Number(aid),
406
+ actionType
407
+ });
408
+ if (menu) {
409
+ setMenuId(menu.id?.toString() ?? "");
410
+ }
411
+ };
412
+ (0, import_react6.useEffect)(() => {
413
+ const firstRecord = configedIconData?.[0];
414
+ const firstChild = firstRecord?.child_id?.[0];
415
+ if (firstChild && firstRecord) {
416
+ handleChangeMenu({ menu: firstRecord, subMenu: firstChild });
417
+ }
418
+ }, [configedIconData]);
419
+ return {
420
+ ...menuData,
421
+ data: configedIconData,
422
+ action: { handleChangeMenu },
423
+ state: { menuid, action },
424
+ context
425
+ };
426
+ };
427
+
428
+ // src/hooks/core/use-profile.ts
429
+ var import_react_query2 = require("@tanstack/react-query");
430
+ var import_react7 = require("react");
431
+ var import_react_i18next = require("react-i18next");
432
+ var import_interface_logic6 = require("@fctc/interface-logic");
433
+ var useProfile = (accessToken) => {
434
+ const getProfile = (0, import_interface_logic6.useGetProfile)();
435
+ const dispatch = (0, import_interface_logic6.useAppDispatch)();
436
+ const { i18n } = (0, import_react_i18next.useTranslation)();
437
+ const fetchUserProfile = async () => {
438
+ return await getProfile.mutateAsync();
439
+ };
440
+ const userInfoQuery = (0, import_react_query2.useQuery)({
441
+ queryKey: ["userInfo", accessToken],
442
+ queryFn: fetchUserProfile,
443
+ enabled: !!accessToken
444
+ });
445
+ (0, import_react7.useEffect)(() => {
446
+ if (userInfoQuery.data) {
447
+ const userInfo = userInfoQuery.data;
448
+ const env = (0, import_interface_logic6.getEnv)();
449
+ env.setUid(userInfo?.sub);
450
+ dispatch((0, import_interface_logic6.setDataUser)(userInfo));
451
+ const userLocale = languages.find((lang) => lang?.id === userInfo?.locale);
452
+ env.setLang(userLocale?.id);
453
+ i18n.changeLanguage(userLocale?.id.split("_")[0]);
454
+ }
455
+ }, [dispatch, userInfoQuery.data]);
456
+ const context = (0, import_react7.useMemo)(() => {
457
+ if (userInfoQuery.data?.sub && userInfoQuery.data?.locale) {
458
+ return {
459
+ uid: Number(userInfoQuery.data.sub),
460
+ allowed_company_ids: [],
461
+ lang: String(userInfoQuery.data.locale),
462
+ tz: "Asia/Saigon"
463
+ };
464
+ }
465
+ return void 0;
466
+ }, [userInfoQuery.data]);
467
+ return { ...userInfoQuery, context };
468
+ };
469
+
470
+ // src/hooks/core/use-user.ts
471
+ var useUser = (accessToken) => {
472
+ const userProfile = useProfile(accessToken);
473
+ const userDetail = useDetail(accessToken, userProfile.data?.sub);
474
+ return { userProfile, userDetail, context: userProfile.context };
475
+ };
476
+
477
+ // src/hooks/core/use-view-v2.ts
478
+ var import_react8 = require("react");
479
+ var import_interface_logic7 = require("@fctc/interface-logic");
480
+ var useViewV2 = ({
481
+ action,
482
+ context
483
+ }) => {
484
+ const viewParams = (0, import_react8.useMemo)(() => {
485
+ if (!action?.result) {
486
+ return void 0;
487
+ }
488
+ const actionResult = action?.result;
489
+ return {
490
+ model: String(actionResult?.res_model),
491
+ views: [
492
+ ...Array.isArray(actionResult?.views) ? actionResult?.views.map(
493
+ (view2) => view2[1] === "list" ? [view2[0], "list"] : view2
494
+ ) : [],
495
+ [
496
+ Array.isArray(actionResult?.search_view_id) ? actionResult?.search_view_id[0] : actionResult?.search_view_id,
497
+ "search"
498
+ ]
499
+ ],
500
+ context
501
+ };
502
+ }, [action, context]);
503
+ const view = (0, import_interface_logic7.useGetView)(
504
+ viewParams || {},
505
+ !!viewParams
506
+ );
507
+ return {
508
+ ...view,
509
+ context
510
+ };
511
+ };
512
+
513
+ // src/hooks/core/use-auth.ts
514
+ var import_interface_logic8 = require("@fctc/interface-logic");
515
+ var useAuth = () => {
516
+ const [[isLoading, accessToken], setAccessToken] = useStorageState("TOKEN");
517
+ const loginMutate = (0, import_interface_logic8.useLoginCredential)();
518
+ const dispatch = (0, import_interface_logic8.useAppDispatch)();
519
+ const signIn = async (email, password) => {
520
+ try {
521
+ loginMutate.mutate(
522
+ {
523
+ email,
524
+ password,
525
+ path: "/authentication/oauth2/token"
526
+ },
527
+ {
528
+ onSuccess: (res) => {
529
+ setAccessToken(res.access_token);
530
+ },
531
+ onError: (err) => {
532
+ }
533
+ }
534
+ );
535
+ } catch (error) {
536
+ throw new Error("Login failed");
537
+ }
538
+ };
539
+ const signOut = async () => {
540
+ dispatch((0, import_interface_logic8.setMenuList)([]));
541
+ dispatch((0, import_interface_logic8.setDataUser)({}));
542
+ dispatch((0, import_interface_logic8.setProfile)({}));
543
+ setAccessToken(null);
544
+ };
545
+ return {
546
+ signIn,
547
+ signOut,
548
+ accessToken,
549
+ isLoading
550
+ };
551
+ };
552
+
553
+ // src/hooks/core/use-app-provider.tsx
554
+ var import_react10 = require("react");
555
+
556
+ // src/hooks/core/use-company.ts
557
+ var import_interface_logic9 = require("@fctc/interface-logic");
558
+ var import_react_query3 = require("@tanstack/react-query");
559
+ var import_react9 = require("react");
560
+ var useCompany = (accessToken) => {
561
+ const getCurrentCompany = (0, import_interface_logic9.useGetCurrentCompany)();
562
+ const fetchCurrentCompany = async () => {
563
+ return await getCurrentCompany.mutateAsync();
564
+ };
565
+ const currentCompany = (0, import_react_query3.useQuery)({
566
+ queryKey: ["currentCompany", accessToken],
567
+ queryFn: fetchCurrentCompany,
568
+ enabled: !!accessToken
569
+ });
570
+ const current_company_id = (0, import_react9.useMemo)(() => {
571
+ return currentCompany.data?.current_company_id;
572
+ }, [currentCompany.data]);
573
+ (0, import_react9.useEffect)(() => {
574
+ if (current_company_id) {
575
+ const companyIDs = [current_company_id];
576
+ const env = (0, import_interface_logic9.getEnv)();
577
+ env.setAllowCompanies([...companyIDs]);
578
+ env.setCompanies(companyIDs);
579
+ }
580
+ }, [current_company_id]);
581
+ const getCompanyInfo = (0, import_interface_logic9.useGetCompanyInfo)();
582
+ const companyInfo = (0, import_react_query3.useQuery)({
583
+ queryKey: ["companyInfoQuery", current_company_id, accessToken],
584
+ queryFn: () => getCompanyInfo.mutateAsync(Number(current_company_id)),
585
+ enabled: !!current_company_id && !!accessToken
586
+ });
587
+ (0, import_react9.useEffect)(() => {
588
+ if (companyInfo.data) {
589
+ const companyInfoData = companyInfo.data;
590
+ if (companyInfoData?.length) {
591
+ const env = (0, import_interface_logic9.getEnv)();
592
+ env.setDefaultCompany(companyInfoData[0]);
593
+ }
594
+ }
595
+ }, [companyInfo.data]);
596
+ return {
597
+ currentCompany,
598
+ companyInfo,
599
+ context: { allowed_company_ids: [current_company_id] }
600
+ };
601
+ };
602
+ var use_company_default = useCompany;
603
+
604
+ // src/hooks/core/use-app-provider.tsx
605
+ var import_interface_logic10 = require("@fctc/interface-logic");
606
+ var import_jsx_runtime = require("react/jsx-runtime");
607
+ var AppProviderInitialValue = {
608
+ config: {},
609
+ user: {},
610
+ auth: {},
611
+ company: {},
612
+ action: {},
613
+ menu: {},
614
+ view: {},
615
+ list: {}
616
+ };
617
+ var ReactContext = (0, import_react10.createContext)(AppProviderInitialValue);
618
+ var AppProvider = ({ children }) => {
619
+ const config = useConfig({});
620
+ const auth = useAuth();
621
+ const user = useUser(auth.accessToken);
622
+ const company = use_company_default(auth.accessToken);
623
+ const menuContext = (0, import_react10.useMemo)(() => {
624
+ return combineContexts([user.context, company.context]);
625
+ }, [user.context, company.context]);
626
+ const menu = useMenu({ context: menuContext });
627
+ const action = (0, import_react10.useMemo)(() => {
628
+ return menu.state.action;
629
+ }, [menu.state.action]);
630
+ const viewContext = (0, import_react10.useMemo)(() => {
631
+ return combineContexts([
632
+ menuContext,
633
+ { ...(0, import_interface_logic10.evalJSONContext)(action?.result?.context) }
634
+ ]);
635
+ }, [menuContext, action?.result?.context]);
636
+ const view = useViewV2({
637
+ action,
638
+ context: viewContext
639
+ });
640
+ const list = useListData({
641
+ action,
642
+ viewResponse: view.data,
643
+ context: viewContext
644
+ });
645
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
646
+ ReactContext.Provider,
647
+ {
648
+ value: {
649
+ config,
650
+ auth,
651
+ user,
652
+ company,
653
+ menu,
654
+ list,
655
+ action,
656
+ view
657
+ },
658
+ children
659
+ }
660
+ );
661
+ };
662
+ var useAppProvider = () => {
663
+ const context = (0, import_react10.useContext)(ReactContext);
664
+ if (!context) {
665
+ return AppProviderInitialValue;
666
+ }
667
+ return context;
668
+ };
669
+
670
+ // src/hooks/utils/use-click-outside.ts
671
+ var import_react11 = require("react");
672
+ var DEFAULT_EVENTS = ["mousedown", "touchstart"];
673
+ var useClickOutside = ({
674
+ handler,
675
+ events = DEFAULT_EVENTS,
676
+ nodes = [],
677
+ refs
678
+ }) => {
679
+ const ref = (0, import_react11.useRef)(null);
680
+ (0, import_react11.useEffect)(() => {
681
+ const listener = (event) => {
682
+ const { target } = event;
683
+ if (refs && refs?.length > 0 && refs?.some((r) => r.current?.contains(target))) {
684
+ return;
685
+ }
686
+ if (!(target instanceof HTMLElement)) return;
687
+ const shouldIgnore = target.hasAttribute("data-ignore-outside-clicks") || !document.body.contains(target) && target.tagName !== "HTML";
688
+ const shouldTrigger = nodes.length > 0 ? nodes.every((node) => node && !event.composedPath().includes(node)) : ref.current && !ref.current.contains(target);
689
+ if (shouldTrigger && !shouldIgnore) {
690
+ handler(event);
691
+ }
692
+ };
693
+ events.forEach((event) => document.addEventListener(event, listener));
694
+ return () => {
695
+ events.forEach((event) => document.removeEventListener(event, listener));
696
+ };
697
+ }, [handler, nodes, events]);
698
+ return ref;
699
+ };
700
+
701
+ // src/hooks/utils/use-debounce.ts
702
+ var import_react12 = require("react");
703
+ function useDebounce(value, delay) {
704
+ const [debouncedValue, setDebouncedValue] = (0, import_react12.useState)(value);
705
+ (0, import_react12.useEffect)(() => {
706
+ const handler = setTimeout(() => {
707
+ setDebouncedValue(value);
708
+ }, delay);
709
+ return () => {
710
+ clearTimeout(handler);
711
+ };
712
+ }, [value, delay]);
713
+ return [debouncedValue];
714
+ }
715
+
716
+ // src/hooks.ts
717
+ __reExport(hooks_exports, require("@fctc/interface-logic/hooks"), module.exports);
718
+ // Annotate the CommonJS export names for ESM import in node:
719
+ 0 && (module.exports = {
720
+ AppProvider,
721
+ useAppProvider,
722
+ useAuth,
723
+ useCallAction,
724
+ useClickOutside,
725
+ useConfig,
726
+ useDebounce,
727
+ useDetail,
728
+ useListData,
729
+ useMenu,
730
+ useProfile,
731
+ useUser,
732
+ useViewV2,
733
+ ...require("@fctc/interface-logic/hooks")
734
+ });