@gravity-ui/dashkit 6.6.0-beta.0 → 6.6.1

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/README.md CHANGED
@@ -120,21 +120,6 @@ Before using `DashKit` as a react component, it must be configured.
120
120
  });
121
121
  ```
122
122
 
123
- ## Settings
124
-
125
- ### Autoupdate
126
-
127
- This functionality allows you to automatically update the items of the Dash. All items that should participate in the auto-update cycle must have the `reload` method.
128
- There are two auto-update scenarios available, the first - the classic interval to enable which is enough to pass the `autoupdateInterval` parameter and the second - the so-called `realtimeMode`.
129
- `realtimeMode` is useful in cases where dash items are updated at different rates and you cannot guarantee data consistency at a given time. `realtimeMode` is based on `Promise.allSettled`.
130
-
131
- | Name | Description | Type | Default |
132
- |:--------------------|:---------------------------------------------------------------------------------------|:---------:|:-------:|
133
- | autoupdateInterval | The time, in **seconds**, the dash should delay in between executions auto-update | `number` | |
134
- | realtimeMode | Enable realtime mode | `boolean` | |
135
- | realtimeModeDelayMs | The time, in **milliseconds**, the dash should delay after all items have been updated | `number` | 5000 |
136
- | silentLoading | Items update status indicator. It will be passed into the Item `reload` method | `boolean` | false |
137
-
138
123
  ### Config
139
124
 
140
125
  ```ts
@@ -18,10 +18,6 @@ export default class GridLayout extends React.PureComponent<any, any, any> {
18
18
  adjustedWidgetLayout: any;
19
19
  }) => void;
20
20
  getItemsMeta: () => any;
21
- getAutoupdateIntervalMs(): number;
22
- startReloadItems(): any;
23
- startReloadItemsTimeout(timeoutMs: any): void;
24
- forceReloadItems(): void;
25
21
  reloadItems(): void;
26
22
  _onStart: () => void;
27
23
  _onStop: (newLayout: any) => void;
@@ -50,48 +50,25 @@ class GridLayout extends react_1.default.PureComponent {
50
50
  document.addEventListener('visibilitychange', this.onVisibilityChange);
51
51
  }
52
52
  componentDidUpdate() {
53
- this.forceReloadItems();
53
+ clearTimeout(this._timeout);
54
+ this.reloadItems();
54
55
  }
55
56
  componentWillUnmount() {
56
57
  clearTimeout(this._timeout);
57
58
  document.removeEventListener('visibilitychange', this.onVisibilityChange);
58
59
  }
59
- getAutoupdateIntervalMs() {
60
- const { settings: { autoupdateInterval } = {} } = this.context;
61
- const autoupdateIntervalMs = Number(autoupdateInterval) * 1000;
62
- return autoupdateIntervalMs;
63
- }
64
- startReloadItems() {
65
- const { editMode, settings: { silentLoading } = {}, reloadItems } = this.context;
66
- const { isPageHidden } = this.state;
67
- if (!isPageHidden && !editMode) {
68
- this._lastReloadAt = new Date().getTime();
69
- return reloadItems(this.pluginsRefs, { silentLoading, noVeil: true });
70
- }
71
- return Promise.resolve();
72
- }
73
- startReloadItemsTimeout(timeoutMs) {
74
- this._timeout = setTimeout(() => this.forceReloadItems(), timeoutMs);
75
- }
76
- forceReloadItems() {
77
- clearTimeout(this._timeout);
78
- this._timeout = undefined;
79
- this.reloadItems();
80
- }
81
60
  reloadItems() {
82
- if (this._timeout)
83
- return;
84
- const { settings: { realtimeMode, realtimeModeDelayMs = 5000 } = {} } = this.context;
85
- if (realtimeMode) {
86
- this.startReloadItems().then(() => {
87
- this.startReloadItemsTimeout(realtimeModeDelayMs);
88
- });
89
- return;
90
- }
91
- const autoupdateIntervalMs = this.getAutoupdateIntervalMs();
61
+ const { editMode, settings: { autoupdateInterval, silentLoading } = {}, reloadItems, } = this.context;
62
+ const { isPageHidden } = this.state;
63
+ const autoupdateIntervalMs = Number(autoupdateInterval) * 1000;
92
64
  if (autoupdateIntervalMs) {
93
- this.startReloadItems();
94
- this.startReloadItemsTimeout(autoupdateIntervalMs);
65
+ const timeSinceLastReload = new Date().getTime() - (this._lastReloadAt || 0);
66
+ const reloadIntervalRemains = autoupdateIntervalMs - timeSinceLastReload;
67
+ if (!isPageHidden && !editMode && reloadIntervalRemains <= 0) {
68
+ this._lastReloadAt = new Date().getTime();
69
+ reloadItems(this.pluginsRefs, { silentLoading, noVeil: true });
70
+ }
71
+ this._timeout = setTimeout(() => this.reloadItems(), reloadIntervalRemains <= 0 ? autoupdateIntervalMs : reloadIntervalRemains);
95
72
  }
96
73
  }
