techsbcn-storybook 2.0.3 → 2.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.
@@ -2,6 +2,6 @@ export interface Session {
2
2
  nameidentifier: string;
3
3
  role: string[];
4
4
  }
5
- export declare const GetSession: () => Session | null;
6
- export declare const SaveSession: (jwt: string) => void;
7
- export declare const ClearSession: () => void;
5
+ export declare const getSession: () => Session | null;
6
+ export declare const saveSession: (jwt: string) => void;
7
+ export declare const clearSession: () => void;
package/dist/index.d.ts CHANGED
@@ -23,3 +23,4 @@ export * from './components/wrappers/TabsComponent/TabsComponent';
23
23
  export * from './components/wrappers/LinearProgressComponent/LinearProgressComponent';
24
24
  export * from './components/fields/DatePickerComponent/DatePickerComponent';
25
25
  export * from './components/wrappers/CardComponent/CardComponent';
26
+ export * from './helpers/index';
package/dist/index.js CHANGED
@@ -594,6 +594,51 @@ var TextFieldComponent = ({
594
594
  ));
595
595
  };
596
596
 
597
+ // src/helpers/AuthHelper.tsx
598
+ var AUTH_COOKIE_NAME = "auth_token";
599
+ var NAME_IDENTIFIER_CLAIM = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier";
600
+ var ROLE_CLAIM = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
601
+ var getCookie = (name) => {
602
+ if (typeof document === "undefined") return null;
603
+ const cookie = document.cookie.split("; ").find((c) => c.startsWith(`${name}=`));
604
+ return cookie ? decodeURIComponent(cookie.substring(name.length + 1)) : null;
605
+ };
606
+ var decodeJwtPayload = (jwt) => {
607
+ try {
608
+ const payload = jwt.split(".")[1];
609
+ if (!payload) return null;
610
+ const base64 = payload.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(payload.length / 4) * 4, "=");
611
+ const binary = window.atob(base64);
612
+ const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
613
+ return JSON.parse(new TextDecoder().decode(bytes));
614
+ } catch {
615
+ return null;
616
+ }
617
+ };
618
+ var getSession = () => {
619
+ const jwt = getCookie(AUTH_COOKIE_NAME);
620
+ if (!jwt) return null;
621
+ const payload = decodeJwtPayload(jwt);
622
+ if (!payload) return null;
623
+ if (typeof payload.exp === "number" && payload.exp * 1e3 <= Date.now()) return null;
624
+ const roles = payload[ROLE_CLAIM];
625
+ return {
626
+ nameidentifier: payload[NAME_IDENTIFIER_CLAIM],
627
+ role: roles === void 0 || roles === null ? [] : Array.isArray(roles) ? roles : [roles]
628
+ };
629
+ };
630
+ var saveSession = (jwt) => {
631
+ if (typeof document === "undefined") return;
632
+ const payload = decodeJwtPayload(jwt);
633
+ const expires = typeof payload?.exp === "number" ? `; expires=${new Date(payload.exp * 1e3).toUTCString()}` : "";
634
+ const secure = window.location.protocol === "https:" ? "; Secure" : "";
635
+ document.cookie = `${AUTH_COOKIE_NAME}=${encodeURIComponent(jwt)}${expires}; path=/; SameSite=Lax${secure}`;
636
+ };
637
+ var clearSession = () => {
638
+ if (typeof document === "undefined") return;
639
+ document.cookie = `${AUTH_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; SameSite=Lax`;
640
+ };
641
+
597
642
  // src/helpers/CheckPermission.tsx
598
643
  var checkPermission = (persmisionAvailable, userRoles) => {
599
644
  if (persmisionAvailable === null || persmisionAvailable === void 0 || persmisionAvailable.length === 0) {
@@ -614,6 +659,46 @@ var checkPermission = (persmisionAvailable, userRoles) => {
614
659
  }
615
660
  return userRoles.indexOf(persmisionAvailable) >= 0;
616
661
  };
662
+
663
+ // src/helpers/GroupBy.tsx
664
+ var GroupBy = (xs, f) => {
665
+ return xs.reduce((r, v, _i, _a, k = f(v)) => {
666
+ (r[k] || (r[k] = [])).push(v);
667
+ return r;
668
+ }, {});
669
+ };
670
+
671
+ // src/helpers/FileDownloader.tsx
672
+ var downloadFileBlob = (binaryString) => {
673
+ const data = window.URL.createObjectURL(new Blob([binaryString.blob]));
674
+ const fileName = binaryString.contentDisposition.split("filename=")[1];
675
+ redirectDownload(data, fileName);
676
+ };
677
+ var redirectDownload = (url, fileName) => {
678
+ const a = document.createElement("a");
679
+ a.setAttribute("href", url);
680
+ a.setAttribute("download", fileName);
681
+ a.style.visibility = "hidden";
682
+ document.body.appendChild(a);
683
+ a.click();
684
+ document.body.removeChild(a);
685
+ };
686
+ var getImageByBlob = (binaryString) => {
687
+ return URL.createObjectURL(new Blob([binaryString.blob])).toString();
688
+ };
689
+ var getImageByBase64 = (content) => {
690
+ const binaryString = window.atob(content);
691
+ const blob = new Uint8Array(binaryString.length).map((_byte, i) => binaryString.charCodeAt(i));
692
+ return URL.createObjectURL(new Blob([blob])).toString();
693
+ };
694
+ var toBase64 = (file) => {
695
+ return new Promise((resolve, reject) => {
696
+ const reader = new FileReader();
697
+ reader.readAsDataURL(file);
698
+ reader.onload = () => reader.result && resolve(reader.result.toString());
699
+ reader.onerror = (error) => reject(error);
700
+ });
701
+ };
617
702
  var usePrevious = (value) => {
618
703
  const ref2 = React7.useRef(void 0);
619
704
  React7.useEffect(() => {
@@ -2682,6 +2767,7 @@ exports.CardComponent = CardComponent;
2682
2767
  exports.ChangePasswordForm = ChangePasswordForm;
2683
2768
  exports.DatePickerComponent = DatePickerComponent;
2684
2769
  exports.DrawerSidebar = DrawerSidebar;
2770
+ exports.GroupBy = GroupBy;
2685
2771
  exports.LinearProgressComponent = LinearProgressComponent;
2686
2772
  exports.LoginForm = LoginForm;
2687
2773
  exports.MainLayoutSimple = MainLayoutSimple;
@@ -2701,5 +2787,16 @@ exports.TextFieldComponent = TextFieldComponent;
2701
2787
  exports.TextSimpleComponent = TextSimpleComponent;
2702
2788
  exports.UserInfo = UserInfo;
2703
2789
  exports.VerticalNav = VerticalNav;
2790
+ exports.checkPermission = checkPermission;
2791
+ exports.clearSession = clearSession;
2792
+ exports.downloadFileBlob = downloadFileBlob;
2793
+ exports.getImageByBase64 = getImageByBase64;
2794
+ exports.getImageByBlob = getImageByBlob;
2795
+ exports.getSession = getSession;
2796
+ exports.redirectDownload = redirectDownload;
2797
+ exports.saveSession = saveSession;
2798
+ exports.toBase64 = toBase64;
2799
+ exports.useIsMounted = useIsMounted;
2800
+ exports.usePrevious = usePrevious;
2704
2801
  //# sourceMappingURL=index.js.map
2705
2802
  //# sourceMappingURL=index.js.map