{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/hooks/index.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport { useEffect, useRef } from 'react';\n\nexport {\n  useDebounceValue,\n  useWindowSize,\n  useOnClickOutside,\n  useInterval,\n  useMediaQuery,\n  useCopyToClipboard,\n  useScrollLock,\n  useEventListener,\n  useResizeObserver,\n} from 'usehooks-ts';\nimport { useDebounceCallback } from './use-debounce-callback';\nexport { useDebounceCallback };\n\n/**\n * Below are hooks that cannot be found in `usehooks-ts`\n * but are used in some projects.\n * 1. usePrevious\n * 2. useEffectDebugger\n * 3. useRenderCount\n */\n\n/**\n * The `usePrevious` hook tracks a variable's previous value in a functional component,\n * useful for comparing current and past values to trigger actions or render changes.\n */\nexport function usePrevious<T>(value: T): T | null {\n  // The ref object is a generic container whose current property is mutable\n  // and can hold any value, similar to an instance property on a class\n\n  const ref = useRef<T>(null);\n  // Store current value in ref\n  useEffect(() => {\n    ref.current = value;\n  }, [value]); // Only re-run if value changes\n  // Return previous value (happens before update in useEffect above)\n  return ref.current;\n}\n\n/**\n * The `useEffectDebugger` hook is a wrapper around the `useEffect` hook\n * that logs the dependencies of the effect when they change.\n *\n * Uses the `usePrevious` hook.\n *\n * Source: https://stackoverflow.com/a/59843241/3247715\n */\nexport function useEffectDebugger(\n  effectHook: React.EffectCallback,\n  dependencies: unknown[],\n  dependencyNames: string[] = [],\n) {\n  const previousDeps = usePrevious(dependencies);\n\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  const changedDeps: any = dependencies.reduce((accum, dependency, index) => {\n    if (previousDeps === undefined) {\n      return accum;\n    }\n    if (previousDeps === null) {\n      return accum;\n    }\n    if (dependency !== previousDeps[index]) {\n      const keyName = dependencyNames[index] || index;\n      return {\n        // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n        ...(accum as any),\n        [keyName]: {\n          after: dependency,\n          before: previousDeps[index],\n        },\n      };\n    }\n\n    return accum;\n  }, {});\n\n  if (Object.keys(changedDeps).length) {\n    console.info('[use-effect-debugger] ', changedDeps);\n  }\n\n  useEffect(() => {\n    return effectHook();\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, dependencies);\n}\n\n/**\n * The `useRenderCount` hook returns the number of times a component has rendered.\n */\nexport function useRenderCount() {\n  const count = useRef(0);\n\n  count.current++;\n\n  return count.current;\n}\n\n/**\n * The `useRenderDebugger` outputs to console what props or states are causing a render (or re-render) of a component\n * if all relevant props and states variables are passed to the hook.\n * For debugging purposes only.\n *\n * Howto use:\n * Place the hook at the top of the component you want to debug and call it like this:\n * useRenderDebugger('MyComponent', props);\n *\n * if there are state variables include them as well:\n *\n * `useRenderDebugger('MyComponent', { ...props, stateVar1, stateVar2 });`\n *\n * @param componentName - Name of the component to debug\n * @param propsAndStates - An object containing all props and states variables of the component\n */\nexport const useRenderDebugger = (\n  componentName: string,\n  propsAndStates: Record<string, unknown>,\n) => {\n  const prev = useRef(propsAndStates);\n\n  useEffect(() => {\n    const changedPropsOrState: Record<string, { old: unknown; new: unknown }> =\n      {};\n\n    Object.entries(propsAndStates).forEach(\n      ([key, newValue]: [string, unknown]) => {\n        if (prev.current[key] !== newValue) {\n          changedPropsOrState[key] = {\n            new: newValue,\n            old: prev.current[key],\n          };\n        }\n      },\n      {},\n    );\n\n    if (Object.keys(changedPropsOrState).length > 0) {\n      console.debug(\n        `[${componentName}] Re-rendered due to:`,\n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