115 lines
6.7 KiB
TypeScript
115 lines
6.7 KiB
TypeScript
import { notFound } from "next/navigation";
|
||
import { Megaphone, RefreshCw, Send } from "lucide-react";
|
||
import { buildPlatformDigest, getPlatformBuild, getPlatformChannel, isSuperadmin, platformDigestEmpty, PLATFORM_CHANNEL_EVENT_NAMES, PLATFORM_CHANNEL_EVENTS, renderPlatformDigest } from "@peppa/core";
|
||
import { ago, Flash, PageHeader, Section } from "@/components/common";
|
||
import { Badge } from "@/components/ui/badge";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Card } from "@/components/ui/card";
|
||
import { Input, Label } from "@/components/ui/input";
|
||
import { Workspace } from "@/components/Workspace";
|
||
import { requireUser } from "@/lib/auth";
|
||
import { db } from "@/lib/db";
|
||
import { checkChannelAction, saveChannelAction, saveChannelSettingsAction, sendDigestNowAction } from "./actions";
|
||
|
||
export const metadata = { title: "Платформа — Пеппа" };
|
||
|
||
/** Telegram HTML of the digest → plain text for the preview. */
|
||
const plain = (html: string) => html.replace(/<[^>]+>/g, "").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
||
|
||
export default async function Platform({ searchParams }: { searchParams: Promise<{ error?: string; sent?: string }> }) {
|
||
const user = await requireUser();
|
||
if (!(await isSuperadmin(db, user))) notFound();
|
||
const { error, sent } = await searchParams;
|
||
const [c, build] = await Promise.all([getPlatformChannel(db), getPlatformBuild(db)]);
|
||
const digest = await buildPlatformDigest(db, { since: new Date(Date.now() - 24 * 3600_000) });
|
||
return (
|
||
<Workspace>
|
||
<div className="max-w-3xl">
|
||
<Flash error={error} />
|
||
{sent && <p className="mb-4 rounded-lg border px-4 py-3 text-sm">{sent === "empty" ? "Дайджест пуст — нечего отправлять." : `Дайджест отправлен: ${sent.replace("channel", "в канал").replace(",users:", " · пользователям: ")}`}</p>}
|
||
<PageHeader title={<><Megaphone className="mr-1 inline size-5 text-primary" />Платформа</>}>
|
||
Telegram-канал Пеппы: новые версии, падения и перезапуски сервисов и ежедневный дайджест — что нового в Пеппе и что происходит сейчас. Раздел видят только суперадмины.
|
||
{build && <> Сейчас: v{build.version} · <span className="font-mono">{build.commit.slice(0, 7)}</span>.</>}
|
||
</PageHeader>
|
||
|
||
<Section title="Канал">
|
||
<Card className="gap-3">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
{c.chatId == null ? (
|
||
<Badge variant="secondary">канал не задан</Badge>
|
||
) : !c.check ? (
|
||
<Badge variant="info">не проверен</Badge>
|
||
) : c.check.ok ? (
|
||
<Badge variant="ok">канал подключён</Badge>
|
||
) : (
|
||
<Badge variant="danger">не работает</Badge>
|
||
)}
|
||
{c.check?.ok && c.title && <span className="font-semibold">{c.title}</span>}
|
||
{c.check && <span className="ml-auto text-xs text-muted-foreground">проверен {ago(new Date(c.check.at))}</span>}
|
||
</div>
|
||
{c.check && !c.check.ok && <p className="m-0 text-sm text-destructive">{c.check.error}</p>}
|
||
<form action={saveChannelAction} className="flex flex-wrap items-end gap-2">
|
||
<Label className="min-w-56 flex-1">
|
||
Chat ID канала
|
||
<Input name="chatId" placeholder="-100… или @имя" defaultValue={c.chatId == null ? "" : String(c.chatId)} className="h-8 font-mono" />
|
||
</Label>
|
||
<Button size="sm">Сохранить и проверить</Button>
|
||
</form>
|
||
{c.chatId != null && (
|
||
<form action={checkChannelAction}>
|
||
<Button size="sm" variant="outline"><RefreshCw />Проверить ещё раз</Button>
|
||
</form>
|
||
)}
|
||
<p className="m-0 text-xs text-muted-foreground">
|
||
Добавьте бота Пеппы в канал администратором с правом «Публикация сообщений». Пока проверка не прошла, Пеппа перепроверяет канал раз в час и ничего туда не пишет. Ответы в канале бот не читает.
|
||
</p>
|
||
</Card>
|
||
</Section>
|
||
|
||
<Section title="Что присылать">
|
||
<Card>
|
||
<form action={saveChannelSettingsAction} className="flex flex-col gap-3">
|
||
{PLATFORM_CHANNEL_EVENTS.map((e) => (
|
||
<label key={e} className="flex items-center gap-2 text-sm">
|
||
<input type="checkbox" name={`event.${e}`} defaultChecked={c.events[e]} className="size-4 accent-primary" />
|
||
{PLATFORM_CHANNEL_EVENT_NAMES[e]} — в канал
|
||
</label>
|
||
))}
|
||
<label className="flex items-center gap-2 text-sm">
|
||
<input type="checkbox" name="users" defaultChecked={c.digest.users} className="size-4 accent-primary" />
|
||
Дайджест ещё и в личные чаты всем, кто привязал Telegram (отписка — «Настройки → Уведомления»)
|
||
</label>
|
||
<div className="grid gap-2 sm:grid-cols-2">
|
||
<Label>
|
||
Время дайджеста
|
||
<Input name="time" type="time" required defaultValue={c.digest.time} className="h-8" />
|
||
</Label>
|
||
<Label>
|
||
Таймзона
|
||
<Input name="timezone" required defaultValue={c.digest.timezone} className="h-8 font-mono" />
|
||
</Label>
|
||
</div>
|
||
<Button size="sm" className="self-start">Сохранить</Button>
|
||
</form>
|
||
</Card>
|
||
</Section>
|
||
|
||
<Section
|
||
title="Дайджест за сутки"
|
||
actions={
|
||
<form action={sendDigestNowAction}>
|
||
<Button size="sm" variant="outline"><Send />Отправить сейчас</Button>
|
||
</form>
|
||
}
|
||
>
|
||
{platformDigestEmpty(digest) ? (
|
||
<p className="m-0 text-sm text-muted-foreground">За сутки ничего нового и ничего не происходит — дайджест не уйдёт.</p>
|
||
) : (
|
||
<pre className="m-0 overflow-x-auto rounded-md bg-muted p-3 text-xs whitespace-pre-wrap">{plain(renderPlatformDigest(digest))}</pre>
|
||
)}
|
||
</Section>
|
||
</div>
|
||
</Workspace>
|
||
);
|
||
}
|