28 lines
757 B
TypeScript
28 lines
757 B
TypeScript
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>;
|
|
}
|