From 71164d4046601f60be76f8e3ab85019781b0a6de Mon Sep 17 00:00:00 2001 From: sdrejkarz <123745148+sdrejkarz@users.noreply.github.com> Date: Thu, 30 May 2024 17:32:25 +0200 Subject: [PATCH] chore: Refactor of current tenant provider (#580) --- .../currentTenantProvider.component.tsx | 41 +++------------- .../currentTenantProvider.utils.ts | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.utils.ts diff --git a/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.component.tsx b/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.component.tsx index 5481efc1a..678e92629 100644 --- a/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.component.tsx +++ b/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.component.tsx @@ -1,14 +1,13 @@ import { getFragmentData } from '@sb/webapp-api-client'; -import { TenantType } from '@sb/webapp-api-client/constants'; import { commonQueryCurrentUserFragment, useCommonQuery } from '@sb/webapp-api-client/providers'; -import { prop } from 'ramda'; import { useEffect, useMemo, useSyncExternalStore } from 'react'; import { useParams } from 'react-router-dom'; import { useTenants } from '../../hooks/useTenants/useTenants.hook'; import currentTenantContext from './currentTenantProvider.context'; import { setCurrentTenantStorageState, store } from './currentTenantProvider.storage'; -import { CurrentTenantProviderProps, CurrentTenantStorageState } from './currentTenantProvider.types'; +import { CurrentTenantProviderProps } from './currentTenantProvider.types'; +import { getCurrentTenant, parseStoredState } from './currentTenantProvider.utils'; export type TenantPathParams = { tenantId: string; @@ -22,43 +21,17 @@ export type TenantPathParams = { * @category Component */ export const CurrentTenantProvider = ({ children }: CurrentTenantProviderProps) => { + const tenants = useTenants(); const params = useParams(); - let { tenantId } = params; const { data } = useCommonQuery(); - const profile = getFragmentData(commonQueryCurrentUserFragment, data?.currentUser); - const userId = profile?.id; - - const tenants = useTenants(); const storedState = useSyncExternalStore(store.subscribe, store.getSnapshot); - let storedTenantId, parsedStoredState: CurrentTenantStorageState; - try { - parsedStoredState = JSON.parse(storedState); - storedTenantId = userId ? parsedStoredState?.[userId] ?? null : null; - } catch (e) { - parsedStoredState = {}; - storedTenantId = null; - } - if ( - !tenantId && - storedTenantId && - tenants - .map(prop('id')) - .filter((id) => !!id) - .includes(storedTenantId) - ) { - tenantId = storedTenantId; - } + const profile = getFragmentData(commonQueryCurrentUserFragment, data?.currentUser); + const userId = profile?.id; - let currentTenant = tenants.filter((t) => t?.membership.invitationAccepted).find((t) => t?.id === tenantId); - if (!currentTenant) { - // select first default - currentTenant = tenants.find((t) => t?.type === TenantType.PERSONAL); + const { parsedStoredState, storedTenantId } = parseStoredState(storedState, userId); - if (!currentTenant && tenants.length > 0) { - currentTenant = tenants[0]; - } - } + const currentTenant = getCurrentTenant(params.tenantId, storedTenantId, tenants); useEffect(() => { if (currentTenant && userId) { diff --git a/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.utils.ts b/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.utils.ts new file mode 100644 index 000000000..ac0bd3ab6 --- /dev/null +++ b/packages/webapp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.utils.ts @@ -0,0 +1,49 @@ +import { CommonQueryTenantItemFragmentFragment } from '@sb/webapp-api-client'; +import { TenantType } from '@sb/webapp-api-client/constants'; +import { prop } from 'ramda'; + +export const parseStoredState = (storedState: string, userId = '') => { + try { + const parsedStoredState = JSON.parse(storedState); + + return { + parsedStoredState, + storedTenantId: parsedStoredState?.[userId] ?? null, + }; + } catch { + return { + parsedStoredState: {}, + storedTenantId: null, + }; + } +}; + +const getTenantId = ( + tenantId = '', + storedTenantId: string | null, + tenants: (CommonQueryTenantItemFragmentFragment | null | undefined)[] +) => { + if (tenantId) return tenantId; + + if (storedTenantId && tenants.map(prop('id')).includes(storedTenantId)) { + return storedTenantId; + } + + return ''; +}; + +export const getCurrentTenant = ( + paramsTenantId = '', + storedTenantId: string | null, + tenants: (CommonQueryTenantItemFragmentFragment | null | undefined)[] +) => { + const tenantId = getTenantId(paramsTenantId, storedTenantId, tenants); + + const currentTenant = tenants.find((t) => t?.id === tenantId && t.membership.invitationAccepted); + if (currentTenant) return currentTenant; + + const firstDefaultTenant = tenants.find((t) => t?.type === TenantType.PERSONAL); + if (firstDefaultTenant) return firstDefaultTenant; + + return tenants[0]; +};