go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/build/components/build_table/context.ts (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { Dispatch, SetStateAction, createContext, useContext } from 'react';
    16  
    17  import { OutputBuild } from '@/build/types';
    18  
    19  const DefaultExpandedContext = createContext<boolean | null>(null);
    20  
    21  export const DefaultExpandedProvider = DefaultExpandedContext.Provider;
    22  
    23  export function useDefaultExpanded() {
    24    const ctx = useContext(DefaultExpandedContext);
    25  
    26    if (ctx === null) {
    27      throw new Error('useDefaultExpanded must be used within BuildTable');
    28    }
    29  
    30    return ctx;
    31  }
    32  
    33  const SetDefaultExpandedContext = createContext<Dispatch<
    34    SetStateAction<boolean>
    35  > | null>(null);
    36  
    37  export const SetDefaultExpandedProvider = SetDefaultExpandedContext.Provider;
    38  
    39  export function useSetDefaultExpanded() {
    40    const ctx = useContext(SetDefaultExpandedContext);
    41  
    42    if (!ctx) {
    43      throw new Error('useSetDefaultExpanded must be used within BuildTable');
    44    }
    45  
    46    return ctx;
    47  }
    48  
    49  const BuildContext = createContext<OutputBuild | null>(null);
    50  
    51  export const BuildProvider = BuildContext.Provider;
    52  
    53  export function useBuild() {
    54    const ctx = useContext(BuildContext);
    55  
    56    if (!ctx) {
    57      throw new Error('useBuild must be used within BuildTableRow');
    58    }
    59  
    60    return ctx;
    61  }
    62  
    63  const RowExpandedContext = createContext<boolean | null>(null);
    64  
    65  export const RowExpandedProvider = RowExpandedContext.Provider;
    66  
    67  export function useRowExpanded() {
    68    const ctx = useContext(RowExpandedContext);
    69  
    70    if (!ctx) {
    71      throw new Error('useRowExpandedState must be used within BuildTableRow');
    72    }
    73  
    74    return ctx;
    75  }
    76  
    77  const SetRowExpandedContext = createContext<Dispatch<
    78    SetStateAction<boolean>
    79  > | null>(null);
    80  
    81  export const SetRowExpandedProvider = SetRowExpandedContext.Provider;
    82  
    83  export function useSetRowExpanded() {
    84    const ctx = useContext(SetRowExpandedContext);
    85  
    86    if (!ctx) {
    87      throw new Error('useSetRowExpanded must be used within BuildTableRow');
    88    }
    89  
    90    return ctx;
    91  }