The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useScopedSettingsWriteAllowed } from "./useScopedSettings"; |
| 2 | import { SettingsIcon } from "lucide-react"; |
| 3 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 4 | import { useState } from "react"; |
| 5 | import { type BackgroundActivityProfile } from "@t3tools/contracts"; |
| 6 | import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings"; |
| 7 | import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; |
| 8 | import * as Duration from "effect/Duration"; |
| 9 | import * as Equal from "effect/Equal"; |
| 10 | import { |
| 11 | useScopedSettings, |
| 12 | useScopedSettingsMixed, |
| 13 | useUpdateScopedSettings, |
| 14 | } from "./useScopedSettings"; |
| 15 | import { useSettingsScope } from "./SettingsScopeContext"; |
| 16 | import { Button } from "../ui/button"; |
| 17 | import { |
| 18 | Dialog, |
| 19 | DialogDescription, |
| 20 | DialogFooter, |
| 21 | DialogHeader, |
| 22 | DialogPanel, |
| 23 | DialogPopup, |
| 24 | DialogTitle, |
| 25 | } from "../ui/dialog"; |
| 26 | import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; |
| 27 | import { Switch } from "../ui/switch"; |
| 28 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 29 | import { |
| 30 | backgroundActivityOverrideSettings, |
| 31 | backgroundActivitySharedPolicySettings, |
| 32 | durationToSeconds, |
| 33 | PROVIDER_HEALTH_INTERVAL_STEP_SECONDS, |
| 34 | resolveBackgroundActivityProfileOption, |
| 35 | } from "./SettingsPanels.logic"; |
| 36 | import { IntervalSecondsField } from "./IntervalSecondsField"; |
| 37 | import { PolicyTooltip, SettingResetButton, SettingsRow } from "./settingsLayout"; |
| 38 | import { useSearchableSetting } from "./settingsSearch"; |
| 39 | |
| 40 | const BACKGROUND_ACTIVITY_PROFILE_LABELS: Record<BackgroundActivityProfile, string> = { |
| 41 | balanced: msg("Balanced"), |
| 42 | performance: msg("Performance"), |
| 43 | "battery-saver": msg("Battery saver"), |
| 44 | }; |
| 45 | |
| 46 | type BackgroundActivityProfileOption = BackgroundActivityProfile | "advanced"; |
| 47 | |
| 48 | const BACKGROUND_ACTIVITY_PROFILE_OPTION_LABELS: Record<BackgroundActivityProfileOption, string> = { |
| 49 | ...BACKGROUND_ACTIVITY_PROFILE_LABELS, |
| 50 | advanced: msg("Advanced", { $context: "background activity power profile" }), |
| 51 | }; |
| 52 | |
| 53 | const BACKGROUND_ACTIVITY_PROFILE_DESCRIPTIONS: Record<BackgroundActivityProfile, string> = { |
| 54 | balanced: msg("Pauses probes for idle clients, locked hosts, or low power mode."), |
| 55 | performance: msg( |
| 56 | "Allows scoped background probes while any subscribed client remains connected.", |
| 57 | ), |
| 58 | "battery-saver": msg("Also pauses background probes when the host or client is on battery."), |
| 59 | }; |
| 60 | |
| 61 | const BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES: ReadonlyArray<{ |
| 62 | readonly key: |
| 63 | | "pauseWhenHostLocked" |
| 64 | | "pauseWhenHostLowPower" |
| 65 | | "pauseWhenClientLowPower" |
| 66 | | "pauseWhenOnBattery"; |
| 67 | readonly label: string; |
| 68 | }> = [ |
| 69 | { key: "pauseWhenHostLocked", label: msg("Pause when host is locked") }, |
| 70 | { key: "pauseWhenHostLowPower", label: msg("Pause on host low power") }, |
| 71 | { key: "pauseWhenClientLowPower", label: msg("Pause on client low power") }, |
| 72 | { key: "pauseWhenOnBattery", label: msg("Pause on battery") }, |
| 73 | ]; |
| 74 | |
| 75 | function resetBackgroundActivitySettings() { |
| 76 | return { |
| 77 | backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity, |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | function backgroundActivityProfileSettings(profile: BackgroundActivityProfile) { |
| 82 | return { |
| 83 | backgroundActivity: { |
| 84 | schemaVersion: 1 as const, |
| 85 | profile, |
| 86 | overrides: {}, |
| 87 | }, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | function BackgroundActivityAdvancedDialog({ |
| 92 | open, |
| 93 | onOpenChange, |
| 94 | }: { |
| 95 | readonly open: boolean; |
| 96 | readonly onOpenChange: (open: boolean) => void; |
| 97 | }) { |
| 98 | const searchableSetting = useSearchableSetting(); |
| 99 | const gt = useGT(); |
| 100 | const m = useMessages(); |
| 101 | const canWriteSettings = useScopedSettingsWriteAllowed(); |
| 102 | const settings = useScopedSettings(); |
| 103 | const updateSettings = useUpdateScopedSettings(); |
| 104 | const resolvedBackgroundActivity = resolveServerBackgroundActivitySettings(settings); |
| 105 | const activeProfile = resolvedBackgroundActivity.profile; |
| 106 | const automaticGitFetchIntervalSeconds = durationToSeconds( |
| 107 | resolvedBackgroundActivity.automaticGitFetchInterval, |
| 108 | ); |
| 109 | const providerHealthRefreshIntervalSeconds = durationToSeconds( |
| 110 | resolvedBackgroundActivity.providerHealthRefreshInterval, |
| 111 | ); |
| 112 | const hostPowerMonitorActiveIntervalSeconds = durationToSeconds( |
| 113 | resolvedBackgroundActivity.hostPowerMonitorActiveInterval, |
| 114 | ); |
| 115 | const hostPowerMonitorIdleIntervalSeconds = durationToSeconds( |
| 116 | resolvedBackgroundActivity.hostPowerMonitorIdleInterval, |
| 117 | ); |
| 118 | |
| 119 | return ( |
| 120 | <Dialog open={open && canWriteSettings} onOpenChange={onOpenChange}> |
| 121 | <DialogPopup className="max-w-xl"> |
| 122 | <DialogHeader> |
| 123 | <DialogTitle> |
| 124 | <T>Background Activity</T> |
| 125 | </DialogTitle> |
| 126 | <DialogDescription> |
| 127 | <T>Tune the shared power policy and the background intervals that feed it.</T> |
| 128 | </DialogDescription> |
| 129 | </DialogHeader> |
| 130 | <DialogPanel> |
| 131 | <fieldset |
| 132 | disabled={!canWriteSettings} |
| 133 | className="min-w-0 overflow-hidden rounded-xl border bg-card text-card-foreground" |
| 134 | > |
| 135 | <div className="flex flex-col gap-3 border-b px-4 py-3 sm:flex-row sm:items-center sm:justify-between"> |
| 136 | <div className="min-w-0 space-y-1"> |
| 137 | <div className="text-sm font-medium"> |
| 138 | <T>Shared policy</T> |
| 139 | </div> |
| 140 | <p className="text-xs leading-relaxed text-muted-foreground"> |
| 141 | <T>Controls whether background work may run after a subscribed interval fires.</T> |
| 142 | </p> |
| 143 | </div> |
| 144 | <Select |
| 145 | value={activeProfile} |
| 146 | onValueChange={(value) => { |
| 147 | if ( |
| 148 | value === "balanced" || |
| 149 | value === "performance" || |
| 150 | value === "battery-saver" |
| 151 | ) { |
| 152 | updateSettings({ |
| 153 | backgroundActivity: backgroundActivitySharedPolicySettings(settings, value), |
| 154 | }); |
| 155 | } |
| 156 | }} |
| 157 | > |
| 158 | <SelectTrigger |
| 159 | size="sm" |
| 160 | className="w-full sm:w-40" |
| 161 | aria-label={gt("Shared background policy")} |
| 162 | > |
| 163 | <SelectValue>{m(BACKGROUND_ACTIVITY_PROFILE_LABELS[activeProfile])}</SelectValue> |
| 164 | </SelectTrigger> |
| 165 | <SelectPopup align="end" alignItemWithTrigger={false}> |
| 166 | <SelectItem hideIndicator value="balanced"> |
| 167 | {m(BACKGROUND_ACTIVITY_PROFILE_LABELS.balanced)} |
| 168 | </SelectItem> |
| 169 | <SelectItem hideIndicator value="performance"> |
| 170 | {m(BACKGROUND_ACTIVITY_PROFILE_LABELS.performance)} |
| 171 | </SelectItem> |
| 172 | <SelectItem hideIndicator value="battery-saver"> |
| 173 | {m(BACKGROUND_ACTIVITY_PROFILE_LABELS["battery-saver"])} |
| 174 | </SelectItem> |
| 175 | </SelectPopup> |
| 176 | </Select> |
| 177 | </div> |
| 178 | |
| 179 | <div className="flex flex-col gap-3 border-b px-4 py-3 sm:flex-row sm:items-center sm:justify-between"> |
| 180 | <div className="min-w-0 space-y-1"> |
| 181 | <div className="text-sm font-medium"> |
| 182 | {searchableSetting("git-fetch-interval").title} |
| 183 | </div> |
| 184 | <p className="text-xs leading-relaxed text-muted-foreground"> |
| 185 | <T>Refresh remote branch status in the background.</T> |
| 186 | </p> |
| 187 | </div> |
| 188 | <div className="flex shrink-0 items-center gap-2"> |
| 189 | <IntervalSecondsField |
| 190 | labels={{ |
| 191 | decrease: gt("Decrease Git fetch interval"), |
| 192 | input: gt("Git fetch interval in seconds"), |
| 193 | increase: gt("Increase Git fetch interval"), |
| 194 | }} |
| 195 | value={automaticGitFetchIntervalSeconds} |
| 196 | min={0} |
| 197 | step={5} |
| 198 | onCommit={(seconds, options) => |
| 199 | updateSettings( |
| 200 | backgroundActivityOverrideSettings( |
| 201 | settings.backgroundActivity, |
| 202 | resolvedBackgroundActivity, |
| 203 | { automaticGitFetchInterval: Duration.seconds(seconds) }, |
| 204 | ), |
| 205 | options, |
| 206 | ) |
| 207 | } |
| 208 | /> |
| 209 | <span className="text-xs text-muted-foreground"> |
| 210 | <T>seconds</T> |
| 211 | </span> |
| 212 | </div> |
| 213 | </div> |
| 214 | |
| 215 | <div className="flex flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center sm:justify-between"> |
| 216 | <div className="min-w-0 space-y-1"> |
| 217 | <div className="text-sm font-medium"> |
| 218 | <T>Provider health interval</T> |
| 219 | </div> |
| 220 | <p className="text-xs leading-relaxed text-muted-foreground"> |
| 221 | <T> |
| 222 | Refresh provider availability, versions, and auth state. Model catalog updates |
| 223 | follow Provider update checks. |
| 224 | </T> |
| 225 | </p> |
| 226 | </div> |
| 227 | <div className="flex shrink-0 items-center gap-2"> |
| 228 | <IntervalSecondsField |
| 229 | labels={{ |
| 230 | decrease: gt("Decrease provider health interval"), |
| 231 | input: gt("Provider health interval in seconds"), |
| 232 | increase: gt("Increase provider health interval"), |
| 233 | }} |
| 234 | value={providerHealthRefreshIntervalSeconds} |
| 235 | min={0} |
| 236 | step={PROVIDER_HEALTH_INTERVAL_STEP_SECONDS} |
| 237 | onCommit={(seconds, options) => |
| 238 | updateSettings( |
| 239 | backgroundActivityOverrideSettings( |
| 240 | settings.backgroundActivity, |
| 241 | resolvedBackgroundActivity, |
| 242 | { providerHealthRefreshInterval: Duration.seconds(seconds) }, |
| 243 | ), |
| 244 | options, |
| 245 | ) |
| 246 | } |
| 247 | /> |
| 248 | <span className="text-xs text-muted-foreground"> |
| 249 | <T>seconds</T> |
| 250 | </span> |
| 251 | </div> |
| 252 | </div> |
| 253 | |
| 254 | <div className="flex flex-col gap-3 border-t px-4 py-3 sm:flex-row sm:items-center sm:justify-between"> |
| 255 | <div className="min-w-0 space-y-1"> |
| 256 | <div className="text-sm font-medium"> |
| 257 | <T>Host power monitor</T> |
| 258 | </div> |
| 259 | <p className="text-xs leading-relaxed text-muted-foreground"> |
| 260 | <T>Poll host power state while clients are active.</T> |
| 261 | </p> |
| 262 | </div> |
| 263 | <div className="flex shrink-0 items-center gap-2"> |
| 264 | <IntervalSecondsField |
| 265 | labels={{ |
| 266 | decrease: gt("Decrease active host power interval"), |
| 267 | input: gt("Active host power interval in seconds"), |
| 268 | increase: gt("Increase active host power interval"), |
| 269 | }} |
| 270 | value={hostPowerMonitorActiveIntervalSeconds} |
| 271 | min={5} |
| 272 | step={5} |
| 273 | onCommit={(seconds, options) => |
| 274 | updateSettings( |
| 275 | backgroundActivityOverrideSettings( |
| 276 | settings.backgroundActivity, |
| 277 | resolvedBackgroundActivity, |
| 278 | { hostPowerMonitorActiveInterval: Duration.seconds(seconds) }, |
| 279 | ), |
| 280 | options, |
| 281 | ) |
| 282 | } |
| 283 | /> |
| 284 | <span className="text-xs text-muted-foreground"> |
| 285 | <T>seconds</T> |
| 286 | </span> |
| 287 | </div> |
| 288 | </div> |
| 289 | |
| 290 | <div className="flex flex-col gap-3 border-t px-4 py-3 sm:flex-row sm:items-center sm:justify-between"> |
| 291 | <div className="min-w-0 space-y-1"> |
| 292 | <div className="text-sm font-medium"> |
| 293 | <T>Idle host monitor</T> |
| 294 | </div> |
| 295 | <p className="text-xs leading-relaxed text-muted-foreground"> |
| 296 | <T>Poll host power state when no foreground client is active.</T> |
| 297 | </p> |
| 298 | </div> |
| 299 | <div className="flex shrink-0 items-center gap-2"> |
| 300 | <IntervalSecondsField |
| 301 | labels={{ |
| 302 | decrease: gt("Decrease idle host power interval"), |
| 303 | input: gt("Idle host power interval in seconds"), |
| 304 | increase: gt("Increase idle host power interval"), |
| 305 | }} |
| 306 | value={hostPowerMonitorIdleIntervalSeconds} |
| 307 | min={5} |
| 308 | step={30} |
| 309 | onCommit={(seconds, options) => |
| 310 | updateSettings( |
| 311 | backgroundActivityOverrideSettings( |
| 312 | settings.backgroundActivity, |
| 313 | resolvedBackgroundActivity, |
| 314 | { hostPowerMonitorIdleInterval: Duration.seconds(seconds) }, |
| 315 | ), |
| 316 | options, |
| 317 | ) |
| 318 | } |
| 319 | /> |
| 320 | <span className="text-xs text-muted-foreground"> |
| 321 | <T>seconds</T> |
| 322 | </span> |
| 323 | </div> |
| 324 | </div> |
| 325 | |
| 326 | <div className="grid gap-0 border-t sm:grid-cols-2"> |
| 327 | {BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES.map(({ key, label }) => ( |
| 328 | <label |
| 329 | key={key} |
| 330 | className="flex items-center justify-between gap-3 border-b px-4 py-3 last:border-b-0 sm:border-r sm:even:border-r-0" |
| 331 | > |
| 332 | <span className="text-sm font-medium">{m(label)}</span> |
| 333 | <Switch |
| 334 | checked={resolvedBackgroundActivity[key]} |
| 335 | onCheckedChange={(checked) => |
| 336 | updateSettings( |
| 337 | backgroundActivityOverrideSettings( |
| 338 | settings.backgroundActivity, |
| 339 | resolvedBackgroundActivity, |
| 340 | { |
| 341 | [key]: Boolean(checked), |
| 342 | }, |
| 343 | ), |
| 344 | ) |
| 345 | } |
| 346 | aria-label={m(label)} |
| 347 | /> |
| 348 | </label> |
| 349 | ))} |
| 350 | </div> |
| 351 | </fieldset> |
| 352 | </DialogPanel> |
| 353 | <DialogFooter> |
| 354 | <Button |
| 355 | variant="outline" |
| 356 | disabled={!canWriteSettings} |
| 357 | onClick={() => updateSettings(resetBackgroundActivitySettings())} |
| 358 | > |
| 359 | <T>Reset all</T> |
| 360 | </Button> |
| 361 | <Button onClick={() => onOpenChange(false)}> |
| 362 | <T>Done</T> |
| 363 | </Button> |
| 364 | </DialogFooter> |
| 365 | </DialogPopup> |
| 366 | </Dialog> |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | export function BackgroundActivitySettingsRow() { |
| 371 | const searchableSetting = useSearchableSetting(); |
| 372 | const gt = useGT(); |
| 373 | const m = useMessages(); |
| 374 | const settings = useScopedSettings(); |
| 375 | const updateSettings = useUpdateScopedSettings(); |
| 376 | const { scope, environment } = useSettingsScope(); |
| 377 | const environmentId = environment?.environmentId ?? null; |
| 378 | const isEnvironmentScope = scope.environmentIds.length === 1 && environmentId !== null; |
| 379 | const [backgroundActivityDialogOpen, setBackgroundActivityDialogOpen] = useState(false); |
| 380 | const mixedBackgroundActivity = useScopedSettingsMixed(["backgroundActivity"]); |
| 381 | const resolvedBackgroundActivity = resolveServerBackgroundActivitySettings(settings); |
| 382 | const activeBackgroundActivityProfile = resolvedBackgroundActivity.profile; |
| 383 | const backgroundActivityProfileOption = resolveBackgroundActivityProfileOption(settings); |
| 384 | const backgroundActivityDescription = |
| 385 | backgroundActivityProfileOption === "advanced" |
| 386 | ? gt("Uses custom intervals. Shared policy: {profile}.", { |
| 387 | profile: m(BACKGROUND_ACTIVITY_PROFILE_LABELS[activeBackgroundActivityProfile]), |
| 388 | }) |
| 389 | : m(BACKGROUND_ACTIVITY_PROFILE_DESCRIPTIONS[resolvedBackgroundActivity.profile]); |
| 390 | const canResetBackgroundActivity = !Equal.equals( |
| 391 | settings.backgroundActivity, |
| 392 | DEFAULT_UNIFIED_SETTINGS.backgroundActivity, |
| 393 | ); |
| 394 | |
| 395 | return ( |
| 396 | <SettingsRow |
| 397 | serverScoped |
| 398 | settingKeys={["backgroundActivity"]} |
| 399 | id={searchableSetting("background-activity").id} |
| 400 | title={ |
| 401 | <span className="inline-flex items-center gap-1.5"> |
| 402 | {searchableSetting("background-activity").title} |
| 403 | <PolicyTooltip> |
| 404 | {gt( |
| 405 | "This shared policy gates background work such as Git refreshes and provider health probes after their individual intervals elapse.", |
| 406 | )} |
| 407 | </PolicyTooltip> |
| 408 | </span> |
| 409 | } |
| 410 | description={backgroundActivityDescription} |
| 411 | resetAction={ |
| 412 | canResetBackgroundActivity ? ( |
| 413 | <SettingResetButton |
| 414 | ariaLabel={gt("Reset background activity to default")} |
| 415 | onClick={() => updateSettings(resetBackgroundActivitySettings())} |
| 416 | /> |
| 417 | ) : null |
| 418 | } |
| 419 | control={ |
| 420 | <> |
| 421 | <Select |
| 422 | value={mixedBackgroundActivity ? null : backgroundActivityProfileOption} |
| 423 | onValueChange={(value) => { |
| 424 | if (value === "advanced") { |
| 425 | if (isEnvironmentScope) setBackgroundActivityDialogOpen(true); |
| 426 | return; |
| 427 | } |
| 428 | if (value === "balanced" || value === "performance" || value === "battery-saver") { |
| 429 | updateSettings(backgroundActivityProfileSettings(value)); |
| 430 | } |
| 431 | }} |
| 432 | > |
| 433 | <SelectTrigger |
| 434 | size="sm" |
| 435 | className="w-full sm:w-40" |
| 436 | aria-label={gt("Background activity profile")} |
| 437 | > |
| 438 | <SelectValue> |
| 439 | {(value: BackgroundActivityProfileOption | null) => |
| 440 | value === null |
| 441 | ? gt("Mixed", { $context: "values differ across targets" }) |
| 442 | : m(BACKGROUND_ACTIVITY_PROFILE_OPTION_LABELS[value]) |
| 443 | } |
| 444 | </SelectValue> |
| 445 | </SelectTrigger> |
| 446 | <SelectPopup align="end" alignItemWithTrigger={false}> |
| 447 | <SelectItem hideIndicator value="balanced"> |
| 448 | {m(BACKGROUND_ACTIVITY_PROFILE_LABELS.balanced)} |
| 449 | </SelectItem> |
| 450 | <SelectItem hideIndicator value="performance"> |
| 451 | {m(BACKGROUND_ACTIVITY_PROFILE_LABELS.performance)} |
| 452 | </SelectItem> |
| 453 | <SelectItem hideIndicator value="battery-saver"> |
| 454 | {m(BACKGROUND_ACTIVITY_PROFILE_LABELS["battery-saver"])} |
| 455 | </SelectItem> |
| 456 | <SelectItem hideIndicator value="advanced" disabled={!isEnvironmentScope}> |
| 457 | {isEnvironmentScope |
| 458 | ? m(BACKGROUND_ACTIVITY_PROFILE_OPTION_LABELS.advanced) |
| 459 | : gt("Advanced (one environment)")} |
| 460 | </SelectItem> |
| 461 | </SelectPopup> |
| 462 | </Select> |
| 463 | {backgroundActivityProfileOption === "advanced" && isEnvironmentScope ? ( |
| 464 | <Tooltip> |
| 465 | <TooltipTrigger |
| 466 | render={ |
| 467 | <Button |
| 468 | size="icon-sm" |
| 469 | variant="outline" |
| 470 | aria-label={gt("Configure advanced background activity")} |
| 471 | onClick={() => setBackgroundActivityDialogOpen(true)} |
| 472 | > |
| 473 | <SettingsIcon className="size-4" /> |
| 474 | </Button> |
| 475 | } |
| 476 | /> |
| 477 | <TooltipPopup side="top"> |
| 478 | <T>Configure background activity</T> |
| 479 | </TooltipPopup> |
| 480 | </Tooltip> |
| 481 | ) : null} |
| 482 | <BackgroundActivityAdvancedDialog |
| 483 | open={backgroundActivityDialogOpen && isEnvironmentScope} |
| 484 | onOpenChange={setBackgroundActivityDialogOpen} |
| 485 | /> |
| 486 | </> |
| 487 | } |
| 488 | /> |
| 489 | ); |
| 490 | } |