@goralys/core 1.0.0 → 1.1.0

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/dist/index.d.ts CHANGED
@@ -168,8 +168,6 @@ type Section = {
168
168
  };
169
169
  declare const helpContent: Section[];
170
170
 
171
- declare function setCookiesExpire(duration: number): void;
172
-
173
171
  declare function isFoolsDay(): boolean;
174
172
 
175
173
  declare function buildArray<T>(...items: (T | false | null | undefined)[]): T[];
@@ -248,7 +246,7 @@ declare function getLongFromShort(short: string): string;
248
246
 
249
247
  declare function fetchSubjectsForRoleClient(role: UserRole): Promise<Response | null>;
250
248
 
251
- declare function configUserCacheCallbacks(cache: () => Promise<void>, empty: () => void): void;
249
+ declare function configureUserCacheCallbacks(cache: () => Promise<void>, empty: () => void): void;
252
250
  declare function cacheUserDataClient(): Promise<void>;
253
251
  declare function emptyUserCacheClient(): void;
254
252
  declare function fetchUsersClient(): Promise<Response>;
@@ -256,4 +254,4 @@ declare function fetchVirtualUsersClient(): Promise<Response>;
256
254
  declare function fetchAdminsClient(): Promise<Response>;
257
255
  declare function fetchVirtualAdminsClient(): Promise<Response>;
258
256
 
