Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/renderer/src/components/mobile/MobilePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { MobilePairingConnectionMode } from '../../../../shared/mobile-pair
type StoreState = {
closeMobilePage: () => void
orcaProfileAuthStatus: { state: 'connected' | 'local' }
settings: { showMobileButton: boolean }
settings: { showMobileButton: boolean; mobilePairingConnectionMode?: MobilePairingConnectionMode }
updateSettings: () => Promise<void>
}

Expand Down Expand Up @@ -60,10 +60,10 @@ vi.mock('./MobilePageContent', () => ({
Continue
</button>
<button type="button" onClick={() => props.handleConnectionModeChange('automatic')}>
Anywhere
Orca Relay
</button>
<button type="button" onClick={() => props.handleConnectionModeChange('local-only')}>
Local only
Local network
</button>
</div>
)
Expand Down Expand Up @@ -110,55 +110,69 @@ describe('MobilePage pairing connection mode', () => {
await user.click(screen.getByRole('button', { name: 'Continue' }))
}

it('defaults signed-in pairing to local-only and rotates when Relay is selected', async () => {
it('defaults signed-in pairing to Anywhere and rotates when same-network is selected', async () => {
const user = userEvent.setup()
await openPairingStep()

await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'local-only' }))
await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'automatic' }))
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('base64,qr'))
expect(screen.getByTestId('mode')).toHaveTextContent('local-only')
expect(screen.getByTestId('mode')).toHaveTextContent('automatic')

let resolveRelayQr: ((value: Record<string, unknown>) => void) | undefined
let resolveLocalQr: ((value: Record<string, unknown>) => void) | undefined
getPairingQR.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveRelayQr = resolve
resolveLocalQr = resolve
})
)
await user.click(screen.getByRole('button', { name: 'Anywhere' }))
await user.click(screen.getByRole('button', { name: 'Local network' }))
await waitFor(() =>
expect(getPairingQR).toHaveBeenLastCalledWith({
connectionMode: 'automatic',
connectionMode: 'local-only',
rotate: true
})
)
expect(screen.getByTestId('mode')).toHaveTextContent('automatic')
expect(screen.getByTestId('mode')).toHaveTextContent('local-only')
expect(screen.getByTestId('pairing-qr')).toHaveTextContent('base64,qr')
expect(screen.getByTestId('pairing-url')).toHaveTextContent('none')
expect(mocks.storeState.updateSettings).toHaveBeenCalledWith({
mobilePairingConnectionMode: 'local-only'
})

resolveRelayQr?.({
resolveLocalQr?.({
available: true,
qrDataUrl: 'data:image/png;base64,relay-qr',
pairingUrl: 'orca://pair#relay'
qrDataUrl: 'data:image/png;base64,local-qr',
pairingUrl: 'orca://pair#local'
})
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('relay-qr'))
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('local-qr'))
})

it('defaults signed-out pairing to local-only', async () => {
mocks.storeState.orcaProfileAuthStatus = { state: 'local' }
it('restores a saved local-only preference without user interaction', async () => {
mocks.storeState.settings = {
showMobileButton: true,
mobilePairingConnectionMode: 'local-only'
}
await openPairingStep()

await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'local-only' }))
expect(screen.getByTestId('mode')).toHaveTextContent('local-only')
})

it('defaults signed-out UI to Orca Relay but only issues a local-only QR', async () => {
mocks.storeState.orcaProfileAuthStatus = { state: 'local' }
await openPairingStep()

await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'local-only' }))
expect(screen.getByTestId('mode')).toHaveTextContent('automatic')
})

it('removes the old QR if policy rotation fails', async () => {
const user = userEvent.setup()
await openPairingStep()
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('base64,qr'))

getPairingQR.mockRejectedValueOnce(new Error('rotation failed'))
await user.click(screen.getByRole('button', { name: 'Anywhere' }))
await user.click(screen.getByRole('button', { name: 'Local network' }))

await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('none'))
expect(screen.getByTestId('pairing-url')).toHaveTextContent('none')
Expand Down
57 changes: 31 additions & 26 deletions src/renderer/src/components/mobile/MobilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { translate } from '@/i18n/i18n'
import { useMobilePageEscape } from './use-mobile-page-escape'
import { MobilePageContent } from './MobilePageContent'
import { useMobileInstallQr } from './use-mobile-install-qr'
import type { MobilePairingConnectionMode } from '../../../../shared/mobile-pairing-connection-mode'
import {
effectiveMobilePairingConnectionMode,
type MobilePairingConnectionMode
} from '../../../../shared/mobile-pairing-connection-mode'
import { useMobilePairingConnectionMode } from './use-mobile-pairing-connection-mode'
import { useMobileInstallActions } from './use-mobile-install-actions'

