import React, { useEffect, useRef, useState, useMemo } from 'react';
import { LogLevel, ILog } from 'Player';
import BottomBlock from '../BottomBlock';
import { Tabs, Input, Icon, NoContent } from 'UI';
import cn from 'classnames';
import ConsoleRow from '../ConsoleRow';
import { PlayerContext } from 'App/components/Session/playerContext';
import { observer } from 'mobx-react-lite';
import { List, CellMeasurer, AutoSizer } from 'react-virtualized';
import { useStore } from 'App/mstore';
import ErrorDetailsModal from 'App/components/Dashboard/components/Errors/ErrorDetailsModal';
import { useModal } from 'App/components/Modal';
import useAutoscroll, { getLastItemTime } from '../useAutoscroll';
import { useRegExListFilterMemo, useTabListFilterMemo } from '../useListFilter'
import useCellMeasurerCache from 'App/hooks/useCellMeasurerCache'
const ALL = 'ALL';
const INFO = 'INFO';
const WARNINGS = 'WARNINGS';
const ERRORS = 'ERRORS';
const LEVEL_TAB = {
[LogLevel.INFO]: INFO,
[LogLevel.LOG]: INFO,
[LogLevel.WARN]: WARNINGS,
[LogLevel.ERROR]: ERRORS,
[LogLevel.EXCEPTION]: ERRORS,
} as const
const TABS = [ALL, ERRORS, WARNINGS, INFO].map((tab) => ({ text: tab, key: tab }));
function renderWithNL(s = '') {
if (typeof s !== 'string') return '';
return s.split('\n').map((line, i) =>
{line}
);
}
const getIconProps = (level: any) => {
switch (level) {
case LogLevel.INFO:
case LogLevel.LOG:
return {
name: 'console/info',
color: 'blue2',
};
case LogLevel.WARN:
return {
name: 'console/warning',
color: 'red2',
};
case LogLevel.ERROR:
return {
name: 'console/error',
color: 'red',
};
}
return null;
};
const INDEX_KEY = 'console';
function ConsolePanel() {
const {
sessionStore: { devTools },
} = useStore()
const filter = devTools[INDEX_KEY].filter;
const activeTab = devTools[INDEX_KEY].activeTab;
// Why do we need to keep index in the store? if we could get read of it it would simplify the code
const activeIndex = devTools[INDEX_KEY].index;
const [ isDetailsModalActive, setIsDetailsModalActive ] = useState(false);
const { showModal } = useModal();
const { player, store } = React.useContext(PlayerContext)
const jump = (t: number) => player.jump(t)
const { logList, exceptionsList, logListNow, exceptionsListNow } = store.get()
const list = useMemo(() =>
logList.concat(exceptionsList).sort((a, b) => a.time - b.time),
[ logList.length, exceptionsList.length ],
) as ILog[]
let filteredList = useRegExListFilterMemo(list, l => l.value, filter)
filteredList = useTabListFilterMemo(filteredList, l => LEVEL_TAB[l.level], ALL, activeTab)
const onTabClick = (activeTab: any) => devTools.update(INDEX_KEY, { activeTab })
const onFilterChange = ({ target: { value } }: any) => devTools.update(INDEX_KEY, { filter: value })
// AutoScroll
const [
timeoutStartAutoscroll,
stopAutoscroll,
] = useAutoscroll(
filteredList,
getLastItemTime(logListNow, exceptionsListNow),
activeIndex,
index => devTools.update(INDEX_KEY, { index })
)
const onMouseEnter = stopAutoscroll
const onMouseLeave = () => {
if (isDetailsModalActive) { return }
timeoutStartAutoscroll()
}
const _list = useRef(); // TODO: fix react-virtualized types & incapsulate scrollToRow logic
useEffect(() => {
if (_list.current) {
// @ts-ignore
_list.current.scrollToRow(activeIndex);
}
}, [activeIndex]);
const cache = useCellMeasurerCache(filteredList)
const showDetails = (log: any) => {
setIsDetailsModalActive(true);
showModal(
,
{
right: true,
onClose: () => {
setIsDetailsModalActive(false)
timeoutStartAutoscroll()
}
});
devTools.update(INDEX_KEY, { index: filteredList.indexOf(log) });
stopAutoscroll()
}
const _rowRenderer = ({ index, key, parent, style }: any) => {
const item = filteredList[index];
return (
{/* @ts-ignore */}
{({ measure }: any) => (
showDetails(item)}
recalcHeight={() => {
measure();
(_list as any).current.recomputeRowHeights(index);
}}
/>
)}
)
}
return (
{/* @ts-ignore */}
Console
{/* @ts-ignore */}
{/* @ts-ignore */}
No Data
}
size="small"
show={filteredList.length === 0}
>
{/* @ts-ignore */}
{({ height, width }: any) => (
// @ts-ignore
)}
{/* @ts-ignore */}
);
}
export default observer(ConsolePanel);