import React, { useEffect, useRef, useState } from 'react'; import './Sidebar.less'; import ReactTooltip from 'react-tooltip'; import { AiOutlineFund, AiOutlineSetting } from 'react-icons/ai'; interface SidebarProps { width?: number; height?: number; onAppSelected: (appId: string) => void; currentApp: string; layout: 'horizontal' | 'vertical'; } //SettingOutlined const mainApps = [ { id: 'explore', icon: 'icon-fontastic-search', name: 'Explore', subApps: [ { id: 'mat-explore', name: 'Materials Explorer', icon: 'icon-fontastic-search' }, { id: 'mol-explore', icon: 'icon-fontastic-phase-diagram', name: 'Molecule Explorer' }, { id: 'porus-explore', icon: 'icon-fontastic-nanoporous', name: 'Nanoporous Explorer' }, { id: 'battery-explore', icon: 'icon-fontastic-battery', name: 'Battery Explorer', svg: } ] }, { id: 'analzye', name: 'Analyze', svg: , subApps: [ { id: 'phase-diagram', icon: 'icon-fontastic-phase-diagram', name: 'Phase Diagram' }, { id: 'pourbaix-diagram', icon: 'icon-fontastic-pourbaix-diagram', name: 'Pourbaix Diagram' }, { id: 'reaction-calc', icon: 'icon-fontastic-reaction', name: 'Reaction Calculator' } ] }, { id: 'char', icon: 'icon-fontastic-xas', name: 'Characterize', subApps: [{ name: 'XAS Matcher', id: 'xas', icon: 'icon-fontastic-xas' }] }, { id: 'design', icon: 'icon-fontastic-toolkit', name: 'Design', subApps: [ { id: 'crystal', icon: 'icon-fontastic-toolkit', name: 'Crystal toolkit' }, { id: 'Structure Predictor', icon: 'icon-fontastic-struct-predictor', name: 'Structure Predictor' } ] }, { id: 'apply', name: 'Apply', svg: , subApps: [] } ]; function build_dico(dico, apps, parentId: string | null) { return apps.reduce((acc, app) => { acc[app.id] = app; parentId && (dico[app.id].parentId = parentId); app.subApps && build_dico(acc, app.subApps, app.id); return acc; }, dico); } const APP_DICO = build_dico({}, mainApps, null); const AppItem = ({ name, idx, icon, svg, selectedAppId }) => { let subApp: string | null = null; const selectedParentAppId = !!selectedAppId.length ? APP_DICO[selectedAppId].parentId : ''; if (!!selectedAppId.length && selectedParentAppId === idx) { icon = APP_DICO[selectedAppId].icon; svg = APP_DICO[selectedAppId].svg; subApp = APP_DICO[selectedAppId].name; } return ( {icon ? ( ) : ( {svg} )} {name} {subApp && {subApp} } ); }; export const Sidebar: React.FC = ({ width, currentApp, onAppSelected, layout, height }) => { const [currentAppId, setCurrentAppId] = useState(''); const tooltip = useRef(null); const isFirst = useRef(false); const sidebar = useRef(null); useEffect(() => { setCurrentAppId(currentApp ? currentApp : ''); }, [currentApp]); const setApp = (id) => { const app = APP_DICO[id]; if (app) { setCurrentAppId(app.id); tooltip.current!.state.show = false; isFirst.current = app.parentId === mainApps[0].id; ReactTooltip.hide(); onAppSelected(app.id); } else { console.error('incorrect app id', id); } }; return (
{ const app = APP_DICO[idx]; if (idx === mainApps[0].id) { tooltip.current!.state.extraClass = 'top'; } else { if (tooltip.current) tooltip.current!.state.extraClass = ''; } return ( <> {app && app.subApps && app.subApps.map((app) => ( setApp(app.id)} key={app.name} data-for="sidebar-menu" data-tip="8" className={`sidebar-menu-item ${currentAppId === app.id ? 'selected' : ''}`} > {app.icon ? ( ) : ( {app.svg} )} {app ? app.name : 'null'} ))} ); }} overridePosition={({ left, top }, currentEvent, currentTarget, node) => { const d = document.documentElement; if (d && node) { left = Math.min(d.clientWidth - node.clientWidth, left); top = Math.min(d.clientHeight - node.clientHeight, top); left = Math.max(0, left); top = Math.max(0, top); } return { top, left }; }} effect="solid" delayHide={50} delayShow={20} delayUpdate={200} border={true} type={'light'} />
{mainApps.map((app, idx) => ( ))}
); };