171 lines
8.5 KiB
TypeScript
171 lines
8.5 KiB
TypeScript
// Project page section «Ошибки» (PEP-58): connect Bugsink, incident policy, incidents list.
|
||
import { Bug, CircleCheck, EyeOff, Inbox, MapPin, Play } from "lucide-react";
|
||
import { INCIDENT_STATUS_RU, incidentPolicies, POLICY_RU, type ErrorSource, type Incident, type IncidentSettings } from "@peppa/core";
|
||
import { connectErrorTrackingAction, decideIncidentAction, resolveIncidentAction, saveIncidentSettings } from "@/app/(app)/actions";
|
||
import { Hidden } from "@/components/cards";
|
||
import { ago, Disclosure, Empty, Key, Section } from "@/components/common";
|
||
import { Badge } from "@/components/ui/badge";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Input, Label, NativeSelect, Textarea } from "@/components/ui/input";
|
||
|
||
const STATUS_TONE: Record<Incident["status"], "danger" | "warn" | "info" | "ok" | "muted"> = { new: "danger", open: "warn", pending: "warn", task: "info", ignored: "muted", resolved: "ok", regressed: "danger" };
|
||
|
||
function Code({ children }: { children: React.ReactNode }) {
|
||
return <code className="block overflow-x-auto rounded-md bg-muted px-2.5 py-1.5 font-mono text-xs">{children}</code>;
|
||
}
|
||
|
||
export function IncidentsSection(props: {
|
||
projectId: number;
|
||
projectKey: string;
|
||
back: string;
|
||
isOwner: boolean;
|
||
settings: IncidentSettings | null;
|
||
sources: ErrorSource[];
|
||
incidents: Incident[];
|
||
taskKeys: Map<number, string>;
|
||
webhookUrl: string | null;
|
||
}) {
|
||
const { projectId, back, settings: s } = props;
|
||
if (!s) {
|
||
return (
|
||
<Section title="Ошибки">
|
||
<p className="m-0 text-sm text-muted-foreground">Ошибки фронта и бэка из прода (Bugsink, SDK Sentry) приходят сюда и становятся задачами по политике проекта.</p>
|
||
{props.isOwner ? (
|
||
<form action={connectErrorTrackingAction}>
|
||
<Hidden fields={{ projectId, back }} />
|
||
<Button variant="default" size="sm">
|
||
<Bug />
|
||
Подключить отслеживание ошибок
|
||
</Button>
|
||
</form>
|
||
) : (
|
||
<Empty>Не подключено — подключает владелец проекта.</Empty>
|
||
)}
|
||
</Section>
|
||
);
|
||
}
|
||
const active = props.incidents.filter((i) => !["ignored", "resolved"].includes(i.status));
|
||
const rest = props.incidents.filter((i) => ["ignored", "resolved"].includes(i.status));
|
||
return (
|
||
<Section title="Ошибки" count={active.length} actions={<span className="text-xs text-muted-foreground">политика: {POLICY_RU[s.policy]}</span>}>
|
||
{active.length === 0 ? <Empty>Открытых ошибок нет.</Empty> : active.map((i) => <IncidentCard key={i.id} i={i} {...props} />)}
|
||
{rest.length > 0 && (
|
||
<Disclosure summary={`Исправленные и игнорируемые (${rest.length})`}>
|
||
<div className="flex flex-col gap-2">
|
||
{rest.map((i) => (
|
||
<IncidentCard key={i.id} i={i} {...props} />
|
||
))}
|
||
</div>
|
||
</Disclosure>
|
||
)}
|
||
|
||
<Disclosure summary="Настройки ошибок">
|
||
<div className="flex flex-col gap-3 text-sm">
|
||
{props.sources.map((src) => (
|
||
<div key={src.id} className="flex flex-col gap-1">
|
||
<span className="font-medium">DSN {src.kind === "web" ? "фронта (web)" : "бэка (api)"}</span>
|
||
<Code>{src.dsn}</Code>
|
||
</div>
|
||
))}
|
||
<p className="m-0 text-muted-foreground">
|
||
В коде: <code className="font-mono">NEXT_PUBLIC_SENTRY_DSN</code> / <code className="font-mono">SENTRY_DSN</code> из окружения, <code className="font-mono">release</code> = SHA деплоя,{" "}
|
||
<code className="font-mono">environment</code> = prod, <code className="font-mono">sendDefaultPii: false</code>.
|
||
</p>
|
||
{!s.webhookAuto && props.webhookUrl && (
|
||
<div className="flex flex-col gap-1">
|
||
<span><b>Вебхук не настроен автоматически.</b> В Bugsink: проект → Alerts → Custom webhook:</span>
|
||
<Code>{props.webhookUrl}</Code>
|
||
<span className="text-muted-foreground">Пока его нет, Пеппа опрашивает Bugsink раз в 2 минуты.</span>
|
||
</div>
|
||
)}
|
||
<form action={saveIncidentSettings} className="flex flex-col gap-2.5">
|
||
<Hidden fields={{ projectId, back }} />
|
||
<Label>
|
||
Политика инцидентов
|
||
<NativeSelect name="policy" defaultValue={s.policy} aria-label="Политика инцидентов">
|
||
{incidentPolicies.map((p) => (
|
||
<option key={p} value={p}>
|
||
{p} — {POLICY_RU[p]}
|
||
</option>
|
||
))}
|
||
</NativeSelect>
|
||
</Label>
|
||
<Textarea
|
||
name="triagePrompt"
|
||
rows={3}
|
||
defaultValue={s.triagePrompt ?? ""}
|
||
aria-label="Как принимать решение (для триажёра)"
|
||
placeholder="Как принимать решение (для политики triage): например, «падения оплаты — сразу в работу, косметику фронта — в бэклог, остальное спроси»"
|
||
/>
|
||
<div className="flex gap-2">
|
||
<Input name="environments" defaultValue={s.environments.join(", ")} aria-label="Окружения" placeholder="Окружения: prod, production" />
|
||
<Input type="number" name="dailyLimit" min={0} defaultValue={s.dailyLimit} aria-label="Задач в сутки" className="w-24" />
|
||
</div>
|
||
<Input name="ignorePattern" defaultValue={s.ignorePattern ?? ""} aria-label="Игнорировать по названию" placeholder="Игнорировать по названию (регэксп), например ResizeObserver loop" />
|
||
<Button size="sm" className="self-start">Сохранить</Button>
|
||
</form>
|
||
</div>
|
||
</Disclosure>
|
||
</Section>
|
||
);
|
||
}
|
||
|
||
function IncidentCard({ i, back, projectId, taskKeys }: { i: Incident; back: string; projectId: number; taskKeys: Map<number, string> }) {
|
||
const taskKey = i.taskId ? taskKeys.get(i.taskId) : null;
|
||
const decide = (action: string, label: string, icon: React.ReactNode) => (
|
||
<form action={decideIncidentAction}>
|
||
<Hidden fields={{ projectId, back, incidentId: i.id, action }} />
|
||
<Button size="xs">
|
||
{icon}
|
||
{label}
|
||
</Button>
|
||
</form>
|
||
);
|
||
return (
|
||
<article className="flex flex-col gap-1.5 rounded-lg border bg-card p-3.5 text-sm">
|
||
<div className="flex items-start gap-2">
|
||
<Badge variant={STATUS_TONE[i.status]}>{INCIDENT_STATUS_RU[i.status]}</Badge>
|
||
<b className="min-w-0 flex-1 font-mono text-[13px] font-medium [overflow-wrap:anywhere]">{i.title}</b>
|
||
<span className="shrink-0 font-mono text-xs text-muted-foreground">{ago(i.lastSeen)}</span>
|
||
</div>
|
||
<div className="flex flex-wrap items-center gap-x-1.5 text-xs text-muted-foreground">
|
||
{i.source} · {i.environment ?? "—"} · релиз {i.lastRelease ?? "—"} · событий {i.events}
|
||
{i.culprit && (
|
||
<span className="inline-flex items-center gap-1">
|
||
· <MapPin className="size-3" /> {i.culprit}
|
||
</span>
|
||
)}
|
||
{i.url && (
|
||
<>
|
||
· <a href={i.url} className="text-primary">Bugsink</a>
|
||
</>
|
||
)}
|
||
{taskKey && (
|
||
<>
|
||
· <Key href={`/t/${taskKey}`}>{taskKey}</Key>
|
||
</>
|
||
)}
|
||
</div>
|
||
{i.decisionReason && <div className="text-xs text-muted-foreground">{i.decidedBy === "triage" ? "🤖 " : ""}{i.decisionReason}</div>}
|
||
<div className="flex flex-wrap gap-1.5">
|
||
{!i.taskId && i.status !== "resolved" && (
|
||
<>
|
||
{decide("backlog", "В бэклог", <Inbox />)}
|
||
{decide("start", "В работу", <Play />)}
|
||
{i.status !== "ignored" && decide("ignore", "Игнорировать", <EyeOff />)}
|
||
</>
|
||
)}
|
||
{i.status !== "resolved" && i.status !== "ignored" && (
|
||
<form action={resolveIncidentAction}>
|
||
<Hidden fields={{ projectId, back, incidentId: i.id }} />
|
||
<Button size="xs">
|
||
<CircleCheck />
|
||
Исправлено
|
||
</Button>
|
||
</form>
|
||
)}
|
||
</div>
|
||
</article>
|
||
);
|
||
}
|