Skip to content

Commit 546748a

Browse files
authored
fix(app): startup efficiency (#18854)
1 parent c9c93ea commit 546748a

33 files changed

Lines changed: 939 additions & 632 deletions

packages/app/src/app.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MarkedProvider } from "@opencode-ai/ui/context/marked"
66
import { File } from "@opencode-ai/ui/file"
77
import { Font } from "@opencode-ai/ui/font"
88
import { Splash } from "@opencode-ai/ui/logo"
9-
import { ThemeProvider } from "@opencode-ai/ui/theme"
9+
import { ThemeProvider } from "@opencode-ai/ui/theme/context"
1010
import { MetaProvider } from "@solidjs/meta"
1111
import { type BaseRouterProps, Navigate, Route, Router } from "@solidjs/router"
1212
import { QueryClient, QueryClientProvider } from "@tanstack/solid-query"
@@ -32,7 +32,7 @@ import { FileProvider } from "@/context/file"
3232
import { GlobalSDKProvider } from "@/context/global-sdk"
3333
import { GlobalSyncProvider } from "@/context/global-sync"
3434
import { HighlightsProvider } from "@/context/highlights"
35-
import { LanguageProvider, useLanguage } from "@/context/language"
35+
import { LanguageProvider, type Locale, useLanguage } from "@/context/language"
3636
import { LayoutProvider } from "@/context/layout"
3737
import { ModelsProvider } from "@/context/models"
3838
import { NotificationProvider } from "@/context/notification"
@@ -130,7 +130,7 @@ function RouterRoot(props: ParentProps<{ appChildren?: JSX.Element }>) {
130130
)
131131
}
132132

133-
export function AppBaseProviders(props: ParentProps) {
133+
export function AppBaseProviders(props: ParentProps<{ locale?: Locale }>) {
134134
return (
135135
<MetaProvider>
136136
<Font />
@@ -139,7 +139,7 @@ export function AppBaseProviders(props: ParentProps) {
139139
void window.api?.setTitlebar?.({ mode })
140140
}}
141141
>
142-
<LanguageProvider>
142+
<LanguageProvider locale={props.locale}>
143143
<UiI18nBridge>
144144
<ErrorBoundary fallback={(error) => <ErrorPage error={error} />}>
145145
<QueryProvider>

packages/app/src/components/dialog-connect-provider.tsx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ProviderAuthAuthorization } from "@opencode-ai/sdk/v2/client"
1+
import type { ProviderAuthAuthorization, ProviderAuthMethod } from "@opencode-ai/sdk/v2/client"
22
import { Button } from "@opencode-ai/ui/button"
33
import { useDialog } from "@opencode-ai/ui/context/dialog"
44
import { Dialog } from "@opencode-ai/ui/dialog"
@@ -9,7 +9,7 @@ import { ProviderIcon } from "@opencode-ai/ui/provider-icon"
99
import { Spinner } from "@opencode-ai/ui/spinner"
1010
import { TextField } from "@opencode-ai/ui/text-field"
1111
import { showToast } from "@opencode-ai/ui/toast"
12-
import { createMemo, Match, onCleanup, onMount, Switch } from "solid-js"
12+
import { createEffect, createMemo, createResource, Match, onCleanup, onMount, Switch } from "solid-js"
1313
import { createStore, produce } from "solid-js/store"
1414
import { Link } from "@/components/link"
1515
import { useGlobalSDK } from "@/context/global-sdk"
@@ -34,15 +34,25 @@ export function DialogConnectProvider(props: { provider: string }) {
3434
})
3535

