diff --git a/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx b/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx index 11fa0a0c7af4..f4b8699c6c98 100644 --- a/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx +++ b/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx @@ -31,6 +31,7 @@ import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic"; import { isPdfFile } from "../../lib/filePreview"; import { tryOpenExternalUrl } from "../../lib/openExternalUrl"; import { useUniwindTheme } from "../../lib/useUniwindTheme"; +import { firstRouteParam } from "../../lib/routeParams"; import type { MediaVideoPreviewSource } from "../../lib/videoPreviewSource"; import { useMediaActions } from "../../state/mediaActions"; import { type MediaActionsSource } from "../../lib/mediaActionsSource"; @@ -175,11 +176,6 @@ function FileHeader(props: { type FileViewMode = "preview" | "source"; -function firstRouteParam(value: string | string[] | undefined): string | null { - const first = Array.isArray(value) ? value[0] : value; - return first === undefined || first.trim().length === 0 ? null : first; -} - function normalizeRoutePath(value: string | string[] | undefined): string | null { const path = Array.isArray(value) ? value.join("/") : value; if (path === undefined || path.trim().length === 0) { diff --git a/apps/mobile/src/features/shortcuts/appShortcuts.test.ts b/apps/mobile/src/features/shortcuts/appShortcuts.test.ts index 489e0f3857ed..4693c573caf6 100644 --- a/apps/mobile/src/features/shortcuts/appShortcuts.test.ts +++ b/apps/mobile/src/features/shortcuts/appShortcuts.test.ts @@ -80,6 +80,7 @@ describe("activeThreadRef", () => { { environmentId: " ", threadId: "thread-2" }, { environmentId: "env-1", threadId: " " }, { environmentId: 42, threadId: "thread-2" }, + { environmentId: null, threadId: "thread-2" }, { environmentId: { nested: true }, threadId: "thread-2" }, { environmentId: [], threadId: [] }, ]; @@ -87,6 +88,14 @@ describe("activeThreadRef", () => { expect(activeThreadRef(navState({ name: "Thread", params }))).toBe(null); } }); + + it("uses the trimmed first value of each param", () => { + const params = { environmentId: [" env-1 ", "env-2"], threadId: "thread-2 " }; + expect(activeThreadRef(navState({ name: "Thread", params }))).toEqual({ + environmentId: "env-1", + threadId: "thread-2", + }); + }); }); describe("launcher shortcut ids", () => { diff --git a/apps/mobile/src/features/shortcuts/appShortcuts.ts b/apps/mobile/src/features/shortcuts/appShortcuts.ts index 87898cbffd6c..d3306d651826 100644 --- a/apps/mobile/src/features/shortcuts/appShortcuts.ts +++ b/apps/mobile/src/features/shortcuts/appShortcuts.ts @@ -3,6 +3,7 @@ import type { NavigationState } from "@react-navigation/native"; import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; import type { RecentThreadShortcut } from "../../persistence/imperative"; +import { firstRouteParam } from "../../lib/routeParams"; const MAX_RECENT_THREAD_SHORTCUTS = 3; @@ -17,14 +18,6 @@ function threadShortcutHref(thread: RecentThreadShortcut): string { return `/threads/${encodeURIComponent(thread.environmentId)}/${encodeURIComponent(thread.threadId)}`; } -function firstRouteParam(value: string | string[] | undefined): string | null { - if (Array.isArray(value)) { - return value[0] ?? null; - } - - return value ?? null; -} - /** Never throw: malformed deep-link or restored ids can fail schema constructors during root-stack render. */ export function activeThreadRef(state: NavigationState): ScopedThreadRef | null { const route = state.routes[state.index]; diff --git a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx index 90fac17f8004..d859dad711c6 100644 --- a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx +++ b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx @@ -48,6 +48,7 @@ import { useEnvironmentQuery } from "../../state/query"; import { appAtomRegistry } from "../../state/atom-registry"; import { useAtomCommand } from "../../state/use-atom-command"; import { uuidv4 } from "../../lib/uuid"; +import { firstRouteParam } from "../../lib/routeParams"; import { useServerConfigs } from "../../state/entities"; import { useConnectionsReady } from "../../state/workspace"; import { @@ -200,11 +201,6 @@ type TerminalToolbarAction = readonly modifier: PendingModifier; }; -function firstRouteParam(value: string | string[] | undefined): string | null { - const first = Array.isArray(value) ? value[0] : value; - return first === undefined || first.trim().length === 0 ? null : first; -} - function inferHostPlatform(environmentLabel: string | null): HostPlatform { const value = environmentLabel?.toLowerCase() ?? ""; if ( diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index ab0b5cdb9bd4..f2fb3cc40d41 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -50,6 +50,7 @@ import { vcsEnvironment } from "../../state/vcs"; import { EmptyState } from "../../components/ui/EmptyState"; import { LoadingScreen } from "../../components/LoadingScreen"; import { scopedThreadKey } from "../../lib/scopedEntities"; +import { firstRouteParam } from "../../lib/routeParams"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { useThreadShell } from "../../state/entities"; import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass"; @@ -125,11 +126,6 @@ import { useStartSideChat } from "./useStartSideChat"; import type { ThreadInspectorMode, ThreadInspectorTarget } from "./thread-inspector-navigation"; import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail"; -function firstRouteParam(value: string | string[] | undefined): string | null { - const first = Array.isArray(value) ? value[0] : value; - return first === undefined || first.trim().length === 0 ? null : first; -} - // The inspector pane's bar belongs to ThreadInspectorHeader; the fallback must not draw its own. const InspectorHeaderShownContext = createContext(false); diff --git a/apps/mobile/src/lib/routeParams.ts b/apps/mobile/src/lib/routeParams.ts new file mode 100644 index 000000000000..06fd50ada4f2 --- /dev/null +++ b/apps/mobile/src/lib/routeParams.ts @@ -0,0 +1,5 @@ +/** Blank counts as missing; a present value is returned untrimmed. */ +export function firstRouteParam(value: string | string[] | undefined): string | null { + const first = Array.isArray(value) ? value[0] : value; + return first === undefined || first.trim().length === 0 ? null : first; +} diff --git a/apps/mobile/src/state/use-thread-selection.ts b/apps/mobile/src/state/use-thread-selection.ts index 35598d74bc36..84c01edbaa9f 100644 --- a/apps/mobile/src/state/use-thread-selection.ts +++ b/apps/mobile/src/state/use-thread-selection.ts @@ -20,6 +20,7 @@ import { import * as Option from "effect/Option"; import { scopedThreadKey } from "../lib/scopedEntities"; +import { firstRouteParam } from "../lib/routeParams"; import { useProject, useQuietThreadShell } from "./entities"; import { useEnvironmentThread } from "./threads"; import { @@ -39,11 +40,6 @@ type ThreadSelectionRouteParams = { readonly threadId?: string | string[]; }; -function firstRouteParam(value: string | string[] | undefined): string | null { - const first = Array.isArray(value) ? value[0] : value; - return first === undefined || first.trim().length === 0 ? null : first; -} - function latestUserMessageAt( projection: OrchestrationV2ThreadProjection, ): OrchestrationV2ThreadShell["latestUserMessageAt"] {