import React, { useEffect, useMemo, useRef, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { Tooltip, Tabs, Input, NoContent, Icon, Toggler } from 'UI';
import { List, CellMeasurer, CellMeasurerCache, AutoSizer } from 'react-virtualized';
import { PlayerContext } from 'App/components/Session/playerContext';
import BottomBlock from '../BottomBlock';
import { useModal } from 'App/components/Modal';
import { useStore } from 'App/mstore';
import { typeList } from 'Types/session/stackEvent';
import StackEventRow from 'Shared/DevTools/StackEventRow';
import StackEventModal from '../StackEventModal';
import useAutoscroll from '../useAutoscroll';
import { useRegExListFilterMemo, useTabListFilterMemo } from '../useListFilter'
const INDEX_KEY = 'stackEvent';
const ALL = 'ALL';
const TAB_KEYS = [ ALL, ...typeList] as const
const TABS = TAB_KEYS.map((tab) => ({ text: tab, key: tab }))
function StackEventPanel() {
const { player, store } = React.useContext(PlayerContext)
const jump = (t: number) => player.jump(t)
const { stackList: list, stackListNow: listNow } = store.get()
const {
sessionStore: { devTools },
} = useStore();
const { showModal } = useModal();
const [isDetailsModalActive, setIsDetailsModalActive] = useState(false) // TODO:embed that into useModal
const filter = devTools[INDEX_KEY].filter
const activeTab = devTools[INDEX_KEY].activeTab)
const activeIndex = devTools[INDEX_KEY].index
let filteredList = useRegExListFilterMemo(list, it => it.name, filter)
filteredList = useTabListFilterMemo(filteredList, it => it.source, ALL, activeTab)
const onTabClick = (activeTab: typeof TAB_KEYS[number]) => devTools.update(INDEX_KEY, { activeTab })
const onFilterChange = ({ target: { value } }: any) => devTools.update(INDEX_KEY, { filter: value })
const tabs = useMemo(() =>
TABS.filter(({ key }) => key === ALL || list.some(({ source }) => key === source)),
[ list.length ],
)
const {
timeoutStartAutoscroll,
stopAutoscroll,
} = useAutoscroll(
activeIndex,
listNow.length,
index => devTools.update(INDEX_KEY, { index })
)
const onMouseEnter = stopAutoscroll
const onMouseLeave = () => {
if (isDetailsModalActive) { return }
timeoutStartAutoscroll()
}
const cache = new CellMeasurerCache({
fixedWidth: true,
keyMapper: (index: number) => filteredList[index],
});
const showDetails = (item: any) => {
setIsDetailsModalActive(true)
showModal(
,
{
right: true,
onClose: () => {
setIsDetailsModalActive(false)
timeoutStartAutoscroll()
}
}
)
devTools.update(INDEX_KEY, { index: filteredList.indexOf(item) })
stopAutoscroll()
}
const _list = React.useRef()
useEffect(() => {
if (_list.current) {
// @ts-ignore
_list.current.scrollToRow(activeIndex)
}
}, [ activeIndex ])
const _rowRenderer = ({ index, key, parent, style }: any) => {
const item = filteredList[index];
return (
// @ts-ignore
{() => (
{
stopAutoscroll()
devTools.update(INDEX_KEY, { index: filteredList.indexOf(item) });
jump(item.time);
}}
onClick={() => showDetails(item)}
/>
)}
);
}
return (
Stack Events
No Data
}
size="small"
show={filteredList.length === 0}
>
{({ height, width }: any) => (
)}
);
}
export default observer(StackEventPanel)