@vizzly/dashboard 0.15.0-dev-67a8f2b3c476fec9b95806eb843a397f087e47f5 → 0.15.0-dev-3d599f1049eba812c1f45d1985540f920617733b

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.
@@ -14,6 +14,7 @@ import ReactDOM, { createPortal } from 'react-dom';
14
14
  import Fuse from 'fuse.js';
15
15
  import { CheckIcon, QuestionMarkCircleIcon, BadgeCheckIcon, DotsVerticalIcon, CloudIcon, SelectorIcon, ChevronUpIcon, ChevronDownIcon, DotsHorizontalIcon, ChevronLeftIcon, ChevronRightIcon, MinusSmIcon, PlusSmIcon } from '@heroicons/react/solid';
16
16
  import { VariableIcon, HashtagIcon, CalendarIcon, SearchIcon } from '@heroicons/react/outline';
17
+ import crypto from 'crypto';
17
18
  import moment$1 from 'moment';
18
19
  import { usePopper } from 'react-popper';
19
20
  import 'react-dates-17/initialize';
@@ -15622,7 +15623,7 @@ var DashboardBehaviourContext = /*#__PURE__*/React__default.createContext( /*#__
15622
15623
  setOverrideDeveloperInfo: noOp
15623
15624
  }));
15624
15625
  var DashboardBehaviourContextProvider = function DashboardBehaviourContextProvider(props) {
15625
- var _props$dashboardBehav, _props$dashboardBehav2, _props$dashboardBehav3, _props$dashboardBehav4, _props$dashboardBehav5;
15626
+ var _props$dashboardBehav, _props$dashboardBehav2, _props$dashboardBehav3, _props$dashboardBehav4, _props$dashboardBehav5, _props$dashboardBehav6;
15626
15627
  var _useFeatureTogglesSet = useFeatureTogglesSetup(props.mode, (_props$dashboardBehav = props.dashboardBehaviour) == null ? void 0 : _props$dashboardBehav.featureToggles, (_props$dashboardBehav2 = props.dashboardBehaviour) == null ? void 0 : _props$dashboardBehav2.queryEngineConfig, (_props$dashboardBehav3 = props.dashboardBehaviour) == null ? void 0 : _props$dashboardBehav3.scope, props.id),
15627
15628
  featureToggles = _useFeatureTogglesSet.featureToggles;
15628
15629
  var textOverride = props.textOverride || defaultText;
@@ -15654,7 +15655,8 @@ var DashboardBehaviourContextProvider = function DashboardBehaviourContextProvid
15654
15655
  id: props.id,
15655
15656
  featureToggles: featureToggles,
15656
15657
  valueAlias: props.valueAlias,
15657
- variables: props.variables
15658
+ variables: props.variables,
15659
+ updateDashboardHash: (_props$dashboardBehav6 = props.dashboardBehaviour) == null ? void 0 : _props$dashboardBehav6.updateDashboardHash
15658
15660
  }),
15659
15661
  children: props.children
15660
15662
  });
@@ -23664,7 +23666,9 @@ var DashboardContextProvider = function DashboardContextProvider(props) {
23664
23666
  var dashboard = props.dashboard,
23665
23667
  children = props.children,
23666
23668
  trackEvent = props.trackEvent,
23667
- componentEditor = props.componentEditor;
23669
+ componentEditor = props.componentEditor,
23670
+ onDashboardUpdate = props.onDashboardUpdate,
23671
+ dashboardHash = props.dashboardHash;
23668
23672
  var _useDashboardReducer = useDashboardReducer({
23669
23673
  componentEditor: componentEditor ? componentEditor : null,
23670
23674
  dashboard: dashboard ? mergeAPIIntoStoredTheme(dashboard, props.theme) : dashboard
@@ -23672,8 +23676,21 @@ var DashboardContextProvider = function DashboardContextProvider(props) {
23672
23676
  state = _useDashboardReducer[0],
23673
23677
  dispatch = _useDashboardReducer[1];
23674
23678
  useEffect(function () {
23675
- (props == null ? void 0 : props.onDashboardUpdate) && (props == null ? void 0 : props.onDashboardUpdate(state.dashboard));
23676
- }, [JSON.stringify(state.dashboard)]);
23679
+ if (!onDashboardUpdate) return;
23680
+ if (!dashboardHash) {
23681
+ onDashboardUpdate(state.dashboard, {
23682
+ hasUnsavedChanges: false
23683
+ });
23684
+ return;
23685
+ }
23686
+ var definition = toSaveableDefinition(state.dashboard);
23687
+ var serializedDefinition = JSON.stringify(definition);
23688
+ var newHash = crypto.createHash('md5').update(serializedDefinition).digest('hex');
23689
+ var hasUnsavedChanges = dashboardHash !== newHash;
23690
+ onDashboardUpdate(state.dashboard, {
23691
+ hasUnsavedChanges: hasUnsavedChanges
23692
+ });
23693
+ }, [JSON.stringify(state.dashboard), dashboardHash, onDashboardUpdate]);
23677
23694
  var trackEventFallback = function trackEventFallback() {};
23678
23695
  return jsx(DashboardContext.Provider, {
23679
23696
  value: _extends({}, state, {
@@ -41386,7 +41403,8 @@ var AdminTools = function AdminTools(props) {
41386
41403
  onSave = _useDashboardBehaviou.onSave,
41387
41404
  queryEngineConfig = _useDashboardBehaviou.queryEngineConfig,
41388
41405
  onOpenConfigManagerUi = _useDashboardBehaviou.onOpenConfigManagerUi,
41389
- disableToolbar = _useDashboardBehaviou.disableToolbar;
41406
+ disableToolbar = _useDashboardBehaviou.disableToolbar,
41407
+ updateDashboardHash = _useDashboardBehaviou.updateDashboardHash;
41390
41408
  var saveableDashboard = toSaveableDefinition(dashboard);
41391
41409
  var supportsConfigManagerUi = supportsFeature(queryEngineConfig, 'configManagerUi') && onOpenConfigManagerUi;
41392
41410
  useEffect(function () {
@@ -41427,6 +41445,7 @@ var AdminTools = function AdminTools(props) {
41427
41445
  if (data.save) {
41428
41446
  logDebug('Received from VizzlyState, `save()`');
41429
41447
  doSave();
41448
+ updateDashboardHash && updateDashboardHash(dashboard);
41430
41449
  }
41431
41450
  }, undefined, [dashboard]);
41432
41451
  if (disableToolbar) return null;
@@ -69738,7 +69757,8 @@ var StudioContexts = function StudioContexts(props) {
69738
69757
  trackEvent: props.trackEvent,
69739
69758
  dashboard: props.dashboard,
69740
69759
  componentEditor: props.componentEditor,
69741
- onDashboardUpdate: props.onDashboardUpdate,
69760
+ onDashboardUpdate: props.dashboardBehaviour.onDashboardUpdate,
69761
+ dashboardHash: props.dashboardHash,
69742
69762
  saving: props.saving,
69743
69763
  runQueriesCallback: function runQueriesCallback(queries, params) {
69744
69764
  var _props$identityConfig;
@@ -73553,848 +73573,74 @@ var EditorView$1 = function EditorView(props) {
73553
73573
  });
73554
73574
  };
73555
73575
 
73556
- var GlobalContext = /*#__PURE__*/createContext(undefined);
73557
-
73558
- var getDefaultParentDashboardId = function getDefaultParentDashboardId(parentDashboardId, parentDashboardIdOnDashboardAccessToken, dashboards) {
73559
- if (parentDashboardId) {
73560
- var parentDashboard = find$1(dashboards, parentDashboardId);
73561
- if (!(parentDashboard != null && parentDashboard.parentDashboardId)) {
73562
- return parentDashboardId;
73563
- } else {
73564
- throw '[Vizzly] `parentDashboardId` value must specify an ID for a parent dashboard.';
73565
- }
73566
- }
73567
- var parentDashboards = getParentDashboards(dashboards);
73568
- var programmaticDashboardId = checkAndReturnProgrammaticDashboardId(parentDashboards);
73569
- if (programmaticDashboardId) return programmaticDashboardId;
73570
- return parentDashboardIdOnDashboardAccessToken || getFirstCreated(parentDashboards).id;
73571
- };
73572
-
73573
- var getStrategy = function getStrategy(vizzly, requestedEndUserParentDashboardId, requestedEndUserDashboardId, isEditor) {
73574
- if (!('dashboards' in vizzly)) return undefined;
73575
- if (vizzly.loading || vizzly.error) return undefined;
73576
- logDebug('Loading dashboard strategy, with props;', {
73577
- dashboardId: requestedEndUserDashboardId,
73578
- parentDashboardId: requestedEndUserParentDashboardId
73579
- });
73580
- // This is a temporary fix for the issue where the dashboard is not loaded due to multiple hooks.
73581
- var checkToStopStrategyCheckingWhenDashboardNotLoadedDueToMultipleHooks = isEditor && vizzly.dashboards.length === 0;
73582
- if (checkToStopStrategyCheckingWhenDashboardNotLoadedDueToMultipleHooks) return undefined;
73583
- var defaultParentDashboardId = getDefaultParentDashboardId(requestedEndUserParentDashboardId, vizzly.identityConfig.dashboardAccess.dashboardId, vizzly.dashboards);
73584
- if (vizzly.accessType == 'admin') {
73585
- logDebug('Admin access found, so we will show the parent dashboard.', defaultParentDashboardId);
73586
- var dashboardId = defaultParentDashboardId;
73587
- // If the user wants a specific dashboard, then fetch the parent dashboard of it.
73588
- if (requestedEndUserDashboardId) {
73589
- var foundRequestedChildDashboard = vizzly.dashboards.find(function (dashboard) {
73590
- return dashboard.id == requestedEndUserDashboardId;
73591
- });
73592
- if (foundRequestedChildDashboard != null && foundRequestedChildDashboard.parentDashboardId) {
73593
- logDebug('A child dashboard was provided whilst in admin mode. Therefore the parent of the given child will be shown to administer.', foundRequestedChildDashboard.parentDashboardId);
73594
- dashboardId = foundRequestedChildDashboard.parentDashboardId;
73595
- }
73596
- }
73597
- logDebug('Admin access found, so we will show the parent dashboard.', dashboardId);
73598
- return {
73599
- type: 'exact',
73600
- dashboardId: dashboardId
73601
- };
73602
- }
73603
- if (requestedEndUserDashboardId) {
73604
- logDebug('Strategy will be to show dashboard', requestedEndUserDashboardId);
73605
- return {
73606
- type: 'exact',
73607
- dashboardId: requestedEndUserDashboardId
73608
- };
73609
- }
73610
- var childDashboards = vizzly.dashboards.filter(function (d) {
73611
- var isChildDashboard = d.parentDashboardId != null;
73612
- var hasNotRequestedEndUserParentDashboardId = requestedEndUserParentDashboardId === undefined;
73613
- var isChildOfRequestedParent = hasNotRequestedEndUserParentDashboardId || d.parentDashboardId === requestedEndUserParentDashboardId;
73614
- return isChildDashboard && isChildOfRequestedParent;
73615
- });
73616
- // Exactly one child dashboard for the user & in read-only mode so someone else is managing the dashboard with the same userReference.
73617
- // We will show that one dashboard to allow for a common SaaS environment where
73618
- // multiple users share the same userReference, but some of those users have `read_write` access and other users have read access.
73619
- // Those with `read` access should see the dashboard managed by those other users...
73620
- if (childDashboards.length === 1 && vizzly.identityConfig.dashboardAccess.scope == 'read') {
73621
- logDebug('Strategy will be to show read-only view of the only child dashboard. If you wish to render a read-only view of the parent dashboard, please make sure the `dashboardId` property matches the `parentDashboardId` property.', defaultParentDashboardId);
73622
- return {
73623
- type: 'exact',
73624
- dashboardId: vizzly.dashboards.find(function (d) {
73625
- return d.parentDashboardId != null;
73626
- }).id
73627
- };
73628
- }
73629
- // No specific dashboard requested, but dashboard access token only grants 'read' access.
73630
- if (vizzly.identityConfig.dashboardAccess.scope == 'read' && defaultParentDashboardId) {
73631
- logDebug('Strategy will be to return read-only view of the parent dashboard.', defaultParentDashboardId);
73632
- return {
73633
- type: 'exact',
73634
- dashboardId: defaultParentDashboardId
73635
- };
73636
- }
73637
- var dashboardIdSetInIdentityConfig = vizzly.identityConfig.dashboardAccess.dashboardId;
73638
- if (dashboardIdSetInIdentityConfig) {
73639
- var _vizzly$identityConfi, _vizzly$identityConfi2;
73640
- logDebug('Strategy will be to show a child of the parent dashboard determined by the identity config', (_vizzly$identityConfi = vizzly.identityConfig.dashboardAccess) == null ? void 0 : _vizzly$identityConfi.dashboardId);
73641
- return {
73642
- type: 'instantCopyOf',
73643
- parentDashboardId: (_vizzly$identityConfi2 = vizzly.identityConfig.dashboardAccess) == null ? void 0 : _vizzly$identityConfi2.dashboardId
73644
- };
73645
- } else {
73646
- logDebug('Strategy will be to show a child of the parent dashboard', defaultParentDashboardId);
73647
- return {
73648
- type: 'instantCopyOf',
73649
- parentDashboardId: defaultParentDashboardId
73650
- };
73651
- }
73652
- };
73653
-
73654
- var Mode;
73655
- (function (Mode) {
73656
- Mode["Admin"] = "admin";
73657
- Mode["EndUser"] = "endUser";
73658
- })(Mode || (Mode = {}));
73659
- var Scope;
73660
- (function (Scope) {
73661
- Scope["Read"] = "read";
73662
- Scope["ReadWrite"] = "read_write";
73663
- })(Scope || (Scope = {}));
73664
-
73665
- /*
73666
- Currently, we just replicate the same behaviour as what we used to have on the API.
73667
- */
73668
- var runStrategy = /*#__PURE__*/function () {
73669
- var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(strategy, vizzly) {
73670
- var currentDashboard, scope, _parentDashboard$perm, parentDashboard, hasParentDashboardWritePermission, dashboardCopies, mostRecentlyUpdatedChildDashboard, created;
73576
+ var getParentDashboards$1 = function getParentDashboards(httpClient) {
73577
+ return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
73578
+ var _yield$httpClient, status, body;
73671
73579
  return _regeneratorRuntime().wrap(function _callee$(_context) {
73672
73580
  while (1) switch (_context.prev = _context.next) {
73673
73581
  case 0:
73674
- if (!(strategy.type == 'exact')) {
73675
- _context.next = 9;
73676
- break;
73677
- }
73678
- currentDashboard = vizzly.dashboards.find(function (d) {
73679
- return d.id === strategy.dashboardId;
73680
- });
73681
- if (currentDashboard.permission) {
73682
- _context.next = 4;
73683
- break;
73684
- }
73685
- throw '';
73686
- case 4:
73687
- scope = currentDashboard.permission.scope;
73688
- logDebug('A specific dashboard has been requested. This will be rendered with', scope, 'scope.', strategy.dashboardId);
73689
- return _context.abrupt("return", {
73690
- dashboardId: strategy.dashboardId,
73691
- mode: scope === 'read_write' && currentDashboard.parentDashboardId === null ? Mode.Admin : Mode.EndUser,
73692
- scope: scope
73693
- });
73694
- case 9:
73695
- if (!(strategy.type == 'instantCopyOf')) {
73696
- _context.next = 29;
73697
- break;
73698
- }
73699
- // If the user has write access to the parent dashboard, then we return that dashboard ID, with mode=admin
73700
- parentDashboard = vizzly.dashboards.find(function (d) {
73701
- return d.id === strategy.parentDashboardId;
73702
- });
73703
- hasParentDashboardWritePermission = ((_parentDashboard$perm = parentDashboard.permission) == null ? void 0 : _parentDashboard$perm.scope) === 'read_write';
73704
- if (!hasParentDashboardWritePermission) {
73705
- _context.next = 15;
73706
- break;
73707
- }
73708
- logDebug('Write access to the parent dashboard has been found, so this dashboard will be shown with admin access.', strategy.parentDashboardId);
73709
- return _context.abrupt("return", {
73710
- dashboardId: strategy.parentDashboardId,
73711
- mode: Mode.Admin,
73712
- scope: Scope.ReadWrite
73713
- });
73714
- case 15:
73715
- dashboardCopies = getCopiesOfParentDashboard(strategy.parentDashboardId, vizzly.dashboards);
73716
- mostRecentlyUpdatedChildDashboard = takeMostRecentlyUpdated(dashboardCopies);
73717
- if (!mostRecentlyUpdatedChildDashboard) {
73718
- _context.next = 20;
73719
- break;
73720
- }
73721
- logDebug('An existing child dashboard exists of the parent dashboard. As no specific dashboardId was provided, we will render the most recently updated child dashboard', mostRecentlyUpdatedChildDashboard.id);
73722
- return _context.abrupt("return", {
73723
- dashboardId: mostRecentlyUpdatedChildDashboard.id,
73724
- mode: Mode.EndUser,
73725
- scope: mostRecentlyUpdatedChildDashboard.permission.scope
73726
- });
73727
- case 20:
73728
- logDebug('No previous child dashboard has been created on behalf of the current user for the parent dashboard', parentDashboard.id, '. Creating a child copy of the parent.');
73729
- _context.next = 23;
73730
- return vizzly.createDashboard({
73731
- parentDashboardId: parentDashboard.id,
73732
- definition: toSaveableDefinition(parentDashboard),
73733
- metadata: {}
73582
+ _context.next = 2;
73583
+ return httpClient({
73584
+ path: "/api/v2/parent-dashboards",
73585
+ method: 'get'
73734
73586
  });
73735
- case 23:
73736
- created = _context.sent;
73737
- if (created) {
73738
- _context.next = 26;
73587
+ case 2:
73588
+ _yield$httpClient = _context.sent;
73589
+ status = _yield$httpClient.status;
73590
+ body = _yield$httpClient.body;
73591
+ if (!(status == 200)) {
73592
+ _context.next = 7;
73739
73593
  break;
73740
73594
  }
73741
- throw 'Failed to create first child dashboard for the user.';
73742
- case 26:
73743
- return _context.abrupt("return", {
73744
- dashboardId: created.id,
73745
- scope: Scope.ReadWrite,
73746
- mode: Mode.EndUser
73747
- });
73748
- case 29:
73749
- throw "Unsupported strategy";
73750
- case 30:
73595
+ return _context.abrupt("return", body.dashboards.map(function (rawDashboard) {
73596
+ return {
73597
+ id: rawDashboard.id,
73598
+ created_at: rawDashboard.created_at,
73599
+ updated_at: rawDashboard.updated_at,
73600
+ webhook_id: rawDashboard.webhook_id
73601
+ };
73602
+ }));
73603
+ case 7:
73604
+ return _context.abrupt("return", undefined);
73605
+ case 8:
73751
73606
  case "end":
73752
73607
  return _context.stop();
73753
73608
  }
73754
73609
  }, _callee);
73755
73610
  }));
73756
- return function runStrategy(_x, _x2) {
73757
- return _ref.apply(this, arguments);
73758
- };
73759
- }();
73760
-
73761
- /*
73762
- Stores which dashboard is the currently active one...
73763
- */
73764
- var useDashboardStrategy = function useDashboardStrategy(strategy, vizzly, programmaticDashboard) {
73765
- var _useState = useState(),
73766
- state = _useState[0],
73767
- setState = _useState[1];
73768
- useEffect(function () {
73769
- var doWork = /*#__PURE__*/function () {
73770
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
73771
- var _yield$runStrategy, mode, dashboardId, scope;
73772
- return _regeneratorRuntime().wrap(function _callee$(_context) {
73773
- while (1) switch (_context.prev = _context.next) {
73774
- case 0:
73775
- if ('dashboards' in vizzly) {
73776
- _context.next = 2;
73777
- break;
73778
- }
73779
- return _context.abrupt("return");
73780
- case 2:
73781
- if (strategy) {
73782
- _context.next = 4;
73783
- break;
73784
- }
73785
- return _context.abrupt("return");
73786
- case 4:
73787
- if (!vizzly.loading) {
73788
- _context.next = 6;
73789
- break;
73790
- }
73791
- return _context.abrupt("return");
73792
- case 6:
73793
- _context.next = 8;
73794
- return runStrategy(strategy, vizzly);
73795
- case 8:
73796
- _yield$runStrategy = _context.sent;
73797
- mode = _yield$runStrategy.mode;
73798
- dashboardId = _yield$runStrategy.dashboardId;
73799
- scope = _yield$runStrategy.scope;
73800
- if (dashboardId != (state == null ? void 0 : state.dashboardId)) setState({
73801
- mode: mode,
73802
- dashboardId: dashboardId,
73803
- scope: scope
73804
- });
73805
- case 13:
73806
- case "end":
73807
- return _context.stop();
73808
- }
73809
- }, _callee);
73810
- }));
73811
- return function doWork() {
73812
- return _ref.apply(this, arguments);
73813
- };
73814
- }();
73815
- doWork();
73816
- }, [JSON.stringify({
73817
- strategy: strategy,
73818
- loading: vizzly.loading
73819
- })]);
73820
- if (!('dashboards' in vizzly)) return null;
73821
- if (!state) return null;
73822
- if (vizzly.loading) return null;
73823
- var dashboard = vizzly.dashboards.find(function (dash) {
73824
- return dash.id == state.dashboardId;
73825
- }) || null;
73826
- return {
73827
- mode: state.mode,
73828
- dashboard: dashboard,
73829
- scope: state.scope,
73830
- isProgrammaticParent: !!programmaticDashboard
73831
- };
73832
73611
  };
