github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/webui/src/lib/hooks/repo.jsx (about)

     1  import React, {useContext, useState, createContext, useEffect} from "react";
     2  
     3  import {repositories, branches, commits, NotFoundError, tags, BadRequestError} from "../api";
     4  import {useRouter} from "./router";
     5  import {RefTypeBranch, RefTypeCommit, RefTypeTag} from "../../constants";
     6  
     7  
     8  export const resolveRef = async (repoId, refId) => {
     9      // try branch
    10      try {
    11          const branch = await branches.get(repoId, refId);
    12          return {id: branch.id, type: RefTypeBranch};
    13      } catch(error) {
    14          if (!(error instanceof NotFoundError) && !(error instanceof BadRequestError)) {
    15              throw error;
    16          }
    17      }
    18      // try tag
    19      try {
    20          const tag = await tags.get(repoId, refId);
    21          return {id: tag.id, type: RefTypeTag};
    22      } catch(error) {
    23          if (!(error instanceof NotFoundError) && !(error instanceof BadRequestError)) {
    24              throw error;
    25          }
    26      }
    27      // try commit
    28      try {
    29          const commit = await commits.get(repoId, refId);
    30          return {id: commit.id,  type: RefTypeCommit};
    31      } catch(error) {
    32          if (!(error instanceof NotFoundError)) {
    33              throw error;
    34          }
    35      }
    36  
    37      throw new NotFoundError('ref not found');
    38  };
    39  
    40  
    41  const RefContext =  createContext(null);
    42  
    43  export const useRefs = () => {
    44      const [ refs ] = useContext(RefContext);
    45      return refs;
    46  }
    47  
    48  const refContextInitialState = {
    49      loading: true,
    50      error: null,
    51      repo: null,
    52      reference: null,
    53      compare: null
    54  };
    55  
    56  export const RefContextProvider = ({ children }) => {
    57      const router = useRouter();
    58      const { repoId } = router.params;
    59      const {ref, compare} = router.query;
    60  
    61      const [refState, setRefState] = useState(refContextInitialState);
    62  
    63      useEffect(() => {
    64          const fetch = async () => {
    65              setRefState(refContextInitialState);
    66              if (!repoId) return;
    67              try {
    68                  const repo = await repositories.get(repoId);
    69                  const reference = await resolveRef(repoId, (ref) ? ref : repo.default_branch);
    70                  const comparedRef = await resolveRef(repoId, (compare)? compare : repo.default_branch);
    71                  setRefState({...refContextInitialState, loading: false, repo, reference, compare: comparedRef});
    72              } catch (err) {
    73                  setRefState({...refContextInitialState, loading: false, error: err});
    74              }
    75          };
    76          fetch();
    77      }, [repoId, ref, compare]);
    78  
    79      return (
    80          <RefContext.Provider value={[refState, fetch]}>
    81              {children}
    82          </RefContext.Provider>
    83      );
    84  };