export default function MobilePage(): React.JSX.Element {
Expand All @@ -33,9 +37,7 @@ export default function MobilePage(): React.JSX.Element {
const [pairingUrl, setPairingUrl] = useState<string | null>(null)
const [pairLoading, setPairLoading] = useState(false)
const signedIn = useAppStore((state) => state.orcaProfileAuthStatus?.state === 'connected')
// Why: Relay is opt-in while compatible mobile builds are limited to the
// TestFlight preview and Android APK.
const [connectionMode, setConnectionMode] = useState<MobilePairingConnectionMode>('local-only')
const [connectionMode, setConnectionMode] = useMobilePairingConnectionMode()
const [networkInterfaces, setNetworkInterfaces] = useState<MobileNetworkInterface[]>([])
const [selectedAddress, setSelectedAddress] = useState<string | undefined>(undefined)
// Why: tracks whether `selectedAddress` came from the user typing a
Expand Down Expand Up @@ -186,7 +188,11 @@ export default function MobilePage(): React.JSX.Element {
}
try {
const address = addressOverride ?? selectedAddress
const nextConnectionMode = connectionModeOverride ?? connectionMode
const preferredMode = connectionModeOverride ?? connectionMode
const nextConnectionMode = effectiveMobilePairingConnectionMode({
preferred: preferredMode,
signedIn
})
const result = await window.api.mobile.getPairingQR({
...(address ? { address } : {}),
connectionMode: nextConnectionMode,
Expand Down Expand Up @@ -231,7 +237,7 @@ export default function MobilePage(): React.JSX.Element {
}
}
},
[connectionMode, mountedRef, selectedAddress]
[connectionMode, mountedRef, selectedAddress, signedIn]
)

const handleConnectionModeChange = useCallback(
Expand All @@ -240,6 +246,7 @@ export default function MobilePage(): React.JSX.Element {
return
}
setConnectionMode(nextMode)
void updateSettings({ mobilePairingConnectionMode: nextMode })
// Why: an offer encodes its connection policy. Invalidate the prior
// request before rotating so a late response cannot restore a stale QR.
pairingRequestIdRef.current += 1
Expand All @@ -250,16 +257,23 @@ export default function MobilePage(): React.JSX.Element {
void generatePairing(true, undefined, nextMode)
}
},
[connectionMode, generatePairing, pairLoading]
[connectionMode, generatePairing, pairLoading, updateSettings, setConnectionMode]
)

// Why: a Relay QR minted while signed in must not linger on a now-signed-out
// desktop — that desktop can no longer service it. On the signed-in->signed-out
// edge, invalidate the pending request and drop the displayed QR; the Step 2
// auto-generate effect re-mints it as local-only. Anywhere stays selected.
useEffect(() => {
const wasSignedIn = wasSignedInRef.current
wasSignedInRef.current = signedIn
if (wasSignedIn && !signedIn) {
handleConnectionModeChange('local-only')
if (wasSignedIn && !signedIn && connectionMode === 'automatic' && hasGeneratedRef.current) {
pairingRequestIdRef.current += 1
hasGeneratedRef.current = false
setPairQrDataUrl(null)
setPairingUrl(null)
}
}, [handleConnectionModeChange, signedIn])
}, [signedIn, connectionMode])

const loadNetworkInterfaces = useCallback(async () => {
if (mountedRef.current) {
Expand Down Expand Up @@ -370,29 +384,20 @@ export default function MobilePage(): React.JSX.Element {
loadDevices: polledLoadDevices
})

const enterFlow = (): void => {
setStepIdx(0)
// Why: entering the flow must mint a fresh pairing token — clear stale QR
// state so we never flash an expired code from a previous session. Step 0 is
// the full flow; Step 1 is "Pair another device" (app already installed).
const startPairingFlow = (nextStep: StepIndex): void => {
setStepIdx(nextStep)
setPairingDeviceBaseline(devices.length)
// Force the auto-generate effect to mint a fresh pairing token on next
// entry into Step 2, and clear stale QR state so we never flash an
// expired code from a previous session.
hasGeneratedRef.current = false
setPairQrDataUrl(null)
setPairingUrl(null)
showStage('flow')
}

// Why: from the paired summary, "Pair another device" jumps straight to
// Step 2 since the app is presumably already installed on the user's phone.
const pairAnotherDevice = (): void => {
setStepIdx(1)
setPairingDeviceBaseline(devices.length)
// Same reset as enterFlow — re-entering must mint a fresh pairing offer.
hasGeneratedRef.current = false
setPairQrDataUrl(null)
setPairingUrl(null)
showStage('flow')
}
const enterFlow = (): void => startPairingFlow(0)
const pairAnotherDevice = (): void => startPairingFlow(1)

const handleBack = (): void => {
if (stepIdx === 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from 'react'
import { useAppStore } from '@/store'
import {
resolveMobilePairingConnectionMode,
type MobilePairingConnectionMode
} from '../../../../shared/mobile-pairing-connection-mode'

/**
* Selected pairing path, seeded from the persisted preference and re-synced
* when it changes. Anywhere is the default; an explicit saved `local-only`
* means the user already chose same-network only. Shared by MobilePage and
* MobilePane so the two surfaces cannot resolve the saved value differently.
*/
export function useMobilePairingConnectionMode(): [
MobilePairingConnectionMode,
React.Dispatch<React.SetStateAction<MobilePairingConnectionMode>>
] {
const savedConnectionMode = useAppStore((s) => s.settings?.mobilePairingConnectionMode)
const [connectionMode, setConnectionMode] = useState<MobilePairingConnectionMode>(() =>
resolveMobilePairingConnectionMode(savedConnectionMode)
)
useEffect(() => {
setConnectionMode(resolveMobilePairingConnectionMode(savedConnectionMode))
}, [savedConnectionMode])
return [connectionMode, setConnectionMode]
}
Loading
Loading