diff --git a/apps/app/src/features/workspace/entry-url.test.ts b/apps/app/src/features/workspace/entry-url.test.ts index 12edd5fbe..da604570a 100644 --- a/apps/app/src/features/workspace/entry-url.test.ts +++ b/apps/app/src/features/workspace/entry-url.test.ts @@ -1353,7 +1353,7 @@ test("workspace preserves bot and message requests while routing native decision }, { requestAnimationFrame: (run: () => void) => run(), setInterval: () => 1, clearInterval() {} }) const stack = { Screen: 'Screen', Protected: 'Protected' } const layout = componentHarness('../../../../../apps/mobile/app/_layout.tsx', { - 'expo-router': { Stack: stack, usePathname: () => '/' }, 'react-native-safe-area-context': { useSafeAreaInsets: () => ({ bottom: 0 }) }, 'expo-linking': { useLinkingURL: () => null }, '@/data/live': { LiveWorkspace: 'LiveWorkspace', useLive: () => shared, useWaiting: () => 0 }, + 'expo-router': { Stack: stack, useRouter: () => ({}), usePathname: () => '/' }, 'react-native-safe-area-context': { useSafeAreaInsets: () => ({ bottom: 0 }) }, 'expo-linking': { useLinkingURL: () => null }, '@/data/live': { LiveWorkspace: 'LiveWorkspace', useLive: () => shared, useWaiting: () => 0 }, '@/data/context': { useWorkspace: () => ({ session: { phase: 'ready' } }) }, '@vibeface/client/identity/context.tsx': { useDevice: () => device }, 'uniwind': { useCSSVariable: () => ['white', 'black'] }, '@/lib/layout': { useWide: () => false }, 'react-native': { Platform: { OS: 'ios' }, View: 'View' }, }) @@ -1907,6 +1907,8 @@ function componentHarness(path: string, dependencies: Record, g focusEffects.set(index, { run, cleanup: focused ? run() ?? undefined : undefined }) }) } + const nativeRouterModule = dependencies['expo-router'] + const nativeRouter = typeof nativeRouterModule === 'object' && nativeRouterModule !== null && 'router' in nativeRouterModule ? nativeRouterModule.router : {} const require = (specifier: string): unknown => { if (specifier === '@vibeface/client/workspace/model.ts') { const override = dependencies[specifier] @@ -1927,7 +1929,7 @@ function componentHarness(path: string, dependencies: Record, g if (specifier === '@/platform/files') return dependencies[specifier] ?? { privateFiles: { run: async (operation: (current: () => boolean) => Promise) => operation(() => true), retain: (uri: string) => ({ uri }), release() {}, releasePicker() {} } } if (specifier === '@/platform') return { mobilePlatform: { storage: platformStorage }, ...dependencies[specifier] as object } if (specifier === '@tanstack/react-router') return { Link: 'Link', useNavigate: () => () => {}, useRouterState: ({ select }: { select: (state: unknown) => unknown }) => select({ location: { search: {} } }), ...dependencies[specifier] as object } - return specifier === 'react' ? react : specifier === 'react/jsx-runtime' ? { jsx, jsxs: jsx } : specifier === 'expo-router' ? { Link: 'Link', useFocusEffect, ...dependencies[specifier] as object } : dependencies[specifier] ?? ui + return specifier === 'react' ? react : specifier === 'react/jsx-runtime' ? { jsx, jsxs: jsx } : specifier === 'expo-router' ? { Link: 'Link', useFocusEffect, useRouter: () => nativeRouter, ...dependencies[specifier] as object } : dependencies[specifier] ?? ui } const filename = fileURLToPath(new URL(path, import.meta.url)) const source = readFileSync(filename, 'utf8') + (privateExport ? `\nexport { ${privateExport} }\n` : '') @@ -3629,7 +3631,7 @@ test('native Stack focus restores non-chat views while the chat route owns its a const views: string[] = [] const stack = { Screen: 'Screen', Protected: 'Protected' } const app = componentHarness('../../../../../apps/mobile/app/_layout.tsx', { - 'expo-router': { Stack: stack, usePathname: () => '/' }, 'react-native-safe-area-context': { useSafeAreaInsets: () => ({ bottom: 0 }) }, 'expo-linking': { useLinkingURL: () => null }, + 'expo-router': { Stack: stack, useRouter: () => ({}), usePathname: () => '/' }, 'react-native-safe-area-context': { useSafeAreaInsets: () => ({ bottom: 0 }) }, 'expo-linking': { useLinkingURL: () => null }, '@/data/live': { LiveWorkspace: 'LiveWorkspace', useLive: () => ({ setView: (view: string) => { views.push(view) } }), useWaiting: () => 0 }, '@/data/context': { useWorkspace: () => ({ session: { phase: 'ready' } }) }, '@vibeface/client/identity/context.tsx': { useDevice: () => ({}) }, 'uniwind': { useCSSVariable: () => ['white', 'black'] }, '@/lib/layout': { useWide: () => false }, 'react-native': { Platform: { OS: 'ios' }, View: 'View' }, @@ -5191,7 +5193,7 @@ test('the native switch confirmation fixes the decoded entry and never erases fo let url = link, erased = 0, canceled = 0 const kept: (string | null)[] = [], failures: unknown[] = [] const app = componentHarness('../../../../../apps/mobile/app/switch-network.tsx', { - 'expo-router': { router: { back: () => { canceled++ } }, useLocalSearchParams: () => ({ link }) }, + 'expo-router': { router: { back() { throw new Error('module router is unbound') } }, useRouter: () => ({ back: () => { canceled++ } }), useLocalSearchParams: () => ({ link }) }, 'expo-linking': { useLinkingURL: () => url }, '@vibeface/client/identity/context.tsx': { useDevice: () => ({ directory: { members: [] }, linkTarget: () => 'other' }) }, '@vibeface/client/identity/copy.ts': { soleManager: () => false, switchCopy: { title: '换到新的网络?', erase: () => '清除应用内数据', confirm: '换网络' } }, diff --git a/apps/mobile/app/_layout.tsx b/apps/mobile/app/_layout.tsx index 51885ed44..5a60e6203 100644 --- a/apps/mobile/app/_layout.tsx +++ b/apps/mobile/app/_layout.tsx @@ -2,7 +2,7 @@ import '@/platform/polyfills' import '../global.css' import { useLinkingURL } from 'expo-linking' -import { Link, Stack, router, usePathname } from 'expo-router' +import { Link, Stack, usePathname, useRouter } from 'expo-router' import { ListChecks, MessageSquare, Settings, X } from 'lucide-react-native' import * as SystemUI from 'expo-system-ui' import { StatusBar } from 'expo-status-bar' @@ -42,6 +42,7 @@ export default function RootLayout() { } function Screens() { + const router = useRouter() const workspace = useWorkspace() const shared = useLive() const [background, foreground] = useCSSVariable(['--background', '--foreground']) as [string, string] @@ -58,7 +59,7 @@ function Screens() { if (device.linkTarget(link) === 'other') router.push({ pathname: '/switch-network', params: { link } }) // Reset to home alone: navigate would stack home on the focused join page, which the protected group cannot drop, and any screen left below keeps a back button. else { if (router.canDismiss()) router.dismissAll(); router.replace('/') } - }, [url, ready]) + }, [url, ready, router]) const wide = useWide() const pathname = usePathname() const insets = useSafeAreaInsets() @@ -106,16 +107,16 @@ function Screens() { - + }} /> {/* Opened from the new bot sheet it is a sheet too, with no back button of its own. */} - ({ title: 'Bot 与任务', headerLeft: (route.params as { sheet?: string } | undefined)?.sheet ? Close : undefined })} /> + ({ title: 'Bot 与任务', headerLeft: (route.params as { sheet?: string } | undefined)?.sheet ? () => : undefined })} /> ({ title: '设置', ...root, ...viaTab(props) })} /> - + }} /> @@ -146,5 +147,6 @@ function Screens() { /** A sheet's own way out, beside the swipe down that not everyone knows. */ function Close() { + const router = useRouter() return } diff --git a/apps/mobile/app/add-device.tsx b/apps/mobile/app/add-device.tsx index 7fef1f6b2..8f85d51f1 100644 --- a/apps/mobile/app/add-device.tsx +++ b/apps/mobile/app/add-device.tsx @@ -1,6 +1,6 @@ import * as Clipboard from 'expo-clipboard' import { openURL } from 'expo-linking' -import { router } from 'expo-router' +import { useRouter } from 'expo-router' import { Check, CircleCheck, Clock, Copy, Download, Laptop, Link2, Plus, ScanEye, Share2, ShieldAlert, Smartphone, Sparkles, SquareTerminal } from 'lucide-react-native' import { useEffect, useRef, useState } from 'react' import { Pressable, ScrollView, Share, View } from 'react-native' @@ -21,6 +21,7 @@ type Kind = (typeof kinds)[number]['value'] /** "添加设备": one single-use link the new device opens (a QR for phones, its own Vibeface page for computers), then both sides compare the safety code. */ export default function AddDevice() { + const router = useRouter() const device = useDevice() const { ready } = device const { busy, error, run } = useRun() diff --git a/apps/mobile/app/b/[botId].tsx b/apps/mobile/app/b/[botId].tsx index a9fb12e3b..bfaee4651 100644 --- a/apps/mobile/app/b/[botId].tsx +++ b/apps/mobile/app/b/[botId].tsx @@ -1,6 +1,6 @@ import { historySections } from '@vibeface/client/workspace/model.ts' import { HistoryControls } from '@/features/chat/history-controls' -import { Link, Stack, router, useFocusEffect, useLocalSearchParams } from 'expo-router' +import { Link, Stack, useFocusEffect, useLocalSearchParams, useRouter } from 'expo-router' import { useHeaderHeight } from 'expo-router/react-navigation' import { ArrowDown, Bot, Crown, LoaderCircle, MessageSquareOff, Settings, Star } from 'lucide-react-native' import { useCallback, useEffect, useRef, useState } from 'react' @@ -92,6 +92,7 @@ function entries(chat: Conversation, extras: ReturnType, b } export default function ChatScreen() { + const router = useRouter() const { botId, routine } = useLocalSearchParams<{ botId: string; routine?: string }>() const workspace = useWorkspace() const device = useDevice() @@ -105,7 +106,8 @@ export default function ChatScreen() { // already ends where the bar's buttons begin. const frame = useSafeAreaFrame().width const wide = useWide() - const titleWidth = ios ? (wide ? frame - 320 : frame) - 120 : undefined + // Compact iPad windows also need leading room for the system window controls. + const titleWidth = Platform.OS === 'ios' ? (wide ? frame - 320 : frame) - (Platform.isPad && !wide ? 200 : 120) : undefined const { theme } = useUniwind() const chrome = useChatChrome() const background = useCSSVariable('--background') @@ -278,9 +280,9 @@ export default function ChatScreen() { 主 Bot : null} - {botCanSend(reach) ? + {botCanSend(reach) ? (!draft.text.trim() ? {starters[bot.project ? 'project' : 'general'].map((example) => )} - : starting ? Bot 正在启动 : <> + : null) : starting ? Bot 正在启动 : <> Bot 还不能回复 {reach === 'offline' || canSetUp ? diff --git a/apps/mobile/app/device/[deviceId].tsx b/apps/mobile/app/device/[deviceId].tsx index 60d90646c..71c071ce0 100644 --- a/apps/mobile/app/device/[deviceId].tsx +++ b/apps/mobile/app/device/[deviceId].tsx @@ -1,4 +1,4 @@ -import { Stack, router, useLocalSearchParams } from 'expo-router' +import { Stack, useLocalSearchParams, useRouter } from 'expo-router' import { ArrowRightLeft, Bot, Check, Crown, Laptop, MonitorSmartphone, Pencil, Sparkles, Trash2, X } from 'lucide-react-native' import { useState } from 'react' import { Alert, Pressable, ScrollView, View } from 'react-native' @@ -20,6 +20,7 @@ import { useRun } from '@/lib/use-run' const tag = 'flex-row items-center gap-1 rounded-md bg-muted px-1.5' export default function DeviceDetail() { + const router = useRouter() const { deviceId } = useLocalSearchParams<{ deviceId: string }>() const device = useDevice() const { members } = useWorkspace() diff --git a/apps/mobile/app/index.tsx b/apps/mobile/app/index.tsx index 5c8ee733c..4caaece93 100644 --- a/apps/mobile/app/index.tsx +++ b/apps/mobile/app/index.tsx @@ -1,4 +1,4 @@ -import { Stack, router, useLocalSearchParams } from 'expo-router' +import { Stack, useLocalSearchParams, useRouter } from 'expo-router' import { useEffect } from 'react' import { useHeaderHeight } from 'expo-router/react-navigation' import { MessageSquare } from 'lucide-react-native' @@ -12,6 +12,7 @@ import { useWide } from '@/lib/layout' /** The phone's home; on a wide window the list lives in the sidebar and this pane only waits for a choice. */ export default function Home() { + const router = useRouter() const wide = useWide() const workspace = useWorkspace() const { conversation, work, routine } = useLocalSearchParams<{ conversation?: string; work?: string; routine?: string }>() @@ -20,7 +21,7 @@ export default function Home() { if (!bot || !conversation) return if (work) router.replace(`/b/${bot.bot_id}/work/${work}`) else router.replace({ pathname: '/b/[botId]', params: { botId: bot.bot_id, ...(routine ? { routine } : {}) } }) - }, [conversation, work, routine, bot?.bot_id]) + }, [conversation, work, routine, bot?.bot_id, router]) const bar = useHeaderHeight() const frame = useSafeAreaFrame() const insets = useSafeAreaInsets() diff --git a/apps/mobile/app/switch-network.tsx b/apps/mobile/app/switch-network.tsx index 870d53275..c4c4bc394 100644 --- a/apps/mobile/app/switch-network.tsx +++ b/apps/mobile/app/switch-network.tsx @@ -1,4 +1,4 @@ -import { router, useLocalSearchParams } from 'expo-router' +import { useLocalSearchParams, useRouter } from 'expo-router' import { useLinkingURL } from 'expo-linking' import { useState } from 'react' import { ArrowRightLeft, Crown } from 'lucide-react-native' @@ -20,6 +20,7 @@ const decode = (ticket: string) => mobilePlatform.secure.ticketDecode(ticket) /** A join link from another network: the phone erases itself, and the join page that follows picks the same link up. */ export default function SwitchNetwork() { + const router = useRouter() const device = useDevice() const { busy, error, run } = useRun() const { link } = useLocalSearchParams<{ link?: string }>() diff --git a/apps/mobile/src/components/kit.tsx b/apps/mobile/src/components/kit.tsx index 56a6fcde2..cf2139c17 100644 --- a/apps/mobile/src/components/kit.tsx +++ b/apps/mobile/src/components/kit.tsx @@ -96,7 +96,7 @@ export function Panel({ icon, title, children }: { icon: LucideIcon; title: stri return - {title} + {title} {children} diff --git a/apps/mobile/src/components/ui/button.tsx b/apps/mobile/src/components/ui/button.tsx index d8225486c..9eb4a85e2 100644 --- a/apps/mobile/src/components/ui/button.tsx +++ b/apps/mobile/src/components/ui/button.tsx @@ -47,10 +47,10 @@ const buttonVariants = cva( link: '', }, size: { - default: cn('min-h-11 px-4 py-2', Platform.select({ web: 'has-[>svg]:px-3' })), - sm: cn('min-h-9 gap-1.5 px-3', Platform.select({ web: 'has-[>svg]:px-2.5' })), + default: cn('min-h-11 px-4 py-2', Platform.select({ ios: 'min-w-11', android: 'min-h-12 min-w-12', web: 'has-[>svg]:px-3' })), + sm: cn('min-h-9 gap-1.5 px-3', Platform.select({ ios: 'min-h-11 min-w-11', android: 'min-h-12 min-w-12', web: 'has-[>svg]:px-2.5' })), // No sm: steps; on iPad widths they would override a caller's size. - icon: 'size-11', + icon: Platform.select({ android: 'size-12', default: 'size-11' }), }, }, defaultVariants: { diff --git a/apps/mobile/src/data/live.tsx b/apps/mobile/src/data/live.tsx index 8a6709f37..02c543ae4 100644 --- a/apps/mobile/src/data/live.tsx +++ b/apps/mobile/src/data/live.tsx @@ -16,7 +16,7 @@ import { mobilePlatform } from '@/platform' import { WorkspaceProvider } from './context' import type { ChatMessage, Connection, Conversation, Member, Session, Workspace, WorkspaceActions } from './types' import type { InputTarget } from '@vibeface/client/workspace/types.ts' -import { router } from 'expo-router' +import { useRouter } from 'expo-router' const busyStates = ['pending_delivery', 'queued', 'steering', 'claimed', 'running', 'cancelling'] const streamingStates = [...busyStates, 'publishing'] @@ -86,6 +86,7 @@ function useConnection(): { session: Session; connection: Connection } { } function Device({ children }: { children: ReactNode }) { + const router = useRouter() const device = useDevice() const { ready } = device if (!ready) return {children} diff --git a/apps/mobile/src/features/devices/scanner.tsx b/apps/mobile/src/features/devices/scanner.tsx index 801088787..85e5a3405 100644 --- a/apps/mobile/src/features/devices/scanner.tsx +++ b/apps/mobile/src/features/devices/scanner.tsx @@ -11,7 +11,7 @@ export function Scanner({ title, onScanned, onCancel }: { title: string; onScann if (!permission) return null if (!permission.granted) return 需要相机权限才能扫码,也可以粘贴链接 - + diff --git a/apps/mobile/src/features/workspace/workspace.tsx b/apps/mobile/src/features/workspace/workspace.tsx index 526d25793..46bced377 100644 --- a/apps/mobile/src/features/workspace/workspace.tsx +++ b/apps/mobile/src/features/workspace/workspace.tsx @@ -1,5 +1,5 @@ import { File } from 'expo-file-system' -import { Link, Stack, router, useFocusEffect } from 'expo-router' +import { Link, Stack, useFocusEffect, useRouter } from 'expo-router' import { BrainCircuit, ChevronRight, CircleQuestionMark, ListChecks, MessagesSquare, Plus, Reply, Settings, X } from 'lucide-react-native' import { useCallback, useEffect, useRef, useState } from 'react' import { Pressable, ScrollView, View } from 'react-native' @@ -72,6 +72,7 @@ export function WorkspaceTasks({ botFilter = 'all', filter = 'all' }: { botFilte } export function CreateTaskScreen() { + const router = useRouter() const shared = useLive() const { busy, error, run } = useRun() const [botId, setBotId] = useState(shared?.selectedBot?.bot_id ?? shared?.bots[0]?.bot_id ?? '') @@ -108,6 +109,7 @@ export function ChannelsScreen() { } export function ChannelSettingsScreen({ channelId }: { channelId?: string }) { + const router = useRouter() const shared = useLive() const { ready } = useDevice() const channel = shared?.workspaceState?.channels.find((item) => item.channel_id === channelId) @@ -166,6 +168,7 @@ export function InputStatus({ pending }: { pending: PendingMessage }) { } export function ScopedWorkspaceScreen({ taskId, channelId }: { taskId?: string; channelId?: string }) { + const router = useRouter() const shared = useLive() const chrome = useChatChrome() const { ready } = useDevice() diff --git a/tests/e2e/workspace.native.ts b/tests/e2e/workspace.native.ts index 10e97c2d1..4b68053ac 100644 --- a/tests/e2e/workspace.native.ts +++ b/tests/e2e/workspace.native.ts @@ -192,6 +192,20 @@ test('native workspace routes preserve Task input and directed Channel results', const toLatest = screen.getByRole('button', '回到底部', { exact: true, visible: true }); await expect(mainComposer).toBeVisible(); await expect(toLatest).toHaveCount(0); + const starter = screen.getByRole('button', '你能帮我完成什么?', { exact: true }); + await expect(starter).toBeVisible(); + await mainComposer.fill('保留未发送的原生草稿'); + await expect(starter).toHaveCount(0); + await expect(mainComposer).toHaveValue('保留未发送的原生草稿'); + await expect(screen.getByText(/^Bot (?:还不能回复|正在启动)$/)).toHaveCount(0); + await mainComposer.clear(); + await expect(starter).toBeVisible(); + const invocationsBeforeStarter = (await inspect()).invocations.length; + await starter.click(); + await expect(mainComposer).toHaveValue('你能帮我完成什么?'); + await expect(starter).toHaveCount(0); + expect((await inspect()).invocations).toHaveLength(invocationsBeforeStarter); + await mainComposer.clear(); await mainComposer.focus(); await expect(keyboard).toHaveCount(1); await expect(toLatest).toHaveCount(0);