"use client" import { useQuery } from "@tanstack/react-query" import { cn } from "@lib/utils" import { Check, Loader2, Lock, Plus, X } from "lucide-react" import { useState } from "react" import { useAuth } from "@lib/auth-context" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@ui/components/command" import { Popover, PopoverContent, PopoverTrigger } from "@ui/components/popover" import { type BrainChannelProactivity, type BrainProactivityDefault, useBrainSettings, useUpdateBrainSettings, } from "@/hooks/use-brain-settings" import { useHasCompanyBrain } from "@/hooks/use-company-brain" import { useOrgMemberRole } from "@/hooks/use-org-member-role" import { dmSans125ClassName } from "@/lib/fonts" const BACKEND = process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai" type Channel = { id: string; name: string; isPrivate: boolean } const HOME_CHANNEL_NAME = "company-brain" const MODES: { id: BrainProactivityDefault label: string description: string }[] = [ { id: "all_channels", label: "All channels", description: "Joins any conversation it's been added to when it can help.", }, { id: "own_channel_only", label: "Only its own channel", description: "Speaks only in #company-brain unless @mentioned or DMed.", }, ] const fieldLabel = cn( dmSans125ClassName(), "text-[11px] font-medium uppercase tracking-[0.06em] text-[#5B6675]", ) const selectContentClass = cn( dmSans125ClassName(), "rounded-[10px] border-white/[0.08] bg-[#1B1F24] text-[#FAFAFA] shadow-[0px_8px_24px_rgba(0,0,0,0.5)]", ) const commandItemClass = "cursor-pointer rounded-[8px] text-[13px] text-[#FAFAFA] data-[selected=true]:bg-white/10 data-[selected=true]:text-white" export default function CompanyBrainProactivity() { const isCompanyBrain = useHasCompanyBrain() const { isAdmin } = useOrgMemberRole(isCompanyBrain) const { org } = useAuth() const settingsQuery = useBrainSettings(isCompanyBrain) const update = useUpdateBrainSettings() const [pickerOpen, setPickerOpen] = useState(false) const slackStatusQuery = useQuery({ queryKey: ["brain", "slack-status", org?.id], queryFn: async () => { const res = await fetch(`${BACKEND}/brain/slack/status`, { credentials: "include", }) if (!res.ok) throw new Error("Failed to load Slack status") return (await res.json()) as { connected: boolean } }, enabled: isCompanyBrain, staleTime: 60_000, }) // Same key + endpoint as the automations picker so react-query dedupes. const channelsQuery = useQuery({ queryKey: ["company-brain-automations", "channels", org?.id], queryFn: async () => { const res = await fetch(`${BACKEND}/brain/automations/channels`, { credentials: "include", }) if (!res.ok) throw new Error("Failed to load channels") return ((await res.json()) as { channels: Channel[] }).channels ?? [] }, enabled: isCompanyBrain, staleTime: 60_000, }) if (!isCompanyBrain) { return (
Company Brain isn't enabled for this organization.
Couldn't load proactivity settings.
) : ({slackStatusQuery.data?.connected === false ? "Connect Slack to set per-channel exceptions." : "Invite Company Brain to a Slack channel to list it here."}
)}