Files
vces/islands/AdminPanel.tsx
T
2026-10-29 21:42:26 -03:00

182 lines
5.4 KiB
TypeScript

import { useSignal, useSignalEffect } from "@preact/signals";
import Button from "@/components/Button.tsx";
import { CopyIcon } from "@/components/Icons.tsx";
import InputField from "@/components/InputField.tsx";
import Form from "@/islands/Form.tsx";
import type { AdminUserEntry, CreatedUser } from "@/src/admin.ts";
import { auth_headers, fetch_json } from "@/src/client.ts";
import { format_bytes } from "@/src/files.ts";
const DATE_FORMAT = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" });
interface CreatedNoticeProps {
user: CreatedUser;
}
function CreatedNotice(props: CreatedNoticeProps) {
const copied = useSignal(false);
const error = useSignal("");
async function copy() {
try {
await navigator.clipboard.writeText(props.user.code);
copied.value = true;
setTimeout(() => copied.value = false, 2000);
} catch {
error.value = "Couldn't copy the code, select it and copy it by hand";
}
}
return (
<div class="mt-6 rounded bg-primary-500/5 p-4 dark:bg-primary-400/10">
<p class="font-medium">Created {props.user.name}</p>
<p class="mt-1 text-sm text-gray-700 dark:text-gray-400">
Send them this code, it's how they log in. It won't be shown again.
</p>
<div class="mt-3 flex flex-row gap-2">
<InputField
type="text"
readOnly
value={props.user.code}
aria-label={`Login code for ${props.user.name}`}
class="h-10 min-w-0 font-mono text-sm"
onFocus={(event) => event.currentTarget.select()}
/>
<Button
variant="primary"
class="flex h-10 flex-none items-center gap-2 px-4 py-0 text-sm"
onClick={copy}
>
<CopyIcon class="size-4" />
{copied.value ? "Copied" : "Copy"}
</Button>
</div>
{error.value && <p role="alert" class="mt-2 text-sm text-error-600 dark:text-error-400">{error.value}</p>}
</div>
);
}
interface UserTableProps {
users: AdminUserEntry[];
}
function UserTable(props: UserTableProps) {
return (
<div class="overflow-x-auto">
<table class="w-full text-left">
<thead class="border-b border-gray-200 text-sm text-gray-500 dark:border-gray-700 dark:text-gray-400">
<tr>
<th class="py-2 pr-4 font-medium">Name</th>
<th class="px-4 py-2 text-right font-medium">Files</th>
<th class="px-4 py-2 text-right font-medium">Storage</th>
<th class="py-2 pl-4 font-medium">Created</th>
</tr>
</thead>
<tbody>
{props.users.map((user) => (
<tr key={user.id} class="border-b border-gray-200 last:border-0 dark:border-gray-700">
<td class="py-3 pr-4">
<span class="font-medium break-all">{user.name}</span>
{user.admin && (
<span class="ml-2 rounded bg-primary-500/10 px-1.5 py-0.5 text-xs font-medium text-primary-600 dark:text-primary-400">
Admin
</span>
)}
{!user.has_key && (
<span class="ml-2 text-sm text-gray-500 dark:text-gray-400">Not set up yet</span>
)}
</td>
<td class="px-4 py-3 text-right tabular-nums">{user.file_count}</td>
<td class="px-4 py-3 text-right whitespace-nowrap tabular-nums">
{format_bytes(user.storage_used)}
</td>
<td class="py-3 pl-4 whitespace-nowrap">
{DATE_FORMAT.format(new Date(user.date_created))}
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default function AdminPanel() {
const users = useSignal<AdminUserEntry[] | null>(null);
const load_error = useSignal("");
const created = useSignal<CreatedUser | null>(null);
// Changing it remounts the form, which clears it
const form_key = useSignal(0);
async function load() {
if (!localStorage.getItem("code")) {
load_error.value = "Log in to see this page";
return;
}
const result = await fetch_json<AdminUserEntry[]>("/api/admin/users", { headers: auth_headers() });
if (result.ok) {
users.value = result.data;
} else {
load_error.value = result.error;
}
}
function on_created(user: CreatedUser) {
created.value = user;
form_key.value++;
users.value = [
...users.value ?? [],
{
id: user.id,
name: user.name,
admin: user.admin,
date_created: new Date().toISOString(),
has_key: false,
file_count: 0,
storage_used: 0,
},
];
}
useSignalEffect(() => {
load();
});
if (!users.value) {
return load_error.value
? <p role="alert" class="text-center text-error-600 dark:text-error-400">{load_error.value}</p>
: <p class="text-center text-gray-800 dark:text-gray-400">Loading...</p>;
}
return (
<div class="flex flex-col gap-10">
<section>
<h1 class="pb-4 text-lg font-bold">
Users <span class="font-normal text-gray-500 dark:text-gray-400">({users.value.length})</span>
</h1>
<UserTable users={users.value} />
</section>
<section class="max-w-md">
<h2 class="pb-4 text-lg font-bold">Create a user</h2>
<Form<CreatedUser>
key={form_key.value}
action="/api/admin/users"
headers={auth_headers()}
on_success={on_created}
>
<div class="flex flex-col gap-4">
<InputField type="text" name="name" placeholder="Name" required maxLength={100} />
<label class="flex items-center gap-2">
<input type="checkbox" name="admin" class="size-4 accent-primary-600" />
Admin
</label>
<Button type="submit" variant="primary">Create user</Button>
</div>
</Form>
{created.value && <CreatedNotice user={created.value} />}
</section>
</div>
);
}