|
| 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 { |
| 12 | + useContext, |
| 13 | + useMemo, |
| 14 | + useCallback, |
| 15 | + memo, |
| 16 | + useState, |
| 17 | + useEffect, |
| 18 | +} from 'react'; |
| 19 | +import styles from './HookChangeSummary.css'; |
| 20 | +import ButtonIcon from '../ButtonIcon'; |
| 21 | +import {InspectedElementContext} from '../Components/InspectedElementContext'; |
| 22 | +import {StoreContext} from '../context'; |
| 23 | + |
| 24 | +import { |
| 25 | + getAlreadyLoadedHookNames, |
| 26 | + getHookSourceLocationKey, |
| 27 | +} from 'react-devtools-shared/src/hookNamesCache'; |
| 28 | +import Toggle from '../Toggle'; |
| 29 | +import type {HooksNode} from 'react-debug-tools/src/ReactDebugHooks'; |
| 30 | +import type {ChangeDescription} from './types'; |
| 31 | + |
| 32 | +// $FlowFixMe: Flow doesn't know about Intl.ListFormat |
| 33 | +const hookListFormatter = new Intl.ListFormat('en', { |
| 34 | + style: 'long', |
| 35 | + type: 'conjunction', |
| 36 | +}); |
| 37 | + |
| 38 | +type HookProps = { |
| 39 | + hook: HooksNode, |
| 40 | + hookNames: Map<string, string> | null, |
| 41 | +}; |
| 42 | + |
| 43 | +const Hook: React.AbstractComponent<HookProps> = memo(({hook, hookNames}) => { |
| 44 | + const hookSource = hook.hookSource; |
| 45 | + const hookName = useMemo(() => { |
| 46 | + if (!hookSource || !hookNames) return null; |
| 47 | + const key = getHookSourceLocationKey(hookSource); |
| 48 | + return hookNames.get(key) || null; |
| 49 | + }, [hookSource, hookNames]); |
| 50 | + |
| 51 | + return ( |
| 52 | + <ul className={styles.Hook}> |
| 53 | + <li> |
| 54 | + {hook.id !== null && ( |
| 55 | + <span className={styles.PrimitiveHookNumber}> |
| 56 | + {String(hook.id + 1)} |
| 57 | + </span> |
| 58 | + )} |
| 59 | + <span |
| 60 | + className={hook.id !== null ? styles.PrimitiveHookName : styles.Name}> |
| 61 | + {hook.name} |
| 62 | + {hookName && <span className={styles.HookName}>({hookName})</span>} |
| 63 | + </span> |
| 64 | + {hook.subHooks?.map((subHook, index) => ( |
| 65 | + <Hook key={hook.id} hook={subHook} hookNames={hookNames} /> |
| 66 | + ))} |
| 67 | + </li> |
| 68 | + </ul> |
| 69 | + ); |
| 70 | +}); |
| 71 | + |
| 72 | +const shouldKeepHook = ( |
| 73 | + hook: HooksNode, |
| 74 | + hooksArray: Array<number>, |
| 75 | +): boolean => { |
| 76 | + if (hook.id !== null && hooksArray.includes(hook.id)) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + const subHooks = hook.subHooks; |
| 80 | + if (subHooks == null) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + return subHooks.some(subHook => shouldKeepHook(subHook, hooksArray)); |
| 85 | +}; |
| 86 | + |
| 87 | +const filterHooks = ( |
| 88 | + hook: HooksNode, |
| 89 | + hooksArray: Array<number>, |
| 90 | +): HooksNode | null => { |
| 91 | + if (!shouldKeepHook(hook, hooksArray)) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + const subHooks = hook.subHooks; |
| 96 | + if (subHooks == null) { |
| 97 | + return hook; |
| 98 | + } |
| 99 | + |
| 100 | + const filteredSubHooks = subHooks |
| 101 | + .map(subHook => filterHooks(subHook, hooksArray)) |
| 102 | + .filter(Boolean); |
| 103 | + return filteredSubHooks.length > 0 |
| 104 | + ? {...hook, subHooks: filteredSubHooks} |
| 105 | + : hook; |
| 106 | +}; |
| 107 | + |
| 108 | +type Props = {| |
| 109 | + fiberID: number, |
| 110 | + hooks: $PropertyType<ChangeDescription, 'hooks'>, |
| 111 | + state: $PropertyType<ChangeDescription, 'state'>, |
| 112 | + displayMode?: 'detailed' | 'compact', |
| 113 | +|}; |
| 114 | + |
| 115 | +const HookChangeSummary: React.AbstractComponent<Props> = memo( |
| 116 | + ({hooks, fiberID, state, displayMode = 'detailed'}: Props) => { |
| 117 | + const {parseHookNames, toggleParseHookNames, inspectedElement} = useContext( |
| 118 | + InspectedElementContext, |
| 119 | + ); |
| 120 | + const store = useContext(StoreContext); |
| 121 | + |
| 122 | + const [parseHookNamesOptimistic, setParseHookNamesOptimistic] = |
| 123 | + useState<boolean>(parseHookNames); |
| 124 | + |
| 125 | + useEffect(() => { |
| 126 | + setParseHookNamesOptimistic(parseHookNames); |
| 127 | + }, [inspectedElement?.id, parseHookNames]); |
| 128 | + |
| 129 | + const handleOnChange = useCallback(() => { |
| 130 | + setParseHookNamesOptimistic(!parseHookNames); |
| 131 | + toggleParseHookNames(); |
| 132 | + }, [toggleParseHookNames, parseHookNames]); |
| 133 | + |
| 134 | + const element = fiberID !== null ? store.getElementByID(fiberID) : null; |
| 135 | + const hookNames = |
| 136 | + element != null ? getAlreadyLoadedHookNames(element) : null; |
| 137 | + |
| 138 | + const filteredHooks = useMemo(() => { |
| 139 | + if (!hooks || !inspectedElement?.hooks) return null; |
| 140 | + return inspectedElement.hooks |
| 141 | + .map(hook => filterHooks(hook, hooks)) |
| 142 | + .filter(Boolean); |
| 143 | + }, [inspectedElement?.hooks, hooks]); |
| 144 | + |
| 145 | + const hookParsingFailed = parseHookNames && hookNames === null; |
| 146 | + |
| 147 | + if (!hooks?.length) { |
| 148 | + return <span>No hooks changed</span>; |
| 149 | + } |
| 150 | + |
| 151 | + if ( |
| 152 | + inspectedElement?.id !== element?.id || |
| 153 | + filteredHooks?.length !== hooks.length || |
| 154 | + displayMode === 'compact' |
| 155 | + ) { |
| 156 | + const hookIds = hooks.map(hookId => String(hookId + 1)); |
| 157 | + const hookWord = hookIds.length === 1 ? '• Hook' : '• Hooks'; |
| 158 | + return ( |
| 159 | + <span> |
| 160 | + {hookWord} {hookListFormatter.format(hookIds)} changed |
| 161 | + </span> |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + let toggleTitle: string; |
| 166 | + if (hookParsingFailed) { |
| 167 | + toggleTitle = 'Hook parsing failed'; |
| 168 | + } else if (parseHookNamesOptimistic) { |
| 169 | + toggleTitle = 'Parsing hook names ...'; |
| 170 | + } else { |
| 171 | + toggleTitle = 'Parse hook names (may be slow)'; |
| 172 | + } |
| 173 | + |
| 174 | + if (filteredHooks == null) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + return ( |
| 179 | + <div> |
| 180 | + {filteredHooks.length > 1 ? '• Hooks changed:' : '• Hook changed:'} |
| 181 | + {(!parseHookNames || hookParsingFailed) && ( |
| 182 | + <Toggle |
| 183 | + className={ |
| 184 | + hookParsingFailed |
| 185 | + ? styles.ToggleError |
| 186 | + : styles.LoadHookNamesToggle |
| 187 | + } |
| 188 | + isChecked={parseHookNamesOptimistic} |
| 189 | + isDisabled={parseHookNamesOptimistic || hookParsingFailed} |
| 190 | + onChange={handleOnChange} |
| 191 | + title={toggleTitle}> |
| 192 | + <ButtonIcon type="parse-hook-names" /> |
| 193 | + </Toggle> |
| 194 | + )} |
| 195 | + {filteredHooks.map(hook => ( |
| 196 | + <Hook |
| 197 | + key={`${inspectedElement?.id ?? 'unknown'}-${hook.id}`} |
| 198 | + hook={hook} |
| 199 | + hookNames={hookNames} |
| 200 | + /> |
| 201 | + ))} |
| 202 | + </div> |
| 203 | + ); |
| 204 | + }, |
| 205 | +); |
| 206 | + |
| 207 | +export default HookChangeSummary; |
0 commit comments