UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

128 lines 5.56 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.useUniquePropertyValuesLoader = useUniquePropertyValuesLoader; require("../../common/DisposePolyfill.js"); const react_1 = require("react"); const rxjs_1 = require("rxjs"); const presentation_common_1 = require("@itwin/presentation-common"); const presentation_frontend_1 = require("@itwin/presentation-frontend"); const Utils_js_1 = require("../../common/Utils.js"); const ItemsLoader_js_1 = require("./ItemsLoader.js"); /** @internal */ function useUniquePropertyValuesLoader({ imodel, ruleset, field, descriptorInputKeys, typeName, filterText, selectedValues, }) { const [itemsLoader, setItemsLoader] = (0, react_1.useState)(); const [initialSelectedValues] = (0, react_1.useState)(selectedValues); const [state, setLoadedOptions] = (0, react_1.useState)({ options: [], isLoading: false, }); // Get initial loader and values (0, react_1.useEffect)(() => { setLoadedOptions({ options: [], isLoading: false }); if (!ruleset || !field) { return; } const loader = new ItemsLoader_js_1.ItemsLoader(() => { setLoadedOptions((prev) => ({ ...prev, isLoading: true })); }, (newItems) => { setLoadedOptions((prev) => ({ options: [...prev.options, ...newItems], isLoading: false, })); }, async (offset) => getItems({ imodel, offset, field: field.getFieldDescriptor(), ruleset, keys: new presentation_common_1.KeySet(descriptorInputKeys) }), (option) => option.displayValue); void loader.loadMatchingItems(initialSelectedValues); setItemsLoader(loader); return () => { loader[Symbol.dispose](); }; }, [imodel, ruleset, field, descriptorInputKeys, initialSelectedValues]); // On filter text change, check if more items need to be loaded (0, react_1.useEffect)(() => { if (!filterText || !itemsLoader) { return; } const timeout = setTimeout(() => { void itemsLoader.loadItems(filterText); }, 250); return () => { clearTimeout(timeout); }; }, [itemsLoader, filterText]); return { selectOptions: (0, react_1.useMemo)(() => { const options = state.options.map((option) => { return { label: formatOptionLabel(option.displayValue, typeName), value: option.displayValue, }; }); if (options.length >= ItemsLoader_js_1.VALUE_BATCH_SIZE) { options.push(ItemsLoader_js_1.FILTER_WARNING_OPTION); } return options; }, [state.options, typeName]), loadedOptions: state.options, isLoading: state.isLoading, }; } function formatOptionLabel(displayValue, type) { if (displayValue === "") { return (0, Utils_js_1.translate)("unique-values-property-editor.empty-value"); } switch (type) { case "dateTime": return new Date(displayValue).toLocaleString(undefined, { year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", fractionalSecondDigits: 3, }); case "shortDate": return new Date(displayValue).toLocaleDateString(); default: return displayValue; } } async function getItems({ imodel, field, offset, ruleset, keys, }) { const requestProps = { imodel, descriptor: {}, fieldDescriptor: field, rulesetOrId: ruleset, paging: { start: offset, size: ItemsLoader_js_1.VALUE_BATCH_SIZE }, keys, }; const items = await new Promise((resolve) => { (presentation_frontend_1.Presentation.presentation.getDistinctValuesIterator ? (0, rxjs_1.from)(presentation_frontend_1.Presentation.presentation.getDistinctValuesIterator(requestProps)).pipe((0, rxjs_1.mergeMap)((result) => result.items), (0, rxjs_1.toArray)()) : // eslint-disable-next-line @typescript-eslint/no-deprecated (0, rxjs_1.from)(presentation_frontend_1.Presentation.presentation.getPagedDistinctValues(requestProps)).pipe((0, rxjs_1.map)((result) => result.items))).subscribe({ next: resolve, error: () => resolve([]), }); }); const hasMore = items.length === ItemsLoader_js_1.VALUE_BATCH_SIZE; const options = []; for (const option of items) { if (option.displayValue === undefined || !presentation_common_1.DisplayValue.isPrimitive(option.displayValue)) { continue; } const groupedValues = option.groupedRawValues.filter((value) => value !== undefined); if (groupedValues.length !== 0) { options.push({ displayValue: option.displayValue, groupedRawValues: groupedValues }); } } return { options, length: items.length, hasMore, }; } //# sourceMappingURL=UseUniquePropertyValuesLoader.js.map