fix: Promise.all on concurrent promises
This commit is contained in:
parent
18999357a8
commit
3deec75715
@ -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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,51 +57,49 @@ export async function action({ request }: ActionFunctionArgs) {
|
|||||||
const method = String(data.get('_method'))
|
const method = String(data.get('_method'))
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'delete': {
|
case 'delete': {
|
||||||
await del(`v1/node/${id}`, session.get('hsApiKey')!)
|
await del(`v1/node/${id}`, session.get('hsApiKey')!)
|
||||||
return json({ message: 'Machine removed' })
|
return json({ message: 'Machine removed' })
|
||||||
}
|
|
||||||
|
|
||||||
case 'expire': {
|
|
||||||
console.log('expire')
|
|
||||||
const data = await post(`v1/node/${id}/expire`, session.get('hsApiKey')!)
|
|
||||||
console.log(data)
|
|
||||||
return json({ message: 'Machine expired' })
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'rename': {
|
|
||||||
if (!data.has('name')) {
|
|
||||||
return json({ message: 'No name provided' }, {
|
|
||||||
status: 400
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const name = String(data.get('name'))
|
case 'expire': {
|
||||||
|
await post(`v1/node/${id}/expire`, session.get('hsApiKey')!)
|
||||||
await post(`v1/node/${id}/rename/${name}`, session.get('hsApiKey')!)
|
return json({ message: 'Machine expired' })
|
||||||
return json({ message: 'Machine renamed' })
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'routes': {
|
|
||||||
if (!data.has('route') || !data.has('enabled')) {
|
|
||||||
return json({ message: 'No route or enabled provided' }, {
|
|
||||||
status: 400
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const route = String(data.get('route'))
|
case 'rename': {
|
||||||
const enabled = data.get('enabled') === 'true'
|
if (!data.has('name')) {
|
||||||
const postfix = enabled ? 'enable' : 'disable'
|
return json({ message: 'No name provided' }, {
|
||||||
|
status: 400,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
await post(`v1/routes/${route}/${postfix}`, session.get('hsApiKey')!)
|
const name = String(data.get('name'))
|
||||||
return json({ message: 'Route updated' })
|
|
||||||
}
|
|
||||||
|
|
||||||
default: {
|
await post(`v1/node/${id}/rename/${name}`, session.get('hsApiKey')!)
|
||||||
return json({ message: 'Invalid method' }, {
|
return json({ message: 'Machine renamed' })
|
||||||
status: 400
|
}
|
||||||
})
|
|
||||||
}
|
case 'routes': {
|
||||||
|
if (!data.has('route') || !data.has('enabled')) {
|
||||||
|
return json({ message: 'No route or enabled provided' }, {
|
||||||
|
status: 400,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const route = String(data.get('route'))
|
||||||
|
const enabled = data.get('enabled') === 'true'
|
||||||
|
const postfix = enabled ? 'enable' : 'disable'
|
||||||
|
|
||||||
|
await post(`v1/routes/${route}/${postfix}`, session.get('hsApiKey')!)
|
||||||
|
return json({ message: 'Route updated' })
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
return json({ message: 'Invalid method' }, {
|
||||||
|
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}>
|
? (
|
||||||
<Button>
|
<TooltipTrigger delay={0}>
|
||||||
<InfoIcon className='w-4 h-4'/>
|
<Button>
|
||||||
</Button>
|
<InfoIcon className="w-4 h-4" />
|
||||||
<Tooltip className={cn(
|
</Button>
|
||||||
'text-sm max-w-xs p-2 rounded-lg mb-2',
|
<Tooltip className={cn(
|
||||||
'bg-white dark:bg-zinc-800',
|
'text-sm max-w-xs p-2 rounded-lg mb-2',
|
||||||
'border border-gray-200 dark:border-zinc-700'
|
'bg-white dark:bg-zinc-800',
|
||||||
)}
|
'border border-gray-200 dark:border-zinc-700',
|
||||||
>
|
)}
|
||||||
Since MagicDNS is enabled, you can access devices
|
>
|
||||||
based on their name and also at
|
Since MagicDNS is enabled, you can access devices
|
||||||
{' '}
|
based on their name and also at
|
||||||
<Code>
|
{' '}
|
||||||
[name].[user].{data.magic}
|
<Code>
|
||||||
</Code>
|
[name].[user].
|
||||||
</Tooltip>
|
{data.magic}
|
||||||
</TooltipTrigger>
|
</Code>
|
||||||
) : undefined}
|
</Tooltip>
|
||||||
|
</TooltipTrigger>
|
||||||
|
)
|
||||||
|
: 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 => (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user