@bygd/nc-report-ui 0.1.10 → 0.1.12

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.
@@ -98,6 +98,18 @@ const apiClient = axios.create({
98
98
  'Content-Type': 'application/json'
99
99
  }
100
100
  });
101
+
102
+ // Cache for dashboard metadata
103
+ const dashboardMetaCache = {};
104
+
105
+ /**
106
+ * Helper function to get the first available dashboard ID from cache
107
+ * @returns {string|null} First dashboard ID or null if cache is empty
108
+ */
109
+ const getFirstCachedDashboardId = () => {
110
+ const cachedIds = Object.keys(dashboardMetaCache);
111
+ return cachedIds.length > 0 ? cachedIds[0] : null;
112
+ };
101
113
  const Api = {
102
114
  setAuth(auth) {
103
115
  this.auth = auth;
@@ -106,9 +118,42 @@ const Api = {
106
118
  if (!accessToken) return;
107
119
  apiClient.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
108
120
  },
121
+ /**
122
+ * Load and cache dashboard metadata
123
+ * @param {Object} params - Parameters object
124
+ * @param {string} params.dashboardId - Dashboard ID
125
+ * @returns {Promise} Dashboard metadata object
126
+ */
127
+ loadDashboardMeta: async ({
128
+ dashboardId
129
+ }) => {
130
+ const {
131
+ data
132
+ } = await apiClient.get(`/dashboard-meta/${dashboardId}`);
133
+
134
+ // Cache the metadata
135
+ dashboardMetaCache[dashboardId] = {
136
+ dashboards: data.dashboards || {},
137
+ charts: data.charts || {},
138
+ reports: data.reports || {},
139
+ reportMetadata: data.reportMetadata || {},
140
+ dateRanges: data.dateRanges || []
141
+ };
142
+ return dashboardMetaCache[dashboardId];
143
+ },
109
144
  getDashboard: async ({
110
- id
145
+ id,
146
+ dashboardId
111
147
  }) => {
148
+ // Use provided dashboardId or fall back to first cached dashboard
149
+ const cacheKey = dashboardId || getFirstCachedDashboardId();
150
+
151
+ // Check cache first
152
+ if (cacheKey && dashboardMetaCache[cacheKey]?.dashboards?.[id]) {
153
+ return dashboardMetaCache[cacheKey].dashboards[id];
154
+ }
155
+
156
+ // Fall back to HTTP request
112
157
  const {
113
158
  data
114
159
  } = await apiClient.get(`/entity/dashboards/${id}`);
@@ -118,11 +163,22 @@ const Api = {
118
163
  * Get chart by ID
119
164
  * @param {Object} params - Parameters object
120
165
  * @param {string} params.id - Chart ID
166
+ * @param {string} [params.dashboardId] - Optional dashboard ID for cache lookup
121
167
  * @returns {Promise} Axios response promise
122
168
  */
123
169
  getChart: async ({
124
- id
170
+ id,
171
+ dashboardId
125
172
  }) => {
173
+ // Use provided dashboardId or fall back to first cached dashboard
174
+ const cacheKey = dashboardId || getFirstCachedDashboardId();
175
+
176
+ // Check cache first
177
+ if (cacheKey && dashboardMetaCache[cacheKey]?.charts?.[id]) {
178
+ return dashboardMetaCache[cacheKey].charts[id];
179
+ }
180
+
181
+ // Fall back to HTTP request
126
182
  const {
127
183
  data
128
184
  } = await apiClient.get(`/entity/charts/${id}`);
@@ -132,11 +188,22 @@ const Api = {
132
188
  * Get report by ID
133
189
  * @param {Object} params - Parameters object
134
190
  * @param {string} params.id - Report ID
191
+ * @param {string} [params.dashboardId] - Optional dashboard ID for cache lookup
135
192
  * @returns {Promise} Axios response promise
136
193
  */
137
194
  getReport: async ({
138
- id
195
+ id,
196
+ dashboardId
139
197
  }) => {
198
+ // Use provided dashboardId or fall back to first cached dashboard
199
+ const cacheKey = dashboardId || getFirstCachedDashboardId();
200
+
201
+ // Check cache first
202
+ if (cacheKey && dashboardMetaCache[cacheKey]?.reports?.[id]) {
203
+ return dashboardMetaCache[cacheKey].reports[id];
204
+ }
205
+
206
+ // Fall back to HTTP request
140
207
  const {
141
208
  data
142
209
  } = await apiClient.get(`/entity/reports/${id}`);
@@ -146,12 +213,23 @@ const Api = {
146
213
  * Get report schema/metadata by ID
147
214
  * @param {Object} params - Parameters object
148
215
  * @param {string} params.id - Report ID
216
+ * @param {string} [params.dashboardId] - Optional dashboard ID for cache lookup
149
217
  * @returns {Promise} Axios response promise
150
218
  */
151
219
  getReportSchema: async ({
152
220
  id,
221
+ dashboardId,
153
222
  query = {}
154
223
  }) => {
224
+ // Use provided dashboardId or fall back to first cached dashboard
225
+ const cacheKey = dashboardId || getFirstCachedDashboardId();
226
+
227
+ // Check cache first
228
+ if (cacheKey && dashboardMetaCache[cacheKey]?.reportMetadata?.[id]) {
229
+ return dashboardMetaCache[cacheKey].reportMetadata[id];
230
+ }
231
+
232
+ // Fall back to HTTP request
155
233
  // console.log({getReportSchema:{id,query}});
156
234
  const {
157
235
  data
@@ -176,7 +254,18 @@ const Api = {
176
254
  } = await apiClient.post(`/reports/${id}/run`, query);
177
255
  return data;
178
256
  },
179
- getDateRanges: async () => {
257
+ getDateRanges: async ({
258
+ dashboardId
259
+ } = {}) => {
260
+ // Use provided dashboardId or fall back to first cached dashboard
261
+ const cacheKey = dashboardId || getFirstCachedDashboardId();
262
+
263
+ // Check cache first
264
+ if (cacheKey && dashboardMetaCache[cacheKey]?.dateRanges) {
265
+ return dashboardMetaCache[cacheKey].dateRanges;
266
+ }
267
+
268
+ // Fall back to HTTP request
180
269
  const {
181
270
  data
182
271
  } = await apiClient.get(`/globals/date-ranges`);
@@ -790,23 +879,6 @@ var Chart = ({
790
879
  setActiveViewIndex(viewIndex);
791
880
  setActiveView(chart.doc.view[viewIndex]);
792
881
  }, [chart]);
793
-
794
- // React.useEffect(() => {
795
- // let cron;
796
- // if (chart?.doc?.refresh?.enabled === true) {
797
- // cron = new CronJob(chart.doc.refresh.cron, () => {
798
- // if (inView) {
799
- // loadSource(filterRef.current);
800
- // }
801
- // });
802
- // cron.start();
803
- // }
804
-
805
- // return () => {
806
- // cron && cron.stop();
807
- // }
808
- // }, [chart, inView]);
809
-
810
882
  React__default.useEffect(() => {
811
883
  if (!id) return;
812
884
  init().catch(error => {
@@ -953,6 +1025,9 @@ function Dashboard({
953
1025
  useFilterManager(channel);
954
1026
  const init = async () => {
955
1027
  Api.setAuth(auth);
1028
+ await Api.loadDashboardMeta({
1029
+ dashboardId: id
1030
+ });
956
1031
 
957
1032
  // get dashboard entity
958
1033
  const dashboardTemp = await Api.getDashboard({