import {fileDecoration, FileDecorationIcons} from '@remix-ui/file-decorators' import {CustomTooltip} from '@remix-ui/helper' import {Plugin} from '@remixproject/engine' import React, {useState, useRef, useEffect, useReducer} from 'react' // eslint-disable-line import {FormattedMessage} from 'react-intl' import {Tab, Tabs, TabList, TabPanel} from 'react-tabs' import './remix-ui-tabs.css' const _paq = (window._paq = window._paq || []) /* eslint-disable-next-line */ export interface TabsUIProps { tabs: Array plugin: Plugin onSelect: (index: number) => void onClose: (index: number) => void onZoomOut: () => void onZoomIn: () => void onReady: (api: any) => void themeQuality: string } export interface TabsUIApi { activateTab: (name: string) => void active: () => string } interface ITabsState { selectedIndex: number fileDecorations: fileDecoration[] currentExt: string } interface ITabsAction { type: string payload: any ext?: string } const initialTabsState: ITabsState = { selectedIndex: -1, fileDecorations: [], currentExt: '' } const tabsReducer = (state: ITabsState, action: ITabsAction) => { switch (action.type) { case 'SELECT_INDEX': return { ...state, currentExt: action.ext, selectedIndex: action.payload } case 'SET_FILE_DECORATIONS': return { ...state, fileDecorations: action.payload as fileDecoration[] } default: return state } } export const TabsUI = (props: TabsUIProps) => { const [tabsState, dispatch] = useReducer(tabsReducer, initialTabsState) const currentIndexRef = useRef(-1) const tabsRef = useRef({}) const tabsElement = useRef(null) const tabs = useRef(props.tabs) tabs.current = props.tabs // we do this to pass the tabs list to the onReady callbacks useEffect(() => { if (props.tabs[tabsState.selectedIndex]) { tabsRef.current[tabsState.selectedIndex].scrollIntoView({ behavior: 'smooth', block: 'center' }) } }, [tabsState.selectedIndex]) const getFileDecorationClasses = (tab: any) => { const fileDecoration = tabsState.fileDecorations.find((fileDecoration: fileDecoration) => { if (`${fileDecoration.workspace.name}/${fileDecoration.path}` === tab.name) return true }) return fileDecoration && fileDecoration.fileStateLabelClass } const getFileDecorationIcons = (tab: any) => { return } const renderTab = (tab, index) => { const classNameImg = 'my-1 mr-1 text-dark ' + tab.iconClass const classNameTab = 'nav-item nav-link d-flex justify-content-center align-items-center px-2 py-1 tab' + (index === currentIndexRef.current ? ' active' : '') const invert = props.themeQuality === 'dark' ? 'invert(1)' : 'invert(0)' return (
{ tabsRef.current[index] = el }} className={classNameTab} data-id={index === currentIndexRef.current ? 'tab-active' : ''} data-path={tab.name} > {tab.icon ? : } {tab.title} {getFileDecorationIcons(tab)} { props.onClose(index) event.stopPropagation() }} >
) } const active = () => { if (currentIndexRef.current < 0) return '' return tabs.current[currentIndexRef.current].name } const activateTab = (name: string) => { const index = tabs.current.findIndex((tab) => tab.name === name) currentIndexRef.current = index dispatch({type: 'SELECT_INDEX', payload: index, ext: getExt(name)}) } const setFileDecorations = (fileStates: fileDecoration[]) => { dispatch({type: 'SET_FILE_DECORATIONS', payload: fileStates}) } const transformScroll = (event) => { if (!event.deltaY) { return } event.currentTarget.scrollLeft += event.deltaY + event.deltaX event.preventDefault() } useEffect(() => { props.onReady({ activateTab, active, setFileDecorations }) return () => { tabsElement.current.removeEventListener('wheel', transformScroll) } }, []) const getExt = (path) => { const root = path.split('#')[0].split('?')[0] const ext = root.indexOf('.') !== -1 ? /[^.]+$/.exec(root) : null if (ext) return ext[0].toLowerCase() else return '' } return (
}> props.onZoomOut()}> }> props.onZoomIn()}>
{ if (tabsElement.current) return tabsElement.current = domEl tabsElement.current.addEventListener('wheel', transformScroll) }} onSelect={(index) => { props.onSelect(index) currentIndexRef.current = index dispatch({ type: 'SELECT_INDEX', payload: index, ext: getExt(props.tabs[currentIndexRef.current].name) }) }} > {props.tabs.map((tab, i) => ( {renderTab(tab, i)} ))}
{props.tabs.map((tab) => ( ))}
) } export default TabsUI