"use client" import { useCallback, useEffect, useRef, useState } from "react" import { useRouter } from "next/navigation" import { LogoFull } from "@ui/assets/Logo" import { Button } from "@ui/components/button" import { AlertTriangle, ChevronRight, Loader2, RotateCw } from "lucide-react" import { useAuth } from "@lib/auth-context" import { cn } from "@lib/utils" import { type BrainEntryOrganization, resolveCompanyBrainEntry, } from "@/lib/company-brain-entry" import { dmSans125ClassName, dmSansClassName } from "@/lib/fonts" import { generateOrgSlug } from "@/components/onboarding-brain/types" const BACKEND = process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai" const modalCardStyle = { boxShadow: "0 2.842px 14.211px 0 rgba(0, 0, 0, 0.25), 0.711px 0.711px 0.711px 0 rgba(255, 255, 255, 0.10) inset", } const inputBevelStyle = { boxShadow: "0px 1px 2px 0px rgba(0,43,87,0.1), inset 0px 0px 0px 1px rgba(43,49,67,0.08), inset 0px 1px 1px 0px rgba(0,0,0,0.08), inset 0px 2px 4px 0px rgba(0,0,0,0.02)", } export default function BrainEntryPage() { const router = useRouter() const { user, org, organizations, isRestoring, setActiveOrg } = useAuth() const { email = null } = user ?? {} const [error, setError] = useState(null) const [choices, setChoices] = useState(null) const [closed, setClosed] = useState(false) const [attempt, setAttempt] = useState(0) const startedRef = useRef(false) const continueWithOrganization = useCallback( async (organization: BrainEntryOrganization) => { if (org?.id !== organization.id) { await setActiveOrg(organization.slug) } const status = await fetch(`${BACKEND}/brain/slack/status`, { credentials: "include", headers: { "X-App-Source": "nova" }, }) .then((res) => (res.ok ? res.json() : null)) .catch(() => null) if (status?.connected) { router.replace("/") return } window.location.href = `${BACKEND}/brain/slack/oauth/install` }, [org?.id, router, setActiveOrg], ) const run = useCallback(async () => { const organizationsWithActiveMetadata = (organizations ?? []).map( (organization) => organization.id === org?.id ? { ...organization, metadata: org.metadata } : organization, ) const decision = resolveCompanyBrainEntry( org?.id, organizationsWithActiveMetadata, ) if (decision.action === "use" || decision.action === "switch") { await continueWithOrganization(decision.organization) return } if (decision.action === "choose") { setChoices(decision.organizations) return } setClosed(true) }, [continueWithOrganization, org, organizations]) const handleChoice = useCallback( (organization: BrainEntryOrganization) => { setChoices(null) setError(null) continueWithOrganization(organization).catch((e) => { startedRef.current = false console.error("Company Brain organization selection failed:", e) setError(e instanceof Error ? e.message : "Something went wrong.") }) }, [continueWithOrganization], ) // Sole caller of run(): the guard is only released on failure, so a dep change // mid-flight can't kick off a second org creation. // biome-ignore lint/correctness/useExhaustiveDependencies: attempt retriggers the retry useEffect(() => { if (!user || organizations === null || isRestoring || startedRef.current) return startedRef.current = true run().catch((e) => { startedRef.current = false console.error("Brain entry failed:", e) setError(e instanceof Error ? e.message : "Something went wrong.") }) }, [user, organizations, isRestoring, run, attempt]) return ( {closed ? (

New signups are paused

Company Brain isn't accepting new workspaces right now. If you have questions, reach us at support@supermemory.com.

) : choices ? (

Choose your Company Brain

You're a member of more than one workspace. Pick the one to open.

{choices.map((organization) => ( ))}
{email && (

Signed in as {email}

)}
) : error ? (

Couldn't set up your Company Brain

{error}

) : (

Setting up your Company Brain

Preparing your workspace, then we'll connect it to Slack.

)}
) } function EntryShell({ children }: { children: React.ReactNode }) { return (
{children}
) }