import { Component, Show, createMemo, createResource, onMount, type JSX } from "solid-js" import { createStore } from "solid-js/store" import { Button } from "@opencode-ai/ui/button" import { Icon } from "@opencode-ai/ui/icon" import { Select } from "@opencode-ai/ui/select" import { Switch } from "@opencode-ai/ui/switch" import { TextField } from "@opencode-ai/ui/text-field" import { Tooltip } from "@opencode-ai/ui/tooltip" import { useTheme, type ColorScheme } from "@opencode-ai/ui/theme/context" import { showToast } from "@opencode-ai/ui/toast" import { useParams } from "@solidjs/router" import { useLanguage } from "@/context/language" import { usePermission } from "@/context/permission" import { usePlatform, type DisplayBackend } from "@/context/platform" import { useGlobalSync } from "@/context/global-sync" import { useGlobalSDK } from "@/context/global-sdk" import { monoDefault, monoFontFamily, monoInput, sansDefault, sansFontFamily, sansInput, terminalDefault, terminalFontFamily, terminalInput, useSettings, } from "@/context/settings" import { decode64 } from "@/utils/base64" import { playSoundById, SOUND_OPTIONS } from "@/utils/sound" import { Link } from "./link" import { SettingsList } from "./settings-list" let demoSoundState = { cleanup: undefined as (() => void) | undefined, timeout: undefined as NodeJS.Timeout | undefined, run: 0, } type ThemeOption = { id: string name: string } type ShellOption = { path: string name: string acceptable: boolean } type ShellSelectOption = { id: string value: string label: string } // To prevent audio from overlapping/playing very quickly when navigating the settings menus, // delay the playback by 100ms during quick selection changes and pause existing sounds. const stopDemoSound = () => { demoSoundState.run += 1 if (demoSoundState.cleanup) { demoSoundState.cleanup() } clearTimeout(demoSoundState.timeout) demoSoundState.cleanup = undefined } const playDemoSound = (id: string | undefined) => { stopDemoSound() if (!id) return const run = ++demoSoundState.run demoSoundState.timeout = setTimeout(() => { void playSoundById(id).then((cleanup) => { if (demoSoundState.run !== run) { cleanup?.() return } demoSoundState.cleanup = cleanup }) }, 100) } export const SettingsGeneral: Component = () => { const theme = useTheme() const language = useLanguage() const permission = usePermission() const platform = usePlatform() const params = useParams() const settings = useSettings() const [store, setStore] = createStore({ checking: false, }) const linux = createMemo(() => platform.platform === "desktop" && platform.os === "linux") const dir = createMemo(() => decode64(params.dir)) const accepting = createMemo(() => { const value = dir() if (!value) return false if (!params.id) return permission.isAutoAcceptingDirectory(value) return permission.isAutoAccepting(params.id, value) }) const toggleAccept = (checked: boolean) => { const value = dir() if (!value) return if (!params.id) { if (permission.isAutoAcceptingDirectory(value) === checked) return permission.toggleAutoAcceptDirectory(value) return } if (checked) { permission.enableAutoAccept(params.id, value) return } permission.disableAutoAccept(params.id, value) } const desktop = createMemo(() => platform.platform === "desktop") const check = () => { if (!platform.checkUpdate) return setStore("checking", true) void platform .checkUpdate() .then((result) => { if (!result.updateAvailable) { showToast({ variant: "success", icon: "circle-check", title: language.t("settings.updates.toast.latest.title"), description: language.t("settings.updates.toast.latest.description", { version: platform.version ?? "" }), }) return } const actions = platform.updateAndRestart ? [ { label: language.t("toast.update.action.installRestart"), onClick: async () => { await platform.updateAndRestart!() }, }, { label: language.t("toast.update.action.notYet"), onClick: "dismiss" as const, }, ] : [ { label: language.t("toast.update.action.notYet"), onClick: "dismiss" as const, }, ] showToast({ persistent: true, icon: "download", title: language.t("toast.update.title"), description: language.t("toast.update.description", { version: result.version ?? "" }), actions, }) }) .catch((err: unknown) => { const message = err instanceof Error ? err.message : String(err) showToast({ title: language.t("common.requestFailed"), description: message }) }) .finally(() => setStore("checking", false)) } const themeOptions = createMemo(() => theme.ids().map((id) => ({ id, name: theme.name(id) }))) const globalSync = useGlobalSync() const globalSdk = useGlobalSDK() const [shells] = createResource( () => globalSdk.client.pty .shells() .then((res) => res.data ?? []) .catch(() => [] as ShellOption[]), { initialValue: [] as ShellOption[] }, ) const [displayBackend, { refetch: refetchDisplayBackend }] = createResource( () => (linux() && platform.getDisplayBackend ? true : false), () => Promise.resolve(platform.getDisplayBackend?.() ?? null).catch(() => null as DisplayBackend | null), { initialValue: null as DisplayBackend | null }, ) onMount(() => { void theme.loadThemes() }) const autoOption = { id: "auto", value: "", label: language.t("settings.general.row.shell.autoDefault") } const currentShell = createMemo(() => globalSync.data.config.shell ?? "") const shellOptions = createMemo(() => { const list = shells.latest const current = globalSync.data.config.shell const nameCounts = new Map() for (const s of list) { nameCounts.set(s.name, (nameCounts.get(s.name) || 0) + 1) } const options = [ autoOption, ...list.map((s) => { const ambiguousName = (nameCounts.get(s.name) || 0) > 1 const text = ambiguousName ? s.path : s.name const label = s.acceptable ? text : `${text} (${language.t("settings.general.row.shell.terminalOnly")})` return { id: s.path, // Prefer name over path - "bash" is much cleaner than the explicit full route even when it may change due to PATH. value: ambiguousName ? s.path : s.name, label, } }), ] if (current && !options.some((o) => o.value === current)) { options.push({ id: current, value: current, label: current }) } return options }) const onDisplayBackendChange = (checked: boolean) => { const update = platform.setDisplayBackend?.(checked ? "wayland" : "auto") if (!update) return void update.finally(() => { void refetchDisplayBackend() }) } const colorSchemeOptions = createMemo((): { value: ColorScheme; label: string }[] => [ { value: "system", label: language.t("theme.scheme.system") }, { value: "light", label: language.t("theme.scheme.light") }, { value: "dark", label: language.t("theme.scheme.dark") }, ]) const languageOptions = createMemo(() => language.locales.map((locale) => ({ value: locale, label: language.label(locale), })), ) const noneSound = { id: "none", label: "sound.option.none" } as const const soundOptions = [noneSound, ...SOUND_OPTIONS] const mono = () => monoInput(settings.appearance.font()) const sans = () => sansInput(settings.appearance.uiFont()) const terminal = () => terminalInput(settings.appearance.terminalFont()) const soundSelectProps = ( enabled: () => boolean, current: () => string, setEnabled: (value: boolean) => void, set: (id: string) => void, ) => ({ options: soundOptions, current: enabled() ? (soundOptions.find((o) => o.id === current()) ?? noneSound) : noneSound, value: (o: (typeof soundOptions)[number]) => o.id, label: (o: (typeof soundOptions)[number]) => language.t(o.label), onHighlight: (option: (typeof soundOptions)[number] | undefined) => { if (!option) return playDemoSound(option.id === "none" ? undefined : option.id) }, onSelect: (option: (typeof soundOptions)[number] | undefined) => { if (!option) return if (option.id === "none") { setEnabled(false) stopDemoSound() return } setEnabled(true) set(option.id) playDemoSound(option.id) }, variant: "secondary" as const, size: "small" as const, triggerVariant: "settings" as const, }) const GeneralSection = () => (
o.value === currentShell()) ?? autoOption} value={(o) => o.id} label={(o) => o.label} onSelect={(option) => { if (!option) return if (option.value === currentShell()) return globalSync.updateConfig({ shell: option.value }) }} variant="secondary" size="small" triggerVariant="settings" triggerStyle={{ "min-width": "180px" }} />
settings.general.setShowReasoningSummaries(checked)} />
settings.general.setShellToolPartsExpanded(checked)} />
settings.general.setEditToolPartsExpanded(checked)} />
settings.general.setShowSessionProgressBar(checked)} />
) const AdvancedSection = () => (

{language.t("settings.general.section.advanced")}

settings.general.setShowFileTree(checked)} />
settings.general.setShowNavigation(checked)} />
settings.general.setShowSearch(checked)} />
settings.general.setShowTerminal(checked)} />
settings.general.setShowStatus(checked)} />
) const AppearanceSection = () => (

{language.t("settings.general.section.appearance")}

o.id === theme.themeId())} value={(o) => o.id} label={(o) => o.name} onSelect={(option) => { if (!option) return theme.setTheme(option.id) }} onHighlight={(option) => { if (!option) return theme.previewTheme(option.id) return () => theme.cancelPreview() }} variant="secondary" size="small" triggerVariant="settings" />
settings.appearance.setUIFont(value)} placeholder={sansDefault} spellcheck={false} autocorrect="off" autocomplete="off" autocapitalize="off" class="text-12-regular" style={{ "font-family": sansFontFamily(settings.appearance.uiFont()) }} />
settings.appearance.setFont(value)} placeholder={monoDefault} spellcheck={false} autocorrect="off" autocomplete="off" autocapitalize="off" class="text-12-regular" style={{ "font-family": monoFontFamily(settings.appearance.font()) }} />
settings.appearance.setTerminalFont(value)} placeholder={terminalDefault} spellcheck={false} autocorrect="off" autocomplete="off" autocapitalize="off" class="text-12-regular" style={{ "font-family": terminalFontFamily(settings.appearance.terminalFont()) }} />
) const NotificationsSection = () => (

{language.t("settings.general.section.notifications")}

settings.notifications.setAgent(checked)} />
settings.notifications.setPermissions(checked)} />
settings.notifications.setErrors(checked)} />
) const SoundsSection = () => (

{language.t("settings.general.section.sounds")}

settings.sounds.permissionsEnabled(), () => settings.sounds.permissions(), (value) => settings.sounds.setPermissionsEnabled(value), (id) => settings.sounds.setPermissions(id), )} />