import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog";
import { Drawer, DrawerContent, DrawerTrigger } from "@/components/ui/drawer";
import { cn } from "@/lib/utils";
import {
ReviewSegment,
ThreatLevel,
THREAT_LEVEL_LABELS,
} from "@/types/review";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { isDesktop } from "react-device-detect";
import { useTranslation } from "react-i18next";
import { MdAutoAwesome } from "react-icons/md";
type GenAISummaryChipProps = {
review?: ReviewSegment;
};
export function GenAISummaryChip({ review }: GenAISummaryChipProps) {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
setIsVisible(review?.data?.metadata != undefined);
}, [review]);
return (
{review?.data.metadata?.title}
);
}
type GenAISummaryDialogProps = {
review?: ReviewSegment;
onOpen?: (open: boolean) => void;
children: React.ReactNode;
};
export function GenAISummaryDialog({
review,
onOpen,
children,
}: GenAISummaryDialogProps) {
const { t } = useTranslation(["views/explore"]);
// data
const aiAnalysis = useMemo(() => review?.data?.metadata, [review]);
const aiThreatLevel = useMemo(() => {
if (
!aiAnalysis ||
(!aiAnalysis.potential_threat_level && !aiAnalysis.other_concerns)
) {
return t("label.none", { ns: "common" });
}
let concerns = "";
const threatLevel = aiAnalysis.potential_threat_level ?? 0;
if (threatLevel > 0) {
let label = "";
switch (threatLevel) {
case ThreatLevel.NEEDS_REVIEW:
label = t("needsReview", { ns: "views/events" });
break;
case ThreatLevel.SECURITY_CONCERN:
label = t("securityConcern", { ns: "views/events" });
break;
default:
label =
THREAT_LEVEL_LABELS[threatLevel as ThreatLevel] ||
t("details.unknown", { ns: "views/classificationModel" });
}
concerns = `• ${label}\n`;
}
(aiAnalysis.other_concerns ?? []).forEach((c) => {
concerns += `• ${c}\n`;
});
return concerns || t("label.none", { ns: "common" });
}, [aiAnalysis, t]);
// layout
const [open, setOpen] = useState(false);
const Overlay = isDesktop ? Dialog : Drawer;
const Trigger = isDesktop ? DialogTrigger : DrawerTrigger;
const Content = isDesktop ? DialogContent : DrawerContent;
const onOpenRef = useRef(onOpen);
onOpenRef.current = onOpen;
useEffect(() => {
onOpenRef.current?.(open);
}, [open]);
if (!aiAnalysis) {
return null;
}
return (
{children}
{t("aiAnalysis.title")}
{t("details.title.label")}
{aiAnalysis.title}
{t("details.description.label")}
{aiAnalysis.scene}
{t("details.score.label")}
{aiAnalysis.confidence * 100}%
{t("concerns.label")}
{aiThreatLevel}
);
}