Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
9.6 KB
Raw Blame
/** @jsxImportSource react */
import { useCallback, useEffect, useMemo, useState } from "react";
import {
  Bell,
  CircleCheck,
  Info,
  OctagonX,
  TriangleAlert,
  type LucideIcon,
} from "lucide-react";

import { Button } from "@/components/ui/button";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import { t } from "@/i18n";
import {
  useNotificationStore,
  type AppNotification,
  type NotificationSeverity,
} from "@/react-app/kernel/notification-store";
import { useNavigate } from "react-router";
import { requestOpenModelPicker } from "./new-providers-listener";
import { useControlAction, type OmniRushControlAction } from "./control/control-provider";
import { openNotificationCenterEvent } from "./notifications";
import { useReloadCoordinator } from "./reload-coordinator";
import { useShellConfig } from "./shell-config";
import { toast } from "@/components/ui/sonner";

const SEVERITY_ICONS: Record<NotificationSeverity, LucideIcon> = {
  info: Info,
  success: CircleCheck,
  warning: TriangleAlert,
  error: OctagonX,
};

const SEVERITY_CLASSES: Record<NotificationSeverity, string> = {
  info: "text-sky-11",
  success: "text-emerald-11",
  warning: "text-warning",
  error: "text-red-11",
};

function formatTimeAgo(timestamp: number): string {
  const elapsed = Date.now() - timestamp;
  if (elapsed < 60_000) return t("notifications.just_now");
  const minutes = Math.floor(elapsed / 60_000);
  if (minutes < 60) return `${minutes}m`;
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return `${hours}h`;
  const days = Math.floor(hours / 24);
  if (days < 7) return `${days}d`;
  return new Date(timestamp).toLocaleDateString();
}

/**
 * Notification bell with unread badge + popover panel. Mounted as a labelled
 * row in the main sidebar and as an icon in the settings header; hidden via
 * the `notifications` shell flag. Closing the panel marks everything read.
 */
