@richardmcquiston01/dashboard-widgets-toolkit 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.cjs CHANGED
@@ -2068,6 +2068,9 @@ function viewChoices(item) {
2068
2068
  }))
2069
2069
  ];
2070
2070
  }
2071
+ function isProviderView(view) {
2072
+ return view.convert === void 0;
2073
+ }
2071
2074
  function isKnownView(item, key) {
2072
2075
  return key !== void 0 && (key === BASE_VIEW_KEY || findView(item, key) !== void 0);
2073
2076
  }
@@ -2077,6 +2080,14 @@ function effectiveViewKey(item, chosen) {
2077
2080
  if (isKnownView(item, item.defaultView)) return item.defaultView;
2078
2081
  return BASE_VIEW_KEY;
2079
2082
  }
2083
+ function loadViewKey(item, chosen) {
2084
+ const key = effectiveViewKey(item, chosen);
2085
+ const view = findView(item, key);
2086
+ return view !== void 0 && isProviderView(view) ? key : BASE_VIEW_KEY;
2087
+ }
2088
+ function loadViewKind(item, chosen) {
2089
+ return findView(item, loadViewKey(item, chosen))?.kind ?? item.kind;
2090
+ }
2080
2091
  function cellValue2(cell) {
2081
2092
  const value = cell?.value;
2082
2093
  return typeof value === "number" && Number.isFinite(value) ? value : void 0;
@@ -2211,6 +2222,11 @@ function convertView(widgetKey, view, data, options = {}) {
2211
2222
  where: `Widget ${JSON.stringify(widgetKey)} view ${JSON.stringify(view.key)}`,
2212
2223
  ...options.locale === void 0 ? {} : { locale: options.locale }
2213
2224
  };
2225
+ if (conversion === void 0) {
2226
+ return err(
2227
+ `${context.where}: the provider supplies this view, so there is nothing to convert.`
2228
+ );
2229
+ }
2214
2230
  const expected = VIEW_CONVERSIONS[conversion.type]?.from;
2215
2231
  if (expected === void 0) {
2216
2232
  return err(
@@ -2238,14 +2254,29 @@ function convertView(widgetKey, view, data, options = {}) {
2238
2254
  function applyView(widget, chosen, options = {}) {
2239
2255
  const key = effectiveViewKey(widget.definition, chosen);
2240
2256
  const view = findView(widget.definition, key);
2241
- if (view === void 0) return widget;
2257
+ const wanted = loadViewKey(widget.definition, chosen);
2258
+ const loaded = widget.status === "ok" ? widget.view ?? BASE_VIEW_KEY : void 0;
2259
+ const mismatched = loaded !== void 0 && loaded !== wanted;
2260
+ if (view === void 0) {
2261
+ return mismatched ? {
2262
+ definition: widget.definition,
2263
+ status: "loading",
2264
+ ...widget.options === void 0 ? {} : { options: widget.options }
2265
+ } : widget;
2266
+ }
2242
2267
  const definition = {
2243
2268
  ...widget.definition,
2244
2269
  kind: view.kind
2245
2270
  };
2246
- if (widget.status === "loading") return { ...widget, definition };
2247
- if (widget.status === "empty") return { ...widget, definition };
2248
- if (widget.status === "error") return { ...widget, definition };
2271
+ if (widget.status !== "ok") return { ...widget, definition };
2272
+ if (mismatched) {
2273
+ return {
2274
+ definition,
2275
+ status: "loading",
2276
+ ...widget.options === void 0 ? {} : { options: widget.options }
2277
+ };
2278
+ }
2279
+ if (isProviderView(view)) return { ...widget, definition };
2249
2280
  const converted = convertView(
2250
2281
  widget.definition.key,
2251
2282
  view,
@@ -5679,7 +5710,10 @@ function resolvePayload(definition, payload, options = {}) {
5679
5710
  } else {
5680
5711
  const result = validateWidgetData(payload, {
5681
5712
  widgetKey: definition.key,
5682
- expectedKind: definition.kind
5713
+ expectedKind: loadViewKind(
5714
+ definition,
5715
+ options.viewValues?.[definition.key]
5716
+ )
5683
5717
  });
5684
5718
  if (!result.ok) {
5685
5719
  return { definition, status: "error", error: result.error };
@@ -5690,7 +5724,14 @@ function resolvePayload(definition, payload, options = {}) {
5690
5724
  if (emptyText !== null) {
5691
5725
  return { definition, status: "empty", emptyText };
5692
5726
  }
5693
- return { definition, status: "ok", data: checked };
5727
+ return {
5728
+ definition,
5729
+ status: "ok",
5730
+ data: checked,
5731
+ ...hasViews(definition) ? {
5732
+ view: loadViewKey(definition, options.viewValues?.[definition.key])
5733
+ } : {}
5734
+ };
5694
5735
  }
5695
5736
  function failedWidget(definition, error) {
5696
5737
  return { definition, status: "error", error };
@@ -5700,7 +5741,14 @@ function loadingWidgets(definitions) {
5700
5741
  }
5701
5742
  function cacheKeyFor(definition, options, values) {
5702
5743
  const base = options.cacheKey === void 0 ? definition.sourceKey ?? definition.key : options.cacheKey(definition);
5703
- return values === void 0 ? base : `${base}${optionsCacheSuffix(definition, values, options)}`;
5744
+ const view = loadViewKey(
5745
+ definition,
5746
+ options.viewValues?.[definition.key]
5747
+ );
5748
+ const viewPart = view === BASE_VIEW_KEY ? "" : `view=${encodeURIComponent(view)}`;
5749
+ const optionPart = values === void 0 ? "" : optionsCacheSuffix(definition, values, options);
5750
+ if (viewPart === "") return `${base}${optionPart}`;
5751
+ return optionPart === "" ? `${base}?${viewPart}` : `${base}?${viewPart}&${optionPart.slice(1)}`;
5704
5752
  }
5705
5753
  function optionValuesFor(definition, options) {
5706
5754
  return resolveOptionValues(
@@ -5711,6 +5759,9 @@ function optionValuesFor(definition, options) {
5711
5759
  }
5712
5760
  function finalizeWidget(widget, values) {
5713
5761
  if (Object.keys(values).length === 0) return widget;
5762
+ if (widget.status === "ok" && widget.view !== void 0 && widget.view !== BASE_VIEW_KEY) {
5763
+ return { ...widget, options: values };
5764
+ }
5714
5765
  if (widget.status === "ok") {
5715
5766
  const data = applyClientOptions(
5716
5767
  widget.definition,
@@ -5761,7 +5812,8 @@ async function resolveWidgetUnapplied(definition, provider, context, options = {
5761
5812
  if (controller.signal.aborted) throw new LoadInterruption();
5762
5813
  return provider(context, definition, {
5763
5814
  signal: controller.signal,
5764
- options: values
5815
+ options: values,
5816
+ view: loadViewKey(definition, options.viewValues?.[definition.key])
5765
5817
  });
5766
5818
  }),
5767
5819
  interrupted
@@ -5809,9 +5861,18 @@ function createWidgetLoader(options) {
5809
5861
  ([key, values]) => values === void 0 ? [] : [[key, values]]
5810
5862
  )
5811
5863
  );
5864
+ const chosenViews = new Map(
5865
+ Object.entries(baseOptions.viewValues ?? {}).flatMap(
5866
+ ([key, view]) => view === void 0 ? [] : [[key, view]]
5867
+ )
5868
+ );
5812
5869
  const resolveOptions = baseOptions;
5813
5870
  function currentOptions() {
5814
- return { ...resolveOptions, optionValues: Object.fromEntries(chosen) };
5871
+ return {
5872
+ ...resolveOptions,
5873
+ optionValues: Object.fromEntries(chosen),
5874
+ viewValues: Object.fromEntries(chosenViews)
5875
+ };
5815
5876
  }
5816
5877
  const unapplied = /* @__PURE__ */ new Map();
5817
5878
  const visible = widgetsFor(definitions, context.roles);
@@ -5944,6 +6005,23 @@ function createWidgetLoader(options) {
5944
6005
  }
5945
6006
  start(definition, true);
5946
6007
  }
6008
+ function setView(key, view) {
6009
+ if (disposed) return;
6010
+ const definition = definitionFor(key);
6011
+ if (definition === void 0) return;
6012
+ const before = loadViewKey(definition, chosenViews.get(key));
6013
+ if (view === void 0 || view === "") chosenViews.delete(key);
6014
+ else chosenViews.set(key, view);
6015
+ const after = loadViewKey(definition, chosenViews.get(key));
6016
+ if (before === after || !started.has(key)) return;
6017
+ const current = snapshot.find(
6018
+ (widget) => widget.definition.key === key
6019
+ );
6020
+ if (current !== void 0 && current.status !== "loading") {
6021
+ replace(key, { definition, status: "loading" });
6022
+ }
6023
+ start(definition, true);
6024
+ }
5947
6025
  function dispose() {
5948
6026
  disposed = true;
5949
6027
  abortAll(new Error("The widget loader was disposed."));
@@ -5962,6 +6040,7 @@ function createWidgetLoader(options) {
5962
6040
  loadAll,
5963
6041
  refresh,
5964
6042
  setOptions,
6043
+ setView,
5965
6044
  dispose
5966
6045
  };
5967
6046
  }
@@ -5977,7 +6056,8 @@ function useWidgets(definitions, providers, context, options = {}) {
5977
6056
  signal,
5978
6057
  cache,
5979
6058
  cacheKey,
5980
- optionValues
6059
+ optionValues,
6060
+ viewValues
5981
6061
  } = options;
5982
6062
  const placeholders = react.useMemo(
5983
6063
  () => loadingWidgets(widgetsFor(definitions, context.roles)),
@@ -5989,6 +6069,8 @@ function useWidgets(definitions, providers, context, options = {}) {
5989
6069
  const gridRef = react.useRef(null);
5990
6070
  const optionValuesRef = react.useRef(optionValues);
5991
6071
  optionValuesRef.current = optionValues;
6072
+ const viewValuesRef = react.useRef(viewValues);
6073
+ viewValuesRef.current = viewValues;
5992
6074
  react.useEffect(() => {
5993
6075
  const created = createWidgetLoader({
5994
6076
  definitions,
@@ -5999,7 +6081,8 @@ function useWidgets(definitions, providers, context, options = {}) {
5999
6081
  ...signal === void 0 ? {} : { signal },
6000
6082
  ...cache === void 0 ? {} : { cache },
6001
6083
  ...cacheKey === void 0 ? {} : { cacheKey },
6002
- ...optionValuesRef.current === void 0 ? {} : { optionValues: optionValuesRef.current }
6084
+ ...optionValuesRef.current === void 0 ? {} : { optionValues: optionValuesRef.current },
6085
+ ...viewValuesRef.current === void 0 ? {} : { viewValues: viewValuesRef.current }
6003
6086
  });
6004
6087
  loaderRef.current = created;
6005
6088
  setWidgets(created.getSnapshot());
@@ -6078,6 +6161,16 @@ function useWidgets(definitions, providers, context, options = {}) {
6078
6161
  if (chosen !== void 0) loader.setOptions(key, chosen);
6079
6162
  }
6080
6163
  }, [loader, optionSignature]);
6164
+ const viewSignature = JSON.stringify(viewValues ?? {});
6165
+ react.useEffect(() => {
6166
+ if (loader === null) return;
6167
+ for (const [key, view] of Object.entries(viewValuesRef.current ?? {})) {
6168
+ loader.setView(key, view);
6169
+ }
6170
+ }, [loader, viewSignature]);
6171
+ const setView = react.useCallback((key, view) => {
6172
+ loaderRef.current?.setView(key, view);
6173
+ }, []);
6081
6174
  const setOptions = react.useCallback(
6082
6175
  (key, chosen) => {
6083
6176
  loaderRef.current?.setOptions(key, chosen);
@@ -6087,7 +6180,7 @@ function useWidgets(definitions, providers, context, options = {}) {
6087
6180
  const refresh = react.useCallback((key) => {
6088
6181
  loaderRef.current?.refresh(key);
6089
6182
  }, []);
6090
- return { widgets, refresh, setOptions, gridRef };
6183
+ return { widgets, refresh, setOptions, setView, gridRef };
6091
6184
  }
6092
6185
 
6093
6186
  exports.AlertListWidget = AlertListWidget;