259
- export { ADMIN_CACHE_RADIX, ADMIN_SYNC_RADIX, type AuthEvent, type ConfirmOptions, type CookieValue, type DraftModalResult, EMAIL_KEY, FULL_NAME_KEY, GoralysActionHandler, type GoralysClientConfig, HTTP_METHODS, type HttpMethod, LOGO_PATH, PERSISTANT_COOKIES, PERSISTANT_LOCALS, type PHPDateTime, PUB_ID_KEY, ROLE_KEY, SUBJECT_CACHES, SUBJECT_CACHE_RADIX, SUBJECT_SYNCS, SUBJECT_SYNC_RADIX, SUPPORT_TICKET_CACHE, SUPPORT_TICKET_SYNC, type StorageAdapter, type Subject, type SubjectStatus, SubjectStatusConfig, type SubjectsSearchField, type SupportReasons, type SupportTicket, type Toast, type ToastFn, USERNAME_KEY, USER_CACHES, USER_CACHE_RADIX, USER_ROLES, USER_SYNCS, USER_SYNC_RADIX, type User, type UserData, type UserEvent, type UserRole, type UserType, buildApiUrl, buildArray, cacheUserDataClient, configUserCacheCallbacks, configureCheckAuth, configureGoralysClient, configureStorage, emitAuthEvent, emitNavigationEvent, emitUserEvent, emptyUserCacheClient, fetchAdminsClient, fetchCsrfClient, fetchSubjectsForRoleClient, fetchUsersClient, fetchVirtualAdminsClient, fetchVirtualUsersClient, getGoralysClientConfig, getLongFromShort, getStatusHelper, getStatusLabel, goralysFetchClient, handleToastRequest, helpContent, isAuthenticated, isFoolsDay, isHTTPMethod, onAuthEvent, onNavigationEvent, onUserEvent, parsePhpDateTime, reasonsConfig, searchFields, setCookiesExpire, storageGet, storageRemove, storageSet, useAdmins, useSubjects, useSupportTickets, useUsers, useVirtualAdmins, useVirtualUsers };
257
+ export { ADMIN_CACHE_RADIX, ADMIN_SYNC_RADIX, type AuthEvent, type CheckAuthImpl, type ConfirmOptions, type CookieValue, type DraftModalResult, EMAIL_KEY, FULL_NAME_KEY, GoralysActionHandler, type GoralysClientConfig, HTTP_METHODS, type HttpMethod, LOGO_PATH, PERSISTANT_COOKIES, PERSISTANT_LOCALS, type PHPDateTime, PUB_ID_KEY, ROLE_KEY, SUBJECT_CACHES, SUBJECT_CACHE_RADIX, SUBJECT_SYNCS, SUBJECT_SYNC_RADIX, SUPPORT_TICKET_CACHE, SUPPORT_TICKET_SYNC, type StorageAdapter, type Subject, type SubjectStatus, SubjectStatusConfig, type SubjectsSearchField, type SupportReasons, type SupportTicket, type Toast, type ToastFn, USERNAME_KEY, USER_CACHES, USER_CACHE_RADIX, USER_ROLES, USER_SYNCS, USER_SYNC_RADIX, type User, type UserData, type UserEvent, type UserRole, type UserType, buildApiUrl, buildArray, cacheUserDataClient, configureCheckAuth, configureGoralysClient, configureStorage, configureUserCacheCallbacks, emitAuthEvent, emitNavigationEvent, emitUserEvent, emptyUserCacheClient, fetchAdminsClient, fetchCsrfClient, fetchSubjectsForRoleClient, fetchUsersClient, fetchVirtualAdminsClient, fetchVirtualUsersClient, getGoralysClientConfig, getLongFromShort, getStatusHelper, getStatusLabel, goralysFetchClient, handleToastRequest, helpContent, isAuthenticated, isFoolsDay, isHTTPMethod, onAuthEvent, onNavigationEvent, onUserEvent, parsePhpDateTime, reasonsConfig, searchFields, storageGet, storageRemove, storageSet, useAdmins, useSubjects, useSupportTickets, useUsers, useVirtualAdmins, useVirtualUsers };
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ function onNavigationEvent(listener) {
26
26
  // src/lib/user/user.client.ts
27
27
  var cacheUserClient;
28
28
  var emptyCacheUserClient;
29
- function configUserCacheCallbacks(cache, empty) {
29
+ function configureUserCacheCallbacks(cache, empty) {
30
30
  cacheUserClient = cache;
31
31
  emptyCacheUserClient = empty;
32
32
  }
@@ -480,6 +480,24 @@ async function storageRemove(key) {
480
480
  await getAdapter().removeItem(key);
481
481
  }
482
482
 
483
+ // src/lib/storage/cookies-adapter.ts
484
+ var adapter2 = null;
485
+ function getAdapter2() {
486
+ if (!adapter2) {
487
+ throw new Error("Cookies not configured. Call configureCookies() first.");
488
+ }
489
+ return adapter2;
490
+ }
491
+ function cookiesGet(key) {
492
+ return getAdapter2().getCookies(key);
493
+ }
494
+ function cookiesSet(key, value, path = "/", maxAge = 1.5 * 60) {
495
+ getAdapter2().setCookies(key, value, path, maxAge);
496
+ }
497
+ function cookiesOnChange(callback) {
498
+ return getAdapter2().onChange(callback);
499
+ }
500
+
483
501
  // src/hooks/useSubjects.ts
484
502
  function useSubjects(role, showToast) {
485
503
  const [subjects, setSubjects] = useState(null);
@@ -505,11 +523,11 @@ function useSubjects(role, showToast) {
505
523
  resolve = r;
506
524
  });
507
525
  try {
508
- const syncValue = await storageGet(syncKey);
526
+ const syncValue = cookiesGet(syncKey);
509
527
  if (syncValue == "1") {
510
528
  const raw = await storageGet(cacheKey);
511
529
  if (raw === null || raw === void 0) {
512
- await storageSet(syncKey, "0");
530
+ cookiesSet(syncKey, "0");
513
531
  await storageRemove(cacheKey);
514
532
  await fetchSubjects();
515
533
  return;
@@ -526,7 +544,7 @@ function useSubjects(role, showToast) {
526
544
  if (res)
527
545
  await handleToastRequest(res, showToastRef.current, false);
528
546
  const data = await res?.json();
529
- await storageSet(syncKey, "1");
547
+ cookiesSet(syncKey, "1");
530
548
  await storageSet(cacheKey, JSON.stringify(data));
531
549
  console.log("[useSubjects] set syncKey and cached to localStorage");
532
550
  const result = Array.isArray(data) ? data : null;
@@ -540,6 +558,16 @@ function useSubjects(role, showToast) {
540
558
  resolve();
541
559
  }
542
560
  }, [role]);
561
+ useEffect(() => {
562
+ const onChange = () => {
563
+ if (inFlightRef.current)
564
+ return;
565
+ if (cookiesGet(SUBJECT_SYNCS[role]) != "1") {
566
+ void fetchSubjects();
567
+ }
568
+ };
569
+ return cookiesOnChange(onChange);
570
+ }, [fetchSubjects, role]);
543
571
  useEffect(() => {
544
572
  void fetchSubjects();
545
573
  }, [fetchSubjects]);
@@ -575,11 +603,11 @@ function useSupportTickets(showToast) {
575
603
  resolve = r;
576
604
  });
577
605
  try {
578
- const syncValue = await storageGet(syncKey) ?? "0";
606
+ const syncValue = cookiesGet(syncKey);
579
607
  if (syncValue == "1") {
580
608
  const raw = await storageGet(cacheKey);
581
609
  if (raw === null || raw === void 0) {
582
- await storageSet(syncKey, "0");
610
+ cookiesSet(syncKey, "0");
583
611
  await storageRemove(cacheKey);
584
612
  await fetchSupportTickets();
585
613
  return;
@@ -596,9 +624,8 @@ function useSupportTickets(showToast) {
596
624
  if (res)
597
625
  await handleToastRequest(res, showToastRef.current, false);
598
626
  const data = await res?.json();
599
- await storageSet(syncKey, "1");
627
+ cookiesSet(syncKey, "1");
600
628
  await storageSet(cacheKey, JSON.stringify(data));
601
- console.log("[useSupportTickets] set syncKey and cached to localStorage");
602
629
  const result = Array.isArray(data) ? data : null;
603
630
  setSupportTickets((prev) => {
604
631
  if (JSON.stringify(prev) === JSON.stringify(result))
@@ -610,13 +637,23 @@ function useSupportTickets(showToast) {
610
637
  resolve();
611
638
  }
612
639
  }, []);
640
+ useEffect2(() => {
641
+ const onChange = () => {
642
+ if (inFlightRef.current)
643
+ return;
644
+ if (cookiesGet(SUPPORT_TICKET_SYNC) != "1") {
645
+ void fetchSupportTickets();
646
+ }
647
+ };
648
+ return cookiesOnChange(onChange);
649
+ }, [fetchSupportTickets]);
613
650
  useEffect2(() => {
614
651
  void fetchSupportTickets();
615
652
  }, [fetchSupportTickets]);
616
653
  return useMemo2(() => ({
617
654
  supportTickets,
618
655
  refetch: fetchSupportTickets,
619
- syncKey: `support-tickets-synced`
656
+ syncKey: SUPPORT_TICKET_SYNC
620
657
  }), [supportTickets, fetchSupportTickets]);
621
658
  }
622
659
 
@@ -643,8 +680,16 @@ function useUserCollection(fetchFn, type, showToast) {
643
680
  resolve = r;
644
681
  });
645
682
  try {
646
- if (await storageGet(syncKey) == "1") {
647
- const cached = JSON.parse(await storageGet(cacheKey) ?? "null");
683
+ const syncValue = cookiesGet(syncKey);
684
+ if (syncValue == "1") {
685
+ const raw = await storageGet(cacheKey);
686
+ if (raw === null || raw === void 0) {
687
+ cookiesSet(syncKey, "0");
688
+ await storageRemove(cacheKey);
689
+ await fetchUsers();
690
+ return;
691
+ }
692
+ const cached = JSON.parse(raw ?? "null");
648
693
  setUsers((prev) => {
649
694
  if (JSON.stringify(prev) === JSON.stringify(cached))
650
695
  return prev;
@@ -657,7 +702,7 @@ function useUserCollection(fetchFn, type, showToast) {
657
702
  await handleToastRequest(res, showToastRef.current, false);
658
703
  const data = await res?.json();
659
704
  if (res?.ok) {
660
- await storageSet(syncKey, "1");
705
+ cookiesSet(syncKey, "1");
661
706
  await storageSet(cacheKey, JSON.stringify(data));
662
707
  }
663
708
  const result = Array.isArray(data) ? data : null;
@@ -671,6 +716,16 @@ function useUserCollection(fetchFn, type, showToast) {
671
716
  resolve();
672
717
  }
673
718
  }, [fetchFn, cacheKey, syncKey]);
719
+ useEffect3(() => {
720
+ const onChange = () => {
721
+ if (inFlightRef.current)
722
+ return;
723
+ if (cookiesGet(syncKey) != "1") {
724
+ void fetchUsers();
725
+ }
726
+ };
727
+ return cookiesOnChange(onChange);
728
+ }, [fetchUsers, syncKey]);
674
729
  useEffect3(() => {
675
730
  void fetchUsers();
676
731
  }, [fetchUsers]);
@@ -695,11 +750,6 @@ var LOGO_PATH = {
695
750
  eye: "m599.07,393.76c7.76-.04,14.2,5.96,14.22,13.26.02,7.46-6.23,13.52-14.08,13.54-7.79.02-14.18-6.07-14.2-13.46-.02-7.3,6.32-13.3,14.06-13.34Z"
696
751
  };
697
752
 
698
- // src/lib/cookies.ts
699
- function setCookiesExpire(duration) {
700
- storageSet("goralys-cookies-expire", String(Date.now() + duration * 1e3));
701
- }
702
-
703
753
  // src/lib/fools.ts
704
754
  function isFoolsDay() {
705
755
  const today = /* @__PURE__ */ new Date();
@@ -837,10 +887,10 @@ export {
837
887
  buildApiUrl,
838
888
  buildArray,
839
889
  cacheUserDataClient,
840
- configUserCacheCallbacks,
841
890
  configureCheckAuth,
842
891
  configureGoralysClient,
843
892
  configureStorage,
893
+ configureUserCacheCallbacks,
844
894
  emitAuthEvent,
845
895
  emitNavigationEvent,
846
896
  emitUserEvent,
@@ -866,7 +916,6 @@ export {
866
916
  parsePhpDateTime,
867
917
  reasonsConfig,
868
918
  searchFields,
869
- setCookiesExpire,
870
919
  storageGet,
871
920
  storageRemove,
872
921
  storageSet,
package/package.json CHANGED
@@ -1,43 +1,43 @@
1
1
  {
2
- "name": "@goralys/core",
3
- "version": "1.0.0",
4
- "license": "LGPL-2.1-or-later",
5
- "description": "Shared business logic for the Goralys web application",
6
- "repository": {
7
- "type": "git",
8
- "url": "https://github.com/SAMSAM-55/Goralys",
9
- "directory": "goralys-core"
10
- },
11
- "type": "module",
12
- "main": "./dist/index.js",
13
- "module": "./dist/index.js",
14
- "types": "./dist/index.d.ts",
15
- "exports": {
16
- ".": {
17
- "types": "./dist/index.d.ts",
18
- "import": "./dist/index.js"
2
+ "name": "@goralys/core",
3
+ "version": "1.1.0",
4
+ "license": "LGPL-2.1-or-later",
5
+ "description": "Shared business logic for the Goralys web application",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/SAMSAM-55/Goralys",
9
+ "directory": "goralys-core"
10
+ },
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "LICENSE",
24
+ "README.md"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "tsup",
31
+ "dev": "tsup --watch"
32
+ },
33
+ "peerDependencies": {
34
+ "react": ">=18"
35
+ },
36
+ "devDependencies": {
37
+ "barrelsby": "^2.8.1",
38
+ "esbuild-plugin-tsconfig-paths": "^1.0.2",
39
+ "react": "^18.0.0",
40
+ "tsup": "^8.0.0",
41
+ "typescript": "^5.0.0"
19
42
  }
20
- },
21
- "files": [
22
- "dist",
23
- "LICENSE",
24
- "README.md"
25
- ],
26
- "publishConfig": {
27
- "access": "public"
28
- },
29
- "peerDependencies": {
30
- "react": ">=18"
31
- },
32
- "devDependencies": {
33
- "barrelsby": "^2.8.1",
34
- "esbuild-plugin-tsconfig-paths": "^1.0.2",
35
- "react": "^18.0.0",
36
- "tsup": "^8.0.0",
37
- "typescript": "^5.0.0"
38
- },
39
- "scripts": {
40
- "build": "tsup",
41
- "dev": "tsup --watch"
42
- }
43
- }
43
+ }