export function NotificationBell({ variant = "icon" }: { variant?: "icon" | "sidebar-icon" }) {
  const { config } = useShellConfig();
  const [open, setOpen] = useState(false);
  const notifications = useNotificationStore((state) => state.notifications);
  const markAllRead = useNotificationStore((state) => state.markAllRead);
  const clearAll = useNotificationStore((state) => state.clearAll);
  const reloadCoordinator = useReloadCoordinator();
  const navigate = useNavigate();

  const notificationsListAction = useMemo<OmniRushControlAction>(() => ({
    id: "notifications.list",
    label: "List notifications",
    description: "Return the current notification center entries.",
    kind: "query",
    effects: { data: "read", ui: "none", external: false },
    sideEffect: "none",
    execute: () => notifications.map((n) => ({
      id: n.id,
      kind: n.kind,
      severity: n.severity,
      title: n.title,
      body: n.body,
      count: n.count,
      readAt: n.readAt,
      actionType: n.action?.type ?? null,
      actionLabel: n.actionLabel ?? null,
    })),
  }), [notifications]);
  useControlAction(notificationsListAction);

  const notificationToastAction = useMemo<OmniRushControlAction>(() => ({
    id: "notifications.show",
    label: "Show notification",
    description: "Show a short status notification in the desktop shell.",
    kind: "command",
    sideEffect: "none",
    requiresArgs: true,
    execute: (args) => {
      const value = args && typeof args === "object" ? args as Record<string, unknown> : {};
      const title = typeof value.title === "string" ? value.title : "OmniRush";
      const body = typeof value.body === "string" ? value.body : "";
      const severity = value.severity === "warning" || value.severity === "error" || value.severity === "success" ? value.severity : "info";
      const show = severity === "warning" ? toast.warning : severity === "error" ? toast.error : severity === "success" ? toast.success : toast.info;
      show(title, body ? { description: body } : undefined);
      return { shown: true };
    },
  }), []);
  useControlAction(notificationToastAction);

  const unreadCount = useMemo(
    () => notifications.filter((notification) => notification.readAt === null).length,
    [notifications],
  );
  const label = unreadCount > 0
    ? `${t("notifications.title")} (${unreadCount})`
    : t("notifications.title");

  useEffect(() => {
    const handler = () => setOpen(true);
    window.addEventListener(openNotificationCenterEvent, handler);
    return () => window.removeEventListener(openNotificationCenterEvent, handler);
  }, []);

  const handleOpenChange = useCallback(
    (next: boolean) => {
      setOpen(next);
      if (!next) markAllRead();
    },
    [markAllRead],
  );

  const runAction = useCallback(
    (notification: AppNotification) => {
      const action = notification.action;
      if (!action) return;
      setOpen(false);
      markAllRead();
      if (action.type === "open-model-picker") {
        requestOpenModelPicker(action.providerIds);
      } else if (action.type === "reload-engine") {
        void reloadCoordinator.reloadWorkspaceEngine();
      } else if (action.type === "open-extensions-marketplace") {
        navigate("/extensions");
      } else if (action.type === "install-marketplace-plugin") {
        navigate("/extensions");
      }
    },
    [markAllRead, navigate, reloadCoordinator],
  );

  if (!config.notifications) return null;

  return (
    <Popover open={open} onOpenChange={handleOpenChange}>
      <Tooltip>
      <TooltipTrigger render={<PopoverTrigger
        render={
          <Button
            variant="ghost"
            size="icon-sm"
            className={variant === "sidebar-icon"
              ? "relative size-7 rounded-md text-sidebar-foreground/70 hover:bg-sidebar-accent hover:text-sidebar-foreground"
              : "rounded-xl text-gray-10 transition-colors hover:bg-muted hover:text-foreground"}
            aria-label={label}
            data-testid="notification-bell"
          >
            <Bell size={variant === "sidebar-icon" ? 16 : 17} />
            {unreadCount > 0 ? (
              <span className="absolute right-0.5 top-0.5 flex min-w-3.5 items-center justify-center rounded-full bg-primary px-1 text-[9px] font-semibold leading-3 text-primary-foreground">
                {unreadCount > 9 ? "9+" : unreadCount}
              </span>
            ) : null}
          </Button>
        }
      />} />
      <TooltipContent side="bottom">{label}</TooltipContent>
      </Tooltip>
      <PopoverContent
        align={variant === "icon" ? "end" : "start"}
        side={variant === "icon" ? "bottom" : "right"}
        sideOffset={8}
        className="w-96 gap-0 p-0"
      >
        <div className="flex items-center justify-between border-b border-border px-4 py-2.5">
          <p className="text-sm font-semibold">{t("notifications.title")}</p>
          {notifications.length > 0 ? (
            <Button
              variant="ghost"
              size="sm"
              className="text-xs text-muted-foreground"
              onClick={clearAll}
            >
              {t("notifications.clear_all")}
            </Button>
          ) : null}
        </div>
        {notifications.length === 0 ? (
          <div className="flex flex-col items-center gap-1 px-6 py-10 text-center">
            <Bell className="mb-2 size-5 text-muted-foreground/60" />
            <p className="text-sm font-medium">{t("notifications.empty")}</p>
            <p className="text-xs text-muted-foreground">{t("notifications.empty_hint")}</p>
          </div>
        ) : (
          <div className="max-h-96 overflow-y-auto py-1">
            {notifications.map((notification) => (
              <NotificationRow
                key={notification.id}
                notification={notification}
                onAction={runAction}
              />
            ))}
          </div>
        )}
      </PopoverContent>
    </Popover>
  );
}

function NotificationRow({
  notification,
  onAction,
}: {
  notification: AppNotification;
  onAction: (notification: AppNotification) => void;
}) {
  const Icon = SEVERITY_ICONS[notification.severity];
  const unread = notification.readAt === null;
  const showCount =
    notification.count > 1 &&
    (notification.severity === "warning" || notification.severity === "error");

  return (
    <div
      className={cn(
        "flex items-start gap-3 px-4 py-3",
        unread ? "bg-primary/5" : "opacity-80",
      )}
    >
      <Icon className={cn("mt-0.5 size-4 shrink-0", SEVERITY_CLASSES[notification.severity])} />
      <div className="flex min-w-0 flex-1 flex-col gap-0.5">
        <div className="flex items-baseline justify-between gap-2">
          <p className="min-w-0 truncate text-sm font-medium">
            {notification.title}
            {showCount ? (
              <span className="ml-1.5 text-xs font-normal text-muted-foreground">
                ×{notification.count}
              </span>
            ) : null}
          </p>
          <span className="flex shrink-0 items-center gap-1.5 text-[11px] text-muted-foreground">
            {formatTimeAgo(notification.updatedAt)}
            {unread ? <span className="size-1.5 rounded-full bg-primary" /> : null}
          </span>
        </div>
        {notification.body ? (
          <p className="line-clamp-2 text-xs text-muted-foreground">{notification.body}</p>
        ) : null}
        {notification.action && notification.actionLabel ? (
          <div className="mt-1.5">
            <Button variant="outline" size="sm" onClick={() => onAction(notification)}>
              {notification.actionLabel}
            </Button>
          </div>
        ) : null}
      </div>
    </div>
  );
}