Skip to content

Commit

Permalink
chore: Refactor of current tenant provider (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrejkarz authored May 30, 2024
1 parent e1963fd commit 71164d4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,43 +21,17 @@ export type TenantPathParams = {
* @category Component
*/
export const CurrentTenantProvider = ({ children }: CurrentTenantProviderProps) => {
const tenants = useTenants();
const params = useParams<TenantPathParams>();
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<string>('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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string>('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];
};

0 comments on commit 71164d4

Please sign in to comment.