peppa/apps/web/app/(app)/users/merge/page.tsx

122 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// glossary-ignore-file: the page is for the superadmin only (accounts, repositories, Gitea spaces).
import Link from "@/components/Link";
import { notFound } from "next/navigation";
import { isSuperadmin, maskIin, mergePlan, PeppaError } from "@peppa/core";
import { Hidden } from "@/components/cards";
import { Flash, Notice, PageHeader, Section } from "@/components/common";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Workspace } from "@/components/Workspace";
import { requireUser } from "@/lib/auth";
import { db } from "@/lib/db";
import { getT } from "@/lib/i18n";
import { mergeAccountsAction } from "../../actions";
import { pageTitle } from "@/lib/title";
// PEP-274: the browser tab says which page it is.
export async function generateMetadata() {
const t = await getT();
return pageTitle(t, t("Объединить учётки"));
}
export default async function MergeAccounts({ searchParams }: { searchParams: Promise<{ from?: string; to?: string; error?: string }> }) {
const me = await requireUser();
if (!(await isSuperadmin(db, me))) notFound();
const t = await getT();
const { from, to, error } = await searchParams;
const plan = await mergePlan(db, Number(from), Number(to)).catch((e) => {
if (e instanceof PeppaError) notFound();
throw e;
});
const back = `/users/merge?from=${plan.from.id}&to=${plan.to.id}`;
const who = (u: typeof plan.from) => (
<span>
<span className="font-mono font-medium">{u.login}</span>
{u.name && <span className="text-muted-foreground"> · {u.name}</span>}
{u.egovIin && <span className="text-muted-foreground"> · eGov {maskIin(u.egovIin)}</span>}
{(u.googleEmail || u.email) && <span className="text-muted-foreground"> · {u.googleEmail ?? u.email}</span>}
</span>
);
const rows: [string, string[]][] = [
[t("Проекты"), plan.projects.map((p) => `${p.key} «${p.name}»`)],
[t("Участие в проектах"), plan.memberships.map((p) => `${p.key} «${p.name}»`)],
[t("Приглашения"), plan.invites ? [String(plan.invites)] : []],
[t("Подписки"), plan.subscriptions],
[t("Пулы подписок"), plan.pools],
[t("Чаты"), [...(plan.masterChat ? [t("личный чат")] : []), ...(plan.groups ? [t("Telegram-группы: {n}", { n: plan.groups })] : [])]],
[t("Telegram"), plan.telegram ? [t("привязка")] : []],
[t("Свой бот"), plan.bot ? [`@${plan.bot}`] : []],
[t("Способы входа"), [...(plan.egov ? ["eGov"] : []), ...(plan.google ? [`Google ${plan.google}`] : [])]],
];
const moving = rows.filter(([, v]) => v.length);
const personal = plan.projects.filter((p) => p.personalRepo);
return (
<Workspace>
<div className="max-w-3xl">
<Flash error={error} />
<PageHeader crumbs={<Link href="/users" className="hover:underline">{t("Пользователи")}</Link>} title={t("Объединить учётки")}>
{t("Всё, с чем человек работает, переедет со старой учётки на главную. Старая учётка будет отключена.")}
</PageHeader>
<Card className="mb-4 gap-2 text-sm">
<div>{t("Старая учётка:")} {who(plan.from)}</div>
<div>{t("Главная учётка (куда переносим):")} {who(plan.to)}</div>
</Card>
{plan.refusal ? (
<Notice>{t.text(plan.refusal)}</Notice>
) : (
<>
<Section title={t("Переедет")}>
{moving.length === 0 ? (
<p className="m-0 text-sm text-muted-foreground">{t("Переносить нечего — старая учётка будет просто отключена.")}</p>
) : (
<ul className="m-0 flex list-none flex-col gap-1 p-0 text-sm">
{moving.map(([k, v]) => (
<li key={k}>
<span className="text-muted-foreground">{k}:</span> {v.join(", ")}
</li>
))}
</ul>
)}
</Section>
<Section title={t("Останется")}>
<ul className="m-0 flex list-none flex-col gap-1 p-0 text-sm">
<li>{t("История (журнал, комментарии, ответы) — под старым логином {login}.", { login: plan.from.login })}</li>
{plan.projects.map((p) => (
<li key={p.id}>
{t("Репозиторий {key}: {repo}", { key: p.key, repo: p.repo })}
{p.personalRepo && <span> — {t("лежит в личном пространстве старой учётки Gitea; остаётся там, агенты работают с ним через peppa-bot")}</span>}
</li>
))}
</ul>
{personal.length > 0 && <Notice>{t("Перенос репозиториев между пространствами Gitea не выполняется.")}</Notice>}
</Section>
<form action={mergeAccountsAction} className="flex flex-col gap-2">
<Hidden fields={{ from: plan.from.id, to: plan.to.id, back }} />
<label className="flex items-center gap-2 text-sm">
<input type="checkbox" name="confirm" value="1" required />
{t("Да, это один человек — объединить")}
</label>
{plan.adminConfirm && (
<label className="flex items-center gap-2 text-sm">
<input type="checkbox" name="confirmAdmin" value="1" required />
{t("Одна из учёток — суперадмин, и это сделано намеренно")}
</label>
)}
<div className="flex gap-2">
<Button size="sm" variant="destructive">{t("Объединить")}</Button>
<Button asChild size="sm" variant="ghost">
<Link href="/users">{t("Отмена")}</Link>
</Button>
</div>
</form>
</>
)}
</div>
</Workspace>
);
}
/** PEP-204: what moves from the old account to the main one — confirmed before anything changes. Superadmin only. */