mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
// @ts-nocheck
|
|
import React from 'react';
|
|
import classNames from 'classnames';
|
|
import { CLASSES } from '@/constants/classes';
|
|
import { useBranches } from '@/hooks/query';
|
|
import { useFeatureCan } from '@/hooks/state';
|
|
import { Features } from '@/constants';
|
|
import { isEmpty } from 'lodash';
|
|
|
|
const BranchesContext = React.createContext();
|
|
|
|
/**
|
|
* Branches data provider.
|
|
*/
|
|
function BranchesProvider({ query, ...props }) {
|
|
// Features guard.
|
|
const { featureCan } = useFeatureCan();
|
|
|
|
const isBranchFeatureCan = featureCan(Features.Branches);
|
|
|
|
// Fetches the branches list.
|
|
const {
|
|
isLoading: isBranchesLoading,
|
|
isFetching: isBranchesFetching,
|
|
data: branches,
|
|
} = useBranches(query, { enabled: isBranchFeatureCan });
|
|
|
|
// Detarmines the datatable empty status.
|
|
const isEmptyStatus =
|
|
(isEmpty(branches) && !isBranchesLoading) || !isBranchFeatureCan;
|
|
|
|
// Provider state.
|
|
const provider = {
|
|
branches,
|
|
isBranchesLoading,
|
|
isBranchesFetching,
|
|
isEmptyStatus,
|
|
};
|
|
|
|
return (
|
|
<div
|
|
className={classNames(
|
|
CLASSES.PREFERENCES_PAGE_INSIDE_CONTENT,
|
|
CLASSES.PREFERENCES_PAGE_INSIDE_CONTENT_BRANCHES,
|
|
)}
|
|
>
|
|
<BranchesContext.Provider value={provider} {...props} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const useBranchesContext = () => React.useContext(BranchesContext);
|
|
export { BranchesProvider, useBranchesContext };
|