UNPKG

@yuntijs/ui

Version:

☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps

147 lines (146 loc) 5.87 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["className", "id", "style", "height", "onScroll", "url", "websocket", "refreshInterval", "onLoad", "onError"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { Icon } from '@lobehub/ui'; import { LazyLog, ScrollFollow } from '@melloware/react-logviewer'; import { ArrowDown, ArrowUp, LoaderCircle, TextSearch } from 'lucide-react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useStyles } from "./style"; import { jsx as _jsx } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; export var LogViewer = function LogViewer(_ref) { var className = _ref.className, id = _ref.id, style = _ref.style, height = _ref.height, _onScroll = _ref.onScroll, urlFromProps = _ref.url, websocket = _ref.websocket, refreshInterval = _ref.refreshInterval, onLoad = _ref.onLoad, onError = _ref.onError, props = _objectWithoutProperties(_ref, _excluded); var _useStyles = useStyles(), cx = _useStyles.cx, styles = _useStyles.styles; var _useState = useState(0), _useState2 = _slicedToArray(_useState, 2), urlHash = _useState2[0], setUrlHash = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), loading = _useState4[0], setLoading = _useState4[1]; var _useState5 = useState(false), _useState6 = _slicedToArray(_useState5, 2), followAfterLoaded = _useState6[0], setFollowAfterLoaded = _useState6[1]; var setLoadingTimeout = useRef(null); var setFollowAfterLoadedTimeout = useRef(null); useEffect(function () { return function () { if (setLoadingTimeout.current) { clearTimeout(setLoadingTimeout.current); } if (setFollowAfterLoadedTimeout.current) { clearTimeout(setFollowAfterLoadedTimeout.current); } }; }, []); useEffect(function () { var intervalTimeout; if (urlFromProps && !websocket && refreshInterval) { intervalTimeout = setInterval(function () { setUrlHash(function (hash) { return hash + 1; }); }, refreshInterval); } return function () { clearInterval(intervalTimeout); }; }, [refreshInterval, urlFromProps, websocket]); var url = useMemo(function () { if (!urlFromProps) { return urlFromProps; } // workaround for onLoad exec twice setLoadingTimeout.current = setTimeout(function () { setLoading(true); setLoadingTimeout.current = null; }, 50); return "".concat(urlFromProps, "#").concat(urlHash); }, [urlFromProps, urlHash]); var handleOnLoad = useCallback(function () { // if has log line clear setLoading timeout if (document.querySelector('.react-lazylog .log-line') && setLoadingTimeout.current) { clearTimeout(setLoadingTimeout.current); } setLoading(false); // scroll to bottom setFollowAfterLoaded(true); setFollowAfterLoadedTimeout.current = setTimeout(function () { return setFollowAfterLoaded(false); }, 100); onLoad === null || onLoad === void 0 || onLoad(); }, [onLoad]); var handleOnError = useCallback(function (error) { setLoading(false); if (setLoadingTimeout.current) { clearTimeout(setLoadingTimeout.current); } onError === null || onError === void 0 || onError(error); }, [onError]); return /*#__PURE__*/_jsxs("div", { className: cx(styles.root, className), id: id, style: _objectSpread({ height: height }, style), children: [/*#__PURE__*/_jsx(ScrollFollow, { render: function render(_ref2) { var follow = _ref2.follow, onFollowScroll = _ref2.onScroll; return /*#__PURE__*/_jsx(LazyLog, _objectSpread(_objectSpread({ url: url, websocket: websocket }, props), {}, { follow: followAfterLoaded || follow, height: height, iconFilterLines: /*#__PURE__*/_jsx(Icon, { className: styles.searchBarIcon, icon: TextSearch }), iconFindNext: /*#__PURE__*/_jsx(Icon, { className: styles.searchBarIcon, icon: ArrowDown }), iconFindPrevious: /*#__PURE__*/_jsx(Icon, { className: styles.searchBarIcon, icon: ArrowUp }), onError: handleOnError, onLoad: handleOnLoad, onScroll: function onScroll(args) { onFollowScroll(args); _onScroll === null || _onScroll === void 0 || _onScroll(args); } })); }, startFollowing: true }), loading && /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx("div", { className: styles.loaderText, children: "loading ..." }), /*#__PURE__*/_jsx(Icon, { className: styles.loaderIcon, icon: LoaderCircle, spin: true })] })] }); };