73833
73612
 
73834
- var NoDataSetResolved = /*#__PURE__*/function (_Error) {
73835
- function NoDataSetResolved() {
73836
- var _this;
73837
- _this = _Error.call(this, 'At least one data set must be built for the Vizzly dashboard to render.') || this;
73838
- _this.name = 'NoDataSetResolved';
73839
- return _this;
73840
- }
73841
- _inheritsLoose(NoDataSetResolved, _Error);
73842
- return NoDataSetResolved;
73843
- }( /*#__PURE__*/_wrapNativeSuper(Error));
73844
-
73845
- var useVizzly = function useVizzly(properties, options) {
73846
- var _useState = useState({
73847
- loading: true,
73848
- error: undefined,
73849
- dashboards: [],
73850
- globalLibraries: []
73851
- }),
73852
- state = _useState[0],
73853
- setState = _useState[1];
73854
- var vizzlyRef = useRef();
73855
- var vizzly = vizzlyRef.current;
73856
- useEffect(function () {
73857
- var abortController = new AbortController();
73858
- var doWork = /*#__PURE__*/function () {
73859
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
73860
- var _yield$Promise$all, globalLibraries, dashboards;
73861
- return _regeneratorRuntime().wrap(function _callee$(_context) {
73862
- while (1) switch (_context.prev = _context.next) {
73863
- case 0:
73864
- _context.prev = 0;
73865
- _context.next = 3;
73866
- return Vizzly$1.load(properties, {
73867
- apiHost: (options == null ? void 0 : options.apiHost) || 'https://api.vizzly.co'
73868
- });
73869
- case 3:
73870
- vizzlyRef.current = _context.sent;
73871
- _context.next = 6;
73872
- return Promise.all([vizzlyRef.current.getGlobalLibraries({
73873
- abortController: abortController
73874
- }), vizzlyRef.current.getDashboards({
73875
- abortController: abortController
73876
- })]);
73877
- case 6:
73878
- _yield$Promise$all = _context.sent;
73879
- globalLibraries = _yield$Promise$all[0];
73880
- dashboards = _yield$Promise$all[1];
73881
- setState({
73882
- loading: false,
73883
- error: undefined,
73884
- dashboards: dashboards,
73885
- globalLibraries: globalLibraries
73886
- });
73887
- _context.next = 16;
73888
- break;
73889
- case 12:
73890
- _context.prev = 12;
73891
- _context.t0 = _context["catch"](0);
73892
- logError('Error loading Vizzly', _context.t0);
73893
- setState({
73894
- loading: false,
73895
- error: _context.t0,
73896
- dashboards: [].concat(state.dashboards),
73897
- globalLibraries: [].concat(state.globalLibraries)
73898
- });
73899
- case 16:
73900
- case "end":
73901
- return _context.stop();
73902
- }
73903
- }, _callee, null, [[0, 12]]);
73904
- }));
73905
- return function doWork() {
73906
- return _ref.apply(this, arguments);
73907
- };
73908
- }();
73909
- doWork();
73910
- return function () {
73911
- return abortController.abort(new LoadingAborted('We stopped loading Vizzly because of an abort signal triggered by an unmount'));
73912
- };
73913
- }, []);
73914
- var refetch = /*#__PURE__*/function () {
73915
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
73916
- var abortController, doWork;
73917
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
73918
- while (1) switch (_context3.prev = _context3.next) {
73919
- case 0:
73920
- abortController = new AbortController();
73921
- doWork = /*#__PURE__*/function () {
73922
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
73923
- var _yield$Promise$all2, globalLibraries, dashboards;
73924
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
73925
- while (1) switch (_context2.prev = _context2.next) {
73926
- case 0:
73927
- _context2.prev = 0;
73928
- _context2.next = 3;
73929
- return Vizzly$1.load(properties, {
73930
- apiHost: (options == null ? void 0 : options.apiHost) || 'https://api.vizzly.co'
73931
- });
73932
- case 3:
73933
- vizzlyRef.current = _context2.sent;
73934
- _context2.next = 6;
73935
- return Promise.all([vizzlyRef.current.getGlobalLibraries({
73936
- abortController: abortController
73937
- }), vizzlyRef.current.getDashboards({
73938
- abortController: abortController
73939
- })]);
73940
- case 6:
73941
- _yield$Promise$all2 = _context2.sent;
73942
- globalLibraries = _yield$Promise$all2[0];
73943
- dashboards = _yield$Promise$all2[1];
73944
- setState({
73945
- loading: false,
73946
- error: undefined,
73947
- dashboards: dashboards,
73948
- globalLibraries: globalLibraries
73949
- });
73950
- _context2.next = 16;
73951
- break;
73952
- case 12:
73953
- _context2.prev = 12;
73954
- _context2.t0 = _context2["catch"](0);
73955
- logError('Error loading Vizzly', _context2.t0);
73956
- setState({
73957
- loading: false,
73958
- error: _context2.t0,
73959
- dashboards: [].concat(state.dashboards),
73960
- globalLibraries: [].concat(state.globalLibraries)
73961
- });
73962
- case 16:
73963
- case "end":
73964
- return _context2.stop();
73965
- }
73966
- }, _callee2, null, [[0, 12]]);
73967
- }));
73968
- return function doWork() {
73969
- return _ref3.apply(this, arguments);
73970
- };
73971
- }();
73972
- doWork();
73973
- return _context3.abrupt("return", function () {
73974
- return abortController.abort(new LoadingAborted('We stopped loading Vizzly because of an abort signal triggered by an unmount'));
73975
- });
73976
- case 4:
73977
- case "end":
73978
- return _context3.stop();
73979
- }
73980
- }, _callee3);
73981
- }));
73982
- return function refetch() {
73983
- return _ref2.apply(this, arguments);
73984
- };
73985
- }();
73986
- var createDashboard = /*#__PURE__*/function () {
73987
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
73988
- var createdDashboard;
73989
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
73990
- while (1) switch (_context4.prev = _context4.next) {
73613
+ // If cronSchedule is `null`, then it means the scheduled
73614
+ // report is deleted.
73615
+ var saveScheduledReport = function saveScheduledReport(httpClient) {
73616
+ return /*#__PURE__*/function () {
73617
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
73618
+ var _yield$httpClient, status;
73619
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
73620
+ while (1) switch (_context.prev = _context.next) {
73991
73621
  case 0:
73992
- if (!vizzly) {
73993
- _context4.next = 15;
73994
- break;
73995
- }
73996
- _context4.prev = 1;
73997
- _context4.next = 4;
73998
- return vizzly.createDashboard({
73999
- definition: params.definition,
74000
- // permissions: params.permissions,
74001
- parentDashboardId: params.parentDashboardId,
74002
- metadata: params.metadata || params.meta,
74003
- abortSignal: params.abortSignal
73622
+ _context.next = 2;
73623
+ return httpClient({
73624
+ path: '/api/scheduled-report',
73625
+ method: 'post',
73626
+ body: {
73627
+ cronSchedule: params.cronSchedule,
73628
+ dashboardAccessToken: params.dashboardAccessToken,
73629
+ scheduledReportId: params.scheduledReportId
73630
+ }
74004
73631
  });
74005
- case 4:
74006
- createdDashboard = _context4.sent;
74007
- setState(_extends({}, state, {
74008
- dashboards: [].concat(state.dashboards, [createdDashboard])
74009
- }));
74010
- return _context4.abrupt("return", createdDashboard);
74011
- case 9:
74012
- _context4.prev = 9;
74013
- _context4.t0 = _context4["catch"](1);
74014
- logError(_context4.t0);
74015
- return _context4.abrupt("return", null);
74016
- case 13:
74017
- _context4.next = 16;
74018
- break;
74019
- case 15:
74020
- throw 'Unable to create a dashboard when vizzly has not been loaded.';
74021
- case 16:
73632
+ case 2:
73633
+ _yield$httpClient = _context.sent;
73634
+ status = _yield$httpClient.status;
73635
+ return _context.abrupt("return", status == 200);
73636
+ case 5:
74022
73637
  case "end":
74023
- return _context4.stop();
73638
+ return _context.stop();
74024
73639
  }
74025
- }, _callee4, null, [[1, 9]]);
73640
+ }, _callee);
74026
73641
  }));
