fix: Promise.all on concurrent promises

This commit is contained in:
Aarnav Tale 2024-05-15 21:53:39 -04:00
parent 18999357a8
commit 3deec75715
No known key found for this signature in database

View File

@ -16,12 +16,14 @@ import MachineRow from './machine'
export async function loader({ request }: LoaderFunctionArgs) { export async function loader({ request }: LoaderFunctionArgs) {
const session = await getSession(request.headers.get('Cookie')) const session = await getSession(request.headers.get('Cookie'))
const [machines, routes] = await Promise.all([
pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!),
pull<{ routes: Route[] }>('v1/routes', session.get('hsApiKey')!),
])
const machines = await pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!)
const routes = await pull<{ routes: Route[] }>('v1/routes', session.get('hsApiKey')!)
const context = await getContext() const context = await getContext()
let magic: string | undefined let magic: string | undefined
if (context.hasConfig) { if (context.hasConfig) {
const config = await getConfig() const config = await getConfig()
if (config.dns_config.magic_dns) { if (config.dns_config.magic_dns) {
@ -32,7 +34,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
return { return {
nodes: machines.nodes, nodes: machines.nodes,
routes: routes.routes, routes: routes.routes,
magic magic,
} }
} }
@ -40,14 +42,14 @@ export async function action({ request }: ActionFunctionArgs) {
const session = await getSession(request.headers.get('Cookie')) const session = await getSession(request.headers.get('Cookie'))
if (!session.has('hsApiKey')) { if (!session.has('hsApiKey')) {
return json({ message: 'Unauthorized' }, { return json({ message: 'Unauthorized' }, {
status: 401 status: 401,
}) })
} }
const data = await request.formData() const data = await request.formData()
if (!data.has('_method') || !data.has('id')) { if (!data.has('_method') || !data.has('id')) {
return json({ message: 'No method or ID provided' }, { return json({ message: 'No method or ID provided' }, {
status: 400 status: 400,
}) })
} }
@ -61,16 +63,14 @@ export async function action({ request }: ActionFunctionArgs) {
} }
case 'expire': { case 'expire': {
console.log('expire') await post(`v1/node/${id}/expire`, session.get('hsApiKey')!)
const data = await post(`v1/node/${id}/expire`, session.get('hsApiKey')!)
console.log(data)
return json({ message: 'Machine expired' }) return json({ message: 'Machine expired' })
} }
case 'rename': { case 'rename': {
if (!data.has('name')) { if (!data.has('name')) {
return json({ message: 'No name provided' }, { return json({ message: 'No name provided' }, {
status: 400 status: 400,
}) })
} }
@ -83,7 +83,7 @@ export async function action({ request }: ActionFunctionArgs) {
case 'routes': { case 'routes': {
if (!data.has('route') || !data.has('enabled')) { if (!data.has('route') || !data.has('enabled')) {
return json({ message: 'No route or enabled provided' }, { return json({ message: 'No route or enabled provided' }, {
status: 400 status: 400,
}) })
} }
@ -97,7 +97,7 @@ export async function action({ request }: ActionFunctionArgs) {
default: { default: {
return json({ message: 'Invalid method' }, { return json({ message: 'Invalid method' }, {
status: 400 status: 400,
}) })
} }
} }
@ -110,42 +110,45 @@ export default function Page() {
return ( return (
<> <>
<h1 className='text-2xl font-medium mb-4'>Machines</h1> <h1 className="text-2xl font-medium mb-4">Machines</h1>
<table className='table-auto w-full rounded-lg'> <table className="table-auto w-full rounded-lg">
<thead className='text-gray-500 dark:text-gray-400'> <thead className="text-gray-500 dark:text-gray-400">
<tr className='text-left uppercase text-xs font-bold px-0.5'> <tr className="text-left uppercase text-xs font-bold px-0.5">
<th className='pb-2'>Name</th> <th className="pb-2">Name</th>
<th className='pb-2'> <th className="pb-2">
<div className='flex items-center gap-x-1'> <div className="flex items-center gap-x-1">
Addresses Addresses
{data.magic ? ( {data.magic
? (
<TooltipTrigger delay={0}> <TooltipTrigger delay={0}>
<Button> <Button>
<InfoIcon className='w-4 h-4'/> <InfoIcon className="w-4 h-4" />
</Button> </Button>
<Tooltip className={cn( <Tooltip className={cn(
'text-sm max-w-xs p-2 rounded-lg mb-2', 'text-sm max-w-xs p-2 rounded-lg mb-2',
'bg-white dark:bg-zinc-800', 'bg-white dark:bg-zinc-800',
'border border-gray-200 dark:border-zinc-700' 'border border-gray-200 dark:border-zinc-700',
)} )}
> >
Since MagicDNS is enabled, you can access devices Since MagicDNS is enabled, you can access devices
based on their name and also at based on their name and also at
{' '} {' '}
<Code> <Code>
[name].[user].{data.magic} [name].[user].
{data.magic}
</Code> </Code>
</Tooltip> </Tooltip>
</TooltipTrigger> </TooltipTrigger>
) : undefined} )
: undefined}
</div> </div>
</th> </th>
<th className='pb-2'>Last Seen</th> <th className="pb-2">Last Seen</th>
</tr> </tr>
</thead> </thead>
<tbody className={cn( <tbody className={cn(
'divide-y divide-zinc-200 dark:divide-zinc-700 align-top', 'divide-y divide-zinc-200 dark:divide-zinc-700 align-top',
'border-t border-zinc-200 dark:border-zinc-700' 'border-t border-zinc-200 dark:border-zinc-700',
)} )}
> >
{data.nodes.map(machine => ( {data.nodes.map(machine => (