| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and 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 * as React from 'react'; |
| 11 | import {useCallback, useContext, useMemo, useRef} from 'react'; |
| 12 | import {useSubscription} from '../hooks'; |
| 13 | import {StoreContext} from '../context'; |
| 14 | import {ProfilerContext} from 'react-devtools-shared/src/devtools/views/Profiler/ProfilerContext'; |
| 15 | |
| 16 | import styles from './SettingsShared.css'; |
| 17 | import typeof {SyntheticEvent} from 'react-dom-bindings/src/events/SyntheticEvent'; |
| 18 | |
| 19 | export default function ProfilerSettings(_: {}): React.Node { |
| 20 | const { |
| 21 | isCommitFilterEnabled, |
| 22 | minCommitDuration, |
| 23 | setIsCommitFilterEnabled, |
| 24 | setMinCommitDuration, |
| 25 | } = useContext(ProfilerContext); |
| 26 | const store = useContext(StoreContext); |
| 27 | |
| 28 | const recordChangeDescriptionsSubscription = useMemo( |
| 29 | () => ({ |
| 30 | getCurrentValue: () => store.recordChangeDescriptions, |
| 31 | subscribe: (callback: Function) => { |
| 32 | store.addListener('recordChangeDescriptions', callback); |
| 33 | return () => store.removeListener('recordChangeDescriptions', callback); |
| 34 | }, |
| 35 | }), |
| 36 | [store], |
| 37 | ); |
| 38 | const recordChangeDescriptions = useSubscription<boolean>( |
| 39 | recordChangeDescriptionsSubscription, |
| 40 | ); |
| 41 | |
| 42 | const updateRecordChangeDescriptions = useCallback( |
| 43 | ({currentTarget}: $FlowFixMe) => { |
| 44 | store.recordChangeDescriptions = currentTarget.checked; |
| 45 | }, |
| 46 | [store], |
| 47 | ); |
| 48 | const updateMinCommitDuration = useCallback( |
| 49 | (event: SyntheticEvent) => { |
| 50 | const newValue = parseFloat(event.currentTarget.value); |
| 51 | setMinCommitDuration( |
| 52 | Number.isNaN(newValue) || newValue <= 0 ? 0 : newValue, |
| 53 | ); |
| 54 | }, |
| 55 | [setMinCommitDuration], |
| 56 | ); |
| 57 | const updateIsCommitFilterEnabled = useCallback( |
| 58 | (event: SyntheticEvent) => { |
| 59 | const checked = event.currentTarget.checked; |
| 60 | setIsCommitFilterEnabled(checked); |
| 61 | if (checked) { |
| 62 | if (minCommitDurationInputRef.current !== null) { |
| 63 | minCommitDurationInputRef.current.focus(); |
| 64 | } |
| 65 | } |
| 66 | }, |
| 67 | [setIsCommitFilterEnabled], |
| 68 | ); |
| 69 | |
| 70 | const minCommitDurationInputRef = useRef<HTMLInputElement | null>(null); |
| 71 | |
| 72 | return ( |
| 73 | <div className={styles.SettingList}> |
| 74 | <div className={styles.SettingWrapper}> |
| 75 | <label className={styles.SettingRow}> |
| 76 | <input |
| 77 | type="checkbox" |
| 78 | checked={recordChangeDescriptions} |
| 79 | onChange={updateRecordChangeDescriptions} |
| 80 | className={styles.SettingRowCheckbox} |
| 81 | /> |
| 82 | Record why each component rendered while profiling |
| 83 | </label> |
| 84 | </div> |
| 85 | |
| 86 | <div className={styles.SettingWrapper}> |
| 87 | <label className={styles.SettingRow}> |
| 88 | <input |
| 89 | checked={isCommitFilterEnabled} |
| 90 | onChange={updateIsCommitFilterEnabled} |
| 91 | type="checkbox" |
| 92 | className={styles.SettingRowCheckbox} |
| 93 | /> |
| 94 | Hide commits below |
| 95 | <input |
| 96 | className={styles.Input} |
| 97 | onChange={updateMinCommitDuration} |
| 98 | ref={minCommitDurationInputRef} |
| 99 | type="number" |
| 100 | value={minCommitDuration} |
| 101 | /> |
| 102 | (ms) |
| 103 | </label> |
| 104 | </div> |
| 105 | </div> |
| 106 | ); |
| 107 | } |