74027
- return function createDashboard(_x) {
74028
- return _ref4.apply(this, arguments);
74029
- };
74030
- }();
74031
- useEffect(function () {
74032
- var abortController = new AbortController();
74033
- var doWork = /*#__PURE__*/function () {
74034
- var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
74035
- var _yield$Promise$all3, globalLibraries;
74036
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
74037
- while (1) switch (_context5.prev = _context5.next) {
74038
- case 0:
74039
- _context5.prev = 0;
74040
- _context5.next = 3;
74041
- return Vizzly$1.load(properties, {
74042
- apiHost: (options == null ? void 0 : options.apiHost) || 'https://api.vizzly.co'
74043
- });
74044
- case 3:
74045
- vizzlyRef.current = _context5.sent;
74046
- _context5.next = 6;
74047
- return Promise.all([vizzlyRef.current.getGlobalLibraries({
74048
- abortController: abortController
74049
- })]);
74050
- case 6:
74051
- _yield$Promise$all3 = _context5.sent;
74052
- globalLibraries = _yield$Promise$all3[0];
74053
- setState(_extends({}, state, {
74054
- globalLibraries: globalLibraries
74055
- }));
74056
- _context5.next = 15;
74057
- break;
74058
- case 11:
74059
- _context5.prev = 11;
74060
- _context5.t0 = _context5["catch"](0);
74061
- logError('Error loading Vizzly', _context5.t0);
74062
- setState({
74063
- loading: false,
74064
- error: _context5.t0,
74065
- dashboards: [].concat(state.dashboards),
74066
- globalLibraries: [].concat(state.globalLibraries)
74067
- });
74068
- case 15:
74069
- case "end":
74070
- return _context5.stop();
74071
- }
74072
- }, _callee5, null, [[0, 11]]);
74073
- }));
74074
- return function doWork() {
74075
- return _ref5.apply(this, arguments);
74076
- };
74077
- }();
74078
- if (vizzly) {
74079
- var unsubscribe = Vizzly$1.subscription(function (eventBus) {
74080
- if (eventBus.data.action === 'newViewsInGlobalLibrary') {
74081
- doWork();
74082
- }
74083
- });
74084
- return function () {
74085
- return unsubscribe();
74086
- };
74087
- }
74088
- return function () {
74089
- return abortController.abort(new LoadingAborted('We stopped loading Vizzly because of an abort signal triggered by an unmount'));
74090
- };
74091
- }, [vizzly]);
74092
- var updateDashboard = /*#__PURE__*/function () {
74093
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(params) {
74094
- var updatedDashboard, dashboards;
74095
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
74096
- while (1) switch (_context6.prev = _context6.next) {
74097
- case 0:
74098
- if (!vizzly) {
74099
- _context6.next = 20;
74100
- break;
74101
- }
74102
- setState(_extends({}, state, {
74103
- saving: SavingState.InProgress
74104
- }));
74105
- _context6.prev = 2;
74106
- _context6.next = 5;
74107
- return vizzly.updateDashboard({
74108
- dashboardId: params.dashboardId,
74109
- definition: params.definition,
74110
- deleted: params.deleted,
74111
- metadata: params.metadata || params.meta,
74112
- abortSignal: params.abortSignal,
74113
- name: params.name
74114
- });
74115
- case 5:
74116
- updatedDashboard = _context6.sent;
74117
- _context6.next = 8;
74118
- return vizzly.getDashboards();
74119
- case 8:
74120
- dashboards = _context6.sent;
74121
- setState(function (prevState) {
74122
- return _extends({}, prevState, {
74123
- dashboards: dashboards,
74124
- saving: SavingState.Saved
74125
- });
74126
- });
74127
- setTimeout(function () {
74128
- setState(_extends({}, state, {
74129
- saving: SavingState.Undefined
74130
- }));
74131
- }, 500);
74132
- return _context6.abrupt("return", updatedDashboard);
74133
- case 14:
74134
- _context6.prev = 14;
74135
- _context6.t0 = _context6["catch"](2);
74136
- logError('Error updating dashboard', _context6.t0);
74137
- return _context6.abrupt("return", null);
74138
- case 18:
74139
- _context6.next = 21;
74140
- break;
74141
- case 20:
74142
- throw 'Unable to update a dashboard when vizzly has not been loaded.';
74143
- case 21:
74144
- case "end":
74145
- return _context6.stop();
74146
- }
74147
- }, _callee6, null, [[2, 14]]);
74148
- }));
74149
- return function updateDashboard(_x2) {
74150
- return _ref6.apply(this, arguments);
74151
- };
74152
- }();
74153
-
74154
- // Are these needed?
74155
- var updateScheduledReport = function updateScheduledReport(dashboardAccessToken, reportId, newCron) {
74156
- if (vizzly) {
74157
- return vizzly.updateScheduledReport(dashboardAccessToken, reportId, newCron);
74158
- }
74159
- throw 'vizzly not set';
74160
- };
74161
- var getScheduledReports = function getScheduledReports(dashboardAccessToken) {
74162
- if (vizzly) {
74163
- return vizzly.getScheduledReports(dashboardAccessToken);
74164
- }
74165
- throw 'vizzly not set';
74166
- };
74167
- logDebug('useVizzly state.', state);
74168
- if (!vizzly || !!state.error || state.loading) {
74169
- return _extends({}, state, {
74170
- loading: true,
74171
- createDashboard: null,
74172
- identityConfig: null,
74173
- updateDashboard: null,
74174
- queryEngineConfig: null,
74175
- accessType: null,
74176
- refetch: null,
74177
- getScheduledReports: null,
74178
- updateScheduledReport: null
74179
- });
74180
- }
74181
- var identityConfig = vizzly.getIdentityConfig();
74182
- return _extends({}, state, {
74183
- loading: false,
74184
- identityConfig: identityConfig,
74185
- createDashboard: createDashboard,
74186
- updateDashboard: updateDashboard,
74187
- queryEngineConfig: vizzly.getViewConfiguration().queryEngineConfig,
74188
- accessType: !!identityConfig.dashboardAdminOverride ? 'admin' : identityConfig.dashboardAccess.accessType,
74189
- filterDashboards: Vizzly$1.filterDashboards,
74190
- refetch: refetch,
74191
- updateScheduledReport: updateScheduledReport,
74192
- getScheduledReports: getScheduledReports
74193
- });
74194
- };
74195
-
74196
- var useScheduledReports = function useScheduledReports(params, dashboardAccessToken) {
74197
- var _useState = useState([]),
74198
- reports = _useState[0],
74199
- setReports = _useState[1];
74200
- useEffect(function () {
74201
- var doWork = /*#__PURE__*/function () {
74202
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
74203
- var _yield$params$getSche, _reports;
74204
- return _regeneratorRuntime().wrap(function _callee$(_context) {
74205
- while (1) switch (_context.prev = _context.next) {
74206
- case 0:
74207
- if (!(params.getScheduledReports && dashboardAccessToken)) {
74208
- _context.next = 6;
74209
- break;
74210
- }
74211
- _context.next = 3;
74212
- return params.getScheduledReports(dashboardAccessToken);
74213
- case 3:
74214
- _yield$params$getSche = _context.sent;
74215
- _reports = _yield$params$getSche.reports;
74216
- setReports(_reports);
74217
- case 6:
74218
- case "end":
74219
- return _context.stop();
74220
- }
74221
- }, _callee);
74222
- }));
74223
- return function doWork() {
74224
- return _ref.apply(this, arguments);
74225
- };
74226
- }();
74227
- doWork();
74228
- }, [JSON.stringify({
74229
- dashboardAccessToken: dashboardAccessToken,
74230
- loadingVizzly: params
74231
- })]);
74232
- var updateScheduledReport = /*#__PURE__*/function () {
74233
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(reportId, newCron) {
74234
- var updatedReport, newReports, uniqueNewReports;
74235
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
74236
- while (1) switch (_context2.prev = _context2.next) {
74237
- case 0:
74238
- if (!(params.updateScheduledReport && dashboardAccessToken)) {
74239
- _context2.next = 7;
74240
- break;
74241
- }
74242
- _context2.next = 3;
74243
- return params.updateScheduledReport(dashboardAccessToken, reportId, newCron);
74244
- case 3:
74245
- updatedReport = _context2.sent;
74246
- if (updatedReport === true) {
74247
- // Remove the deleted report...
74248
- setReports([].concat(reports).filter(function (r) {
74249
- return r.id != reportId;
74250
- }));
74251
- } else if (updatedReport) {
74252
- // Upsert the report
74253
- newReports = [].concat(reports, [updatedReport.report]).map(function (r) {
74254
- return r.id == reportId ? updatedReport.report : r;
74255
- });
74256
- uniqueNewReports = uniqBy(newReports, function (r) {
74257
- return r.id;
74258
- });
74259
- setReports(uniqueNewReports);
74260
- }
74261
- _context2.next = 8;
74262
- break;
74263
- case 7:
74264
- console.warn('Function to update scheduled report not loaded.');
74265
- case 8:
74266
- case "end":
74267
- return _context2.stop();
74268
- }
74269
- }, _callee2);
74270
- }));
74271
- return function updateScheduledReport(_x, _x2) {
74272
- return _ref2.apply(this, arguments);
74273
- };
74274
- }();
74275
- return {
74276
- reports: reports,
74277
- getScheduledReports: params.getScheduledReports,
74278
- updateScheduledReport: updateScheduledReport
74279
- };
74280
- };
74281
-
74282
- /*
74283
- A context for managing the dashboard session.
74284
- */
74285
- var STILL_LOADING = null;
74286
- var useSessionContext = function useSessionContext(params) {
74287
- var _loadedStrategy$dashb, _loadedStrategy$dashb2;
74288
- var vizzly = useVizzly({
74289
- queryEngine: params.queryEngine,
74290
- identity: params.identityCallback,
74291
- dataSets: params.loadDataSetsCallback,
74292
- programmaticDashboard: params.programmaticDashboard
74293
- }, {
74294
- apiHost: params.apiConfig.host
74295
- });
74296
- var loadedStrategy = useDashboardStrategy(getStrategy(vizzly, params.parentDashboardId, params.dashboardId, params.isEditor), vizzly, params.programmaticDashboard);
74297
- var _useScheduledReports = useScheduledReports(vizzly, loadedStrategy == null || (_loadedStrategy$dashb = loadedStrategy.dashboard) == null || (_loadedStrategy$dashb = _loadedStrategy$dashb.permission) == null ? void 0 : _loadedStrategy$dashb.token),
74298
- reports = _useScheduledReports.reports,
74299
- updateScheduledReport = _useScheduledReports.updateScheduledReport;
74300
- if (!vizzly) return STILL_LOADING;
74301
- if ('error' in vizzly && vizzly.error) return {
74302
- error: vizzly.error
74303
- };
74304
- if ('loading' in vizzly && vizzly.loading) return STILL_LOADING;
74305
- if (!('dashboards' in vizzly)) return STILL_LOADING;
74306
- var identityConfig = vizzly.identityConfig,
74307
- queryEngineConfig = vizzly.queryEngineConfig;
74308
- if (!loadedStrategy) return null;
74309
- if (((loadedStrategy == null || (_loadedStrategy$dashb2 = loadedStrategy.dashboard) == null ? void 0 : _loadedStrategy$dashb2.dataSets) || []).length === 0) {
74310
- return {
74311
- error: new NoDataSetResolved()
74312
- };
74313
- }
74314
- var providedValues = {
74315
- vizzly: vizzly,
74316
- scope: loadedStrategy.scope,
74317
- queryEngineConfig: queryEngineConfig,
74318
- dashboard: loadedStrategy.dashboard,
74319
- identityConfig: identityConfig,
74320
- mode: loadedStrategy.mode,
74321
- vizzlyApiConfig: params.apiConfig,
74322
- isProgrammaticParent: loadedStrategy.isProgrammaticParent,
74323
- globalLibraries: vizzly.globalLibraries,
74324
- reports: reports,
74325
- updateScheduledReport: updateScheduledReport
74326
- };
74327
- return providedValues;
74328
- };
74329
-
74330
- var getParentDashboards$1 = function getParentDashboards(httpClient) {
74331
- return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
74332
- var _yield$httpClient, status, body;
74333
- return _regeneratorRuntime().wrap(function _callee$(_context) {
74334
- while (1) switch (_context.prev = _context.next) {
74335
- case 0:
74336
- _context.next = 2;
74337
- return httpClient({
74338
- path: "/api/v2/parent-dashboards",
74339
- method: 'get'
74340
- });
74341
- case 2:
74342
- _yield$httpClient = _context.sent;
74343
- status = _yield$httpClient.status;
74344
- body = _yield$httpClient.body;
74345
- if (!(status == 200)) {
74346
- _context.next = 7;
74347
- break;
74348
- }
74349
- return _context.abrupt("return", body.dashboards.map(function (rawDashboard) {
74350
- return {
74351
- id: rawDashboard.id,
74352
- created_at: rawDashboard.created_at,
74353
- updated_at: rawDashboard.updated_at,
74354
- webhook_id: rawDashboard.webhook_id
74355
- };
74356
- }));
74357
- case 7:
74358
- return _context.abrupt("return", undefined);
74359
- case 8:
74360
- case "end":
74361
- return _context.stop();
74362
- }
74363
- }, _callee);
74364
- }));
74365
- };
74366
-
74367
- // If cronSchedule is `null`, then it means the scheduled
74368
- // report is deleted.
74369
- var saveScheduledReport = function saveScheduledReport(httpClient) {
74370
- return /*#__PURE__*/function () {
74371
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
74372
- var _yield$httpClient, status;
74373
- return _regeneratorRuntime().wrap(function _callee$(_context) {
74374
- while (1) switch (_context.prev = _context.next) {
74375
- case 0:
74376
- _context.next = 2;
74377
- return httpClient({
74378
- path: '/api/scheduled-report',
74379
- method: 'post',
74380
- body: {
74381
- cronSchedule: params.cronSchedule,
74382
- dashboardAccessToken: params.dashboardAccessToken,
74383
- scheduledReportId: params.scheduledReportId
74384
- }
74385
- });
74386
- case 2:
74387
- _yield$httpClient = _context.sent;
74388
- status = _yield$httpClient.status;
74389
- return _context.abrupt("return", status == 200);
74390
- case 5:
74391
- case "end":
74392
- return _context.stop();
74393
- }
74394
- }, _callee);
74395
- }));
74396
- return function (_x) {
74397
- return _ref.apply(this, arguments);
73642
+ return function (_x) {
73643
+ return _ref.apply(this, arguments);
74398
73644
  };
74399
73645
  }();
