From 47c575b25ac955f382c24ad302693b6fd1080425 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sun, 1 Nov 2026 16:39:36 +0000 Subject: [PATCH] refactor(mobile): share one route-param parser Four mobile route files carried byte-identical copies of firstRouteParam, and app shortcuts kept a looser variant whose caller trimmed and rejected blank ids itself. Move the parser to lib/routeParams and import it everywhere. Every caller returns the same result as before. Co-Authored-By: Claude Opus 5.5 --- .../mobile/src/features/files/ThreadFilesRouteScreen.tsx | 6 +----- apps/mobile/src/features/shortcuts/appShortcuts.test.ts | 9 +++++++++ apps/mobile/src/features/shortcuts/appShortcuts.ts | 9 +-------- .../src/features/terminal/ThreadTerminalRouteScreen.tsx | 6 +----- apps/mobile/src/features/threads/ThreadRouteScreen.tsx | 6 +----- apps/mobile/src/lib/routeParams.ts | 5 +++++ apps/mobile/src/state/use-thread-selection.ts | 6 +----- 7 files changed, 19 insertions(+), 28 deletions(-) create mode 100644 apps/mobile/src/lib/routeParams.ts diff --git a/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx b/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx index 4844063f5d16..2c4dbc590730 100644 --- a/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx +++ b/apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx @@ -28,6 +28,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"; @@ -184,11 +185,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 016b75cfb305..20bc3f39fb13 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 a07aedfabf7e..7dab1cdff116 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -44,6 +44,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"; @@ -126,11 +127,6 @@ function InspectorPaneRoleActivation() { return null; } -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 ThreadRouteFallbackLayout(props: { readonly children: ReactNode }) { const goBack = useWorkspaceBackNavigation("threads"); const { layout } = useAdaptiveWorkspaceLayout(); 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 17a4a26a1862..42fe0985a4c2 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 "../state/entities"; import { useEnvironmentThread } from "../state/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"] {