ChatbotX icon indicating copy to clipboard operation
ChatbotX copied to clipboard

Channel picker forwards the whole query string when switching channel: the previous channel's error message follows the operator, and a repeated `channel` param arrives as an array behind a `string | null` type

Open rawdaymx opened this issue 2 months ago • 1 comments

Summary

handleInboxSelect rebuilds the URL by concatenating the current query string with a new channel:

// apps/builder/src/features/inboxes/components/inbox-select-card.tsx:45-52
const handleInboxSelect = useCallback(
  (channel: ChannelType) => {
    router.push(
      `/channels/create?${searchParams.toString()}&channel=${channel}`,
    )
  },
  [router, searchParams],
)

Two things follow from that one line, and they are independent — neither causes the other.

1. The error message follows the operator to a channel where it never happened. ?error= is this page's own parameter: create/page.tsx:186-187 reads searchParams.error and renders it. An operator bounced back here with ?error=workspaceLimitReached who then picks a different channel carries that reason into the new attempt, where it no longer describes anything.

2. A repeated channel parameter arrives as an array while the type says it cannot. Because the new channel is appended rather than replacing the old one, a URL that already carries channel= ends up with two. Next's own parser turns that into an array, and create/page.tsx:29 declares channel?: string | null. Every selectedChannel === "..." comparison — there are ten — is then array === string, always false, so no channel branch is taken and the page silently re-renders the picker, ignoring the click.

Environment

Measured against upstream/main @ 96032013e2313891f816fcf8400412b44cf158a2
How reading the repository at that commit, plus node against the installed next for the query parsing. Not measured against a running server
Next 16.3.0 (the version installed in this repo)
Date 2026-09-11

Defect 1 — the reason follows you

The route in, traced in the repository:

  1. An operator with no workspace opens /channels/create and picks a channel.
  2. The provider's OAuth round-trip returns to callback.ts:323 (or channels/create/messenger/route.ts:67 for the Facebook SSO-reuse path).
  3. createFirstWorkspace fails on a plan limit, and createChannelErrorPath (create-first-workspace.ts:34) redirects to /channels/create?error=<code>.
  4. The picker renders with the destructive <Alert> — create/page.tsx:186-193 → inbox-select-card.tsx:64-66. Correct so far.
  5. The operator picks another channel. handleInboxSelect forwards error=<code> into the new URL, and the new attempt still shows the previous one's reason.

The three codes this can carry are workspaceLimitReached, trialExpired and macLimitReached (create-first-workspace.ts:16-19).

Defect 2 — the repeated parameter

Measured with the parser Next actually uses, at the version installed here:

$ node -e 'const sp=new URLSearchParams("channel=messenger&error=x");
           console.log(sp.toString() + "&channel=telegram")'
channel=messenger&error=x&channel=telegram

$ node -e 'const {parseRelativeUrl}=require("next/dist/shared/lib/router/utils/parse-relative-url");
           console.log(JSON.stringify(parseRelativeUrl(
             "/channels/create?channel=messenger&error=x&channel=telegram").query))'
{"channel":["messenger","telegram"],"error":"x"}

searchParamsToUrlQuery (next/dist/shared/lib/router/utils/querystring.js) keeps the first occurrence as a string and converts to [existing, value] on the second. That object is what reaches the Server Component as searchParams.

Against a declared type that does not allow it:

$ git show 96032013e:apps/builder/src/app/\(no-sidebar\)/channels/create/page.tsx | sed -n '27,34p'
type CreateChannelPageProps = {
  searchParams: Promise<{
    channel?: string | null

and ten comparisons that all become false:

$ git show 96032013e:.../create/page.tsx | grep -n 'selectedChannel'
44:  const selectedChannel = searchParams.channel
67:  if (selectedChannel === "telegram" && isVisible("telegram")) {
71, 75, 107, 121, 131, 136, 151, 162, 167 …

When is this reachable? It needs the URL to already carry a channel when the operator clicks. That happens whenever a ?channel= value does not match any branch — a channel with no credential configured, one hidden by the visibility policy, or a stale value — because create/page.tsx then falls through to return <InboxSelectCard …/> (:190-196) with the parameter still in the URL. The next click appends the second one.

It is not reachable on the first click after an error, and we want to be precise about that: createChannelErrorPath builds /channels/create?error=<code> with no channel, so that URL carries only the newly appended one. The two defects share a line, not a scenario.

Why we think this is a defect rather than the intended design

Forwarding the query is clearly intentional in one respect: workspaceId has to survive the channel switch, and concatenating is the shortest way to keep it. So the goal is right.

What we think is unintended is that the mechanism is "forward everything" rather than "forward what still applies". ?error= is documented in the page's own props as "Set by the OAuth callback / SSO-reuse route when the first-channel workspace could not be created (plan limit)" — a statement about one specific attempt, which stops being true the moment the operator picks a different channel. And &channel= appended to a string that may already contain channel= is a URL-construction slip rather than a decision: the declared type shows nobody expected two.

Suggested fix

Build the query through URLSearchParams so the new channel replaces rather than appends, and drop the parameter that no longer applies:

 const handleInboxSelect = useCallback(
   (channel: ChannelType) => {
-    router.push(
-      `/channels/create?${searchParams.toString()}&channel=${channel}`,
-    )
+    const params = new URLSearchParams(searchParams.toString())
+    // The reason for a stumble does not travel to the next channel: explaining
+    // a Messenger failure on the Telegram screen describes something that did
+    // not happen there.
+    params.delete("error")
+    params.set("channel", channel)
+    router.push(`/channels/create?${params.toString()}`)
   },
   [router, searchParams],
 )

set fixes defect 2 by construction; delete fixes defect 1. workspaceId and anything else still rides along.

We have this on our fork and can send the PR — it is this one function.

If you would rather keep ?error= across a channel switch deliberately, defect 2 still stands on its own and the set half is worth taking alone.

What we did not verify

  • We did not run a Next server end-to-end. We executed the two functions Next uses for this leg (parseRelativeUrl → searchParamsToUrlQuery) at the installed version, and read the path from there to createServerSearchParamsForServerPage. The final step — that a Server Component's searchParams.channel is that same array — follows from the code we read, not from an observed render.
  • We did not check whether any code downstream already tolerates an array channel; we only observed that the ten comparisons on this page do not.
  • We did not look at whether other pages build URLs the same way. This is not searched, not proven absent.
  • We did not measure how often a non-matching ?channel= occurs in practice; we only established that the page falls through to the picker when it does.

Related issues

We searched open and closed issues and PRs for channels create error and connect picker and found nothing describing either defect. #1107 introduced the errorMessageKey rendering this forwards, but not handleInboxSelect, which predates it.

rawdaymx avatar Sep 12 '26 05:09 rawdaymx

Re-verified after #1125 bumped Next to 16.3.3: the measurement in the report still holds.

dist/shared/lib/router/utils/querystring.js is byte-identical between 16.3.0 and 16.3.3 (diff over the whole file returns nothing), so searchParamsToUrlQuery still keeps the first occurrence of a key as a string and converts to [existing, value] on the second. Nothing in the report needs restating for the newer version.

Also checked that none of the six commits between 96032013e (what the report was measured against) and be7234b93 touch inbox-select-card.tsx or channels/create/page.tsx.

rawdaymx avatar Sep 12 '26 06:09 rawdaymx