74400
73646
  };
@@ -75134,6 +74380,28 @@ var api = function api(config) {
75134
74380
  };
75135
74381
  };
75136
74382
 
74383
+ var buildQueryEngineSignInUrl = function buildQueryEngineSignInUrl(queryEngineEndpoint, queryEngineAccessToken) {
74384
+ if (!queryEngineAccessToken) return '';
74385
+ var authToken = queryEngineAccessToken;
74386
+ authToken = encode(JSON.stringify({
74387
+ queryEngineAccessToken: queryEngineAccessToken,
74388
+ projectAccessToken: undefined
74389
+ }));
74390
+ var endpoint = buildPath(getHostFromQueryEngineEndpoint(queryEngineEndpoint), "/sign-in#" + authToken);
74391
+ return endpoint;
74392
+ };
74393
+
74394
+ var FailedToSaveError = /*#__PURE__*/function (_Error) {
74395
+ function FailedToSaveError(message) {
74396
+ var _this;
74397
+ _this = _Error.call(this, "Failed to save dashboard.") || this;
74398
+ _this.message = message;
74399
+ return _this;
74400
+ }
74401
+ _inheritsLoose(FailedToSaveError, _Error);
74402
+ return FailedToSaveError;
74403
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
74404
+
75137
74405
  var StatusLine = function StatusLine(props) {
75138
74406
  return jsx("div", {
75139
74407
  children: jsxs("span", {
@@ -75604,6 +74872,17 @@ var InvalidIdentityFunctionNotice = function InvalidIdentityFunctionNotice(props
75604
74872
  // />
75605
74873
  // );
75606
74874
 
74875
+ var NoDataSetResolved = /*#__PURE__*/function (_Error) {
74876
+ function NoDataSetResolved() {
74877
+ var _this;
74878
+ _this = _Error.call(this, 'At least one data set must be built for the Vizzly dashboard to render.') || this;
74879
+ _this.name = 'NoDataSetResolved';
74880
+ return _this;
74881
+ }
74882
+ _inheritsLoose(NoDataSetResolved, _Error);
74883
+ return NoDataSetResolved;
74884
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
74885
+
75607
74886
  var SetupError = function SetupError(props) {
75608
74887
  logError(props.error);
75609
74888
  var renderedError = jsx("p", {
@@ -75665,15 +74944,765 @@ var SetupError = function SetupError(props) {
75665
74944
  });
75666
74945
  };
75667
74946
 
75668
- var buildQueryEngineSignInUrl = function buildQueryEngineSignInUrl(queryEngineEndpoint, queryEngineAccessToken) {
75669
- if (!queryEngineAccessToken) return '';
75670
- var authToken = queryEngineAccessToken;
75671
- authToken = encode(JSON.stringify({
75672
- queryEngineAccessToken: queryEngineAccessToken,
75673
- projectAccessToken: undefined
74947
+ var getDefaultParentDashboardId = function getDefaultParentDashboardId(parentDashboardId, parentDashboardIdOnDashboardAccessToken, dashboards) {
74948
+ if (parentDashboardId) {
74949
+ var parentDashboard = find$1(dashboards, parentDashboardId);
74950
+ if (!(parentDashboard != null && parentDashboard.parentDashboardId)) {
74951
+ return parentDashboardId;
74952
+ } else {
74953
+ throw '[Vizzly] `parentDashboardId` value must specify an ID for a parent dashboard.';
74954
+ }
74955
+ }
74956
+ var parentDashboards = getParentDashboards(dashboards);
74957
+ var programmaticDashboardId = checkAndReturnProgrammaticDashboardId(parentDashboards);
74958
+ if (programmaticDashboardId) return programmaticDashboardId;
74959
+ return parentDashboardIdOnDashboardAccessToken || getFirstCreated(parentDashboards).id;
74960
+ };
74961
+
74962
+ var getStrategy = function getStrategy(vizzly, requestedEndUserParentDashboardId, requestedEndUserDashboardId, isEditor) {
74963
+ if (!('dashboards' in vizzly)) return undefined;
74964
+ if (vizzly.loading || vizzly.error) return undefined;
74965
+ logDebug('Loading dashboard strategy, with props;', {
74966
+ dashboardId: requestedEndUserDashboardId,
74967
+ parentDashboardId: requestedEndUserParentDashboardId
74968
+ });
74969
+ // This is a temporary fix for the issue where the dashboard is not loaded due to multiple hooks.
74970
+ var checkToStopStrategyCheckingWhenDashboardNotLoadedDueToMultipleHooks = isEditor && vizzly.dashboards.length === 0;
74971
+ if (checkToStopStrategyCheckingWhenDashboardNotLoadedDueToMultipleHooks) return undefined;
74972
+ var defaultParentDashboardId = getDefaultParentDashboardId(requestedEndUserParentDashboardId, vizzly.identityConfig.dashboardAccess.dashboardId, vizzly.dashboards);
74973
+ if (vizzly.accessType == 'admin') {
74974
+ logDebug('Admin access found, so we will show the parent dashboard.', defaultParentDashboardId);
74975
+ var dashboardId = defaultParentDashboardId;
74976
+ // If the user wants a specific dashboard, then fetch the parent dashboard of it.
74977
+ if (requestedEndUserDashboardId) {
74978
+ var foundRequestedChildDashboard = vizzly.dashboards.find(function (dashboard) {
74979
+ return dashboard.id == requestedEndUserDashboardId;
74980
+ });
74981
+ if (foundRequestedChildDashboard != null && foundRequestedChildDashboard.parentDashboardId) {
74982
+ logDebug('A child dashboard was provided whilst in admin mode. Therefore the parent of the given child will be shown to administer.', foundRequestedChildDashboard.parentDashboardId);
74983
+ dashboardId = foundRequestedChildDashboard.parentDashboardId;
74984
+ }
74985
+ }
74986
+ logDebug('Admin access found, so we will show the parent dashboard.', dashboardId);
74987
+ return {
74988
+ type: 'exact',
74989
+ dashboardId: dashboardId
74990
+ };
74991
+ }
74992
+ if (requestedEndUserDashboardId) {
74993
+ logDebug('Strategy will be to show dashboard', requestedEndUserDashboardId);
74994
+ return {
74995
+ type: 'exact',
74996
+ dashboardId: requestedEndUserDashboardId
74997
+ };
74998
+ }
74999
+ var childDashboards = vizzly.dashboards.filter(function (d) {
75000
+ var isChildDashboard = d.parentDashboardId != null;
75001
+ var hasNotRequestedEndUserParentDashboardId = requestedEndUserParentDashboardId === undefined;
75002
+ var isChildOfRequestedParent = hasNotRequestedEndUserParentDashboardId || d.parentDashboardId === requestedEndUserParentDashboardId;
75003
+ return isChildDashboard && isChildOfRequestedParent;
75004
+ });
75005
+ // Exactly one child dashboard for the user & in read-only mode so someone else is managing the dashboard with the same userReference.
75006
+ // We will show that one dashboard to allow for a common SaaS environment where
75007
+ // multiple users share the same userReference, but some of those users have `read_write` access and other users have read access.
75008
+ // Those with `read` access should see the dashboard managed by those other users...
75009
+ if (childDashboards.length === 1 && vizzly.identityConfig.dashboardAccess.scope == 'read') {
75010
+ logDebug('Strategy will be to show read-only view of the only child dashboard. If you wish to render a read-only view of the parent dashboard, please make sure the `dashboardId` property matches the `parentDashboardId` property.', defaultParentDashboardId);
75011
+ return {
75012
+ type: 'exact',
75013
+ dashboardId: vizzly.dashboards.find(function (d) {
75014
+ return d.parentDashboardId != null;
75015
+ }).id
75016
+ };
75017
+ }
75018
+ // No specific dashboard requested, but dashboard access token only grants 'read' access.
75019
+ if (vizzly.identityConfig.dashboardAccess.scope == 'read' && defaultParentDashboardId) {
75020
+ logDebug('Strategy will be to return read-only view of the parent dashboard.', defaultParentDashboardId);
75021
+ return {
75022
+ type: 'exact',
75023
+ dashboardId: defaultParentDashboardId
75024
+ };
75025
+ }
75026
+ var dashboardIdSetInIdentityConfig = vizzly.identityConfig.dashboardAccess.dashboardId;
75027
+ if (dashboardIdSetInIdentityConfig) {
75028
+ var _vizzly$identityConfi, _vizzly$identityConfi2;
75029
+ logDebug('Strategy will be to show a child of the parent dashboard determined by the identity config', (_vizzly$identityConfi = vizzly.identityConfig.dashboardAccess) == null ? void 0 : _vizzly$identityConfi.dashboardId);
75030
+ return {
75031
+ type: 'instantCopyOf',
75032
+ parentDashboardId: (_vizzly$identityConfi2 = vizzly.identityConfig.dashboardAccess) == null ? void 0 : _vizzly$identityConfi2.dashboardId
75033
+ };
75034
+ } else {
75035
+ logDebug('Strategy will be to show a child of the parent dashboard', defaultParentDashboardId);
75036
+ return {
75037
+ type: 'instantCopyOf',
75038
+ parentDashboardId: defaultParentDashboardId
75039
+ };
75040
+ }
75041
+ };
75042
+
75043
+ var Mode;
75044
+ (function (Mode) {
75045
+ Mode["Admin"] = "admin";
75046
+ Mode["EndUser"] = "endUser";
75047
+ })(Mode || (Mode = {}));
75048
+ var Scope;
75049
+ (function (Scope) {
75050
+ Scope["Read"] = "read";
75051
+ Scope["ReadWrite"] = "read_write";
75052
+ })(Scope || (Scope = {}));
75053
+
75054
+ /*
75055
+ Currently, we just replicate the same behaviour as what we used to have on the API.
75056
+ */
75057
+ var runStrategy = /*#__PURE__*/function () {
75058
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(strategy, vizzly) {
75059
+ var currentDashboard, scope, _parentDashboard$perm, parentDashboard, hasParentDashboardWritePermission, dashboardCopies, mostRecentlyUpdatedChildDashboard, created;
75060
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
75061
+ while (1) switch (_context.prev = _context.next) {
75062
+ case 0:
75063
+ if (!(strategy.type == 'exact')) {
75064
+ _context.next = 9;
75065
+ break;
75066
+ }
75067
+ currentDashboard = vizzly.dashboards.find(function (d) {
75068
+ return d.id === strategy.dashboardId;
75069
+ });
75070
+ if (currentDashboard.permission) {
75071
+ _context.next = 4;
75072
+ break;
75073
+ }
75074
+ throw '';
75075
+ case 4:
75076
+ scope = currentDashboard.permission.scope;
75077
+ logDebug('A specific dashboard has been requested. This will be rendered with', scope, 'scope.', strategy.dashboardId);
75078
+ return _context.abrupt("return", {
75079
+ dashboardId: strategy.dashboardId,
75080
+ mode: scope === 'read_write' && currentDashboard.parentDashboardId === null ? Mode.Admin : Mode.EndUser,
75081
+ scope: scope
75082
+ });
75083
+ case 9:
75084
+ if (!(strategy.type == 'instantCopyOf')) {
75085
+ _context.next = 29;
75086
+ break;
75087
+ }
75088
+ // If the user has write access to the parent dashboard, then we return that dashboard ID, with mode=admin
75089
+ parentDashboard = vizzly.dashboards.find(function (d) {
75090
+ return d.id === strategy.parentDashboardId;
75091
+ });
75092
+ hasParentDashboardWritePermission = ((_parentDashboard$perm = parentDashboard.permission) == null ? void 0 : _parentDashboard$perm.scope) === 'read_write';
75093
+ if (!hasParentDashboardWritePermission) {
75094
+ _context.next = 15;
75095
+ break;
75096
+ }
75097
+ logDebug('Write access to the parent dashboard has been found, so this dashboard will be shown with admin access.', strategy.parentDashboardId);
75098
+ return _context.abrupt("return", {
75099
+ dashboardId: strategy.parentDashboardId,
75100
+ mode: Mode.Admin,
75101
+ scope: Scope.ReadWrite
75102
+ });
75103
+ case 15:
75104
+ dashboardCopies = getCopiesOfParentDashboard(strategy.parentDashboardId, vizzly.dashboards);
75105
+ mostRecentlyUpdatedChildDashboard = takeMostRecentlyUpdated(dashboardCopies);
75106
+ if (!mostRecentlyUpdatedChildDashboard) {
75107
+ _context.next = 20;
75108
+ break;
75109
+ }
75110
+ logDebug('An existing child dashboard exists of the parent dashboard. As no specific dashboardId was provided, we will render the most recently updated child dashboard', mostRecentlyUpdatedChildDashboard.id);
75111
+ return _context.abrupt("return", {
75112
+ dashboardId: mostRecentlyUpdatedChildDashboard.id,
75113
+ mode: Mode.EndUser,
75114
+ scope: mostRecentlyUpdatedChildDashboard.permission.scope
75115
+ });
75116
+ case 20:
75117
+ logDebug('No previous child dashboard has been created on behalf of the current user for the parent dashboard', parentDashboard.id, '. Creating a child copy of the parent.');
75118
+ _context.next = 23;
75119
+ return vizzly.createDashboard({
75120
+ parentDashboardId: parentDashboard.id,
75121
+ definition: toSaveableDefinition(parentDashboard),
75122
+ metadata: {}
75123
+ });
75124
+ case 23:
75125
+ created = _context.sent;
75126
+ if (created) {
75127
+ _context.next = 26;
75128
+ break;
75129
+ }
75130
+ throw 'Failed to create first child dashboard for the user.';
75131
+ case 26:
75132
+ return _context.abrupt("return", {
75133
+ dashboardId: created.id,
75134
+ scope: Scope.ReadWrite,
75135
+ mode: Mode.EndUser
75136
+ });
75137
+ case 29:
75138
+ throw "Unsupported strategy";
75139
+ case 30:
75140
+ case "end":
75141
+ return _context.stop();
75142
+ }
75143
+ }, _callee);
75674
75144
  }));
75675
- var endpoint = buildPath(getHostFromQueryEngineEndpoint(queryEngineEndpoint), "/sign-in#" + authToken);
75676
- return endpoint;
75145
+ return function runStrategy(_x, _x2) {
75146
+ return _ref.apply(this, arguments);
75147
+ };
75148
+ }();
75149
+
75150
+ /*
75151
+ Stores which dashboard is the currently active one...
75152
+ */
75153
+ var useDashboardStrategy = function useDashboardStrategy(strategy, vizzly, programmaticDashboard) {
75154
+ var _useState = useState(),
75155
+ state = _useState[0],
75156
+ setState = _useState[1];
75157
+ useEffect(function () {
75158
+ var doWork = /*#__PURE__*/function () {
75159
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
75160
+ var _yield$runStrategy, mode, dashboardId, scope;
75161
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
75162
+ while (1) switch (_context.prev = _context.next) {
75163
+ case 0:
75164
+ if ('dashboards' in vizzly) {
75165
+ _context.next = 2;
75166
+ break;
75167
+ }
75168
+ return _context.abrupt("return");
75169
+ case 2:
75170
+ if (strategy) {
75171
+ _context.next = 4;
75172
+ break;
75173
+ }
75174
+ return _context.abrupt("return");
75175
+ case 4:
75176
+ if (!vizzly.loading) {
75177
+ _context.next = 6;
75178
+ break;
75179
+ }
75180
+ return _context.abrupt("return");
75181
+ case 6:
75182
+ _context.next = 8;
75183
+ return runStrategy(strategy, vizzly);
75184
+ case 8:
75185
+ _yield$runStrategy = _context.sent;
75186
+ mode = _yield$runStrategy.mode;
75187
+ dashboardId = _yield$runStrategy.dashboardId;
75188
+ scope = _yield$runStrategy.scope;
75189
+ if (dashboardId != (state == null ? void 0 : state.dashboardId)) setState({
75190
+ mode: mode,
75191
+ dashboardId: dashboardId,
75192
+ scope: scope
75193
+ });
75194
+ case 13:
75195
+ case "end":
75196
+ return _context.stop();
75197
+ }
75198
+ }, _callee);
75199
+ }));
75200
+ return function doWork() {
75201
+ return _ref.apply(this, arguments);
75202
+ };
75203
+ }();
75204
+ doWork();
75205
+ }, [JSON.stringify({
75206
+ strategy: strategy,
75207
+ loading: vizzly.loading
75208
+ })]);
75209
+ if (!('dashboards' in vizzly)) return null;
75210
+ if (!state) return null;
75211
+ if (vizzly.loading) return null;
75212
+ var dashboard = vizzly.dashboards.find(function (dash) {
75213
+ return dash.id == state.dashboardId;
75214
+ }) || null;
75215
+ return {
75216
+ mode: state.mode,
75217
+ dashboard: dashboard,
75218
+ scope: state.scope,
75219
+ isProgrammaticParent: !!programmaticDashboard
75220
+ };
75221
+ };
75222
+
75223
+ var useVizzly = function useVizzly(properties, options) {
75224
+ var _useState = useState({
75225
+ loading: true,
75226
+ error: undefined,
75227
+ dashboards: [],
75228
+ globalLibraries: []
75229
+ }),
75230
+ state = _useState[0],
75231
+ setState = _useState[1];
75232
+ var vizzlyRef = useRef();
75233
+ var vizzly = vizzlyRef.current;
75234
+ useEffect(function () {
75235
+ var abortController = new AbortController();
75236
+ var doWork = /*#__PURE__*/function () {
75237
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
75238
+ var _yield$Promise$all, globalLibraries, dashboards;
75239
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
75240
+ while (1) switch (_context.prev = _context.next) {
75241
+ case 0:
75242
+ _context.prev = 0;
75243
+ _context.next = 3;
75244
+ return Vizzly$1.load(properties, {
75245
+ apiHost: (options == null ? void 0 : options.apiHost) || 'https://api.vizzly.co'
75246
+ });
75247
+ case 3:
75248
+ vizzlyRef.current = _context.sent;
75249
+ _context.next = 6;
75250
+ return Promise.all([vizzlyRef.current.getGlobalLibraries({
75251
+ abortController: abortController
75252
+ }), vizzlyRef.current.getDashboards({
75253
+ abortController: abortController
75254
+ })]);
75255
+ case 6:
75256
+ _yield$Promise$all = _context.sent;
75257
+ globalLibraries = _yield$Promise$all[0];
75258
+ dashboards = _yield$Promise$all[1];
75259
+ setState({
75260
+ loading: false,
75261
+ error: undefined,
75262
+ dashboards: dashboards,
75263
+ globalLibraries: globalLibraries
75264
+ });
75265
+ _context.next = 16;
75266
+ break;
75267
+ case 12:
75268
+ _context.prev = 12;
75269
+ _context.t0 = _context["catch"](0);
75270
+ logError('Error loading Vizzly', _context.t0);
75271
+ setState({
75272
+ loading: false,
75273
+ error: _context.t0,
75274
+ dashboards: [].concat(state.dashboards),
75275
+ globalLibraries: [].concat(state.globalLibraries)
75276
+ });
75277
+ case 16:
75278
+ case "end":
75279
+ return _context.stop();
75280
+ }
75281
+ }, _callee, null, [[0, 12]]);
75282
+ }));
75283
+ return function doWork() {
75284
+ return _ref.apply(this, arguments);
75285
+ };
75286
+ }();
75287
+ doWork();
75288
+ return function () {
75289
+ return abortController.abort(new LoadingAborted('We stopped loading Vizzly because of an abort signal triggered by an unmount'));
75290
+ };
75291
+ }, []);
75292
+ var refetch = /*#__PURE__*/function () {
75293
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
75294
+ var abortController, doWork;
75295
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
75296
+ while (1) switch (_context3.prev = _context3.next) {
75297
+ case 0:
75298
+ abortController = new AbortController();
75299
+ doWork = /*#__PURE__*/function () {
75300
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
75301
+ var _yield$Promise$all2, globalLibraries, dashboards;
75302
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
75303
+ while (1) switch (_context2.prev = _context2.next) {
75304
+ case 0:
75305
+ _context2.prev = 0;
75306
+ _context2.next = 3;
75307
+ return Vizzly$1.load(properties, {
75308
+ apiHost: (options == null ? void 0 : options.apiHost) || 'https://api.vizzly.co'
75309
+ });
75310
+ case 3:
75311
+ vizzlyRef.current = _context2.sent;
75312
+ _context2.next = 6;
75313
+ return Promise.all([vizzlyRef.current.getGlobalLibraries({
75314
+ abortController: abortController
75315
+ }), vizzlyRef.current.getDashboards({
75316
+ abortController: abortController
75317
+ })]);
75318
+ case 6:
75319
+ _yield$Promise$all2 = _context2.sent;
75320
+ globalLibraries = _yield$Promise$all2[0];
75321
+ dashboards = _yield$Promise$all2[1];
75322
+ setState({
75323
+ loading: false,
75324
+ error: undefined,
75325
+ dashboards: dashboards,
75326
+ globalLibraries: globalLibraries
75327
+ });
75328
+ _context2.next = 16;
75329
+ break;
75330
+ case 12:
75331
+ _context2.prev = 12;
75332
+ _context2.t0 = _context2["catch"](0);
75333
+ logError('Error loading Vizzly', _context2.t0);
75334
+ setState({
75335
+ loading: false,
75336
+ error: _context2.t0,
75337
+ dashboards: [].concat(state.dashboards),
75338
+ globalLibraries: [].concat(state.globalLibraries)
75339
+ });
75340
+ case 16:
75341
+ case "end":
75342
+ return _context2.stop();
75343
+ }
75344
+ }, _callee2, null, [[0, 12]]);
75345
+ }));
75346
+ return function doWork() {
75347
+ return _ref3.apply(this, arguments);
75348
+ };
75349
+ }();
75350
+ doWork();
75351
+ return _context3.abrupt("return", function () {
75352
+ return abortController.abort(new LoadingAborted('We stopped loading Vizzly because of an abort signal triggered by an unmount'));
75353
+ });
75354
+ case 4:
75355
+ case "end":
75356
+ return _context3.stop();
75357
+ }
75358
+ }, _callee3);
75359
+ }));
75360
+ return function refetch() {
75361
+ return _ref2.apply(this, arguments);
75362
+ };
75363
+ }();
75364
+ var createDashboard = /*#__PURE__*/function () {
75365
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
75366
+ var createdDashboard;
75367
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
75368
+ while (1) switch (_context4.prev = _context4.next) {
75369
+ case 0:
75370
+ if (!vizzly) {
75371
+ _context4.next = 15;
75372
+ break;
75373
+ }
75374
+ _context4.prev = 1;
75375
+ _context4.next = 4;
75376
+ return vizzly.createDashboard({
75377
+ definition: params.definition,
75378
+ // permissions: params.permissions,
75379
+ parentDashboardId: params.parentDashboardId,
75380
+ metadata: params.metadata || params.meta,
75381
+ abortSignal: params.abortSignal
75382
+ });
75383
+ case 4:
75384
+ createdDashboard = _context4.sent;
75385
+ setState(_extends({}, state, {
75386
+ dashboards: [].concat(state.dashboards, [createdDashboard])
75387
+ }));
75388
+ return _context4.abrupt("return", createdDashboard);
75389
+ case 9:
75390
+ _context4.prev = 9;
75391
+ _context4.t0 = _context4["catch"](1);
75392
+ logError(_context4.t0);
75393
+ return _context4.abrupt("return", null);
75394
+ case 13:
75395
+ _context4.next = 16;
75396
+ break;
75397
+ case 15:
75398
+ throw 'Unable to create a dashboard when vizzly has not been loaded.';
75399
+ case 16:
75400
+ case "end":
75401
+ return _context4.stop();
75402
+ }
75403
+ }, _callee4, null, [[1, 9]]);
75404
+ }));
75405
+ return function createDashboard(_x) {
75406
+ return _ref4.apply(this, arguments);
75407
+ };
75408
+ }();
75409
+ useEffect(function () {
75410
+ var abortController = new AbortController();
75411
+ var doWork = /*#__PURE__*/function () {
75412
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
75413
+ var _yield$Promise$all3, globalLibraries;
75414
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
75415
+ while (1) switch (_context5.prev = _context5.next) {
75416
+ case 0:
75417
+ _context5.prev = 0;
75418
+ _context5.next = 3;
75419
+ return Vizzly$1.load(properties, {
75420
+ apiHost: (options == null ? void 0 : options.apiHost) || 'https://api.vizzly.co'
75421
+ });
75422
+ case 3:
75423
+ vizzlyRef.current = _context5.sent;
75424
+ _context5.next = 6;
75425
+ return Promise.all([vizzlyRef.current.getGlobalLibraries({
75426
+ abortController: abortController
75427
+ })]);
75428
+ case 6:
75429
+ _yield$Promise$all3 = _context5.sent;
75430
+ globalLibraries = _yield$Promise$all3[0];
75431
+ setState(_extends({}, state, {
75432
+ globalLibraries: globalLibraries
75433
+ }));
75434
+ _context5.next = 15;
75435
+ break;
75436
+ case 11:
75437
+ _context5.prev = 11;
75438
+ _context5.t0 = _context5["catch"](0);
75439
+ logError('Error loading Vizzly', _context5.t0);
75440
+ setState({
75441
+ loading: false,
75442
+ error: _context5.t0,
75443
+ dashboards: [].concat(state.dashboards),
75444
+ globalLibraries: [].concat(state.globalLibraries)
75445
+ });
75446
+ case 15:
75447
+ case "end":
75448
+ return _context5.stop();
75449
+ }
75450
+ }, _callee5, null, [[0, 11]]);
75451
+ }));
75452
+ return function doWork() {
75453
+ return _ref5.apply(this, arguments);
75454
+ };
75455
+ }();
75456
+ if (vizzly) {
75457
+ var unsubscribe = Vizzly$1.subscription(function (eventBus) {
75458
+ if (eventBus.data.action === 'newViewsInGlobalLibrary') {
75459
+ doWork();
75460
+ }
75461
+ });
75462
+ return function () {
75463
+ return unsubscribe();
75464
+ };
75465
+ }
75466
+ return function () {
75467
+ return abortController.abort(new LoadingAborted('We stopped loading Vizzly because of an abort signal triggered by an unmount'));
75468
+ };
75469
+ }, [vizzly]);
75470
+ var updateDashboard = /*#__PURE__*/function () {
75471
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(params) {
75472
+ var updatedDashboard, dashboards;
75473
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
75474
+ while (1) switch (_context6.prev = _context6.next) {
75475
+ case 0:
75476
+ if (!vizzly) {
75477
+ _context6.next = 20;
75478
+ break;
75479
+ }
75480
+ setState(_extends({}, state, {
75481
+ saving: SavingState.InProgress
75482
+ }));
75483
+ _context6.prev = 2;
75484
+ _context6.next = 5;
75485
+ return vizzly.updateDashboard({
75486
+ dashboardId: params.dashboardId,
75487
+ definition: params.definition,
75488
+ deleted: params.deleted,
75489
+ metadata: params.metadata || params.meta,
75490
+ abortSignal: params.abortSignal,
75491
+ name: params.name
75492
+ });
75493
+ case 5:
75494
+ updatedDashboard = _context6.sent;
75495
+ _context6.next = 8;
75496
+ return vizzly.getDashboards();
75497
+ case 8:
75498
+ dashboards = _context6.sent;
75499
+ setState(function (prevState) {
75500
+ return _extends({}, prevState, {
75501
+ dashboards: dashboards,
75502
+ saving: SavingState.Saved
75503
+ });
75504
+ });
75505
+ setTimeout(function () {
75506
+ setState(_extends({}, state, {
75507
+ saving: SavingState.Undefined
75508
+ }));
75509
+ }, 500);
75510
+ return _context6.abrupt("return", updatedDashboard);
75511
+ case 14:
75512
+ _context6.prev = 14;
75513
+ _context6.t0 = _context6["catch"](2);
75514
+ logError('Error updating dashboard', _context6.t0);
75515
+ return _context6.abrupt("return", null);
75516
+ case 18:
75517
+ _context6.next = 21;
75518
+ break;
75519
+ case 20:
75520
+ throw 'Unable to update a dashboard when vizzly has not been loaded.';
75521
+ case 21:
75522
+ case "end":
75523
+ return _context6.stop();
75524
+ }
75525
+ }, _callee6, null, [[2, 14]]);
75526
+ }));
75527
+ return function updateDashboard(_x2) {
75528
+ return _ref6.apply(this, arguments);
75529
+ };
75530
+ }();
75531
+
75532
+ // Are these needed?
75533
+ var updateScheduledReport = function updateScheduledReport(dashboardAccessToken, reportId, newCron) {
75534
+ if (vizzly) {
75535
+ return vizzly.updateScheduledReport(dashboardAccessToken, reportId, newCron);
75536
+ }
75537
+ throw 'vizzly not set';
75538
+ };
75539
+ var getScheduledReports = function getScheduledReports(dashboardAccessToken) {
75540
+ if (vizzly) {
75541
+ return vizzly.getScheduledReports(dashboardAccessToken);
75542
+ }
75543
+ throw 'vizzly not set';
75544
+ };
75545
+ logDebug('useVizzly state.', state);
75546
+ if (!vizzly || !!state.error || state.loading) {
75547
+ return _extends({}, state, {
75548
+ loading: true,
75549
+ createDashboard: null,
75550
+ identityConfig: null,
75551
+ updateDashboard: null,
75552
+ queryEngineConfig: null,
75553
+ accessType: null,
75554
+ refetch: null,
75555
+ getScheduledReports: null,
75556
+ updateScheduledReport: null
75557
+ });
75558
+ }
75559
+ var identityConfig = vizzly.getIdentityConfig();
75560
+ return _extends({}, state, {
75561
+ loading: false,
75562
+ identityConfig: identityConfig,
75563
+ createDashboard: createDashboard,
75564
+ updateDashboard: updateDashboard,
75565
+ queryEngineConfig: vizzly.getViewConfiguration().queryEngineConfig,
75566
+ accessType: !!identityConfig.dashboardAdminOverride ? 'admin' : identityConfig.dashboardAccess.accessType,
75567
+ filterDashboards: Vizzly$1.filterDashboards,
75568
+ refetch: refetch,
75569
+ updateScheduledReport: updateScheduledReport,
75570
+ getScheduledReports: getScheduledReports
75571
+ });
75572
+ };
75573
+
75574
+ var useScheduledReports = function useScheduledReports(params, dashboardAccessToken) {
75575
+ var _useState = useState([]),
75576
+ reports = _useState[0],
75577
+ setReports = _useState[1];
75578
+ useEffect(function () {
75579
+ var doWork = /*#__PURE__*/function () {
75580
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
75581
+ var _yield$params$getSche, _reports;
75582
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
75583
+ while (1) switch (_context.prev = _context.next) {
75584
+ case 0:
75585
+ if (!(params.getScheduledReports && dashboardAccessToken)) {
75586
+ _context.next = 6;
75587
+ break;
75588
+ }
75589
+ _context.next = 3;
75590
+ return params.getScheduledReports(dashboardAccessToken);
75591
+ case 3:
75592
+ _yield$params$getSche = _context.sent;
75593
+ _reports = _yield$params$getSche.reports;
75594
+ setReports(_reports);
75595
+ case 6:
75596
+ case "end":
75597
+ return _context.stop();
75598
+ }
75599
+ }, _callee);
75600
+ }));
75601
+ return function doWork() {
75602
+ return _ref.apply(this, arguments);
75603
+ };
75604
+ }();
75605
+ doWork();
75606
+ }, [JSON.stringify({
75607
+ dashboardAccessToken: dashboardAccessToken,
75608
+ loadingVizzly: params
75609
+ })]);
75610
+ var updateScheduledReport = /*#__PURE__*/function () {
75611
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(reportId, newCron) {
75612
+ var updatedReport, newReports, uniqueNewReports;
75613
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
75614
+ while (1) switch (_context2.prev = _context2.next) {
75615
+ case 0:
75616
+ if (!(params.updateScheduledReport && dashboardAccessToken)) {
75617
+ _context2.next = 7;
75618
+ break;
75619
+ }
75620
+ _context2.next = 3;
75621
+ return params.updateScheduledReport(dashboardAccessToken, reportId, newCron);
75622
+ case 3:
75623
+ updatedReport = _context2.sent;
75624
+ if (updatedReport === true) {
75625
+ // Remove the deleted report...
75626
+ setReports([].concat(reports).filter(function (r) {
75627
+ return r.id != reportId;
75628
+ }));
75629
+ } else if (updatedReport) {
75630
+ // Upsert the report
75631
+ newReports = [].concat(reports, [updatedReport.report]).map(function (r) {
75632
+ return r.id == reportId ? updatedReport.report : r;
75633
+ });
75634
+ uniqueNewReports = uniqBy(newReports, function (r) {
75635
+ return r.id;
75636
+ });
75637
+ setReports(uniqueNewReports);
75638
+ }
75639
+ _context2.next = 8;
75640
+ break;
75641
+ case 7:
75642
+ console.warn('Function to update scheduled report not loaded.');
75643
+ case 8:
75644
+ case "end":
75645
+ return _context2.stop();
75646
+ }
75647
+ }, _callee2);
75648
+ }));
75649
+ return function updateScheduledReport(_x, _x2) {
75650
+ return _ref2.apply(this, arguments);
75651
+ };
75652
+ }();
75653
+ return {
75654
+ reports: reports,
75655
+ getScheduledReports: params.getScheduledReports,
75656
+ updateScheduledReport: updateScheduledReport
75657
+ };
75658
+ };
75659
+
75660
+ /*
75661
+ A context for managing the dashboard session.
75662
+ */
75663
+ var STILL_LOADING = null;
75664
+ var useSessionContext = function useSessionContext(params) {
75665
+ var _loadedStrategy$dashb, _loadedStrategy$dashb2;
75666
+ var vizzly = useVizzly({
75667
+ queryEngine: params.queryEngine,
75668
+ identity: params.identityCallback,
75669
+ dataSets: params.loadDataSetsCallback,
75670
+ programmaticDashboard: params.programmaticDashboard
75671
+ }, {
75672
+ apiHost: params.apiConfig.host
75673
+ });
75674
+ var loadedStrategy = useDashboardStrategy(getStrategy(vizzly, params.parentDashboardId, params.dashboardId, params.isEditor), vizzly, params.programmaticDashboard);
75675
+ var _useScheduledReports = useScheduledReports(vizzly, loadedStrategy == null || (_loadedStrategy$dashb = loadedStrategy.dashboard) == null || (_loadedStrategy$dashb = _loadedStrategy$dashb.permission) == null ? void 0 : _loadedStrategy$dashb.token),
75676
+ reports = _useScheduledReports.reports,
75677
+ updateScheduledReport = _useScheduledReports.updateScheduledReport;
75678
+ if (!vizzly) return STILL_LOADING;
75679
+ if ('error' in vizzly && vizzly.error) return {
75680
+ error: vizzly.error
75681
+ };
75682
+ if ('loading' in vizzly && vizzly.loading) return STILL_LOADING;
75683
+ if (!('dashboards' in vizzly)) return STILL_LOADING;
75684
+ var identityConfig = vizzly.identityConfig,
75685
+ queryEngineConfig = vizzly.queryEngineConfig;
75686
+ if (!loadedStrategy) return null;
75687
+ if (((loadedStrategy == null || (_loadedStrategy$dashb2 = loadedStrategy.dashboard) == null ? void 0 : _loadedStrategy$dashb2.dataSets) || []).length === 0) {
75688
+ return {
75689
+ error: new NoDataSetResolved()
75690
+ };
75691
+ }
75692
+ var providedValues = {
75693
+ vizzly: vizzly,
75694
+ scope: loadedStrategy.scope,
75695
+ queryEngineConfig: queryEngineConfig,
75696
+ dashboard: loadedStrategy.dashboard,
75697
+ identityConfig: identityConfig,
75698
+ mode: loadedStrategy.mode,
75699
+ vizzlyApiConfig: params.apiConfig,
75700
+ isProgrammaticParent: loadedStrategy.isProgrammaticParent,
75701
+ globalLibraries: vizzly.globalLibraries,
75702
+ reports: reports,
75703
+ updateScheduledReport: updateScheduledReport
75704
+ };
75705
+ return providedValues;
75677
75706
  };
