import { API_BASE_PATH } from '../../../../common/constants' ; import { Repository , EmptyRepository } from '../../../../common/types' ; import { UIM_REPOSITORY_CREATE , UIM_REPOSITORY_UPDATE , UIM_REPOSITORY_DELETE , UIM_REPOSITORY_DELETE_MANY , UIM_REPOSITORY_DETAIL_PANEL_VERIFY , UIM_REPOSITORY_DETAIL_PANEL_CLEANUP , } from '../../constants' ; import { UiMetricService } from '../ui_metric' ; import { sendRequest , useRequest } from './use_request' ; let uiMetricService ; export const setUiMetricServiceRepository = ( _uiMetricService ) => { uiMetricService = _uiMetricService ; } ; export const useLoadRepositories = ( ) => { return useRequest ( { path : `${ API_BASE_PATH }repositories` , method : 'get' , initialData : [ ] , } ) ; } ; export const useLoadRepository = ( name ) => { return useRequest ( { path : `${ API_BASE_PATH }repositories/${ encodeURIComponent ( name ) }` , method : 'get' , } ) ; } ; export const verifyRepository = async ( name ) => { const result = await sendRequest ( { path : `${ API_BASE_PATH }repositories/${ encodeURIComponent ( name ) }/verify` , method : 'get' , } ) ; uiMetricService . trackUiMetric ( UIM_REPOSITORY_DETAIL_PANEL_VERIFY ) ; return result ; } ; export const cleanupRepository = async ( name ) => { const result = await sendRequest ( { path : `${ API_BASE_PATH }repositories/${ encodeURIComponent ( name ) }/cleanup` , method : 'post' , body : undefined , } ) ; uiMetricService . trackUiMetric ( UIM_REPOSITORY_DETAIL_PANEL_CLEANUP ) ; return result ; } ; export const useLoadRepositoryTypes = ( ) => { return useRequest ( { path : `${ API_BASE_PATH }repository_types` , method : 'get' , initialData : [ ] , } ) ; } ; export const addRepository = async ( newRepository ) => { const result = await sendRequest ( { path : `${ API_BASE_PATH }repositories` , method : 'put' , body : newRepository , } ) ; uiMetricService . trackUiMetric ( UIM_REPOSITORY_CREATE ) ; return result ; } ; export const editRepository = async ( editedRepository ) => { const result = await sendRequest ( { path : `${ API_BASE_PATH }repositories/${ encodeURIComponent ( editedRepository . name ) }` , method : 'put' , body : editedRepository , } ) ; uiMetricService . trackUiMetric ( UIM_REPOSITORY_UPDATE ) ; return result ; } ; export const deleteRepositories = async ( names ) => { const result = await sendRequest ( { path : `${ API_BASE_PATH }repositories/${ names . map ( ( name ) => encodeURIComponent ( name ) ) . join ( ',' ) }` , method : 'delete' , } ) ; uiMetricService . trackUiMetric ( names . length > 1 ? UIM_REPOSITORY_DELETE_MANY : UIM_REPOSITORY_DELETE ) ; return result ; } ;