mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
128 lines
3.3 KiB
JavaScript
128 lines
3.3 KiB
JavaScript
import React from 'react';
|
|
import {
|
|
useAuthenticatedAccount,
|
|
useCurrentOrganization,
|
|
useDashboardMeta,
|
|
} from '../../hooks/query';
|
|
import { useSplashLoading } from '../../hooks/state';
|
|
import { useWatch, useWatchImmediate, useWhen } from '../../hooks';
|
|
import { setCookie, getCookie } from '../../utils';
|
|
|
|
/**
|
|
* Dashboard meta async booting.
|
|
*/
|
|
export function useDashboardMetaBoot() {
|
|
const {
|
|
data: dashboardMeta,
|
|
isLoading: isDashboardMetaLoading,
|
|
isSuccess: isDashboardMetaSuccess,
|
|
} = useDashboardMeta();
|
|
|
|
const [startLoading, stopLoading] = useSplashLoading();
|
|
|
|
useWatchImmediate((value) => {
|
|
value && startLoading();
|
|
}, isDashboardMetaLoading);
|
|
|
|
useWatchImmediate(() => {
|
|
isDashboardMetaSuccess && stopLoading();
|
|
}, isDashboardMetaSuccess);
|
|
|
|
return {
|
|
isLoading: isDashboardMetaLoading,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Dashboard async booting.
|
|
* @returns {{ isLoading: boolean }}
|
|
*/
|
|
export function useDashboardBoot() {
|
|
const { isLoading } = useDashboardMetaBoot();
|
|
|
|
return { isLoading };
|
|
}
|
|
|
|
/**
|
|
* Application async booting.
|
|
*/
|
|
export function useApplicationBoot() {
|
|
// Fetches the current user's organization.
|
|
const {
|
|
isSuccess: isCurrentOrganizationSuccess,
|
|
isLoading: isOrgLoading,
|
|
data: organization,
|
|
} = useCurrentOrganization();
|
|
|
|
// Authenticated user.
|
|
const { isSuccess: isAuthUserSuccess, isLoading: isAuthUserLoading } =
|
|
useAuthenticatedAccount();
|
|
|
|
// Initial locale cookie value.
|
|
const localeCookie = getCookie('locale');
|
|
|
|
// Is the dashboard booted.
|
|
const isBooted = React.useRef(false);
|
|
|
|
// Syns the organization language with locale cookie.
|
|
React.useEffect(() => {
|
|
if (organization?.metadata?.language) {
|
|
setCookie('locale', organization.metadata.language);
|
|
}
|
|
}, [organization]);
|
|
|
|
React.useEffect(() => {
|
|
// Can't continue if the organization metadata is not loaded yet.
|
|
if (!organization?.metadata?.language) {
|
|
return;
|
|
}
|
|
// Can't continue if the organization is already booted.
|
|
if (isBooted.current) {
|
|
return;
|
|
}
|
|
// Reboot the application in case the initial locale not equal
|
|
// the current organization language.
|
|
if (localeCookie !== organization.metadata.language) {
|
|
window.location.reload();
|
|
}
|
|
}, [localeCookie, organization]);
|
|
|
|
const [startLoading, stopLoading] = useSplashLoading();
|
|
|
|
// Splash loading when organization request loading and
|
|
// applicaiton still not booted.
|
|
useWatchImmediate((value) => {
|
|
value && !isBooted.current && startLoading();
|
|
}, isOrgLoading);
|
|
|
|
// Splash loading when request authenticated user loading and
|
|
// application still not booted yet.
|
|
useWatchImmediate((value) => {
|
|
value && !isBooted.current && startLoading();
|
|
}, isAuthUserLoading);
|
|
|
|
// Stop splash loading once organization request success.
|
|
useWatch((value) => {
|
|
value && stopLoading();
|
|
}, isCurrentOrganizationSuccess);
|
|
|
|
// Stop splash loading once authenticated user request success.
|
|
useWatch((value) => {
|
|
value && stopLoading();
|
|
}, isAuthUserSuccess);
|
|
|
|
// Once the all requests complete change the app loading state.
|
|
useWhen(
|
|
isAuthUserSuccess &&
|
|
isCurrentOrganizationSuccess &&
|
|
localeCookie === organization?.metadata?.language,
|
|
() => {
|
|
isBooted.current = true;
|
|
},
|
|
);
|
|
|
|
return {
|
|
isLoading: isOrgLoading || isAuthUserLoading,
|
|
};
|
|
}
|