@@ -2,7 +2,7 @@ import generateId from '@/lib/generateId'
22import { invoke } from '@tauri-apps/api/core'
33import { appConfigDir } from '@tauri-apps/api/path'
44import { BaseDirectory , exists , mkdir , readTextFile , writeTextFile } from '@tauri-apps/plugin-fs'
5- import { useEffect , useState } from 'react'
5+ import { useEffect , useRef , useState } from 'react'
66import { toast } from 'sonner'
77import { useProcesses } from './useProcesses'
88import { useServiceMetrics } from './useServiceMetrics'
@@ -49,6 +49,8 @@ function normalizeSettings(raw: Partial<AppSettings> & Record<string, any>): App
4949 servicesPath : raw . servicesPath || '' ,
5050 onServices,
5151 favoriteServices : raw . favoriteServices || { } ,
52+ startServicesOnLaunch : raw . startServicesOnLaunch ,
53+ serviceGroups : raw . serviceGroups || [ ] ,
5254 }
5355}
5456
@@ -62,6 +64,14 @@ export function useCreateSettingsContext() {
6264 favoriteServices : { } ,
6365 } )
6466
67+ // Ref to track the latest onServices synchronously (avoids stale closures in setServiceOn)
68+ const onServicesRef = useRef ( settings . onServices )
69+
70+ // Keep ref in sync when settings load or change externally
71+ useEffect ( ( ) => {
72+ onServicesRef . current = settings . onServices
73+ } , [ settings . onServices ] )
74+
6575 const services = useServices ( settings )
6676
6777 useEffect ( ( ) => {
@@ -86,6 +96,14 @@ export function useCreateSettingsContext() {
8696 baseDir : BaseDirectory . AppConfig ,
8797 } )
8898 const parsedSettings = normalizeSettings ( JSON . parse ( settingsData ) )
99+
100+ // If auto-start is disabled, reset all services to off in memory (disk keeps original values)
101+ if ( parsedSettings . startServicesOnLaunch === false ) {
102+ for ( const key of Object . keys ( parsedSettings . onServices ) ) {
103+ parsedSettings . onServices [ key ] = false
104+ }
105+ }
106+
89107 setSettings ( parsedSettings )
90108 } else {
91109 // Create the settings file with default values if it doesn't exist
@@ -132,6 +150,9 @@ export function useCreateSettingsContext() {
132150 }
133151
134152 const setServiceOn = async ( category : string , service : string , on : boolean ) => {
153+ // Update ref synchronously so rapid sequential calls see the latest state
154+ onServicesRef . current = { ...onServicesRef . current , [ `${ category } .${ service } ` ] : on }
155+
135156 setSettings ( prev => {
136157 const newSettings = {
137158 ...prev ,
@@ -146,15 +167,11 @@ export function useCreateSettingsContext() {
146167
147168 // Trigger service management to start/stop services as needed
148169 try {
149- const allServices = [ ...services . servicesList ]
150-
151- // Update the specific service's 'on' state in the list
152- const updatedServices = allServices . map ( s => {
153- if ( s . fullName === `${ category } .${ service } ` ) {
154- return { ...s , on}
155- }
156- return s
157- } )
170+ // Use the ref (not stale servicesList) to get the latest on/off state
171+ const updatedServices = services . servicesList . map ( s => ( {
172+ ...s ,
173+ on : onServicesRef . current [ s . fullName ] ?? false ,
174+ } ) )
158175
159176 // Convert to Rust format
160177 const rustServices = updatedServices . map ( service => ( {
0 commit comments