@navinc/base-react-components
Version:
Nav's Pattern Library
35 lines • 2.13 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var _a, _b;
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { global, isProduction } from '@navinc/utils';
import { styled } from 'styled-components';
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- not sure if it is safe to remove the optional chaining
const { CLIENT_CDN_URL } = (_b = (_a = global === null || global === void 0 ? void 0 : global.process) === null || _a === void 0 ? void 0 : _a.env) !== null && _b !== void 0 ? _b : {};
/** A simple component for displaying an asset from any passed directory on the CDN. */
const UnstyledCDNAsset = (_a) => {
var { directory, filename } = _a, props = __rest(_a, ["directory", "filename"]);
const BASE_URL = CLIENT_CDN_URL;
const [hasError, setHasError] = useState(false);
// using a useEffect to crash the app hard in development if the asset fails to load. This is to prevent missing assets in production.
useEffect(() => {
if (!isProduction() && hasError) {
setHasError(false);
throw new Error(`CDNAsset - failed to load asset: ${BASE_URL}/${directory}/${filename}`);
}
// eslint-disable-next-line react-hooks/exhaustive-deps -- only need to run if hasError changes. The URL doesn't matter
}, [hasError]);
return (_jsx("img", Object.assign({ alt: filename }, props, { onError: () => setHasError(true), src: `${BASE_URL}/${directory}/${filename}` })));
};
export const CDNAsset = styled(UnstyledCDNAsset).withConfig({ displayName: "brc-sc-CDNAsset", componentId: "brc-sc-xsmxs1" }) ``;
//# sourceMappingURL=cdn-asset.js.map