Skip to content

An open-source, faster Devin alternative

TypeScript

Slack wizard: an active automated setup is shown inline on both steps

Igor Zalutskic8529cd54f89
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
index d4e952a0..843bd91f 100644
--- a/docs/agents/api.mdx
+++ b/docs/agents/api.mdx
@@ -540,7 +540,7 @@ Error codes, as HTTP errors or as `error.code` on the setup:
| Code | Meaning |
| --- | --- |
| `slack_setup_conflict` | The request key was already used with a different target or name |
-| `slack_setup_active` | Another setup is in progress for the target; `error.setupId` names it, and finding it by target returns the same record |
+| `slack_setup_active` | Another setup is in progress for the target; `error.setupId` names it, and finding it by target returns the same record. Also answered by the manual create or reconnect (`POST /channels/slack/connections`) while an automated setup works on the target |
| `slack_already_connected` | The target already has a connected app; disconnect it first |
| `slack_setup_unavailable` | Automatic setup is not available; use manual setup |
| `slack_setup_not_found` | `404`: no setup with that id in this project |
@@ -549,7 +549,6 @@ Error codes, as HTTP errors or as `error.code` on the setup:
| `slack_setup_busy` | `409` on cancel: the setup is creating or exchanging; wait for it to finish |
| `slack_setup_cancelled` | Recorded on a setup cancelled after the app was created or its creation was uncertain: the Slack app may still appear in the workspace's app list and can be removed there |
| `slack_manual_completion_blocked` | `409` on the manual credential save (`PUT /channels/slack/connections/<id>`): an automated setup owns the connection, or the pasted manifest is no longer current; cancel the setup, generate a new manifest, then enter credentials |
-| `slack_connection_changed` | `409` on the manual create or reconnect (`POST /channels/slack/connections`): the connection changed while the request was in flight, for example the automated setup connected it; reload before trying again |
| `slack_configuration_token_invalid`, `slack_configuration_token_expired` | The token was rejected; generate a new one and submit it with the same key |
| `slack_manifest_rejected` | Slack rejected the generated app configuration; `pointer` names the field. Not recoverable in place: fix the declaration, redeploy, cancel the setup and start again |
| `slack_app_limit_reached` | The workspace cannot hold another app |
diff --git a/web/src/managed-agents/SlackWizard.test.tsx b/web/src/managed-agents/SlackWizard.test.tsx
index 71a22ed8..58689822 100644
--- a/web/src/managed-agents/SlackWizard.test.tsx
+++ b/web/src/managed-agents/SlackWizard.test.tsx
@@ -24,6 +24,8 @@ const BLOCKED =
'Manual completion is blocked: cancel the automated setup for this connection, then generate a new manifest (Reconnect) before entering credentials.'
const CHANGED =
'This connection changed while the request was in flight. Reload the page to see its current state before trying again.'
+const ACTIVE =
+ 'A Slack setup is already in progress for this agent and environment. Resume it instead of starting another.'
// A connection whose events are rejected: the wizard offers Edit credentials,
// which starts at the details step without a manifest round trip.
@@ -226,4 +228,69 @@ describe('ManagedSlackWizard blocked manual completion', () => {
queryKey: ['managed-slack-setup'],
})
})
+
+ it('shows an active automated setup inline on the manifest step and the credential step, pointing at its card', async () => {
+ const active = () =>
+ new ApiError(ACTIVE, 409, 'slack_setup_active', { setupId: 'setup_9' })
+ const anchor = document.createElement('div')
+ anchor.id = slackSetupAnchorId({
+ agentId: 'coder',
+ alias: 'development',
+ channelId: undefined,
+ })
+ const scrollIntoView = vi.fn()
+ anchor.scrollIntoView = scrollIntoView
+ document.body.append(anchor)
+ const invalidate = vi.spyOn(client, 'invalidateQueries')
+
+ // Manifest step, no connection yet.
+ api.startManagedAgentSlack.mockReset()
+ api.startManagedAgentSlack.mockRejectedValue(active())
+ render(undefined, null)
+ act(() => button(container, 'Connect Slack').click())
+ await settle(
+ () => document.body.querySelector('#managed-slack-app-name') !== null,
+ 'the create step',
+ )
+ act(() => button(document.body, 'Generate Slack manifest').click())
+ await settle(
+ () => document.body.querySelector('[role="alert"]') !== null,
+ 'the active notice on the manifest step',
+ )
+ let alert = document.body.querySelector('[role="alert"]') as HTMLElement
+ expect(alert.textContent).toContain(ACTIVE)
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: ['managed-agent-channels'],
+ })
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: ['managed-slack-setup'],
+ })
+ act(() => button(alert, 'Show the automated setup').click())
+ await settle(
+ () => document.body.querySelector('#managed-slack-app-name') === null,
+ 'the dialog to close',
+ )
+ expect(scrollIntoView).toHaveBeenCalledTimes(1)
+
+ // Credential step, pending connection.
+ act(() => root.unmount())
+ root = createRoot(container)
+ document.body.replaceChildren(container, anchor)
+ invalidate.mockClear()
+ api.completeManagedAgentSlack.mockReset()
+ api.completeManagedAgentSlack.mockRejectedValue(active())
+ render()
+ await saveCredentials()
+ alert = document.body.querySelector('[role="alert"]') as HTMLElement
+ expect(alert.textContent).toContain(ACTIVE)
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: ['managed-slack-setup'],
+ })
+ act(() => button(alert, 'Show the automated setup').click())
+ await settle(
+ () => document.body.querySelector('#managed-slack-token') === null,
+ 'the dialog to close',
+ )
+ expect(scrollIntoView).toHaveBeenCalledTimes(2)
+ })
})
diff --git a/web/src/managed-agents/SlackWizard.tsx b/web/src/managed-agents/SlackWizard.tsx
index 0daf93ea..eb6f87fe 100644
--- a/web/src/managed-agents/SlackWizard.tsx
+++ b/web/src/managed-agents/SlackWizard.tsx
@@ -205,23 +205,40 @@ export function ManagedSlackWizard({
const [copied, markCopied] = useTransientFlag(1500)
// The platform's reason a manual save is refused while an automated setup
// owns this connection or the pasted manifest is stale.
- const [blocked, setBlocked] = useState<string>()
- // The connection changed under this request (the automated flow may have
- // connected it): what the page knows is stale and is re-read.
- const [changed, setChanged] = useState<string>()
+ // A platform refusal the person can act on from here: an automated setup
+ // already working on this target (resume it on its card), a manual
+ // completion that setup blocks, or a connection that changed under the
+ // request. The first and last make what the page knows stale; it is
+ // re-read.
+ const [notice, setNotice] = useState<{
+ message: string
+ pointToSetup: boolean
+ }>()
const setupAnchor = slackSetupAnchorId({ agentId, alias, channelId })
- const setupOnPage = Boolean(blocked && document.getElementById(setupAnchor))
- const connectionChanged = (error: unknown) => {
- if (
- !(error instanceof ApiError) ||
- error.type !== 'slack_connection_changed'
- ) {
- return false
- }
- setChanged(error.message)
+ const setupOnPage = Boolean(
+ notice?.pointToSetup && document.getElementById(setupAnchor),
+ )
+ const reread = () => {
void queryClient.invalidateQueries({ queryKey: ['managed-agent-channels'] })
void queryClient.invalidateQueries({ queryKey: ['managed-slack-setup'] })
- return true
+ }
+ const refusal = (error: unknown) => {
+ if (!(error instanceof ApiError)) return false
+ switch (error.type) {
+ case 'slack_setup_active':
+ reread()
+ setNotice({ message: error.message, pointToSetup: true })
+ return true
+ case 'slack_manual_completion_blocked':
+ setNotice({ message: error.message, pointToSetup: true })
+ return true
+ case 'slack_connection_changed':
+ reread()
+ setNotice({ message: error.message, pointToSetup: false })
+ return true
+ default:
+ return false
+ }
}
const invalidate = () =>
@@ -240,7 +257,7 @@ export function ManagedSlackWizard({
void invalidate()
},
onError: (error) => {
- if (connectionChanged(error)) return
+ if (refusal(error)) return
notifyError("Couldn't prepare the Slack app.", error)
},
})
@@ -262,14 +279,7 @@ export function ManagedSlackWizard({
void invalidate()
},
onError: (error) => {
- if (connectionChanged(error)) return
- if (
- error instanceof ApiError &&
- error.type === 'slack_manual_completion_blocked'
- ) {
- setBlocked(error.message)
- return
- }
+ if (refusal(error)) return
notifyError('Slack rejected those values. Double-check them.', error)
},
})
@@ -283,8 +293,7 @@ export function ManagedSlackWizard({
})
const reset = () => {
- setBlocked(undefined)
- setChanged(undefined)
+ setNotice(undefined)
setStep('create')
setEditing(false)
setManifest(undefined)
@@ -350,6 +359,27 @@ export function ManagedSlackWizard({
return () => window.clearInterval(interval)
}, [eventVerified, open, queryClient, step, verificationFailed])
+ const noticeElement = notice ? (
+ <WizardNotice message={notice.message}>
+ {notice.pointToSetup ? (
+ setupOnPage ? (
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ onClick={showAutomatedSetup}
+ >
+ Show the automated setup
+ </Button>
+ ) : connectionsHref ? (
+ <Button asChild variant="outline" size="sm">
+ <Link to={connectionsHref}>Open Connections</Link>
+ </Button>
+ ) : null
+ ) : null}
+ </WizardNotice>
+ ) : null
+
return (
<>
<div className="flex flex-wrap items-center justify-between gap-4 border-b px-5 py-4">
@@ -510,7 +540,7 @@ export function ManagedSlackWizard({
</pre>
</>
)}
- {changed ? <WizardNotice message={changed} /> : null}
+ {noticeElement}
<DialogFooter>
<Button variant="ghost" onClick={() => setOpen(false)}>
Cancel
@@ -588,25 +618,7 @@ export function ManagedSlackWizard({
placeholder="xoxb-…"
/>
</Field>
- {changed ? <WizardNotice message={changed} /> : null}
- {blocked ? (
- <WizardNotice message={blocked}>
- {setupOnPage ? (
- <Button
- type="button"
- variant="outline"
- size="sm"
- onClick={showAutomatedSetup}
- >
- Show the automated setup
- </Button>
- ) : connectionsHref ? (
- <Button asChild variant="outline" size="sm">
- <Link to={connectionsHref}>Open Connections</Link>
- </Button>
- ) : null}
- </WizardNotice>
- ) : null}
+ {noticeElement}
<DialogFooter>
<Button
type="button"