Admin page
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { useSignal, useSignalEffect } from "@preact/signals";
|
||||
|
||||
import { button_class } from "@/components/Button.tsx";
|
||||
import type { MeResponse } from "@/src/admin.ts";
|
||||
import { auth_headers, fetch_json } from "@/src/client.ts";
|
||||
|
||||
// Only shown to admins, the server checks again on every admin request
|
||||
export default function AdminButton() {
|
||||
const is_admin = useSignal(false);
|
||||
|
||||
useSignalEffect(() => {
|
||||
const fn = async () => {
|
||||
if (!localStorage.getItem("code")) {
|
||||
return;
|
||||
}
|
||||
const result = await fetch_json<MeResponse>("/api/me", { headers: auth_headers() });
|
||||
is_admin.value = result.ok && result.data.admin;
|
||||
};
|
||||
fn();
|
||||
});
|
||||
|
||||
if (!is_admin.value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <a href="/admin" class={button_class("link")}>Admin</a>;
|
||||
}
|
||||
Reference in New Issue
Block a user