@evergis/react 3.1.129 → 3.1.131

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.
@@ -1,4 +1,5 @@
1
1
  export * from './useAutoCompleteControl';
2
+ export * from './useFetchWithAuth';
2
3
  export * from './useChartChange';
3
4
  export * from './useChartData';
4
5
  export * from './useDashboardHeader';
@@ -0,0 +1 @@
1
+ export declare const useFetchWithAuth: <T>(url: string | null, transform: (res: Response) => Promise<T | null>, cleanup?: (value: T) => void) => T | null;
package/dist/index.js CHANGED
@@ -3923,6 +3923,37 @@ const useAutoCompleteControl = (items) => {
3923
3923
  };
3924
3924
  };
3925
3925
 
3926
+ const useFetchWithAuth = (url, transform, cleanup) => {
3927
+ const [data, setData] = React.useState(null);
3928
+ const loadingRef = React.useRef(false);
3929
+ const transformRef = React.useRef(transform);
3930
+ const cleanupRef = React.useRef(cleanup);
3931
+ transformRef.current = transform;
3932
+ cleanupRef.current = cleanup;
3933
+ const fetchData = React.useCallback(() => {
3934
+ if (!url || loadingRef.current)
3935
+ return;
3936
+ loadingRef.current = true;
3937
+ const token = window.localStorage.getItem(api.STORAGE_TOKEN_KEY);
3938
+ fetch(url, {
3939
+ headers: token ? { Authorization: `Bearer ${token}` } : {},
3940
+ })
3941
+ .then(res => (res.ok ? transformRef.current(res) : null))
3942
+ .then(setData)
3943
+ .finally(() => {
3944
+ loadingRef.current = false;
3945
+ });
3946
+ }, [url]);
3947
+ React.useEffect(() => {
3948
+ fetchData();
3949
+ }, [fetchData]);
3950
+ React.useEffect(() => () => {
3951
+ if (data !== null)
3952
+ cleanupRef.current?.(data);
3953
+ }, [data]);
3954
+ return data;
3955
+ };
3956
+
3926
3957
  const DashboardContext = React.createContext({});
3927
3958
  const DashboardProvider = React.memo(({ children, ...props }) => {
3928
3959
  return jsxRuntime.jsx(DashboardContext.Provider, { value: props, children: children });
@@ -8514,6 +8545,7 @@ const ElementIcon = React.memo(({ type, elementConfig }) => {
8514
8545
  return jsxRuntime.jsx(StyledIcon, { kind: iconValue, fontSize: fontSize, fontColor: fontColor, style: style });
8515
8546
  });
8516
8547
 
8548
+ const toObjectUrl = (res) => res.blob().then(blob => URL.createObjectURL(blob));
8517
8549
  const ElementImage = React.memo(({ type, elementConfig }) => {
8518
8550
  const { attributes } = useWidgetContext(type);
8519
8551
  const { value, attributeName, options, style } = elementConfig || {};
@@ -8531,7 +8563,10 @@ const ElementImage = React.memo(({ type, elementConfig }) => {
8531
8563
  return null;
8532
8564
  return getResourceUrl(imageUrl);
8533
8565
  }, [attributeName, attributes, value]);
8534
- return firstImage ? jsxRuntime.jsx("img", { src: firstImage, alt: firstImage, width: width, style: style }) : null;
8566
+ const blobUrl = useFetchWithAuth(firstImage, toObjectUrl, URL.revokeObjectURL);
8567
+ if (!blobUrl)
8568
+ return null;
8569
+ return (jsxRuntime.jsx("img", { src: blobUrl, alt: firstImage ?? "", width: width, style: style }));
8535
8570
  });
8536
8571
 
8537
8572
  const ElementLegend = React.memo(({ type, element, elementConfig }) => {
@@ -11680,25 +11715,9 @@ const StyledSvg = styled(uilibGl.Flex) `
11680
11715
  `;
11681
11716
 
11682
11717
  const SvgImage = React.memo(({ url, width, height, fontColor }) => {
11683
- const [svg, setSvg] = React.useState(null);
11684
- const [loading, setLoading] = React.useState(false);
11685
- React.useEffect(() => {
11686
- if (!url || loading) {
11687
- return;
11688
- }
11689
- setLoading(true);
11690
- fetch(url)
11691
- .then(res => (res.ok ? res.text() : null))
11692
- .then(setSvg)
11693
- .finally(() => {
11694
- setLoading(false);
11695
- });
11696
- }, [url]);
11697
- React.useEffect(() => () => {
11698
- setSvg(null);
11699
- }, []);
11718
+ const svg = useFetchWithAuth(url, res => res.text());
11700
11719
  const regexp = /<\/svg>\s*$/im;
11701
- if (!regexp.test(svg))
11720
+ if (!svg || !regexp.test(svg))
11702
11721
  return null;
11703
11722
  return (jsxRuntime.jsx(StyledSvg, { "$width": width, "$height": height, "$fontColor": fontColor, dangerouslySetInnerHTML: { __html: svg } }));
11704
11723
  });
@@ -12238,6 +12257,7 @@ exports.useDebouncedCallback = useDebouncedCallback;
12238
12257
  exports.useDiffPage = useDiffPage;
12239
12258
  exports.useExpandableContainers = useExpandableContainers;
12240
12259
  exports.useExportPdf = useExportPdf;
12260
+ exports.useFetchWithAuth = useFetchWithAuth;
12241
12261
  exports.useGetConfigLayer = useGetConfigLayer;
12242
12262
  exports.useGlobalContext = useGlobalContext;
12243
12263
  exports.useHeaderRender = useHeaderRender;