"use client"; import { timeSince } from "@formbricks/lib/time"; import { TAction } from "@formbricks/types/v1/actions"; import { TEnvironment, TEnvironmentUpdateInput } from "@formbricks/types/v1/environment"; import { Confetti } from "@formbricks/ui"; import { ArrowDownIcon, CheckIcon, ExclamationTriangleIcon } from "@heroicons/react/24/solid"; import clsx from "clsx"; import Link from "next/link"; import { useEffect, useMemo, useState } from "react"; interface WidgetStatusIndicatorProps { environment: TEnvironment; type: "large" | "mini"; actions: TAction[]; updateEnvironmentAction: ( environmentId: string, data: Partial ) => Promise; } export default function WidgetStatusIndicator({ environment, type, actions, updateEnvironmentAction, }: WidgetStatusIndicatorProps) { const [confetti, setConfetti] = useState(false); useEffect(() => { if (!environment?.widgetSetupCompleted && actions && actions.length > 0) { updateEnvironmentAction(environment.id, { widgetSetupCompleted: true }); } }, [environment, actions, updateEnvironmentAction]); const stati = { notImplemented: { icon: ArrowDownIcon, color: "slate", title: "Connect Formbricks to your app.", subtitle: "You have not yet connected Formbricks to your app. Follow setup guide.", }, running: { icon: CheckIcon, color: "green", title: "Receiving data.", subtitle: "Last action received:" }, issue: { icon: ExclamationTriangleIcon, color: "amber", title: "There might be an issue.", subtitle: "Last action received:", }, }; const status = useMemo(() => { if (actions && actions.length > 0) { const lastEvent = actions[0]; const currentTime = new Date(); const lastEventTime = new Date(lastEvent.createdAt); const timeDifference = currentTime.getTime() - lastEventTime.getTime(); if (timeDifference <= 24 * 60 * 60 * 1000) { setConfetti(true); return "running"; } else { return "issue"; } } else { return "notImplemented"; } }, [actions]); const currentStatus = stati[status]; if (type === "large") { return (

{currentStatus.title}

{currentStatus.subtitle}{" "} {status !== "notImplemented" && {timeSince(actions[0].createdAt.toISOString())}}

{confetti && }
); } if (type === "mini") { return (

{currentStatus.subtitle}{" "} {status !== "notImplemented" && {timeSince(actions[0].createdAt.toISOString())}}

); } else { return null; } }