UNPKG

@gpa-gemstone/common-pages

Version:
145 lines (144 loc) 8.78 kB
"use strict"; //****************************************************************************************************** // TimeFilter.tsx - Gbtc // // Copyright © 2020, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 09/16/2021 - Christoph Lackner // Generated original version of source code. // 06/20/2024 - Ali Karrar // Moved TimeFilter from SEBrowser to gemstone //****************************************************************************************************** var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTimeWindowFromFilter = getTimeWindowFromFilter; var React = __importStar(require("react")); var TimeWindowUtils_1 = require("./TimeWindowUtils"); var moment_1 = __importDefault(require("moment")); var QuickSelects_1 = require("./QuickSelects"); var lodash_1 = __importDefault(require("lodash")); var StartEndFilter_1 = __importDefault(require("./StartEndFilter/StartEndFilter")); var helper_functions_1 = require("@gpa-gemstone/helper-functions"); var WindowFilter_1 = __importDefault(require("./WindowFilter/WindowFilter")); var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols"); var TimeFilter = function (props) { var _a, _b, _c, _d, _e; var containerRef = React.useRef(null); var width = (0, helper_functions_1.useGetContainerPosition)(containerRef).width; var format = (0, QuickSelects_1.getFormat)(props.format); var _f = React.useState(-1), activeQuickSelect = _f[0], setActiveQuickSelect = _f[1]; var _g = React.useState(getTimeWindowFromFilter(props.filter, format)), filter = _g[0], setFilter = _g[1]; var _h = React.useState(false), collapsed = _h[0], setCollapsed = _h[1]; //Effect to set parent filter when internal filter changes React.useEffect(function () { if (!isEqual(filter, props.filter)) { props.setFilter(filter.start, filter.end, filter.unit, filter.duration); } }, [filter]); //Effect to sync filter if external filter changes React.useEffect(function () { if (!isEqual(filter, props.filter)) { var flt = getTimeWindowFromFilter(props.filter, format); setFilter(flt); } }, [props.filter]); //Effect to push collapse state to parent React.useEffect(function () { if (props.setCollapsed == null) return; props.setCollapsed(collapsed); }, [collapsed]); // Checks typing of ITimeFilter and then compares to ITimeWindow var isEqual = function (timeWindow, timeFilter) { var flt = getTimeWindowFromFilter(timeFilter, format); return lodash_1.default.isEqual(timeWindow, flt); }; var helpMessaage = ((_a = props.showHelpMessage) !== null && _a !== void 0 ? _a : true) ? "All times shown are in system time (".concat(props.timeZone, ").") : undefined; return (React.createElement("fieldset", { className: "border", style: { padding: '10px', height: '100%', overflow: 'hidden' }, ref: containerRef }, React.createElement("legend", { className: "w-auto", style: { fontSize: 'large' } }, React.createElement("div", { className: "d-flex align-items-center" }, React.createElement("span", { className: "mr-2" }, "Date/Time Filter:"), ((_b = props.enableCollapse) !== null && _b !== void 0 ? _b : false) ? React.createElement("button", { type: "button", className: "btn p-0 ml-auto", onClick: function () { return setCollapsed(function (x) { return !x; }); } }, collapsed ? React.createElement(gpa_symbols_1.ReactIcons.ArrowDropDown, null) : React.createElement(gpa_symbols_1.ReactIcons.ArrowDropUp, null)) : null)), collapsed ? null : props.dateTimeSetting === 'startEnd' ? React.createElement(StartEndFilter_1.default, { TimeWindowFilter: filter, SetTimeWindowFilter: setFilter, Timezone: props.timeZone, ActiveQP: activeQuickSelect, SetActiveQP: setActiveQuickSelect, SetFilter: props.setFilter, Accuracy: props.accuracy, Format: format, DateUnit: (_c = props.format) !== null && _c !== void 0 ? _c : 'datetime-local', ShowQuickSelects: props.showQuickSelect, ContainerWidth: width, HelpMessage: helpMessaage }) : props.dateTimeSetting === 'startWindow' ? React.createElement(WindowFilter_1.default, { TimeWindowFilter: filter, SetTimeWindowFilter: setFilter, Timezone: props.timeZone, ActiveQP: activeQuickSelect, SetActiveQP: setActiveQuickSelect, SetFilter: props.setFilter, Accuracy: props.accuracy, Format: format, DateUnit: (_d = props.format) !== null && _d !== void 0 ? _d : 'datetime-local', ShowQuickSelects: props.showQuickSelect, ContainerWidth: width, HelpMessage: helpMessaage, Window: 'start' }) : React.createElement(WindowFilter_1.default, { TimeWindowFilter: filter, SetTimeWindowFilter: setFilter, Timezone: props.timeZone, ActiveQP: activeQuickSelect, SetActiveQP: setActiveQuickSelect, SetFilter: props.setFilter, Accuracy: props.accuracy, Format: format, DateUnit: (_e = props.format) !== null && _e !== void 0 ? _e : 'datetime-local', ShowQuickSelects: props.showQuickSelect, ContainerWidth: width, HelpMessage: helpMessaage, Window: 'end' }))); }; // Converts ITimeFilter to an ITimeWindow filter function getTimeWindowFromFilter(flt, format) { var start; var end; var unit; var duration; var formatFunction = function (start, end, unit, duration) { return ({ start: start.format(format), end: end.format(format), unit: unit, duration: duration, }); }; if ('start' in flt && 'duration' in flt) { // type is IStartDuration start = (0, moment_1.default)(flt.start, format); duration = flt.duration; unit = flt.unit; end = (0, TimeWindowUtils_1.addDuration)(start, duration, unit); return formatFunction(start, end, unit, duration); } if ('end' in flt && 'duration' in flt) { // type is IEndDuration end = (0, moment_1.default)(flt.end, format); duration = flt.duration; unit = flt.unit; start = (0, TimeWindowUtils_1.addDuration)(end, -duration, unit); return formatFunction(start, end, unit, duration); } if ('start' in flt && 'end' in flt) { // type is IStartEnd start = (0, moment_1.default)(flt.start, format); end = (0, moment_1.default)(flt.end, format); unit = (0, TimeWindowUtils_1.findAppropriateUnit)(start, end); duration = end.diff(start, unit); return formatFunction(start, end, unit, duration); } throw TypeError("Unexpected type in getTimeWindowFromFilter, filter is: ".concat(flt.toString())); } exports.default = TimeFilter;