@@ -24,13 +24,15 @@ import * as Dialog from "../../components/ui/Dialog";
2424import { useEnvironments , type EnvironmentPresentation } from "../../state/environments" ;
2525import {
2626 chooseVoiceDestination ,
27+ cloudVoiceConnectionAtom ,
2728 voiceConnectionAtom ,
2829 voiceGrantAtom ,
2930 voiceSpeechStatusAtom ,
3031 hasEnvironmentVoiceCapture ,
3132} from "../voice-input/voiceDestination" ;
3233import { VoiceModelSection } from "./VoiceModelSection" ;
33- import { CloudVoiceSection } from "./CloudVoiceSection" ;
34+ import { CloudVoiceChoice } from "./CloudVoiceSection" ;
35+ import type { VoiceGrant } from "../voice-input/voiceGrantStorage" ;
3436import type { EnvironmentId } from "@t3tools/contracts" ;
3537import { ThreadSwipeable } from "../home/thread-swipe-actions" ;
3638import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow" ;
@@ -127,45 +129,96 @@ function saveDestination(id: EnvironmentId | null) {
127129function EnvironmentChoice ( {
128130 environment,
129131 selected,
132+ onCloudOnly,
130133} : {
131134 environment : EnvironmentPresentation ;
132135 selected : boolean ;
136+ /** Called instead of a local grant when the environment only offers cloud transcription. */
137+ onCloudOnly : ( ) => void ;
133138} ) {
134- const usable = useAtomValue ( voiceConnectionAtom ( environment . environmentId ) ) !== null ;
135- const status = AsyncResult . getOrElse (
136- useAtomValue ( voiceSpeechStatusAtom ( environment . environmentId ) ) ,
137- ( ) => null ,
138- ) ;
139+ const local = useAtomValue ( voiceConnectionAtom ( environment . environmentId ) ) !== null ;
140+ const cloud = useAtomValue ( cloudVoiceConnectionAtom ( environment . environmentId ) ) !== null ;
141+ const usable = local || cloud ;
139142 return (
140143 < ListChoiceRow
141144 label = { environment . label }
142145 selected = { selected }
143146 disabled = { ! usable }
144147 description = {
145- ! usable
146- ? environment . connection . phase !== "connected"
147- ? "Not connected"
148- : "Transcription unavailable"
149- : status ?. supported === false
150- ? status . reason
151- : status ?. state === "missing-model"
152- ? "No transcription model installed"
153- : ( environment . displayUrl ?? "Send finished recordings to this environment." )
148+ environment . connection . phase !== "connected"
149+ ? "Not connected"
150+ : ! usable
151+ ? "Transcription unavailable"
152+ : ( environment . displayUrl ?? "Send finished recordings to this environment." )
154153 }
155154 separated
156155 onPress = { ( ) => {
157- if ( ! selected )
158- Dialog . confirm ( {
159- title : `Send microphone audio to ${ environment . label } ?` ,
160- message : `Finished recordings will be sent to ${ environment . displayUrl ?? environment . label } for transcription. You can switch back to This device at any time.` ,
161- confirmText : "Use environment" ,
162- onConfirm : ( ) => saveDestination ( environment . environmentId ) ,
163- } ) ;
156+ if ( selected ) return ;
157+ if ( ! local ) return onCloudOnly ( ) ;
158+ Dialog . confirm ( {
159+ title : `Send microphone audio to ${ environment . label } ?` ,
160+ message : `Finished recordings will be sent to ${ environment . displayUrl ?? environment . label } for transcription. You can switch back to This device at any time.` ,
161+ confirmText : "Use environment" ,
162+ onConfirm : ( ) => saveDestination ( environment . environmentId ) ,
163+ } ) ;
164164 } }
165165 />
166166 ) ;
167167}
168168
169+ /** How the selected environment transcribes: its own model or a cloud provider. */
170+ function EnvironmentMethodSection ( {
171+ environment,
172+ grant,
173+ cloudOpen,
174+ onCloudOpenChange,
175+ } : {
176+ environment : EnvironmentPresentation ;
177+ grant : VoiceGrant | null ;
178+ cloudOpen : boolean ;
179+ onCloudOpenChange : ( open : boolean ) => void ;
180+ } ) {
181+ const id = environment . environmentId ;
182+ const local = useAtomValue ( voiceConnectionAtom ( id ) ) !== null ;
183+ const status = AsyncResult . getOrElse ( useAtomValue ( voiceSpeechStatusAtom ( id ) ) , ( ) => null ) ;
184+ const destination = grant ?. destination . environmentId === id ? grant . destination : null ;
185+ const cloudCapable =
186+ environment . serverConfig ?. environment . capabilities . cloudVoiceTranscription === true ||
187+ destination ?. kind === "cloud" ;
188+ return (
189+ < SettingsSection title = { `Method on ${ environment . label } ` } >
190+ < ListChoiceRow
191+ label = "Local model"
192+ description = {
193+ ! local
194+ ? "Not available on this environment"
195+ : status ?. supported === false
196+ ? status . reason
197+ : status ?. state === "missing-model"
198+ ? "No model installed"
199+ : status
200+ ? status . model
201+ : "Runs on the environment"
202+ }
203+ selected = { destination ?. kind === "environment" }
204+ disabled = { ! local }
205+ onPress = { ( ) => {
206+ // The user already agreed to send audio to this environment.
207+ if ( destination ?. kind !== "environment" ) saveDestination ( id ) ;
208+ } }
209+ />
210+ { cloudCapable ? (
211+ < CloudVoiceChoice
212+ environment = { environment }
213+ active = { destination ?. kind === "cloud" ? destination : null }
214+ open = { cloudOpen }
215+ onOpenChange = { onCloudOpenChange }
216+ />
217+ ) : null }
218+ </ SettingsSection >
219+ ) ;
220+ }
221+
169222export function SettingsVoiceRouteScreen ( ) {
170223 const transcriber = useAtomValue ( localVoiceTranscriberAtom ) ;
171224 const grant = useAtomValue ( voiceGrantAtom ) ;
@@ -175,6 +228,12 @@ export function SettingsVoiceRouteScreen() {
175228 ( environment ) => environment . environmentId === grant . destination . environmentId ,
176229 )
177230 : undefined ;
231+ // An environment whose cloud sheet is open; it may not hold the grant yet.
232+ const [ cloudSheet , setCloudSheet ] = useState < EnvironmentId | null > ( null ) ;
233+ const sheetEnvironment = environments . find (
234+ ( environment ) => environment . environmentId === cloudSheet ,
235+ ) ;
236+ const methodEnvironment = sheetEnvironment ?? grantEnvironment ;
178237 const insets = useSafeAreaInsets ( ) ;
179238 const preferencesResult = useAtomValue ( mobilePreferencesAtom ) ;
180239 const savePreferences = useAtomSet ( updateMobilePreferencesAtom ) ;
@@ -234,7 +293,7 @@ export function SettingsVoiceRouteScreen() {
234293 < ListChoiceRow
235294 label = "This device"
236295 description = "Audio stays on this device and is transcribed here."
237- selected = { ! grant }
296+ selected = { ! grant && ! sheetEnvironment }
238297 onPress = { ( ) => saveDestination ( null ) }
239298 />
240299 { hasEnvironmentVoiceCapture &&
@@ -243,9 +302,11 @@ export function SettingsVoiceRouteScreen() {
243302 key = { environment . environmentId }
244303 environment = { environment }
245304 selected = {
246- grant ?. destination . kind === "environment" &&
247- grant . destination . environmentId === environment . environmentId
305+ sheetEnvironment
306+ ? sheetEnvironment . environmentId === environment . environmentId
307+ : grant ?. destination . environmentId === environment . environmentId
248308 }
309+ onCloudOnly = { ( ) => setCloudSheet ( environment . environmentId ) }
249310 />
250311 ) ) }
251312 { isReady && grant && ! grantEnvironment ? (
@@ -259,27 +320,19 @@ export function SettingsVoiceRouteScreen() {
259320 />
260321 ) : null }
261322 </ SettingsSection >
262- { hasEnvironmentVoiceCapture &&
263- environments
264- . filter (
265- ( env ) =>
266- env . serverConfig ?. environment . capabilities . cloudVoiceTranscription ||
267- ( grant ?. destination . kind === "cloud" &&
268- grant . destination . environmentId === env . environmentId ) ,
269- )
270- . map ( ( environment ) => (
271- < CloudVoiceSection
272- key = { environment . environmentId }
273- environment = { environment }
274- active = {
275- grant ?. destination . kind === "cloud" &&
276- grant . destination . environmentId === environment . environmentId
277- ? grant . destination
278- : null
279- }
280- />
281- ) ) }
282- { grantEnvironment && grant ?. destination . kind === "environment" ? (
323+ { hasEnvironmentVoiceCapture && methodEnvironment ? (
324+ < EnvironmentMethodSection
325+ environment = { methodEnvironment }
326+ grant = { grant }
327+ cloudOpen = { cloudSheet === methodEnvironment . environmentId }
328+ onCloudOpenChange = { ( open ) =>
329+ setCloudSheet ( open ? methodEnvironment . environmentId : null )
330+ }
331+ />
332+ ) : null }
333+ { grantEnvironment &&
334+ methodEnvironment === grantEnvironment &&
335+ grant ?. destination . kind === "environment" ? (
283336 < VoiceModelSection
284337 environmentId = { grantEnvironment . environmentId }
285338 label = { grantEnvironment . label }
0 commit comments