feat(*): allow to select auth providers + add multiworkspace with subdomain management (#8656)
## Summary Add support for multi-workspace feature and adjust configurations and states accordingly. - Introduced new state isMultiWorkspaceEnabledState. - Updated ClientConfigProviderEffect component to handle multi-workspace. - Modified GraphQL schema and queries to include multi-workspace related configurations. - Adjusted server environment variables and their respective documentation to support multi-workspace toggle. - Updated server-side logic to handle new multi-workspace configurations and conditions.
This commit is contained in:
@ -0,0 +1,96 @@
|
||||
import { useRecoilValue, useSetRecoilState, useRecoilState } from 'recoil';
|
||||
|
||||
import { useGetPublicWorkspaceDataBySubdomainQuery } from '~/generated/graphql';
|
||||
|
||||
import { workspacePublicDataState } from '@/auth/states/workspacePublicDataState';
|
||||
import { authProvidersState } from '@/client-config/states/authProvidersState';
|
||||
import { useEffect } from 'react';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { lastAuthenticateWorkspaceState } from '@/auth/states/lastAuthenticateWorkspaceState';
|
||||
import { isMultiWorkspaceEnabledState } from '@/client-config/states/isMultiWorkspaceEnabledState';
|
||||
import { useUrlManager } from '@/url-manager/hooks/useUrlManager';
|
||||
|
||||
export const WorkspaceProviderEffect = () => {
|
||||
const workspacePublicData = useRecoilValue(workspacePublicDataState);
|
||||
|
||||
const setAuthProviders = useSetRecoilState(authProvidersState);
|
||||
const setWorkspacePublicDataState = useSetRecoilState(
|
||||
workspacePublicDataState,
|
||||
);
|
||||
|
||||
const [lastAuthenticateWorkspace, setLastAuthenticateWorkspace] =
|
||||
useRecoilState(lastAuthenticateWorkspaceState);
|
||||
|
||||
const {
|
||||
redirectToHome,
|
||||
getWorkspaceSubdomain,
|
||||
redirectToWorkspace,
|
||||
isTwentyHomePage,
|
||||
} = useUrlManager();
|
||||
|
||||
const isMultiWorkspaceEnabled = useRecoilValue(isMultiWorkspaceEnabledState);
|
||||
|
||||
useGetPublicWorkspaceDataBySubdomainQuery({
|
||||
skip:
|
||||
(isMultiWorkspaceEnabled && isTwentyHomePage) ||
|
||||
isDefined(workspacePublicData),
|
||||
onCompleted: (data) => {
|
||||
setAuthProviders(data.getPublicWorkspaceDataBySubdomain.authProviders);
|
||||
setWorkspacePublicDataState(data.getPublicWorkspaceDataBySubdomain);
|
||||
},
|
||||
onError: (error) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
setLastAuthenticateWorkspace(null);
|
||||
redirectToHome();
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
isMultiWorkspaceEnabled &&
|
||||
isDefined(workspacePublicData?.subdomain) &&
|
||||
workspacePublicData.subdomain !== getWorkspaceSubdomain
|
||||
) {
|
||||
redirectToWorkspace(workspacePublicData.subdomain);
|
||||
}
|
||||
}, [
|
||||
getWorkspaceSubdomain,
|
||||
isMultiWorkspaceEnabled,
|
||||
redirectToWorkspace,
|
||||
workspacePublicData,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
isMultiWorkspaceEnabled &&
|
||||
isDefined(lastAuthenticateWorkspace?.subdomain) &&
|
||||
isTwentyHomePage
|
||||
) {
|
||||
redirectToWorkspace(lastAuthenticateWorkspace.subdomain);
|
||||
}
|
||||
}, [
|
||||
isMultiWorkspaceEnabled,
|
||||
isTwentyHomePage,
|
||||
lastAuthenticateWorkspace,
|
||||
redirectToWorkspace,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
if (isDefined(workspacePublicData?.logo)) {
|
||||
const link: HTMLLinkElement =
|
||||
document.querySelector("link[rel*='icon']") ||
|
||||
document.createElement('link');
|
||||
link.rel = 'icon';
|
||||
link.href = workspacePublicData.logo;
|
||||
document.getElementsByTagName('head')[0].appendChild(link);
|
||||
}
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(err);
|
||||
}
|
||||
}, [workspacePublicData]);
|
||||
|
||||
return <></>;
|
||||
};
|
||||
Reference in New Issue
Block a user