opendash
Version:
open.DASH data visualization framework
30 lines • 1.31 kB
JavaScript
import * as React from "react";
import { useStorage, useDataItems } from "..";
/**
* Use this hook to get the currently selected data item for the widget.
* It may come from two places:
*
* 1. If your widget requests the selection of an data item
* it may be the one selected by the user.
* 2. If your widget is placed on a linked dashboard, the linked
* data item may be returned.
*
* If your widget fits none of the above, the hook will throw an Error
*/
export function useWidgetDataItem() {
var _a, _b;
var items = useDataItems();
var _c = useStorage("device", "opendash:data:linked_item"), currentItem = _c[0], setCurrentItem = _c[1];
var currentItemId = (_a = currentItem) === null || _a === void 0 ? void 0 : _a[0];
var dimension = (_b = currentItem) === null || _b === void 0 ? void 0 : _b[1];
var value = React.useMemo(function () { return items.find(function (item) { return item.id === currentItemId; }); }, [items, currentItem]);
function setValue(item) {
if (Array.isArray(item) && item.length === 2) {
setCurrentItem(item);
return;
}
throw new Error("Bad usage of useWidgetDataItem");
}
return [value, dimension, setValue];
}
//# sourceMappingURL=useWidgetDataItem.js.map