"use client";
/**
* `` โ admin AI quality preview (v1.4.16 phase B5e).
*
* Surfaces the aggregator-written helpful-rate per (severity x
* provider x prompt-version) bucket. The pg-boss `feedback-aggregator`
* queue writes the underlying summary daily at 04:00 Europe/Berlin
* to `AppSettings.adminAiInsightsFeedbackSummary`; this component
* reads it via `GET /api/admin/ai-quality` (admin-gated).
*
* Empty-state ("no summary yet") rendered as a quiet message rather
* than a fake zero row so the operator can tell "no feedback" apart
* from "all-zeros".
*
* Layout: a compact table with one row per bucket. Mobile ( = {
info: "text-info",
suggestion: "text-primary",
important: "text-warning",
urgent: "text-destructive",
};
export function AiQualitySection() {
const { t } = useTranslations();
const fmt = useFormatters();
const query = useQuery({
queryKey: queryKeys.adminAiQuality(),
queryFn: async () => {
const data = await apiGet<{ summary: FeedbackSummary | null } | null>(
"/api/admin/ai-quality",
);
return data?.summary ?? null;
},
});
const summary = query.data ?? null;
const buckets = summary?.buckets ?? [];
const hasData = !!summary && buckets.length > 0;
// v1.18.1 โ keep the outer card + header rendered across every query
// state (loading / error / empty / data) so the heading baseline stays at
// a constant Y-offset and the section never snaps downward when the data
// resolves. Mirrors (header outside the
// fetch-state branch).
return (
{t("admin.aiQuality.windowLabel", {
days: String(summary.windowDays),
})}
{" ยท "}
{t("admin.aiQuality.generatedAtLabel")}{" "}
{fmt.dateTime(summary.generatedAt)}
) : null
}
/>
{query.isLoading ? (
{t("admin.aiQuality.loading")}
) : query.isError ? (
{t("admin.aiQuality.loadError")}
) : !hasData ? (
{t("admin.aiQuality.empty")}
) : (
|
{t("admin.aiQuality.colSeverity")}
|
{t("admin.aiQuality.colMetric")}
|
{t("admin.aiQuality.colProvider")}
|
{t("admin.aiQuality.colPromptVersion")}
|
{t("admin.aiQuality.colHelpful")}
|
{t("admin.aiQuality.colNotHelpful")}
|
{t("admin.aiQuality.colHelpfulRate")}
|
{buckets.map((bucket, idx) => {
const ratePct = Math.round(bucket.helpfulRate * 100);
return (
|
{bucket.severity}
|
{bucket.metricSourceType} |
{bucket.providerType} |
{bucket.promptVersion}
|
{bucket.helpful} |
{bucket.notHelpful}
|
{ratePct}%
|
);
})}
)}
);
}