11import { useState } from "react" ;
22import { useT } from "../i18n/shared" ;
3- import { useKeyedClientResource } from "../client-resource" ;
43import { readOpenBrowserPref , writeOpenBrowserPref } from "../oauth-open-browser-pref" ;
54
65/**
@@ -15,32 +14,22 @@ import { readOpenBrowserPref, writeOpenBrowserPref } from "../oauth-open-browser
1514 * nothing is spawned on the proxy's machine, so the operator opens the link
1615 * wherever they actually want to be signed in.
1716 */
18- export function OpenBrowserPrefToggle ( { apiBase } : { apiBase ?: string } ) {
17+ /**
18+ * `serverDefault` is the persisted `oauthOpenBrowser` when the caller already
19+ * has it. This component deliberately does **not** fetch it: an auth panel that
20+ * quietly issued its own `/api/settings` request would make every surrounding
21+ * surface's request accounting wrong, and it did — it broke the account-import
22+ * tests, which assert exactly how many calls a selection makes.
23+ *
24+ * Not fetching costs nothing that matters. With no local preference the request
25+ * omits `openBrowser` entirely, so the persisted setting still governs what the
26+ * proxy actually does; only the initial checkbox rendering falls back to the
27+ * historical auto-open.
28+ */
29+ export function OpenBrowserPrefToggle ( { serverDefault = true } : { serverDefault ?: boolean } ) {
1930 const t = useT ( ) ;
20- // A local preference wins outright; otherwise the box mirrors the persisted
21- // setting, so the checkbox and the config file never disagree on screen.
22- const localPref = readOpenBrowserPref ( ) ;
23- const [ choice , setChoice ] = useState < boolean | undefined > ( localPref ) ;
24-
25- // The server default is a fetched RESOURCE, not component state, so it does
26- // not need a post-await setState — which is both the react-doctor rule and
27- // the honest model: this component owns the operator's choice, not the
28- // server's setting.
29- const serverPref = useKeyedClientResource (
30- `oauth-open-browser:${ apiBase ?? "" } ` ,
31- [ apiBase ] ,
32- async ( signal ) => {
33- if ( ! apiBase ) return true ;
34- const res = await fetch ( `${ apiBase } /api/settings` , { signal } ) ;
35- if ( ! res . ok ) return true ;
36- const data = await res . json ( ) as { oauthOpenBrowser ?: boolean } ;
37- return typeof data . oauthOpenBrowser === "boolean" ? data . oauthOpenBrowser : true ;
38- } ,
39- ) ;
40-
41- // Until this operator chooses, follow the server; a failed read leaves the
42- // historical auto-open on screen.
43- const open = choice ?? serverPref . data ?? true ;
31+ const [ choice , setChoice ] = useState < boolean | undefined > ( readOpenBrowserPref ) ;
32+ const open = choice ?? serverDefault ;
4433
4534 return (
4635 < label className = "open-browser-pref" >
0 commit comments