use-wait-for-grammarly-check.ts 919 B

1234567891011121314151617181920212223242526272829
  1. import { useEffect, useState } from 'react'
  2. /**
  3. *
  4. * @param {number} delay how long to wait before checking for grammarly in ms
  5. * @param {boolean} initialState the initial state we should set grammarlyInstalled to before checking after the delay
  6. * @returns {boolean} a stateful boolean which is initially false, then updates to reflect whether grammarly is installed after the delay to check
  7. */
  8. export default function useWaitForGrammarlyCheck({
  9. delay = 3000,
  10. initialState = false,
  11. }) {
  12. const [grammarlyInstalled, setGrammarlyInstalled] = useState(() => {
  13. return initialState
  14. })
  15. useEffect(() => {
  16. const timer = setTimeout(
  17. () => setGrammarlyInstalled(grammarlyExtensionPresent()),
  18. delay
  19. )
  20. return () => clearTimeout(timer)
  21. }, [delay])
  22. return grammarlyInstalled
  23. }
  24. function grammarlyExtensionPresent() {
  25. return !!document.querySelector('grammarly-desktop-integration')
  26. }