@gen3/frontend 0.11.24 → 0.11.26

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/cjs/index.js CHANGED
@@ -46,7 +46,7 @@ var MiniSearch = require('minisearch');
46
46
  var uniq = require('lodash/uniq');
47
47
  var sum = require('lodash/sum');
48
48
  var lu = require('react-icons/lu');
49
- require('universal-cookie');
49
+ var Cookies = require('universal-cookie');
50
50
  var GraphiQL = require('graphiql');
51
51
  var fs = require('fs');
52
52
  var path = require('path');
@@ -1433,7 +1433,7 @@ const CredentialsLogin = ({ handleLogin })=>{
1433
1433
  };
1434
1434
 
1435
1435
  // separate function for hashing logic
1436
- function hashString(str) {
1436
+ function hashString$1(str) {
1437
1437
  let hash = 0;
1438
1438
  for(let i = 0; i < str.length; i++){
1439
1439
  hash = str.charCodeAt(i) + ((hash << 5) - hash);
@@ -1445,7 +1445,7 @@ function hashCode(str) {
1445
1445
  if (str === null || str === '') {
1446
1446
  return 0;
1447
1447
  }
1448
- return hashString(str);
1448
+ return hashString$1(str);
1449
1449
  }
1450
1450
 
1451
1451
  /**
@@ -11181,6 +11181,22 @@ const CountsValue = ({ label, isSuccess, counts })=>{
11181
11181
  });
11182
11182
  };
11183
11183
 
11184
+ const hashString = (s)=>s.split('').reduce((acc, c)=>(acc << 5) - acc + c.charCodeAt(0), 0);
11185
+ const getBody = (iframe)=>{
11186
+ const document1 = iframe?.contentWindow || iframe?.contentDocument;
11187
+ return document1?.document?.body || document1?.body;
11188
+ };
11189
+ const toHtml = (key, value)=>`<input
11190
+ type="hidden"
11191
+ name="${key}"
11192
+ value="${lodash.isPlainObject(value) ? JSON.stringify(value).replace(/"/g, '&quot;') : value}"
11193
+ />`;
11194
+ const customKeys = [
11195
+ 'expand',
11196
+ 'fields',
11197
+ 'facets'
11198
+ ];
11199
+ const processParamObj = (key, value)=>lodash.includes(customKeys, key) ? [].concat(value).join() : value;
11184
11200
  /**
11185
11201
  * A notification box that is used to alert the user that their
11186
11202
  * selected file downloads are in progress
@@ -11206,6 +11222,136 @@ const CountsValue = ({ label, isSuccess, counts })=>{
11206
11222
  ]
11207
11223
  });
11208
11224
  };
11225
+ /**
11226
+ * Trigger a download by attaching an iFrame to the document with the parameters of the request as fields in a form
11227
+ * @param endpoint - endpoint to be attached with
11228
+ * @param params - body to be attached with post request
11229
+ * @param method - Request Method: GET, PUT, POST
11230
+ * @param dispatch - dispatch send from the parent component to dispatch Modals
11231
+ * @param done - callback function to be called after the download has been initiated
11232
+ * @param Modal400 - Modal for 400 error
11233
+ * @param Modal403 - Modal for 403 error
11234
+ * @param customErrorMessage - custom message to be passed for 400 errors
11235
+ * @param hideNotification - hide the notification
11236
+ */ const download = async ({ endpoint, params, method, dispatch, done, Modal400 = core.Modals.GeneralErrorModal, Modal403 = core.Modals.NoAccessModal, customErrorMessage, hideNotification = false })=>{
11237
+ const cookies = new Cookies();
11238
+ /* Create a cookie with a unique identifier to attach to request. Response from server will use the set-cookie header
11239
+ to remove the cookie when the response is received (aka the download starts). This will only work on when the FE and BE
11240
+ are on the same domain.
11241
+ */ const downloadToken = lodash.uniqueId(`${+new Date()}-`);
11242
+ const cookieKey = navigator.cookieEnabled ? Math.abs(hashString(JSON.stringify(params) + downloadToken)).toString(16) : null;
11243
+ if (cookieKey) {
11244
+ cookies.set(cookieKey, downloadToken);
11245
+ }
11246
+ // place notification in timeout to avoid flicker on fast calls
11247
+ const showNotificationTimeout = setTimeout(()=>notifications.showNotification({
11248
+ message: /*#__PURE__*/ jsxRuntime.jsx(DownloadNotification, {
11249
+ onClick: ()=>{
11250
+ iFrame.remove();
11251
+ notifications.cleanNotifications();
11252
+ if (done) {
11253
+ done();
11254
+ }
11255
+ }
11256
+ }),
11257
+ styles: ()=>({
11258
+ root: {
11259
+ textAlign: 'center',
11260
+ display: hideNotification ? 'none' : 'block'
11261
+ },
11262
+ closeButton: {
11263
+ color: 'black',
11264
+ '&:hover': {
11265
+ backgroundColor: 'lightslategray'
11266
+ }
11267
+ }
11268
+ }),
11269
+ closeButtonProps: {
11270
+ 'aria-label': 'Close notification'
11271
+ }
11272
+ }), 100); // set to 100 as that is perceived as instant
11273
+ const fields = lodash.reduce({
11274
+ ...params,
11275
+ downloadCookieKey: cookieKey,
11276
+ downloadCookiePath: '/',
11277
+ attachment: true,
11278
+ ...params.filename !== undefined ? {
11279
+ download: params.filename
11280
+ } : {}
11281
+ }, (result, value, key)=>{
11282
+ const paramValue = processParamObj(key, value);
11283
+ return result + [].concat(paramValue).reduce((acc, v)=>acc + toHtml(key, v), '');
11284
+ }, '');
11285
+ const iFrame = document.createElement('iframe');
11286
+ iFrame.style.display = 'none';
11287
+ iFrame.src = 'about:blank';
11288
+ // Appending to document body to allow navigation away from the current
11289
+ // page and downloads in the background
11290
+ document.body.appendChild(iFrame);
11291
+ const pollForDownloadResult = async ()=>{
11292
+ const executePoll = async (resolve)=>{
11293
+ // Request has been canceled
11294
+ const body = getBody(iFrame);
11295
+ if (body === undefined) {
11296
+ resolve();
11297
+ return;
11298
+ }
11299
+ const content = body?.textContent;
11300
+ // Download has started
11301
+ if (cookieKey !== null && !cookies.get(cookieKey)) {
11302
+ clearTimeout(showNotificationTimeout);
11303
+ notifications.cleanNotifications();
11304
+ if (done) {
11305
+ done();
11306
+ }
11307
+ resolve();
11308
+ } else {
11309
+ const iFrameHasContent = iFrame.contentWindow?.document.body;
11310
+ const requestError = iFrame.contentWindow?.document.getElementsByTagName('form').length === 0 && content !== '';
11311
+ if (iFrameHasContent && requestError) {
11312
+ clearTimeout(showNotificationTimeout);
11313
+ notifications.cleanNotifications();
11314
+ const errorMessage = /{"(?:message|error)":"([^"]*)"/g.exec(content ?? 'Undefined')?.[1];
11315
+ if (errorMessage === 'internal server error' || errorMessage === undefined) {
11316
+ dispatch(core.showModal({
11317
+ modal: Modal400,
11318
+ message: errorMessage
11319
+ }));
11320
+ } else if (errorMessage === 'Your token is invalid or expired. Please get a new token.') {
11321
+ dispatch(core.showModal({
11322
+ modal: Modal403,
11323
+ message: errorMessage
11324
+ }));
11325
+ } else {
11326
+ dispatch(core.showModal({
11327
+ modal: Modal400,
11328
+ message: customErrorMessage || errorMessage
11329
+ }));
11330
+ }
11331
+ if (done) {
11332
+ done();
11333
+ }
11334
+ resolve();
11335
+ } else {
11336
+ setTimeout(executePoll, 1000, resolve);
11337
+ }
11338
+ }
11339
+ };
11340
+ return new Promise(executePoll);
11341
+ };
11342
+ const addFormAndSubmit = ()=>{
11343
+ //do all of this together for FireFox support
11344
+ const form = document.createElement('form');
11345
+ form.method = method.toUpperCase();
11346
+ form.action = endpoint;
11347
+ form.innerHTML = fields;
11348
+ getBody(iFrame).appendChild(form);
11349
+ form.submit();
11350
+ };
11351
+ addFormAndSubmit();
11352
+ await pollForDownloadResult();
11353
+ iFrame.remove();
11354
+ };
11209
11355
 
11210
11356
  const showErrorMessage = (error)=>{
11211
11357
  notifications.showNotification({
@@ -19739,6 +19885,78 @@ border-solid
19739
19885
  rounded-[7px]
19740
19886
  `;
19741
19887
 
19888
+ /**
19889
+ * A Button component that downloads data from a given endpoint.
19890
+ * The component will handle all download logic including fetching
19891
+ * the data, creating the file, and downloading the file.
19892
+ * @param endpoint - The endpoint to download from.
19893
+ * @param disabled - Whether the button is disabled.
19894
+ * @param inactiveText - The text to display when the button is inactive.
19895
+ * @param activeText - The text to display when the button is active.
19896
+ * @param filename - The name of the file to download.
19897
+ * @param size - The size of the download.
19898
+ * @param format - The format of the download.
19899
+ * @param fields - The fields to download.
19900
+ * @param caseFilters - The case filters to download.
19901
+ * @param filters - The filters to download.
19902
+ * @param extraParams - Any extra parameters to download.
19903
+ * @param method - The method to use for the download.
19904
+ * @param customStyle - Any custom styles to apply to the button.
19905
+ * @param showLoading - Whether to show the loading icon.
19906
+ * @param showIcon - Whether to show the download icon.
19907
+ * @param preventClickEvent - Whether to prevent the default click event.
19908
+ * @param onClick - The function to call when the button is clicked.
19909
+ * @param setActive - The function to call when the button is set active.
19910
+ * @param active - Whether the button is active.
19911
+ * @param Modal403 - The modal to display when a 403 error occurs.
19912
+ * @param Modal400 - The modal to display when a 400 error occurs.
19913
+ * @param toolTip - The tooltip to display.
19914
+ * @category Buttons
19915
+ */ const DownloadButton = /*#__PURE__*/ React.forwardRef(({ endpoint = '', disabled = false, inactiveText, activeText, params = {}, method = 'POST', customStyle, setActive, onClick, showLoading = true, showIcon = true, preventClickEvent = false, active, Modal400 = core.Modals.GeneralErrorModal, Modal403 = core.Modals.NoAccessModal, toolTip, ...buttonProps }, ref)=>{
19916
+ const text = active ? activeText : inactiveText;
19917
+ const dispatch = core.useCoreDispatch();
19918
+ const Icon = active ? /*#__PURE__*/ jsxRuntime.jsx(core$1.Loader, {
19919
+ size: "sm",
19920
+ className: "p-1"
19921
+ }) : /*#__PURE__*/ jsxRuntime.jsx(fi.FiDownload, {
19922
+ title: "download",
19923
+ size: 16
19924
+ });
19925
+ return /*#__PURE__*/ jsxRuntime.jsx(core$1.Tooltip, {
19926
+ disabled: !toolTip,
19927
+ label: toolTip,
19928
+ children: /*#__PURE__*/ jsxRuntime.jsx(core$1.Button, {
19929
+ ref: ref,
19930
+ leftSection: showIcon && inactiveText && /*#__PURE__*/ jsxRuntime.jsx(fi.FiDownload, {}),
19931
+ disabled: disabled,
19932
+ className: customStyle || `text-base-lightest ${disabled ? 'bg-base' : 'bg-primary hover:bg-primary-darker'} `,
19933
+ loading: showLoading && active,
19934
+ onClick: ()=>{
19935
+ if (!preventClickEvent && onClick) {
19936
+ onClick();
19937
+ return;
19938
+ }
19939
+ dispatch(core.hideModal());
19940
+ if (setActive) setActive(true);
19941
+ download({
19942
+ endpoint,
19943
+ params,
19944
+ method,
19945
+ dispatch,
19946
+ done: ()=>{
19947
+ if (setActive) setActive(false);
19948
+ },
19949
+ Modal400,
19950
+ Modal403
19951
+ });
19952
+ },
19953
+ ...buttonProps,
19954
+ children: text || Icon
19955
+ })
19956
+ });
19957
+ });
19958
+ DownloadButton.displayName = 'DownloadButton';
19959
+
19742
19960
  // Define theme for mantine v7
19743
19961
  const createMantineTheme = (fonts, colors)=>{
19744
19962
  const theme = core$1.createTheme({