feat(housekeeping): cut over administration to ase
This commit is contained in:
1 parent
c9e35cf602
commit
2b8f73a91d
457 files changed
+589
-55936
No files matched your search
@@ -1,218 +0,0 @@
|
||||
import { asc, count, eq, inArray } from "drizzle-orm";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { disbandGuild } from "@/actions/admin-guilds";
|
||||
import Link from "@/components/link";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, Guilds, GuildsForumsThreads, GuildsMembers, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
|
||||
export default async function AdminGuildDetailPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>;
|
||||
}) {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.USERS_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
const canEdit = canAccess(permissions, PERMS.USERS_EDIT, session.user.rank);
|
||||
const t = await getTranslations("pages.admin.guilds");
|
||||
const { id: rawId } = await params;
|
||||
const id = Number(rawId);
|
||||
if (!Number.isFinite(id) || id <= 0) notFound();
|
||||
|
||||
const [guild] = await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
description: Guilds.description,
|
||||
userId: Guilds.userId,
|
||||
roomId: Guilds.roomId,
|
||||
badge: Guilds.badge,
|
||||
dateCreated: Guilds.dateCreated,
|
||||
state: Guilds.state,
|
||||
forum: Guilds.forum,
|
||||
})
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, id))
|
||||
.limit(1);
|
||||
if (!guild) notFound();
|
||||
|
||||
const [ownerRows, members, memberCountRows, threadCountRows] =
|
||||
await Promise.all([
|
||||
db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(eq(User.id, guild.userId))
|
||||
.limit(1)
|
||||
.catch(() => []),
|
||||
db
|
||||
.select({
|
||||
userId: GuildsMembers.userId,
|
||||
levelId: GuildsMembers.levelId,
|
||||
memberSince: GuildsMembers.memberSince,
|
||||
})
|
||||
.from(GuildsMembers)
|
||||
.where(eq(GuildsMembers.guildId, id))
|
||||
.orderBy(asc(GuildsMembers.levelId), asc(GuildsMembers.memberSince))
|
||||
.limit(100)
|
||||
.catch(() => []),
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(GuildsMembers)
|
||||
.where(eq(GuildsMembers.guildId, id))
|
||||
.catch(() => [{ total: 0 }]),
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(GuildsForumsThreads)
|
||||
.where(eq(GuildsForumsThreads.guildId, id))
|
||||
.catch(() => [{ total: 0 }]),
|
||||
]);
|
||||
|
||||
const owner = ownerRows[0] ?? null;
|
||||
const memberCount = Number(memberCountRows[0]?.total ?? 0);
|
||||
const threadCount = Number(threadCountRows[0]?.total ?? 0);
|
||||
|
||||
const memberUserIds = members.map((m) => m.userId).filter((uid) => uid > 0);
|
||||
const memberUsers =
|
||||
memberUserIds.length > 0
|
||||
? await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, memberUserIds))
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const usernameById = new Map(memberUsers.map((u) => [u.id, u.username]));
|
||||
|
||||
return (
|
||||
<main className="space-y-6">
|
||||
<div className="flex flex-wrap items-start justify-between gap-4">
|
||||
<div className="flex items-center gap-4">
|
||||
{guild.badge ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img
|
||||
src={`https://habbo-stories-content.s3.amazonaws.com/badge-part-images/${guild.badge}.gif`}
|
||||
alt=""
|
||||
width={64}
|
||||
height={64}
|
||||
className="h-16 w-16 object-contain"
|
||||
/>
|
||||
) : null}
|
||||
<div>
|
||||
<h1 className="m-0 text-xl font-bold">
|
||||
{guild.name || `#${guild.id}`}
|
||||
</h1>
|
||||
<p className="muted m-0 text-sm">#{guild.id}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button variant="ghost" size="sm" asChild>
|
||||
<Link href="/admin/guilds">{t("back")}</Link>
|
||||
</Button>
|
||||
<Button variant="ghost" size="sm" asChild>
|
||||
<Link href={`/guilds/${guild.id}`} target="_blank">
|
||||
{t("viewPublic")}
|
||||
</Link>
|
||||
</Button>
|
||||
{canEdit ? (
|
||||
<form action={disbandGuild}>
|
||||
<input type="hidden" name="id" value={guild.id} />
|
||||
<Button
|
||||
type="submit"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
style={{ color: "var(--admin-error)" }}
|
||||
>
|
||||
{t("disband")}
|
||||
</Button>
|
||||
</form>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{guild.description ? (
|
||||
<p className="text-sm text-[var(--admin-text-muted)]">
|
||||
{guild.description}
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(160px,1fr))] gap-3">
|
||||
<div className="admin-card">
|
||||
<div className="text-xs muted">{t("colOwner")}</div>
|
||||
<div>
|
||||
{owner ? (
|
||||
<Link href={`/admin/users/show/${owner.id}`}>
|
||||
{owner.username}
|
||||
</Link>
|
||||
) : (
|
||||
`#${guild.userId}`
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="admin-card">
|
||||
<div className="text-xs muted">{t("colMembers")}</div>
|
||||
<div className="text-lg font-bold">{memberCount}</div>
|
||||
</div>
|
||||
<div className="admin-card">
|
||||
<div className="text-xs muted">{t("colThreads")}</div>
|
||||
<div className="text-lg font-bold">{threadCount}</div>
|
||||
</div>
|
||||
<div className="admin-card">
|
||||
<div className="text-xs muted">{t("colCreated")}</div>
|
||||
<div>{formatDate(new Date(guild.dateCreated * 1000))}</div>
|
||||
</div>
|
||||
<div className="admin-card">
|
||||
<div className="text-xs muted">{t("colRoom")}</div>
|
||||
<div>
|
||||
{guild.roomId > 0 ? (
|
||||
<Link href={`/admin/rooms/show/${guild.roomId}`}>
|
||||
#{guild.roomId}
|
||||
</Link>
|
||||
) : (
|
||||
"—"
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section>
|
||||
<h2 className="admin-section-title">
|
||||
{t("members")} ({Math.min(members.length, memberCount)})
|
||||
</h2>
|
||||
{members.length === 0 ? (
|
||||
<div className="admin-empty">{t("noMembers")}</div>
|
||||
) : (
|
||||
<div className="admin-card overflow-x-auto" style={{ padding: 0 }}>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{t("colUser")}</th>
|
||||
<th>{t("colLevel")}</th>
|
||||
<th>{t("colJoined")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{members.map((m) => (
|
||||
<tr key={`${m.userId}-${m.memberSince}`}>
|
||||
<td>
|
||||
<Link href={`/admin/users/show/${m.userId}`}>
|
||||
{usernameById.get(m.userId) ?? `#${m.userId}`}
|
||||
</Link>
|
||||
</td>
|
||||
<td className="muted">{m.levelId}</td>
|
||||
<td className="muted">
|
||||
{formatDate(new Date(m.memberSince * 1000))}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
import { count, desc, eq, inArray, like, or } from "drizzle-orm";
|
||||
import { Shield } from "lucide-react";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { AdminPageShell } from "@/components/admin/admin-page-shell";
|
||||
import { AdminSimplePager } from "@/components/admin/admin-simple-pager";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import Link from "@/components/link";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { db, Guilds, GuildsMembers, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
|
||||
export default async function AdminGuildsPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<Record<string, string>>;
|
||||
}) {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.USERS_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
const t = await getTranslations("pages.admin.guilds");
|
||||
const raw = await searchParams;
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
const q = parsed.search.trim();
|
||||
|
||||
let total = 0;
|
||||
let rows: Array<{
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
userId: number;
|
||||
badge: string;
|
||||
dateCreated: number;
|
||||
roomId: number;
|
||||
}> = [];
|
||||
let loadError = false;
|
||||
|
||||
try {
|
||||
const where = q
|
||||
? or(
|
||||
like(Guilds.name, `%${q}%`),
|
||||
like(Guilds.description, `%${q}%`),
|
||||
...(Number.isFinite(Number(q)) && Number(q) > 0
|
||||
? [eq(Guilds.id, Number(q))]
|
||||
: []),
|
||||
)
|
||||
: undefined;
|
||||
|
||||
const [totalRow] = await db
|
||||
.select({ total: count() })
|
||||
.from(Guilds)
|
||||
.where(where);
|
||||
total = Number(totalRow?.total ?? 0);
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
rows = await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
description: Guilds.description,
|
||||
userId: Guilds.userId,
|
||||
badge: Guilds.badge,
|
||||
dateCreated: Guilds.dateCreated,
|
||||
roomId: Guilds.roomId,
|
||||
})
|
||||
.from(Guilds)
|
||||
.where(where)
|
||||
.orderBy(desc(Guilds.id))
|
||||
.limit(pagination.perPage)
|
||||
.offset(pagination.offset);
|
||||
} catch {
|
||||
loadError = true;
|
||||
}
|
||||
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
const ownerIds = [
|
||||
...new Set(rows.map((r) => r.userId).filter((id) => id > 0)),
|
||||
];
|
||||
const guildIds = rows.map((r) => r.id);
|
||||
|
||||
const [owners, memberGroups] = await Promise.all([
|
||||
ownerIds.length
|
||||
? db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, ownerIds))
|
||||
.catch(() => [])
|
||||
: Promise.resolve([]),
|
||||
guildIds.length
|
||||
? db
|
||||
.select({
|
||||
guildId: GuildsMembers.guildId,
|
||||
total: count(),
|
||||
})
|
||||
.from(GuildsMembers)
|
||||
.where(inArray(GuildsMembers.guildId, guildIds))
|
||||
.groupBy(GuildsMembers.guildId)
|
||||
.catch(() => [])
|
||||
: Promise.resolve([]),
|
||||
]);
|
||||
|
||||
const ownerById = new Map(owners.map((u) => [u.id, u.username]));
|
||||
const membersByGuild = new Map(
|
||||
memberGroups.map((g) => [g.guildId, Number(g.total)]),
|
||||
);
|
||||
|
||||
return (
|
||||
<AdminPageShell icon={Shield} title={t("title")} subtitle={t("subtitle")}>
|
||||
<main>
|
||||
{loadError ? (
|
||||
<div className="mb-4 rounded-lg bg-[var(--admin-error-subtle)] p-3 text-xs text-[var(--admin-error)]">
|
||||
{t("loadError")}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<form className="mb-4 flex gap-2" method="get">
|
||||
<input
|
||||
type="search"
|
||||
name="search"
|
||||
defaultValue={q}
|
||||
placeholder={t("searchPlaceholder")}
|
||||
className="admin-input flex-1"
|
||||
/>
|
||||
<Button type="submit" variant="default" size="sm">
|
||||
{t("search")}
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
||||
<StatusCard label={t("totalGuilds")} value={total} icon="🛡️" />
|
||||
</div>
|
||||
|
||||
<section>
|
||||
<h2 className="admin-section-title">
|
||||
{t("listHeading", { count: total })}
|
||||
</h2>
|
||||
{rows.length === 0 ? (
|
||||
<div className="admin-empty">{t("empty")}</div>
|
||||
) : (
|
||||
<div className="admin-card overflow-x-auto" style={{ padding: 0 }}>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{t("colId")}</th>
|
||||
<th>{t("colBadge")}</th>
|
||||
<th>{t("colName")}</th>
|
||||
<th>{t("colOwner")}</th>
|
||||
<th>{t("colMembers")}</th>
|
||||
<th>{t("colCreated")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((g) => (
|
||||
<tr key={g.id}>
|
||||
<td className="muted">{g.id}</td>
|
||||
<td>
|
||||
{g.badge ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img
|
||||
src={`https://habbo-stories-content.s3.amazonaws.com/badge-part-images/${g.badge}.gif`}
|
||||
alt=""
|
||||
width={40}
|
||||
height={40}
|
||||
className="h-10 w-10 object-contain"
|
||||
/>
|
||||
) : (
|
||||
<span className="muted">—</span>
|
||||
)}
|
||||
</td>
|
||||
<td>
|
||||
<Link
|
||||
href={`/admin/guilds/${g.id}`}
|
||||
className="font-medium hover:underline"
|
||||
>
|
||||
{g.name || `#${g.id}`}
|
||||
</Link>
|
||||
{g.description ? (
|
||||
<div className="muted text-xs line-clamp-1 max-w-xs">
|
||||
{g.description}
|
||||
</div>
|
||||
) : null}
|
||||
</td>
|
||||
<td>
|
||||
<Link href={`/admin/users/show/${g.userId}`}>
|
||||
{ownerById.get(g.userId) ?? `#${g.userId}`}
|
||||
</Link>
|
||||
</td>
|
||||
<td>{membersByGuild.get(g.id) ?? 0}</td>
|
||||
<td className="muted">
|
||||
{formatDate(new Date(g.dateCreated * 1000), "date")}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
<AdminSimplePager
|
||||
page={pagination.page}
|
||||
lastPage={pagination.lastPage}
|
||||
total={total}
|
||||
basePath="/admin/guilds"
|
||||
params={q ? { search: q } : undefined}
|
||||
/>
|
||||
</section>
|
||||
</main>
|
||||
</AdminPageShell>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user