An open-source, faster Devin alternative
TypeScript
web: a Slack workspace that is already another organization's gets a welcome, not an error
Slack U06GFN8DJ7Pc7de0f6d884f
diff --git a/web/src/managed-agents/Computer.test.tsx b/web/src/managed-agents/Computer.test.tsxindex 694f90f3..8cc168f3 100644--- a/web/src/managed-agents/Computer.test.tsx+++ b/web/src/managed-agents/Computer.test.tsx@@ -201,6 +201,51 @@ describe('Computer page', () => {expect(container.textContent).toContain('Unlink')})+ it('welcomes someone whose Slack workspace is already another organization', async () => {+ computerApi.getComputer.mockResolvedValue(provisioning)++ render('/computer?slack=error&reason=team_already_linked¬ified=1')+ await settle(() => location() === '/computer', 'clean URL')+ await settle(+ () =>+ container.textContent?.includes('Your team is already here.') === true,+ 'already-linked panel',+ )+ const panel = container.querySelector(+ 'section[aria-labelledby="already-linked-title"]',+ )!+ expect(panel.textContent).toContain(+ "We've messaged the person who set it up in Slack to invite you.",+ )+ expect(panel.textContent).toContain('menu at the top left')+ expect(panel.textContent).toContain(+ 'Or connect a different Slack workspace',+ )+ expect(panel.textContent).toContain('Install Slack app')+ expect(container.textContent).not.toContain("Couldn't connect Slack")++ act(() =>+ (+ panel.querySelector('button[aria-label="Dismiss"]') as HTMLButtonElement+ ).click(),+ )+ expect(container.textContent).not.toContain('Your team is already here.')+ expect(location()).toBe('/computer')+ })++ it('asks for a teammate when nobody could be messaged', async () => {+ computerApi.getComputer.mockResolvedValue(provisioning)++ render('/computer?slack=error&reason=team_already_linked¬ified=0')+ await settle(+ () =>+ container.textContent?.includes('Ask a teammate who uses it') === true,+ 'already-linked panel without a notified owner',+ )+ expect(container.textContent).not.toContain("We've messaged")+ await settle(() => location() === '/computer', 'clean URL')+ })+it('sends an organization without projects to its computer', async () => {projectsApi.getManagedProjects.mockResolvedValue([])computerApi.getComputer.mockResolvedValue(provisioning)diff --git a/web/src/managed-agents/Computer.tsx b/web/src/managed-agents/Computer.tsxindex a6797e17..db6f1fd8 100644--- a/web/src/managed-agents/Computer.tsx+++ b/web/src/managed-agents/Computer.tsx@@ -16,6 +16,8 @@ import {Sparkles,Terminal,Unplug,+ UsersRound,+ X,} from 'lucide-react'import { ConfirmDialog } from '@/components/confirm-dialog'import { EmptyState } from '@/components/empty-state'@@ -49,6 +51,8 @@ import {} from './computer-api'import {EXAMPLE_ASKS,+ SLACK_RETURN_PARAMS,+ alreadyLinkedCopy,SESSION_STATUS,START_SESSION_COPY,compactStatusLine,@@ -95,7 +99,17 @@ export default function ComputerPage() {createNow()}, [status.data?.computer, createNow])- // Slack's install redirect lands here with ?slack=connected|error.+ // Slack's install redirect lands here with ?slack=connected|error. A+ // workspace that is another organization's gets a panel, not a toast: the+ // person is welcome, they just need an invite. The redirect is a full page+ // load, so the panel's state is read once, from the URL it arrived with;+ // the effect below then cleans the URL.+ const [alreadyLinked, setAlreadyLinked] = useState(() => {+ const slackReturn = slackReturnFromSearch(searchParams)+ return slackReturn?.kind === 'already_linked'+ ? { notified: slackReturn.notified }+ : null+ })const handledSlackReturn = useRef<string | null>(null)useEffect(() => {const slackReturn = slackReturnFromSearch(searchParams)@@ -105,13 +119,12 @@ export default function ComputerPage() {handledSlackReturn.current = keyif (slackReturn.kind === 'connected') {notifySuccess('Slack is connected.', START_SESSION_COPY)- } else {+ } else if (slackReturn.kind === 'error') {notifyError("Couldn't connect Slack.", new Error(slackReturn.message))}void queryClient.invalidateQueries({ queryKey: computerQueryKey })const next = new URLSearchParams(searchParams)- next.delete('slack')- next.delete('reason')+ for (const param of SLACK_RETURN_PARAMS) next.delete(param)setSearchParams(next, { replace: true })}, [queryClient, searchParams, setSearchParams])@@ -143,11 +156,19 @@ export default function ComputerPage() {const { live, recent } = splitSessions(sessions.data ?? [])const sessionsLoading = hasComputer && sessions.isLoadingconst createError = create.isError ? create.error : null+ const alreadyLinkedPanel = alreadyLinked ? (+ <AlreadyLinked+ notified={alreadyLinked.notified}+ status={status.data}+ onDismiss={() => setAlreadyLinked(null)}+ />+ ) : null// Once it is in Slack and knows the code, the page is about the work.if (computerIsOnboarded(status.data)) {return (<div className="mx-auto max-w-5xl space-y-8">+ {alreadyLinkedPanel}<CompactHeaderstatus={status.data}liveCount={live.length}@@ -180,6 +201,7 @@ export default function ComputerPage() {return (<div className="mx-auto max-w-5xl space-y-6">+ {alreadyLinkedPanel}<Herostatus={status.data}createError={createError}@@ -576,6 +598,62 @@ function Hero({)}+/**+ * Someone tried to link a Slack workspace that already works with+ * OpenComputer in another organization. Not an error: their team is already+ * here, and the way in is an invite (the person who set it up may have been+ * asked in Slack already).+ */+function AlreadyLinked({+ notified,+ status,+ onDismiss,+}: {+ notified: boolean+ status: ComputerStatus+ onDismiss: () => void+}) {+ const copy = alreadyLinkedCopy(notified)+ return (+ <section+ aria-labelledby="already-linked-title"+ className="from-primary/10 via-panel to-panel relative overflow-hidden rounded-2xl border bg-linear-to-br px-8 py-8 sm:px-10"+ >+ <Button+ variant="ghost"+ size="icon"+ className="text-muted-foreground absolute top-3 right-3"+ aria-label="Dismiss"+ onClick={onDismiss}+ >+ <X className="size-4" aria-hidden />+ </Button>+ <div className="flex gap-5">+ <div className="bg-primary/15 text-primary flex size-11 shrink-0 items-center justify-center rounded-full">+ <UsersRound className="size-5" aria-hidden />+ </div>+ <div className="max-w-2xl min-w-0 pr-8">+ <h2+ id="already-linked-title"+ className="text-2xl font-semibold tracking-tight text-balance"+ >+ {copy.headline}+ </h2>+ <p className="text-muted-foreground mt-2 text-balance">{copy.body}</p>+ {status.slack.installed ? null : (+ <div className="mt-6 flex flex-wrap items-center gap-3">+ <span className="text-muted-foreground text-sm">+ Or connect a different Slack workspace+ </span>+ <SlackInstallButton variant="outline" />+ </div>+ )}+ </div>+ </div>+ </section>+ )+}+const REASONS = [{icon: MessagesSquare,@@ -742,11 +820,6 @@ function SlackAction({}) {const queryClient = useQueryClient()const [confirmUnlink, setConfirmUnlink] = useState(false)- const install = useMutation({- mutationFn: startComputerSlackInstall,- onSuccess: (url) => window.location.assign(url),- onError: (error) => notifyError("Couldn't start the Slack install.", error),- })const unlink = useMutation({mutationFn: unlinkComputerSlack,onSuccess: async () => {@@ -760,22 +833,7 @@ function SlackAction({onError: (error) => notifyError("Couldn't unlink Slack.", error),})- if (!status.slack.installed) {- return (- <Button- // Stays pending through the navigation to Slack.- disabled={install.isPending || install.isSuccess}- onClick={() => install.mutate()}- >- {install.isPending || install.isSuccess ? (- <Loader2 className="size-4 animate-spin" aria-hidden />- ) : (- <Send className="size-4" aria-hidden />- )}- Install Slack app- </Button>- )- }+ if (!status.slack.installed) return <SlackInstallButton />const teamName = status.slack.teamName ?? 'this Slack workspace'return (<>@@ -802,6 +860,33 @@ function SlackAction({)}+function SlackInstallButton({+ variant = 'default',+}: {+ variant?: 'default' | 'outline'+}) {+ const install = useMutation({+ mutationFn: startComputerSlackInstall,+ onSuccess: (url) => window.location.assign(url),+ onError: (error) => notifyError("Couldn't start the Slack install.", error),+ })+ return (+ <Button+ variant={variant}+ // Stays pending through the navigation to Slack.+ disabled={install.isPending || install.isSuccess}+ onClick={() => install.mutate()}+ >+ {install.isPending || install.isSuccess ? (+ <Loader2 className="size-4 animate-spin" aria-hidden />+ ) : (+ <Send className="size-4" aria-hidden />+ )}+ Install Slack app+ </Button>+ )+}+function GitHubAction({ connected }: { connected: boolean }) {const [starting, setStarting] = useState(false)if (connected) {diff --git a/web/src/managed-agents/computer-state.test.ts b/web/src/managed-agents/computer-state.test.tsindex 5d394bb8..a08cb855 100644--- a/web/src/managed-agents/computer-state.test.ts+++ b/web/src/managed-agents/computer-state.test.ts@@ -20,6 +20,7 @@ import {heroCopy,relativeTime,sessionPollInterval,+ alreadyLinkedCopy,slackReturnFromSearch,splitSessions,} from './computer-state'@@ -250,6 +251,40 @@ describe('Slack install return', () => {})expect(slackReturnFromSearch(new URLSearchParams(''))).toBeNull()})++ it('reads an already-linked workspace and whether its owner was asked', () => {+ expect(+ slackReturnFromSearch(+ new URLSearchParams(+ 'slack=error&reason=team_already_linked¬ified=1',+ ),+ ),+ ).toEqual({ kind: 'already_linked', notified: true })+ expect(+ slackReturnFromSearch(+ new URLSearchParams(+ 'slack=error&reason=team_already_linked¬ified=0',+ ),+ ),+ ).toEqual({ kind: 'already_linked', notified: false })+ // Redirects from before `notified` existed: nobody was asked.+ expect(+ slackReturnFromSearch(+ new URLSearchParams('slack=error&reason=team_already_linked'),+ ),+ ).toEqual({ kind: 'already_linked', notified: false })+ expect(alreadyLinkedCopy(true).body).toContain(+ "We've messaged the person who set it up",+ )+ expect(alreadyLinkedCopy(false).body).toContain(+ 'Ask a teammate who uses it to invite you',+ )+ for (const notified of [true, false]) {+ expect(alreadyLinkedCopy(notified).headline).toBe(+ 'Your team is already here.',+ )+ }+ })})function session(diff --git a/web/src/managed-agents/computer-state.ts b/web/src/managed-agents/computer-state.tsindex 9da37b2d..dc7ab1e1 100644--- a/web/src/managed-agents/computer-state.ts+++ b/web/src/managed-agents/computer-state.ts@@ -166,27 +166,37 @@ const SLACK_RETURN_REASONS = new Map<string, string>([['cancelled', 'The Slack install was cancelled.'],['invalid_state', 'The install link expired. Start the install again.'],['state_expired', 'The install link expired. Start the install again.'],- [- 'already_linked',- 'That Slack workspace is already linked to another organization.',- ],- [- 'team_already_linked',- 'That Slack workspace is already linked to another organization.',- ],+])++/** Reasons that mean the workspace is already another organization's. */+const ALREADY_LINKED_REASONS = new Set([+ 'team_already_linked',+ 'already_linked',])export type SlackReturn =| { kind: 'connected' }| { kind: 'error'; message: string }+ /**+ * The workspace already works with OpenComputer in another organization.+ * `notified`: the person who set it up there was asked in Slack to invite+ * this person.+ */+ | { kind: 'already_linked'; notified: boolean }| null-/** Reads `?slack=connected|error&reason=` from Slack's install redirect. */+/** The query parameters Slack's install redirect adds, removed once handled. */+export const SLACK_RETURN_PARAMS = ['slack', 'reason', 'notified'] as const++/** Reads `?slack=connected|error&reason=¬ified=` from Slack's install redirect. */export function slackReturnFromSearch(search: URLSearchParams): SlackReturn {const slack = search.get('slack')if (slack === 'connected') return { kind: 'connected' }if (slack !== 'error') return nullconst reason = search.get('reason') ?? ''+ if (ALREADY_LINKED_REASONS.has(reason)) {+ return { kind: 'already_linked', notified: search.get('notified') === '1' }+ }return {kind: 'error',message:@@ -195,6 +205,19 @@ export function slackReturnFromSearch(search: URLSearchParams): SlackReturn {}}+/** What the already-linked panel says; the other organization is never named. */+export function alreadyLinkedCopy(notified: boolean): {+ headline: string+ body: string+} {+ return {+ headline: 'Your team is already here.',+ body: notified+ ? "Your Slack workspace already works with OpenComputer in another organization. We've messaged the person who set it up in Slack to invite you. Once you're invited, switch to that organization from the menu at the top left."+ : 'Your Slack workspace already works with OpenComputer in another organization. Ask a teammate who uses it to invite you, then switch to that organization from the menu at the top left.',+ }+}+export type HeroCopy = { headline: string; subline: string }/**