75678
75707
 
75679
75708
  var buildImplementationMeta = function buildImplementationMeta(props) {
@@ -75695,7 +75724,9 @@ var buildImplementationMeta = function buildImplementationMeta(props) {
75695
75724
  };
75696
75725
  };
75697
75726
 
75698
- var useOnDashboardLoadCallback = function useOnDashboardLoadCallback(session, onDashboardLoad) {
75727
+ var GlobalContext = /*#__PURE__*/createContext(undefined);
75728
+
75729
+ var useOnDashboardLoadCallback = function useOnDashboardLoadCallback(session, onDashboardLoad, updateDashboardHash) {
75699
75730
  var _session$dashboard2;
75700
75731
  useEffect(function () {
75701
75732
  var _session$dashboard;
@@ -75706,6 +75737,7 @@ var useOnDashboardLoadCallback = function useOnDashboardLoadCallback(session, on
75706
75737
  scope: session.scope,
75707
75738
  dataSets: session.dashboard.dataSets
75708
75739
  });
75740
+ updateDashboardHash(session.dashboard);
75709
75741
  }
75710
75742
  }, [JSON.stringify({
75711
75743
  loaded: !!session,
@@ -75713,17 +75745,6 @@ var useOnDashboardLoadCallback = function useOnDashboardLoadCallback(session, on
75713
75745
  })]);
75714
75746
  };
