headplane/app/routes/machines/dialogs/move.tsx

73 lines
2 KiB
TypeScript
Raw Normal View History

2024-12-31 10:30:14 +05:30
import { Form, useSubmit } from 'react-router';
import { type Dispatch, type SetStateAction, useState } from 'react';
2024-12-31 10:30:14 +05:30
import Code from '~/components/Code';
import Dialog from '~/components/Dialog';
import Select from '~/components/Select';
2024-12-31 10:31:50 +05:30
import type { Machine, User } from '~/types';
interface MoveProps {
2024-12-31 10:30:14 +05:30
readonly machine: Machine;
readonly users: User[];
readonly state: [boolean, Dispatch<SetStateAction<boolean>>];
readonly magic?: string;
}
export default function Move({ machine, state, magic, users }: MoveProps) {
2024-12-31 10:30:14 +05:30
const [owner, setOwner] = useState(machine.user.name);
const submit = useSubmit();
return (
<Dialog>
<Dialog.Panel control={state}>
2024-12-31 10:30:14 +05:30
{(close) => (
<>
2024-12-31 10:30:14 +05:30
<Dialog.Title>Change the owner of {machine.givenName}</Dialog.Title>
<Dialog.Text>
The owner of the machine is the user associated with it.
</Dialog.Text>
<Form
method="POST"
onSubmit={(e) => {
2024-12-31 10:30:14 +05:30
submit(e.currentTarget);
}}
>
<input type="hidden" name="_method" value="move" />
<input type="hidden" name="id" value={machine.id} />
<Select
label="Owner"
name="to"
placeholder="Select a user"
state={[owner, setOwner]}
>
2024-12-31 10:30:14 +05:30
{users.map((user) => (
<Select.Item key={user.id} id={user.name}>
{user.name}
</Select.Item>
))}
</Select>
2024-12-31 10:30:14 +05:30
{magic ? (
<p className="text-sm text-gray-500 dark:text-gray-300 leading-tight">
This machine is accessible by the hostname{' '}
<Code className="text-sm">
{machine.givenName}.{magic}
</Code>
.
</p>
) : undefined}
<div className="mt-6 flex justify-end gap-2 mt-6">
2024-12-31 10:30:14 +05:30
<Dialog.Action variant="cancel" onPress={close}>
Cancel
</Dialog.Action>
2024-12-31 10:30:14 +05:30
<Dialog.Action variant="confirm" onPress={close}>
Change owner
</Dialog.Action>
</div>
</Form>
</>
)}
</Dialog.Panel>
</Dialog>
2024-12-31 10:30:14 +05:30
);
}