1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and its affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import type {ConsolePatchSettings} from 'react-devtools-shared/src/backend/types';
    
  11. import {writeConsolePatchSettingsToWindow} from 'react-devtools-shared/src/backend/console';
    
  12. import {castBool, castBrowserTheme} from 'react-devtools-shared/src/utils';
    
  13. 
    
  14. // Note: all keys should be optional in this type, because users can use newer
    
  15. // versions of React DevTools with older versions of React Native, and the object
    
  16. // provided by React Native may not include all of this type's fields.
    
  17. export type DevToolsSettingsManager = {
    
  18.   getConsolePatchSettings: ?() => string,
    
  19.   setConsolePatchSettings: ?(key: string) => void,
    
  20. };
    
  21. 
    
  22. export function initializeUsingCachedSettings(
    
  23.   devToolsSettingsManager: DevToolsSettingsManager,
    
  24. ) {
    
  25.   initializeConsolePatchSettings(devToolsSettingsManager);
    
  26. }
    
  27. 
    
  28. function initializeConsolePatchSettings(
    
  29.   devToolsSettingsManager: DevToolsSettingsManager,
    
  30. ) {
    
  31.   if (devToolsSettingsManager.getConsolePatchSettings == null) {
    
  32.     return;
    
  33.   }
    
  34.   const consolePatchSettingsString =
    
  35.     devToolsSettingsManager.getConsolePatchSettings();
    
  36.   if (consolePatchSettingsString == null) {
    
  37.     return;
    
  38.   }
    
  39.   const parsedConsolePatchSettings = parseConsolePatchSettings(
    
  40.     consolePatchSettingsString,
    
  41.   );
    
  42.   if (parsedConsolePatchSettings == null) {
    
  43.     return;
    
  44.   }
    
  45.   writeConsolePatchSettingsToWindow(parsedConsolePatchSettings);
    
  46. }
    
  47. 
    
  48. function parseConsolePatchSettings(
    
  49.   consolePatchSettingsString: string,
    
  50. ): ?ConsolePatchSettings {
    
  51.   const parsedValue = JSON.parse(consolePatchSettingsString ?? '{}');
    
  52.   const {
    
  53.     appendComponentStack,
    
  54.     breakOnConsoleErrors,
    
  55.     showInlineWarningsAndErrors,
    
  56.     hideConsoleLogsInStrictMode,
    
  57.     browserTheme,
    
  58.   } = parsedValue;
    
  59.   return {
    
  60.     appendComponentStack: castBool(appendComponentStack) ?? true,
    
  61.     breakOnConsoleErrors: castBool(breakOnConsoleErrors) ?? false,
    
  62.     showInlineWarningsAndErrors: castBool(showInlineWarningsAndErrors) ?? true,
    
  63.     hideConsoleLogsInStrictMode: castBool(hideConsoleLogsInStrictMode) ?? false,
    
  64.     browserTheme: castBrowserTheme(browserTheme) ?? 'dark',
    
  65.   };
    
  66. }
    
  67. 
    
  68. export function cacheConsolePatchSettings(
    
  69.   devToolsSettingsManager: DevToolsSettingsManager,
    
  70.   value: ConsolePatchSettings,
    
  71. ): void {
    
  72.   if (devToolsSettingsManager.setConsolePatchSettings == null) {
    
  73.     return;
    
  74.   }
    
  75.   devToolsSettingsManager.setConsolePatchSettings(JSON.stringify(value));
    
  76. }