311 lines
16 KiB
TypeScript
311 lines
16 KiB
TypeScript
import { Form, Head, Link, router, usePage } from '@inertiajs/react';
|
|
import { Eye, Pencil, Plus, Trash2 } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import { Input } from '@/components/ui/input';
|
|
|
|
type Device = {
|
|
id: number;
|
|
name: string;
|
|
management_address: string;
|
|
management_port: number;
|
|
connection_type: string;
|
|
connection_ports?: Record<string, number>;
|
|
status: string;
|
|
location?: string;
|
|
vendor: { name: string };
|
|
type: { name: string };
|
|
model?: { name: string };
|
|
creator?: { name: string };
|
|
user_assignments_count: number;
|
|
};
|
|
type Props = {
|
|
devices: {
|
|
data: Device[];
|
|
links: { url: string | null; label: string; active: boolean }[];
|
|
};
|
|
vendors: { id: number; name: string }[];
|
|
filters: { search?: string; vendor?: string; status?: string };
|
|
policy: {
|
|
tenant_user_can_create: boolean;
|
|
tenant_user_can_update_own: boolean;
|
|
tenant_user_can_delete_own: boolean;
|
|
};
|
|
isTenantAdmin: boolean;
|
|
};
|
|
|
|
export default function DeviceIndex({
|
|
devices,
|
|
vendors,
|
|
filters,
|
|
policy,
|
|
isTenantAdmin,
|
|
}: Props) {
|
|
const { auth } = usePage().props;
|
|
const canCreate =
|
|
auth.permissions.includes('device.create') &&
|
|
(isTenantAdmin || policy.tenant_user_can_create);
|
|
const search = (event: React.FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault();
|
|
const data = new FormData(event.currentTarget);
|
|
router.get('/devices', Object.fromEntries(data), {
|
|
preserveState: true,
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Head title="Perangkat" />
|
|
<div className="flex flex-1 flex-col gap-6 p-4 md:p-6">
|
|
<div className="flex items-end justify-between gap-4">
|
|
<div>
|
|
<h1 className="text-2xl font-semibold">
|
|
Perangkat Jaringan
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
Inventaris perangkat tenant yang terisolasi dan
|
|
aman.
|
|
</p>
|
|
</div>
|
|
{canCreate && (
|
|
<Button asChild>
|
|
<Link href="/devices/create">
|
|
<Plus className="size-4" />
|
|
Tambah Perangkat
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
</div>
|
|
{isTenantAdmin && (
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-base">
|
|
Aturan Tenant User
|
|
</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<Form
|
|
action="/device-policy"
|
|
method="put"
|
|
className="flex flex-wrap gap-5 text-sm"
|
|
>
|
|
{[
|
|
[
|
|
'tenant_user_can_create',
|
|
'Boleh menambah perangkat',
|
|
],
|
|
[
|
|
'tenant_user_can_update_own',
|
|
'Boleh mengubah perangkat sendiri',
|
|
],
|
|
[
|
|
'tenant_user_can_delete_own',
|
|
'Boleh menghapus perangkat sendiri',
|
|
],
|
|
].map(([name, label]) => (
|
|
<label
|
|
key={name}
|
|
className="flex items-center gap-2"
|
|
>
|
|
<input
|
|
type="hidden"
|
|
name={name}
|
|
value="0"
|
|
/>
|
|
<input
|
|
type="checkbox"
|
|
name={name}
|
|
value="1"
|
|
defaultChecked={
|
|
policy[
|
|
name as keyof typeof policy
|
|
]
|
|
}
|
|
/>
|
|
{label}
|
|
</label>
|
|
))}
|
|
<Button size="sm">Simpan Aturan</Button>
|
|
</Form>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<form
|
|
onSubmit={search}
|
|
className="mb-5 grid gap-3 md:grid-cols-4"
|
|
>
|
|
<Input
|
|
name="search"
|
|
defaultValue={filters.search}
|
|
placeholder="Cari nama, alamat, lokasi..."
|
|
/>
|
|
<select
|
|
name="vendor"
|
|
defaultValue={filters.vendor ?? ''}
|
|
className="h-10 rounded-md border bg-background px-3"
|
|
>
|
|
<option value="">Semua vendor</option>
|
|
{vendors.map((v) => (
|
|
<option key={v.id} value={v.id}>
|
|
{v.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<select
|
|
name="status"
|
|
defaultValue={filters.status ?? ''}
|
|
className="h-10 rounded-md border bg-background px-3"
|
|
>
|
|
<option value="">Semua status</option>
|
|
<option value="online">Online</option>
|
|
<option value="offline">Offline</option>
|
|
<option value="unknown">Unknown</option>
|
|
</select>
|
|
<Button variant="outline">Filter</Button>
|
|
</form>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full min-w-[980px] table-fixed text-sm [&_td]:p-3 [&_td]:align-middle [&_th]:p-3">
|
|
<colgroup>
|
|
<col className="w-[20%]" />
|
|
<col className="w-[18%]" />
|
|
<col className="w-[16%]" />
|
|
<col className="w-[14%]" />
|
|
<col className="w-[10%]" />
|
|
<col className="w-[10%]" />
|
|
<col className="w-[12%]" />
|
|
</colgroup>
|
|
<thead>
|
|
<tr className="border-b text-left text-muted-foreground">
|
|
<th className="p-3">Perangkat</th>
|
|
<th className="p-3">Alamat</th>
|
|
<th className="p-3">User</th>
|
|
<th className="p-3">Vendor / Tipe</th>
|
|
<th className="p-3">Lokasi</th>
|
|
<th className="p-3">Status</th>
|
|
<th className="p-3 text-right">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{devices.data.map((device) => (
|
|
<tr
|
|
key={device.id}
|
|
className="border-b"
|
|
>
|
|
<td className="p-3">
|
|
<div className="font-medium">
|
|
{device.name}
|
|
</div>
|
|
<div className="text-xs text-muted-foreground">
|
|
{device.model?.name ??
|
|
'Model belum ditentukan'}
|
|
</div>
|
|
</td>
|
|
<td className="p-3 break-words">
|
|
<div>
|
|
{device.management_address}
|
|
</div>
|
|
<div className="text-xs text-muted-foreground">
|
|
{Object.entries(
|
|
device.connection_ports ?? {
|
|
[device.connection_type]:
|
|
device.management_port,
|
|
},
|
|
)
|
|
.map(
|
|
([
|
|
protocol,
|
|
port,
|
|
]) =>
|
|
`${protocol}:${port}`,
|
|
)
|
|
.join(' · ')}
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<Link
|
|
className="font-medium text-primary hover:underline"
|
|
href={`/device-users?device_id=${device.id}`}
|
|
>
|
|
{
|
|
device.user_assignments_count
|
|
}{' '}
|
|
user
|
|
</Link>
|
|
</td>
|
|
<td>
|
|
{device.vendor.name} ·{' '}
|
|
{device.type.name}
|
|
</td>
|
|
<td>{device.location ?? '-'}</td>
|
|
<td>
|
|
<span className="rounded-full bg-muted px-2 py-1 text-xs uppercase">
|
|
{device.status}
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<div className="flex justify-end gap-1">
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
asChild
|
|
>
|
|
<Link
|
|
href={`/devices/${device.id}`}
|
|
>
|
|
<Eye className="size-4" />
|
|
</Link>
|
|
</Button>
|
|
{auth.permissions.includes(
|
|
'device.update',
|
|
) && (
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
asChild
|
|
>
|
|
<Link
|
|
href={`/devices/${device.id}/edit`}
|
|
>
|
|
<Pencil className="size-4" />
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
{auth.permissions.includes(
|
|
'device.delete',
|
|
) && (
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
onClick={() =>
|
|
confirm(
|
|
'Hapus perangkat ini?',
|
|
) &&
|
|
router.delete(
|
|
`/devices/${device.id}`,
|
|
)
|
|
}
|
|
>
|
|
<Trash2 className="size-4" />
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
{devices.data.length === 0 && (
|
|
<div className="py-12 text-center text-muted-foreground">
|
|
Belum ada perangkat.
|
|
</div>
|
|
)}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|