github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/dashboard/frontend/src/contexts/loading.tsx (about)

     1  import React, { FC, createContext, useMemo, useState } from 'react'
     2  
     3  export interface ILoadingContext {
     4    loading: boolean,
     5    setLoading: {
     6      (val: boolean): void,
     7    },
     8  }
     9  
    10  export const LoadingContext = createContext<ILoadingContext>({
    11    loading: false,
    12    setLoading: () => {},
    13  })
    14  
    15  export const useLoadingContext = (): ILoadingContext => {
    16    const [ loading, setLoading ] = useState(false)
    17    const contextValue = useMemo<ILoadingContext>(() => ({
    18      loading,
    19      setLoading,
    20    }), [
    21      loading,
    22      setLoading,
    23    ])
    24    return contextValue
    25  }
    26  
    27  export const LoadingContextProvider: FC = ({ children }) => {
    28    const value = useLoadingContext()
    29    return (
    30      <LoadingContext.Provider value={ value }>
    31        { children }
    32      </LoadingContext.Provider>
    33    )
    34  }