import React, { useMemo } from 'react' import Taro from '@tarojs/taro' import { Text, View } from '@tarojs/components' import { ArrowLeft } from '@nutui/icons-react-taro' import { isSkyline } from '@/utils/taro/is-skyline' import './header.scss' import config from '../../config.json' interface HeaderProps { title?: string } const Header: React.FC = ({ title }) => { const isWeb = Taro.getEnv() === 'WEB' const navigateTo = () => { if (isWeb) { Taro.navigateBack({ delta: 1 }) return } try { const pages = Taro.getCurrentPages ? Taro.getCurrentPages() : [] if (pages.length > 1) { Taro.navigateBack({ delta: 1 }) } else { Taro.reLaunch({ url: '/pages/index/index' }) } } catch (e) { Taro.navigateBack({ delta: 1 }) } } const compTitle = useMemo(() => { if (title) return title let hashCompName = '' if (isWeb) { if (typeof location !== 'undefined' && location.hash) { const parts = location.hash.split('pages/') if (parts[1]) { hashCompName = parts[1].replace('/index', '').toLowerCase() } } } else { try { const pages = Taro.getCurrentPages ? Taro.getCurrentPages() : [] const curr = pages[pages.length - 1] const route = curr?.route || Taro.getCurrentInstance()?.router?.path || '' const match = route.match(/pages\/([^/]+)\/index/) if (match && match[1]) { hashCompName = match[1].toLowerCase() } } catch (e) {} } if (!hashCompName) return '' let allComps: any[] = [] config.nav.forEach((item: any) => { if (item.packages) { allComps = [...allComps, ...item.packages] } }) const targetComp = allComps.find( (item: any) => hashCompName === item.name.toLowerCase() ) return ( targetComp?.name || hashCompName.charAt(0).toUpperCase() + hashCompName.slice(1) ) }, [isWeb, title]) const navStyles = useMemo(() => { if (isWeb) return { statusBarHeight: 0, navBarHeight: 57 } let statusBarHeight = 44 let navBarHeight = 44 try { const windowInfo = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync() if (windowInfo?.statusBarHeight) { statusBarHeight = windowInfo.statusBarHeight } if (Taro.getMenuButtonBoundingClientRect) { const rect = Taro.getMenuButtonBoundingClientRect() if (rect && rect.top && rect.height) { navBarHeight = (rect.top - statusBarHeight) * 2 + rect.height } } } catch (e) {} return { statusBarHeight, navBarHeight, } }, [isWeb]) const skylineMode = useMemo(() => isSkyline(), []) if (isWeb) { return (
{compTitle}
) } return ( {compTitle} {skylineMode ? 'Skyline' : 'WebView'} ) } export default Header