75715
75747
 
75716
- var FailedToSaveError = /*#__PURE__*/function (_Error) {
75717
- function FailedToSaveError(message) {
75718
- var _this;
75719
- _this = _Error.call(this, "Failed to save dashboard.") || this;
75720
- _this.message = message;
75721
- return _this;
75722
- }
75723
- _inheritsLoose(FailedToSaveError, _Error);
75724
- return FailedToSaveError;
75725
- }( /*#__PURE__*/_wrapNativeSuper(Error));
75726
-
75727
75748
  var GlobalProvider = function GlobalProvider(props) {
75728
75749
  var _useState = useState(0),
75729
75750
  resetKey = _useState[0],
@@ -75749,6 +75770,9 @@ var GlobalProvider = function GlobalProvider(props) {
75749
75770
  };
75750
75771
  var GlobalProviderContents = function GlobalProviderContents(props) {
75751
75772
  var _props$id, _session$identityConf, _props$dateFilterOpti, _session$vizzly$savin;
75773
+ var _useState2 = useState(undefined),
75774
+ dashboardHash = _useState2[0],
75775
+ setDashboardHash = _useState2[1];
75752
75776
  var implementationMeta = buildImplementationMeta(props);
75753
75777
  var apiConfig = props.vizzlyApiHost ? {
75754
75778
  host: props.vizzlyApiHost
@@ -75772,7 +75796,11 @@ var GlobalProviderContents = function GlobalProviderContents(props) {
75772
75796
  textOverride: textOverride,
75773
75797
  isEditor: props.isEditor
75774
75798
  });
75775
- useOnDashboardLoadCallback(session, props.onDashboardLoad);
75799
+ var updateDashboardHash = function updateDashboardHash(dashboard) {
75800
+ var definition = toSaveableDefinition(dashboard);
75801
+ setDashboardHash(crypto.createHash('md5').update(JSON.stringify(definition)).digest('hex'));
75802
+ };
75803
+ useOnDashboardLoadCallback(session, props.onDashboardLoad, updateDashboardHash);
75776
75804
  var variables = useVariables(props.variables, (_props$id = props.id) != null ? _props$id : '');
75777
75805
  if (session === null) return jsx(LoadingComponent, {
75778
75806
  override: props.renderLoadingComponent
@@ -75968,14 +75996,16 @@ var GlobalProviderContents = function GlobalProviderContents(props) {
75968
75996
  labelFormat: props == null ? void 0 : props.labelFormat,
75969
75997
  filterLibrary: props == null ? void 0 : props.filterLibrary,
75970
75998
  valueAlias: props == null ? void 0 : props.valueAlias,
75971
- variables: variables
75999
+ variables: variables,
76000
+ onDashboardUpdate: props == null ? void 0 : props.onDashboardUpdate,
76001
+ updateDashboardHash: updateDashboardHash
75972
76002
  },
75973
76003
  ai: {
75974
76004
  onAction: aiOnAction,
75975
76005
  onAsk: aiOnAsk,
75976
76006
  fetchSuggestions: aiFetchSuggestions
75977
76007
  },
75978
- onDashboardUpdate: props == null ? void 0 : props.onDashboardUpdate,
76008
+ dashboardHash: dashboardHash,
75979
76009
  dashboard: session.dashboard,
75980
76010
  runQueriesCallback: runQueriesCallback,
75981
76011
  implementationMeta: implementationMeta,