-
Notifications
You must be signed in to change notification settings - Fork 11
feat(mobile): persist last Run-on destination #5988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
apps/mobile/src/lib/hooks/use-persisted-run-on-destination.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { useSyncExternalStore } from 'react'; | ||
|
|
||
| import { createSecureStorePreference } from '@/lib/hooks/secure-store-preference'; | ||
| import { parseStoredRunOnDestination } from '@/lib/run-on-destination'; | ||
| import { LAST_RUN_ON_DESTINATION_KEY } from '@/lib/storage-keys'; | ||
|
|
||
| const store = createSecureStorePreference<string | null>({ | ||
| key: LAST_RUN_ON_DESTINATION_KEY, | ||
| defaultValue: null, | ||
| parse: parseStoredRunOnDestination, | ||
| serialize: value => value ?? '', | ||
| }); | ||
|
|
||
| export function clearRunOnDestinationPreference() { | ||
| store.clear(); | ||
| } | ||
|
|
||
| function saveRunOn(connectionId: string | null) { | ||
| store.set(connectionId); | ||
| } | ||
|
|
||
| export function usePersistedRunOnDestination() { | ||
| const storedConnectionId = useSyncExternalStore(store.subscribe, store.get); | ||
| const hasLoaded = useSyncExternalStore(store.subscribe, store.getHasLoaded); | ||
| return { storedConnectionId, hasLoaded, saveRunOn }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { parseStoredRunOnDestination, resolvePersistedRunOn } from './run-on-destination'; | ||
|
|
||
| const CLI = { connectionId: 'cli-1', name: 'MacBook' }; | ||
| const REMOTE = { connectionId: 'remote-1', name: 'VM' }; | ||
|
|
||
| describe('parseStoredRunOnDestination', () => { | ||
| it('treats missing or empty storage as Cloud Agent', () => { | ||
| expect(parseStoredRunOnDestination(null)).toBeNull(); | ||
| expect(parseStoredRunOnDestination('')).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns a stored connection id', () => { | ||
| expect(parseStoredRunOnDestination('cli-1')).toBe('cli-1'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resolvePersistedRunOn', () => { | ||
| it('defaults to Cloud Agent when nothing is stored', () => { | ||
| expect(resolvePersistedRunOn(null, [CLI])).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns the live row when the stored id is in the list', () => { | ||
| expect(resolvePersistedRunOn('cli-1', [REMOTE, CLI])).toBe(CLI); | ||
| }); | ||
|
|
||
| it('falls back to Cloud Agent when the stored id is gone', () => { | ||
| expect(resolvePersistedRunOn('cli-1', [REMOTE])).toBeNull(); | ||
| expect(resolvePersistedRunOn('cli-1', [])).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export function parseStoredRunOnDestination(raw: string | null): string | null { | ||
| if (!raw) { | ||
| return null; | ||
| } | ||
| return raw; | ||
| } | ||
|
|
||
| export function resolvePersistedRunOn<T extends { connectionId: string }>( | ||
| storedConnectionId: string | null, | ||
| instances: readonly T[] | ||
| ): T | null { | ||
| if (!storedConnectionId) { | ||
| return null; | ||
| } | ||
| return instances.find(instance => instance.connectionId === storedConnectionId) ?? null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Empty SecureStore values fail on iOS, so picking Cloud Agent will not persist
serializewrites''when the user selects Cloud Agent (saveRunOn(null)). iOS Keychain / expo-secure-store reject empty values, so the write fails (error toast) and the previous CLI connection id stays on disk. On the next visit, restore still selects that CLI.Use a non-empty sentinel, or
store.clear()when the destination is Cloud Agent.Reply with
@kilocode-bot fix itto have Kilo Code address this issue.