go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/gitiles/components/commit_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 { OutputCommit } from '@/gitiles/types'; 18 19 const RepoContext = createContext<string | null>(null); 20 21 export const RepoUrlProvider = RepoContext.Provider; 22 23 export function useRepoUrl() { 24 const ctx = useContext(RepoContext); 25 26 if (ctx === null) { 27 throw new Error('useRepoUrl must be used within CommitTable'); 28 } 29 30 return ctx; 31 } 32 33 const DefaultExpandedStateContext = createContext< 34 readonly [boolean, Dispatch<SetStateAction<boolean>>] | null 35 >(null); 36 37 export const DefaultExpandedStateProvider = 38 DefaultExpandedStateContext.Provider; 39 40 export function useDefaultExpandedState() { 41 const ctx = useContext(DefaultExpandedStateContext); 42 43 if (ctx === null) { 44 throw new Error('useDefaultExpandedState must be used within CommitTable'); 45 } 46 47 return ctx; 48 } 49 50 const CommitContext = createContext<OutputCommit | null>(null); 51 52 export const CommitProvider = CommitContext.Provider; 53 54 export function useCommit() { 55 const ctx = useContext(CommitContext); 56 57 if (ctx === null) { 58 throw new Error('useCommit must be used within CommitTableRow'); 59 } 60 61 return ctx; 62 }