3636
const provider = createMemo(() => globalSync.data.provider.all.find((x) => x.id === props.provider)!)
37-
const methods = createMemo(
38-
() =>
39-
globalSync.data.provider_auth[props.provider] ?? [
40-
{
41-
type: "api",
42-
label: language.t("provider.connect.method.apiKey"),
43-
},
44-
],
37+
const fallback = createMemo<ProviderAuthMethod[]>(() => [
38+
{
39+
type: "api" as const,
40+
label: language.t("provider.connect.method.apiKey"),
41+
},
42+
])
43+
const [auth] = createResource(
44+
() => props.provider,
45+
async () => {
46+
const cached = globalSync.data.provider_auth[props.provider]
47+
if (cached) return cached
48+
const res = await globalSDK.client.provider.auth()
49+
if (!alive.value) return fallback()
50+
globalSync.set("provider_auth", res.data ?? {})
51+
return res.data?.[props.provider] ?? fallback()
52+
},
4553
)
54+
const loading = createMemo(() => auth.loading && !globalSync.data.provider_auth[props.provider])
55+
const methods = createMemo(() => auth.latest ?? globalSync.data.provider_auth[props.provider] ?? fallback())
4656
const [store, setStore] = createStore({
4757
methodIndex: undefined as undefined | number,
4858
authorization: undefined as undefined | ProviderAuthAuthorization,
@@ -177,7 +187,11 @@ export function DialogConnectProvider(props: { provider: string }) {
177187
index: 0,
178188
})
179189

180-
const prompts = createMemo(() => method()?.prompts ?? [])
190+
const prompts = createMemo<NonNullable<ProviderAuthMethod["prompts"]>>(() => {
191+
const value = method()
192+
if (value?.type !== "oauth") return []
193+
return value.prompts ?? []
194+
})
181195
const matches = (prompt: NonNullable<ReturnType<typeof prompts>[number]>, value: Record<string, string>) => {
182196
if (!prompt.when) return true
183197
const actual = value[prompt.when.key]
@@ -296,8 +310,12 @@ export function DialogConnectProvider(props: { provider: string }) {
296310
listRef?.onKeyDown(e)
297311
}
298312

299-
onMount(() => {
313+
let auto = false
314+
createEffect(() => {
315+
if (auto) return
316+
if (loading()) return
300317
if (methods().length === 1) {
318+
auto = true
301319
selectMethod(0)
302320
}
303321
})
@@ -573,6 +591,14 @@ export function DialogConnectProvider(props: { provider: string }) {
573591
<div class="px-2.5 pb-10 flex flex-col gap-6">
574592
<div onKeyDown={handleKey} tabIndex={0} autofocus={store.methodIndex === undefined ? true : undefined}>
575593
<Switch>
594+
<Match when={loading()}>
595+
<div class="text-14-regular text-text-base">
596+
<div class="flex items-center gap-x-2">
597+
<Spinner />
598+
<span>{language.t("provider.connect.status.inProgress")}</span>
599+
</div>
600+
</div>
601+
</Match>
576602
<Match when={store.methodIndex === undefined}>
577603
<MethodSelection />
578604
</Match>

packages/app/src/components/settings-general.tsx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,61 @@
1-
import { Component, Show, createMemo, createResource, type JSX } from "solid-js"
1+
import { Component, Show, createMemo, createResource, onMount, type JSX } from "solid-js"
22
import { createStore } from "solid-js/store"
33
import { Button } from "@opencode-ai/ui/button"
44
import { Icon } from "@opencode-ai/ui/icon"
55
import { Select } from "@opencode-ai/ui/select"
66
import { Switch } from "@opencode-ai/ui/switch"
77
import { Tooltip } from "@opencode-ai/ui/tooltip"
8-
import { useTheme, type ColorScheme } from "@opencode-ai/ui/theme"
8+
import { useTheme, type ColorScheme } from "@opencode-ai/ui/theme/context"
99
import { showToast } from "@opencode-ai/ui/toast"
1010
import { useLanguage } from "@/context/language"
1111
import { usePlatform } from "@/context/platform"
1212
import { useSettings, monoFontFamily } from "@/context/settings"
13-
import { playSound, SOUND_OPTIONS } from "@/utils/sound"
13+
import { playSoundById, SOUND_OPTIONS } from "@/utils/sound"
1414
import { Link } from "./link"
1515
import { SettingsList } from "./settings-list"
1616

1717
let demoSoundState = {
1818
cleanup: undefined as (() => void) | undefined,
1919
timeout: undefined as NodeJS.Timeout | undefined,
20+
run: 0,
21+
}
22+
23+
type ThemeOption = {
24+
id: string
25+
name: string
26+
}
27+
28+
let font: Promise<typeof import("@opencode-ai/ui/font-loader")> | undefined
29+
30+
function loadFont() {
31+
font ??= import("@opencode-ai/ui/font-loader")
32+
return font
2033
}
2134

2235
// To prevent audio from overlapping/playing very quickly when navigating the settings menus,
2336
// delay the playback by 100ms during quick selection changes and pause existing sounds.
2437
const stopDemoSound = () => {
38+
demoSoundState.run += 1
2539
if (demoSoundState.cleanup) {
2640
demoSoundState.cleanup()
2741
}
2842
clearTimeout(demoSoundState.timeout)
2943
demoSoundState.cleanup = undefined
3044
}
3145

32-
const playDemoSound = (src: string | undefined) => {
46+
const playDemoSound = (id: string | undefined) => {
3347
stopDemoSound()
34-
if (!src) return
48+
if (!id) return
3549

50+
const run = ++demoSoundState.run
3651
demoSoundState.timeout = setTimeout(() => {
37-
demoSoundState.cleanup = playSound(src)
52+
void playSoundById(id).then((cleanup) => {
53+
if (demoSoundState.run !== run) {
54+
cleanup?.()
55+
return
56+
}
57+
demoSoundState.cleanup = cleanup
58+
})
3859
}, 100)
3960
}
4061

@@ -44,6 +65,10 @@ export const SettingsGeneral: Component = () => {
4465
const platform = usePlatform()
4566
const settings = useSettings()
4667

68+
onMount(() => {
69+
void theme.loadThemes()
70+
})
71+
4772
const [store, setStore] = createStore({
4873
checking: false,
4974
})
@@ -104,9 +129,7 @@ export const SettingsGeneral: Component = () => {
104129
.finally(() => setStore("checking", false))
105130
}
106131

107-
const themeOptions = createMemo(() =>
108-
Object.entries(theme.themes()).map(([id, def]) => ({ id, name: def.name ?? id })),
109-
)
132+
const themeOptions = createMemo<ThemeOption[]>(() => theme.ids().map((id) => ({ id, name: theme.name(id) })))
110133

111134
const colorSchemeOptions = createMemo((): { value: ColorScheme; label: string }[] => [
112135
{ value: "system", label: language.t("theme.scheme.system") },
@@ -143,7 +166,7 @@ export const SettingsGeneral: Component = () => {
143166
] as const
144167
const fontOptionsList = [...fontOptions]
145168

146-
const noneSound = { id: "none", label: "sound.option.none", src: undefined } as const
169+
const noneSound = { id: "none", label: "sound.option.none" } as const
147170
const soundOptions = [noneSound, ...SOUND_OPTIONS]
148171

149172
const soundSelectProps = (
@@ -158,7 +181,7 @@ export const SettingsGeneral: Component = () => {
158181
label: (o: (typeof soundOptions)[number]) => language.t(o.label),
159182
onHighlight: (option: (typeof soundOptions)[number] | undefined) => {
160183
if (!option) return
161-
playDemoSound(option.src)
184+
playDemoSound(option.id === "none" ? undefined : option.id)
162185
},
163186
onSelect: (option: (typeof soundOptions)[number] | undefined) => {
164187
if (!option) return
@@ -169,7 +192,7 @@ export const SettingsGeneral: Component = () => {
169192
}
170193
setEnabled(true)
171194
set(option.id)
172-
playDemoSound(option.src)
195+
playDemoSound(option.id)
173196
},
174197
variant: "secondary" as const,
175198
size: "small" as const,
@@ -321,6 +344,9 @@ export const SettingsGeneral: Component = () => {
321344
current={fontOptionsList.find((o) => o.value === settings.appearance.font())}
322345
value={(o) => o.value}
323346
label={(o) => language.t(o.label)}
347+
onHighlight={(option) => {
348+
void loadFont().then((x) => x.ensureMonoFont(option?.value))
349+
}}
324350
onSelect={(option) => option && settings.appearance.setFont(option.value)}
325351
variant="secondary"
326352
size="small"

packages/app/src/components/status-popover.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { useSDK } from "@/context/sdk"
1616
import { normalizeServerUrl, ServerConnection, useServer } from "@/context/server"
1717
import { useSync } from "@/context/sync"
1818
import { useCheckServerHealth, type ServerHealth } from "@/utils/server-health"
19-
import { DialogSelectServer } from "./dialog-select-server"
2019

2120
const pollMs = 10_000
2221

@@ -54,11 +53,15 @@ const listServersByHealth = (
5453
})
5554
}
5655

57-
const useServerHealth = (servers: Accessor<ServerConnection.Any[]>) => {
56+
const useServerHealth = (servers: Accessor<ServerConnection.Any[]>, enabled: Accessor<boolean>) => {
5857
const checkServerHealth = useCheckServerHealth()
5958
const [status, setStatus] = createStore({} as Record<ServerConnection.Key, ServerHealth | undefined>)
6059

6160
createEffect(() => {
61+
if (!enabled()) {
62+
setStatus(reconcile({}))
63+
return
64+
}
6265
const list = servers()
6366
let dead = false
6467

@@ -162,14 +165,20 @@ export function StatusPopover() {
162165
const navigate = useNavigate()
163166

164167
const [shown, setShown] = createSignal(false)
168+
let dialogRun = 0
169+
let dialogDead = false
170+
onCleanup(() => {
171+
dialogDead = true
172+
dialogRun += 1
173+
})
165174
const servers = createMemo(() => {
166175
const current = server.current
167176
const list = server.list
168177
if (!current) return list
169178
if (list.every((item) => ServerConnection.key(item) !== ServerConnection.key(current))) return [current, ...list]
170179
return [current, ...list.filter((item) => ServerConnection.key(item) !== ServerConnection.key(current))]
171180
})
172-
const health = useServerHealth(servers)
181+
const health = useServerHealth(servers, shown)
173182
const sortedServers = createMemo(() => listServersByHealth(servers(), server.key, health))
174183
const toggleMcp = useMcpToggleMutation()
175184
const defaultServer = useDefaultServerKey(platform.getDefaultServer)
@@ -300,7 +309,13 @@ export function StatusPopover() {
300309
<Button
301310
variant="secondary"
302311
class="mt-3 self-start h-8 px-3 py-1.5"
303-
onClick={() => dialog.show(() => <DialogSelectServer />, defaultServer.refresh)}
312+
onClick={() => {
313+
const run = ++dialogRun
314+
void import("./dialog-select-server").then((x) => {
315+
if (dialogDead || dialogRun !== run) return
316+
dialog.show(() => <x.DialogSelectServer />, defaultServer.refresh)
317+
})
318+
}}
304319
>
305320
{language.t("status.popover.action.manageServers")}
306321
</Button>

packages/app/src/components/terminal.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { type HexColor, resolveThemeVariant, useTheme, withAlpha } from "@opencode-ai/ui/theme"
1+
import { withAlpha } from "@opencode-ai/ui/theme/color"
2+
import { useTheme } from "@opencode-ai/ui/theme/context"
3+
import { resolveThemeVariant } from "@opencode-ai/ui/theme/resolve"
4+
import type { HexColor } from "@opencode-ai/ui/theme/types"
25
import { showToast } from "@opencode-ai/ui/toast"
36
import type { FitAddon, Ghostty, Terminal as Term } from "ghostty-web"
47
import { type ComponentProps, createEffect, createMemo, onCleanup, onMount, splitProps } from "solid-js"

packages/app/src/components/titlebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IconButton } from "@opencode-ai/ui/icon-button"
55
import { Icon } from "@opencode-ai/ui/icon"
66
import { Button } from "@opencode-ai/ui/button"
77
import { Tooltip, TooltipKeybind } from "@opencode-ai/ui/tooltip"
8-
import { useTheme } from "@opencode-ai/ui/theme"
8+
import { useTheme } from "@opencode-ai/ui/theme/context"
99

1010
import { useLayout } from "@/context/layout"
1111
import { usePlatform } from "@/context/platform"

0 commit comments

Comments
 (0)