# All files / src/_hooks InjectedScript.tsx

94.44% Statements 34/36
62.5% Branches 5/8
87.5% Functions 7/8
92.3% Lines 24/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    1002x

    9x
    9x

    9x
    7x

    7x

    7x
    7x
    7x
    7x
    7x

    7x

    7x

    9x

    2x
    2x

    2x

    2x
    1x

    2x

    2x
    2x

    2x
    3x

     import React from 'react'

    /* TYPES */

    interface InjectedScriptConfig {
      id: string
      src: string
    }

    interface InjectedScriptApi {
      isLoaded: boolean
      setLoaded: (isLoaded: boolean) => void
    }

    interface Props extends InjectedScriptConfig {
      children?: React.ReactNode
    }

    interface RenderProps extends InjectedScriptConfig {
      children?: ((props: InjectedScriptApi) => React.ReactNode) | null
    }

    /* HOOK */

    /**
     * @deprecatedSince 11
     * @deprecated Not a concern of the Design System. See source to duplicate https://github.com/procore/core/blob/%40procore/core-react%4010.30.0/packages/core-react/src/state/InjectedScript.tsx#L25
     */
    export function useInjectedScript({ id, src }: InjectedScriptConfig) {
      const [isLoaded, setLoaded] = React.useState(false)

      React.useEffect(() => {
        const existingScript = document.getElementById(id)

        Iif (existingScript) {
          setLoaded(true)
        } else {
          const script = document.createElement('script')
          script.type = 'text/javascript'
          script.async = true
          script.id = id
          script.onload = () => {
            setLoaded(true)
          }
          script.src = src

          document.head && document.head.appendChild(script)
        }
      }, [id, src])

      return { isLoaded, setLoaded }
    }

    /* RENDER PROP */
    /**
     * @deprecatedSince 11
     * @deprecated Not a concern of the Design System. See source to duplicate https://github.com/procore/core/blob/%40procore/core-react%4010.30.0/packages/core-react/src/state/InjectedScript.tsx#L25
     */
    export function InjectedScript({ children = null, ...props }: RenderProps) {
      const injectedScript = useInjectedScript(props)

      return typeof children === 'function' ? children(injectedScript) : children
    }

    /* CONTEXT */

    const noop = (method: String) => () =>
      console.warn(
        `Could not call \`${method}\`, please wrap your component in a \`<InjectedScriptProvider>\``
      )

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const InjectedScriptContext = React.createContext<InjectedScriptApi>({
      isLoaded: false,
      setLoaded: noop('setLoaded'),
    })

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function InjectedScriptProvider({ children, ...props }: Props) {
      return (
        <InjectedScriptContext.Provider value={useInjectedScript(props)}>
          {children}
        </InjectedScriptContext.Provider>
      )
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useInjectedScriptContext() {
      return React.useContext(InjectedScriptContext)
    }