97
74
  render() {
@@ -38,7 +38,16 @@ interface OverlayControlsProps extends OverlayControlsDefaultProps {
38
38
  items?: OverlayControlItem[];
39
39
  overlayControls?: Record<string, OverlayControlItem[]>;
40
40
  }
41
+ type PreparedCopyItemOptionsArg = Pick<ConfigItem, 'data' | 'type' | 'defaults' | 'namespace'> & {
42
+ timestamp: number;
43
+ layout: {
44
+ w: number;
45
+ h: number;
46
+ };
47
+ };
48
+ export type PreparedCopyItemOptions = PreparedCopyItemOptionsArg;
41
49
  type DashKitCtx = React.Context<{
50
+ context: Record<string, any>;
42
51
  registerManager: RegisterManager;
43
52
  itemsParams: Record<string, StringParams>;
44
53
  itemsState: Record<string, ItemState>;
@@ -56,10 +56,11 @@ class OverlayControls extends react_1.default.Component {
56
56
  return controls;
57
57
  };
58
58
  this.onCopyItem = () => {
59
+ var _a;
59
60
  const correspondedItemLayout = this.context.config.layout.find((item) => {
60
61
  return item.i === this.props.configItem.id;
61
62
  });
62
- const options = {
63
+ let options = {
63
64
  timestamp: Date.now(),
64
65
  data: this.props.configItem.data,
65
66
  type: this.props.configItem.type,
@@ -70,6 +71,9 @@ class OverlayControls extends react_1.default.Component {
70
71
  h: correspondedItemLayout.h,
71
72
  },
72
73
  };
74
+ if (typeof ((_a = this.context.context) === null || _a === void 0 ? void 0 : _a.getPreparedCopyItemOptions) === 'function') {
75
+ options = this.context.context.getPreparedCopyItemOptions(options);
76
+ }
73
77
  localStorage.setItem(constants_1.COPIED_WIDGET_STORE_KEY, JSON.stringify(options));
74
78
  // https://stackoverflow.com/questions/35865481/storage-event-not-firing
75
79
  window.dispatchEvent(new Event('storage'));
@@ -1,3 +1,3 @@
1
1
  export * from './DashKit';
2
- export type { OverlayControlItem } from './OverlayControls/OverlayControls';
2
+ export type { OverlayControlItem, PreparedCopyItemOptions } from './OverlayControls/OverlayControls';
3
3
  export * from './ActionPanel/ActionPanel';
@@ -121,8 +121,7 @@ function useMemoStateContext(props) {
121
121
  });
122
122
  }, [props.layout, layoutUpdateCounter]);
123
123
  const reloadItems = react_1.default.useCallback((pluginsRefs, data) => {
124
- const promises = pluginsRefs.map((ref) => ref && ref.reload && ref.reload(data));
125
- return Promise.allSettled(promises);
124
+ pluginsRefs.forEach((ref) => ref && ref.reload && ref.reload(data));
126
125
  }, []);
127
126
  return react_1.default.useMemo(() => ({
128
127
  layout: resultLayout,
@@ -9,10 +9,8 @@ export interface Settings {
9
9
  }
10
10
  export type MenuItem = (typeof MenuItems)[keyof typeof MenuItems] | OverlayCustomControlItem;
11
11
  export interface SettingsProps {
12
- autoupdateInterval?: number;
12
+ autoupdateInterval: number;
13
13
  silentLoading: boolean;
14
- realtimeMode?: boolean;
15
- realtimeModeDelayMs?: number;
16
14
  }
17
15
  export interface ContextProps {
18
16
  [key: string]: any;
@@ -18,10 +18,6 @@ export default class GridLayout extends React.PureComponent<any, any, any> {
18
18
  adjustedWidgetLayout: any;
19
19
  }) => void;
20
20
  getItemsMeta: () => any;
21
- getAutoupdateIntervalMs(): number;
22
- startReloadItems(): any;
23
- startReloadItemsTimeout(timeoutMs: any): void;
24
- forceReloadItems(): void;
25
21
  reloadItems(): void;
26
22
  _onStart: () => void;
27
23
  _onStop: (newLayout: any) => void;
@@ -47,48 +47,25 @@ export default class GridLayout extends React.PureComponent {
47
47
  document.addEventListener('visibilitychange', this.onVisibilityChange);
48
48
  }
49
49
  componentDidUpdate() {
50
- this.forceReloadItems();
50
+ clearTimeout(this._timeout);
51
+ this.reloadItems();
51
52
  }
52
53
  componentWillUnmount() {
53
54
  clearTimeout(this._timeout);
54
55
  document.removeEventListener('visibilitychange', this.onVisibilityChange);
55
56
  }
56
- getAutoupdateIntervalMs() {
57
- const { settings: { autoupdateInterval } = {} } = this.context;
58
- const autoupdateIntervalMs = Number(autoupdateInterval) * 1000;
59
- return autoupdateIntervalMs;
60
- }
61
- startReloadItems() {
62
- const { editMode, settings: { silentLoading } = {}, reloadItems } = this.context;
63
- const { isPageHidden } = this.state;
64
- if (!isPageHidden && !editMode) {
65
- this._lastReloadAt = new Date().getTime();
66
- return reloadItems(this.pluginsRefs, { silentLoading, noVeil: true });
67
- }
68
- return Promise.resolve();
69
- }
70
- startReloadItemsTimeout(timeoutMs) {
71
- this._timeout = setTimeout(() => this.forceReloadItems(), timeoutMs);
72
- }
73
- forceReloadItems() {
74
- clearTimeout(this._timeout);
75
- this._timeout = undefined;
76
- this.reloadItems();
77
- }
78
57
  reloadItems() {
79
- if (this._timeout)
80
- return;
81
- const { settings: { realtimeMode, realtimeModeDelayMs = 5000 } = {} } = this.context;
82
- if (realtimeMode) {
83
- this.startReloadItems().then(() => {
84
- this.startReloadItemsTimeout(realtimeModeDelayMs);
85
- });
86
- return;
87
- }
88
- const autoupdateIntervalMs = this.getAutoupdateIntervalMs();
58
+ const { editMode, settings: { autoupdateInterval, silentLoading } = {}, reloadItems, } = this.context;
59
+ const { isPageHidden } = this.state;
60
+ const autoupdateIntervalMs = Number(autoupdateInterval) * 1000;
89
61
  if (autoupdateIntervalMs) {
90
- this.startReloadItems();
91
- this.startReloadItemsTimeout(autoupdateIntervalMs);
62
+ const timeSinceLastReload = new Date().getTime() - (this._lastReloadAt || 0);
63
+ const reloadIntervalRemains = autoupdateIntervalMs - timeSinceLastReload;
64
+ if (!isPageHidden && !editMode && reloadIntervalRemains <= 0) {
65
+ this._lastReloadAt = new Date().getTime();
66
+ reloadItems(this.pluginsRefs, { silentLoading, noVeil: true });
67
+ }
68
+ this._timeout = setTimeout(() => this.reloadItems(), reloadIntervalRemains <= 0 ? autoupdateIntervalMs : reloadIntervalRemains);
92
69
  }
93
70
  }
94
71
  render() {
@@ -39,7 +39,16 @@ interface OverlayControlsProps extends OverlayControlsDefaultProps {
39
39
  items?: OverlayControlItem[];
40
40
  overlayControls?: Record<string, OverlayControlItem[]>;
41
41
  }
42
+ type PreparedCopyItemOptionsArg = Pick<ConfigItem, 'data' | 'type' | 'defaults' | 'namespace'> & {
43
+ timestamp: number;
44
+ layout: {
45
+ w: number;
46
+ h: number;
47
+ };
48
+ };
49
+ export type PreparedCopyItemOptions = PreparedCopyItemOptionsArg;
42
50
  type DashKitCtx = React.Context<{
51
+ context: Record<string, any>;
43
52
  registerManager: RegisterManager;
44
53
  itemsParams: Record<string, StringParams>;
45
54
  itemsState: Record<string, ItemState>;
@@ -53,10 +53,11 @@ class OverlayControls extends React.Component {
53
53
  return controls;
54
54
  };
55
55
  this.onCopyItem = () => {
56
+ var _a;
56
57
  const correspondedItemLayout = this.context.config.layout.find((item) => {
57
58
  return item.i === this.props.configItem.id;
58
59
  });
59
- const options = {
60
+ let options = {
60
61
  timestamp: Date.now(),
61
62
  data: this.props.configItem.data,
62
63
  type: this.props.configItem.type,
@@ -67,6 +68,9 @@ class OverlayControls extends React.Component {
67
68
  h: correspondedItemLayout.h,
68
69
  },
69
70
  };
71
+ if (typeof ((_a = this.context.context) === null || _a === void 0 ? void 0 : _a.getPreparedCopyItemOptions) === 'function') {
72
+ options = this.context.context.getPreparedCopyItemOptions(options);
73
+ }
70
74
  localStorage.setItem(COPIED_WIDGET_STORE_KEY, JSON.stringify(options));
71
75
  // https://stackoverflow.com/questions/35865481/storage-event-not-firing
72
76
  window.dispatchEvent(new Event('storage'));
@@ -1,3 +1,3 @@
1
1
  export * from './DashKit';
2
- export type { OverlayControlItem } from './OverlayControls/OverlayControls';
2
+ export type { OverlayControlItem, PreparedCopyItemOptions } from './OverlayControls/OverlayControls';
3
3
  export * from './ActionPanel/ActionPanel';
@@ -117,8 +117,7 @@ function useMemoStateContext(props) {
117
117
  });
118
118
  }, [props.layout, layoutUpdateCounter]);
119
119
  const reloadItems = React.useCallback((pluginsRefs, data) => {
120
- const promises = pluginsRefs.map((ref) => ref && ref.reload && ref.reload(data));
121
- return Promise.allSettled(promises);
120
+ pluginsRefs.forEach((ref) => ref && ref.reload && ref.reload(data));
122
121
  }, []);
123
122
  return React.useMemo(() => ({
124
123
  layout: resultLayout,
@@ -9,10 +9,8 @@ export interface Settings {
9
9
  }
10
10
  export type MenuItem = (typeof MenuItems)[keyof typeof MenuItems] | OverlayCustomControlItem;
11
11
  export interface SettingsProps {
12
- autoupdateInterval?: number;
12
+ autoupdateInterval: number;
13
13
  silentLoading: boolean;
14
- realtimeMode?: boolean;
15
- realtimeModeDelayMs?: number;
16
14
  }
17
15
  export interface ContextProps {
18
16
  [key: string]: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/dashkit",
3
- "version": "6.6.0-beta.0",
3
+ "version": "6.6.1",
4
4
  "description": "Library for rendering dashboard grid layout",
5
5
  "license": "MIT",
6
6
  "repository": {