@@ -277,7 +277,6 @@ const validateOptions = (options: Partial<Options>): Partial<Options> => {
277277 if ( errors . length > 0 ) {
278278 // eslint-disable-next-line no-console
279279 console . warn ( `[React Scan] Invalid options:\n${ errors . join ( '\n' ) } ` ) ;
280- return { } ;
281280 }
282281
283282 return validOptions ;
@@ -295,49 +294,47 @@ export const getReport = (type?: React.ComponentType<any>) => {
295294 return Store . legacyReportData ;
296295} ;
297296
298- const initializeScanOptions = ( userOptions ?: Partial < Options > ) => {
299- const options = ReactScanInternals . options . value ;
300-
301- const localStorageOptions = readLocalStorage < LocalStorageOptions > ( 'react-scan-options' ) ;
302- if ( localStorageOptions ) {
303- ( Object . keys ( localStorageOptions ) as Array < keyof LocalStorageOptions > ) . forEach ( key => {
304- const value = localStorageOptions [ key ] ;
305- if ( key in options && value !== null ) {
306- ( options as any ) [ key ] = value ;
307- }
308- } ) ;
309- }
310-
311- ReactScanInternals . options . value = validateOptions ( {
312- ...options ,
313- ...userOptions ,
314- } ) ;
315-
316- saveLocalStorage ( 'react-scan-options' , ReactScanInternals . options . value ) ;
317-
318- return ReactScanInternals . options . value ;
319- } ;
320-
321297export const setOptions = ( userOptions : Partial < Options > ) => {
298+ // Validate user options first
322299 const validOptions = validateOptions ( userOptions ) ;
323300
301+ // Skip if no valid options
324302 if ( Object . keys ( validOptions ) . length === 0 ) {
325303 return ;
326304 }
327305
306+ // Special handling for sound + enabled state
307+ if ( 'playSound' in validOptions && validOptions . playSound ) {
308+ validOptions . enabled = true ;
309+ }
310+
311+ // Update options with validated values
312+ const newOptions = {
313+ ...ReactScanInternals . options . value ,
314+ ...validOptions
315+ } ;
316+
317+ // Update instrumentation state if needed
328318 const { instrumentation } = ReactScanInternals ;
329- if ( instrumentation ) {
319+ if ( instrumentation && 'enabled' in validOptions ) {
330320 instrumentation . isPaused . value = validOptions . enabled === false ;
331321 }
332322
333- const newOptions = initializeScanOptions ( validOptions ) ;
323+ // Update options
324+ ReactScanInternals . options . value = newOptions ;
334325
335- if ( toolbarContainer && ! newOptions . showToolbar ) {
336- toolbarContainer . remove ( ) ;
337- }
326+ // Save to localStorage
327+ saveLocalStorage ( 'react-scan-options' , newOptions ) ;
328+
329+ // Handle toolbar visibility only if showToolbar changed
330+ if ( 'showToolbar' in validOptions ) {
331+ if ( toolbarContainer && ! newOptions . showToolbar ) {
332+ toolbarContainer . remove ( ) ;
333+ }
338334
339- if ( newOptions . showToolbar && toolbarContainer && shadowRoot ) {
340- toolbarContainer = createToolbar ( shadowRoot ) ;
335+ if ( newOptions . showToolbar && toolbarContainer && shadowRoot ) {
336+ toolbarContainer = createToolbar ( shadowRoot ) ;
337+ }
341338 }
342339} ;
343340
@@ -421,6 +418,18 @@ const startFlushOutlineInterval = (ctx: CanvasRenderingContext2D) => {
421418export const start = ( ) => {
422419 if ( typeof window === 'undefined' ) return ;
423420
421+ // Load options from localStorage first
422+ const localStorageOptions = readLocalStorage < LocalStorageOptions > ( 'react-scan-options' ) ;
423+ if ( localStorageOptions ) {
424+ const validLocalOptions = validateOptions ( localStorageOptions ) ;
425+ if ( Object . keys ( validLocalOptions ) . length > 0 ) {
426+ ReactScanInternals . options . value = {
427+ ...ReactScanInternals . options . value ,
428+ ...validLocalOptions
429+ } ;
430+ }
431+ }
432+
424433 const audioContext =
425434 typeof window !== 'undefined'
426435 ? new (
0 commit comments