Skip to content

Commit 400483b

Browse files
Fix errores
1 parent 950e3a8 commit 400483b

2 files changed

Lines changed: 52 additions & 34 deletions

File tree

src/pages/Settings/useServices/index.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {invoke} from '@tauri-apps/api/core'
2-
import {useEffect, useState} from 'react'
2+
import {useEffect, useRef, useState} from 'react'
33
import {toast} from 'sonner'
44
import {AppSettings} from '../useSettings'
55

@@ -32,6 +32,7 @@ interface ServicesResponse {
3232
export function useServices(settings: AppSettings) {
3333
const [loaded, setLoaded] = useState(false)
3434
const [servicesList, setServicesList] = useState<ServiceData[]>([])
35+
const didAutoStart = useRef(false)
3536

3637
useEffect(() => {
3738
;(async () => {
@@ -75,31 +76,31 @@ export function useServices(settings: AppSettings) {
7576

7677
setServicesList(servicesList)
7778

78-
// Ensure services are running according to their 'on' state on startup
79-
const shouldStartServices = settings.startServicesOnLaunch !== false
80-
const allServices = [...servicesList]
81-
if (allServices.length > 0) {
82-
try {
83-
// Convert to Rust format for the command
84-
const rustServices = allServices.map(service => ({
85-
name: service.name,
86-
path: service.path,
87-
port: service.port,
88-
full_name: service.fullName,
89-
on: shouldStartServices ? service.on : false,
90-
category: service.category,
91-
config: service.config,
92-
start_command: service.startCommand,
93-
}))
79+
// Ensure services are running only on initial startup, not on every settings change
80+
if (!didAutoStart.current && servicesList.length > 0) {
81+
didAutoStart.current = true
82+
const shouldStartServices = settings.startServicesOnLaunch !== false
83+
if (shouldStartServices) {
84+
try {
85+
const rustServices = servicesList.map(service => ({
86+
name: service.name,
87+
path: service.path,
88+
port: service.port,
89+
full_name: service.fullName,
90+
on: service.on,
91+
category: service.category,
92+
config: service.config,
93+
start_command: service.startCommand,
94+
}))
9495

95-
const actions = await invoke<string[]>('ensure_services_running', {
96-
services: rustServices,
97-
})
96+
const actions = await invoke<string[]>('ensure_services_running', {
97+
services: rustServices,
98+
})
9899

99-
console.log('Startup service management actions:', actions)
100-
} catch (error) {
101-
console.error('Failed to manage services on startup:', error)
102-
// Don't show error toast on startup to avoid overwhelming the user
100+
console.log('Startup service management actions:', actions)
101+
} catch (error) {
102+
console.error('Failed to manage services on startup:', error)
103+
}
103104
}
104105
}
105106
})()

src/pages/Settings/useSettings.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import generateId from '@/lib/generateId'
22
import {invoke} from '@tauri-apps/api/core'
33
import {appConfigDir} from '@tauri-apps/api/path'
44
import {BaseDirectory, exists, mkdir, readTextFile, writeTextFile} from '@tauri-apps/plugin-fs'
5-
import {useEffect, useState} from 'react'
5+
import {useEffect, useRef, useState} from 'react'
66
import {toast} from 'sonner'
77
import {useProcesses} from './useProcesses'
88
import {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

Comments
 (0)