metacubexd/src/components/Header.tsx

215 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-09-01 23:25:37 +08:00
import { useI18n } from '@solid-primitives/i18n'
2023-08-27 22:25:41 +08:00
import { A, useLocation, useNavigate } from '@solidjs/router'
2023-08-24 04:20:53 +08:00
import {
2023-08-30 00:22:40 +08:00
IconArrowsExchange,
2023-08-24 04:20:53 +08:00
IconFileStack,
IconGlobe,
2023-09-02 16:58:22 +08:00
IconGlobeFilled,
2023-08-24 04:20:53 +08:00
IconHome,
2023-09-01 23:57:58 +08:00
IconLanguage,
2023-08-30 00:22:40 +08:00
IconMenu,
2023-08-24 04:20:53 +08:00
IconNetwork,
2023-08-28 22:28:30 +08:00
IconPalette,
2023-08-24 04:20:53 +08:00
IconRuler,
IconSettings,
} from '@tabler/icons-solidjs'
import { For, ParentComponent, Show, createMemo, createSignal } from 'solid-js'
2023-08-31 02:21:41 +08:00
import { twMerge } from 'tailwind-merge'
import { Button } from '~/components'
2023-09-03 03:36:12 +08:00
import { LANG, ROUTES, themes } from '~/constants'
import {
renderProxiesInSamePage,
setCurTheme,
setSelectedEndpoint,
useProxies,
} from '~/signals'
2023-08-24 04:20:53 +08:00
2023-08-29 00:03:32 +08:00
const Nav: ParentComponent<{ href: string; tooltip: string }> = ({
href,
tooltip,
children,
}) => (
<li class="tooltip tooltip-bottom" data-tip={tooltip}>
<A class="rounded-box" href={href}>
2023-08-29 00:03:32 +08:00
{children}
</A>
</li>
)
2023-08-30 00:22:40 +08:00
const ThemeSwitcher = () => (
<div class="drawer drawer-end w-auto sm:ml-auto">
<input id="themes" type="checkbox" class="drawer-toggle" />
2023-08-24 04:20:53 +08:00
2023-08-30 00:22:40 +08:00
<div class="drawer-content flex items-center">
<label
for="themes"
class="btn btn-circle btn-primary drawer-button btn-sm"
>
<IconPalette />
</label>
</div>
2023-08-24 04:20:53 +08:00
2023-08-30 00:22:40 +08:00
<div class="drawer-side">
<label for="themes" class="drawer-overlay" />
2023-08-24 04:20:53 +08:00
<ul class="menu rounded-l-box gap-2 bg-base-300 p-2 shadow">
2023-08-30 00:22:40 +08:00
<For each={themes}>
{(theme) => (
<li
data-theme={theme}
class="btn btn-xs"
onClick={() => setCurTheme(theme)}
>
{theme}
</li>
)}
</For>
</ul>
</div>
</div>
)
2023-08-24 04:20:53 +08:00
2023-08-30 00:22:40 +08:00
export const Header = () => {
2023-09-01 23:57:58 +08:00
const [t, { locale }] = useI18n()
2023-09-02 20:32:27 +08:00
const { proxyProviders } = useProxies()
2023-09-02 16:58:22 +08:00
const navs = createMemo(() => {
const list = [
{
2023-09-03 03:36:12 +08:00
href: ROUTES.Overview,
2023-09-02 16:58:22 +08:00
name: t('overview'),
icon: <IconHome />,
},
{
2023-09-03 03:36:12 +08:00
href: ROUTES.Proxies,
2023-09-02 16:58:22 +08:00
name: t('proxies'),
icon: <IconGlobe />,
},
{
2023-09-03 03:36:12 +08:00
href: ROUTES.Rules,
2023-09-02 16:58:22 +08:00
name: t('rules'),
icon: <IconRuler />,
},
{
2023-09-03 03:36:12 +08:00
href: ROUTES.Conns,
2023-09-02 16:58:22 +08:00
name: t('connections'),
icon: <IconNetwork />,
},
{
2023-09-03 03:36:12 +08:00
href: ROUTES.Log,
2023-09-02 16:58:22 +08:00
name: t('logs'),
icon: <IconFileStack />,
},
{
2023-09-03 03:36:12 +08:00
href: ROUTES.Config,
2023-09-02 16:58:22 +08:00
name: t('config'),
icon: <IconSettings />,
},
]
if (proxyProviders().length > 0 && !renderProxiesInSamePage()) {
2023-09-02 16:58:22 +08:00
list.splice(2, 0, {
2023-09-03 20:23:02 +08:00
href: ROUTES.ProxyProvider,
2023-09-02 16:58:22 +08:00
name: t('proxyProviders'),
icon: <IconGlobeFilled />,
})
}
2023-09-01 23:25:37 +08:00
2023-09-02 16:58:22 +08:00
return list
})
2023-09-01 23:25:37 +08:00
2023-08-30 00:22:40 +08:00
const location = useLocation()
const navigate = useNavigate()
2023-08-24 04:20:53 +08:00
2023-08-31 02:21:41 +08:00
const [openedDrawer, setOpenedDrawer] = createSignal(false)
2023-08-30 00:22:40 +08:00
const onSwitchEndpointClick = () => {
setSelectedEndpoint('')
2023-09-04 13:08:05 +08:00
navigate(ROUTES.Setup)
2023-08-30 00:22:40 +08:00
}
2023-08-24 04:20:53 +08:00
2023-08-30 00:22:40 +08:00
return (
2023-08-31 01:30:08 +08:00
<ul class="navbar rounded-box sticky inset-x-0 top-2 z-10 mx-2 mt-2 flex w-auto items-center justify-center bg-base-300 px-4">
<div class="navbar-start gap-4">
2023-08-31 02:21:41 +08:00
<div class={twMerge('drawer w-auto lg:hidden', '')}>
<input
id="navs"
type="checkbox"
class="drawer-toggle"
onChange={(e) => setOpenedDrawer(e.target.checked)}
checked={openedDrawer()}
/>
2023-08-24 04:20:53 +08:00
2023-08-30 00:22:40 +08:00
<div class="drawer-content flex items-center">
<label for="navs" class="btn btn-circle drawer-button btn-sm">
<IconMenu />
</label>
</div>
2023-08-27 22:25:41 +08:00
2023-08-30 00:22:40 +08:00
<div class="drawer-side">
<label for="navs" class="drawer-overlay" />
2023-08-27 22:25:41 +08:00
<ul class="menu rounded-r-box min-h-full w-2/5 gap-2 bg-base-300 pt-20 shadow">
2023-08-30 00:22:40 +08:00
<For each={navs()}>
{({ href, name }) => (
2023-08-31 02:21:41 +08:00
<li onClick={() => setOpenedDrawer(false)}>
2023-08-30 00:22:40 +08:00
<A href={href}>{name}</A>
</li>
)}
</For>
</ul>
</div>
2023-08-27 22:25:41 +08:00
</div>
2023-08-30 00:22:40 +08:00
<a
2023-09-04 13:03:38 +08:00
class="flex whitespace-nowrap text-xl font-bold uppercase"
2023-08-30 00:22:40 +08:00
href="https://github.com/metacubex/metacubexd"
target="_blank"
>
2023-09-04 13:03:38 +08:00
<span class="mr-2">metacube, </span>
<div class="transition-transform hover:rotate-90 hover:scale-125">
xd
</div>
2023-08-30 00:22:40 +08:00
</a>
</div>
2023-09-04 13:08:05 +08:00
<Show when={location.pathname !== ROUTES.Setup}>
2023-08-30 00:22:40 +08:00
<div class="navbar-center hidden lg:flex">
2023-08-31 01:30:08 +08:00
<ul class="menu menu-horizontal gap-2">
2023-08-30 00:22:40 +08:00
<For each={navs()}>
{({ href, name, icon }) => (
<Nav href={href} tooltip={name}>
{icon}
</Nav>
2023-08-27 22:25:41 +08:00
)}
</For>
</ul>
</div>
2023-08-30 00:22:40 +08:00
</Show>
<div class="navbar-end">
<div class="flex items-center gap-2">
<Button
class="btn-circle btn-sm"
2023-09-01 23:57:58 +08:00
onClick={() => {
const curLocale = locale()
2023-09-02 13:50:24 +08:00
locale(curLocale === LANG.EN ? LANG.ZH : LANG.EN)
2023-09-01 23:57:58 +08:00
}}
>
<IconLanguage />
</Button>
2023-09-01 23:57:58 +08:00
2023-08-30 00:22:40 +08:00
<ThemeSwitcher />
<Button
class="btn-circle btn-secondary btn-sm"
2023-08-30 00:22:40 +08:00
onClick={onSwitchEndpointClick}
>
<IconArrowsExchange />
</Button>
2023-08-30 00:22:40 +08:00
</div>
2023-08-27 22:25:41 +08:00
</div>
2023-08-28 22:28:30 +08:00
</ul>
2023-08-24 04:20:53 +08:00
)
}