2024-12-05 02:32:55 -05:00
|
|
|
import { LoaderFunctionArgs, redirect } from '@remix-run/node'
|
2024-05-11 23:07:24 -04:00
|
|
|
import { Outlet, useLoaderData, useNavigation } from '@remix-run/react'
|
|
|
|
|
import { ProgressBar } from 'react-aria-components'
|
2024-03-25 17:51:11 -04:00
|
|
|
|
2024-03-29 16:14:35 -04:00
|
|
|
import { ErrorPopup } from '~/components/Error'
|
2024-05-04 15:04:22 -04:00
|
|
|
import Header from '~/components/Header'
|
2024-12-05 02:32:55 -05:00
|
|
|
import Link from '~/components/Link'
|
2024-05-11 23:07:24 -04:00
|
|
|
import { cn } from '~/utils/cn'
|
2024-05-20 14:05:09 -04:00
|
|
|
import { loadContext } from '~/utils/config/headplane'
|
2024-03-26 09:04:27 -04:00
|
|
|
import { HeadscaleError, pull } from '~/utils/headscale'
|
|
|
|
|
import { destroySession, getSession } from '~/utils/sessions'
|
2024-03-25 17:51:11 -04:00
|
|
|
|
|
|
|
|
export async function loader({ request }: LoaderFunctionArgs) {
|
|
|
|
|
const session = await getSession(request.headers.get('Cookie'))
|
|
|
|
|
if (!session.has('hsApiKey')) {
|
|
|
|
|
return redirect('/login')
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 09:04:27 -04:00
|
|
|
try {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
|
await pull('v1/apikey', session.get('hsApiKey')!)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof HeadscaleError) {
|
|
|
|
|
// Safest to just redirect to login if we can't pull
|
|
|
|
|
return redirect('/login', {
|
|
|
|
|
headers: {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2024-05-20 14:05:09 -04:00
|
|
|
'Set-Cookie': await destroySession(session),
|
|
|
|
|
},
|
2024-03-26 09:04:27 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise propagate to boundary
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 14:05:09 -04:00
|
|
|
const context = await loadContext()
|
2024-03-30 02:44:06 -04:00
|
|
|
return {
|
2024-05-26 19:23:24 -04:00
|
|
|
config: context.config,
|
2024-12-05 02:32:55 -05:00
|
|
|
url: context.headscalePublicUrl ?? context.headscaleUrl,
|
|
|
|
|
debug: context.debug,
|
2024-05-20 14:05:09 -04:00
|
|
|
user: session.get('user'),
|
2024-03-30 02:44:06 -04:00
|
|
|
}
|
2024-03-25 17:51:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 02:32:55 -05:00
|
|
|
interface FooterProps {
|
|
|
|
|
url: string
|
|
|
|
|
debug: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Footer({ url, debug, integration }: FooterProps) {
|
|
|
|
|
return (
|
|
|
|
|
<footer className={cn(
|
|
|
|
|
'fixed bottom-0 left-0 z-50 w-full h-14',
|
|
|
|
|
'bg-ui-100 dark:bg-ui-900 text-ui-500',
|
|
|
|
|
'flex flex-col justify-center gap-1',
|
2024-12-06 19:06:27 -05:00
|
|
|
'border-t border-ui-200 dark:border-ui-800',
|
2024-12-05 02:32:55 -05:00
|
|
|
)}>
|
|
|
|
|
<p className="container text-xs">
|
|
|
|
|
Headplane is entirely free to use.
|
|
|
|
|
{' '}
|
|
|
|
|
If you find it useful, consider
|
|
|
|
|
{' '}
|
|
|
|
|
<Link
|
|
|
|
|
to="https://github.com/sponsors/tale"
|
|
|
|
|
name="Aarnav's GitHub Sponsors"
|
|
|
|
|
>
|
|
|
|
|
donating
|
|
|
|
|
</Link>
|
|
|
|
|
{' '}
|
|
|
|
|
to support development.
|
|
|
|
|
{' '}
|
|
|
|
|
</p>
|
|
|
|
|
<p className="container text-xs opacity-75">
|
|
|
|
|
Version: {__VERSION__}
|
|
|
|
|
{' | '}
|
|
|
|
|
Connecting to
|
|
|
|
|
{' '}
|
|
|
|
|
<strong>{url}</strong>
|
|
|
|
|
{' '}
|
|
|
|
|
{debug && '(Debug mode enabled)'}
|
|
|
|
|
</p>
|
|
|
|
|
</footer>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-25 17:51:11 -04:00
|
|
|
export default function Layout() {
|
2024-03-30 01:48:35 -04:00
|
|
|
const data = useLoaderData<typeof loader>()
|
2024-05-11 23:07:24 -04:00
|
|
|
const nav = useNavigation()
|
2024-05-01 02:21:54 -04:00
|
|
|
|
2024-03-25 17:51:11 -04:00
|
|
|
return (
|
|
|
|
|
<>
|
2024-05-11 23:07:24 -04:00
|
|
|
<ProgressBar
|
2024-05-20 14:05:09 -04:00
|
|
|
aria-label="Loading..."
|
2024-05-11 23:07:24 -04:00
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
'fixed top-0 left-0 z-50 w-1/2 h-1',
|
|
|
|
|
'bg-blue-500 dark:bg-blue-400 opacity-0',
|
2024-05-20 14:05:09 -04:00
|
|
|
nav.state === 'loading' && 'animate-loading opacity-100',
|
2024-05-11 23:07:24 -04:00
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</ProgressBar>
|
2024-05-20 14:05:09 -04:00
|
|
|
<Header data={data} />
|
|
|
|
|
<main className="container mx-auto overscroll-contain mt-4 mb-24">
|
|
|
|
|
<Outlet />
|
2024-03-25 17:51:11 -04:00
|
|
|
</main>
|
2024-12-05 02:32:55 -05:00
|
|
|
<Footer {...data} />
|
2024-03-25 17:51:11 -04:00
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 16:14:35 -04:00
|
|
|
export function ErrorBoundary() {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2024-05-20 14:05:09 -04:00
|
|
|
<Header />
|
|
|
|
|
<ErrorPopup type="embedded" />
|
2024-12-05 02:32:55 -05:00
|
|
|
<Footer url="Unknown" debug={false} />
|
2024-03-29 16:14:35 -04:00
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|