Skip to content

Commit

Permalink
fix: prevent context color flickering while navigating (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbacherycz authored Nov 14, 2024
1 parent 1905627 commit fe107a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 14 additions & 1 deletion packages/frontend/src/api/useParties.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import type { PartiesQuery, PartyFieldsFragment } from 'bff-types-generated';
import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';
import { useLocation, useSearchParams } from 'react-router-dom';
import { normalizeFlattenParties } from '../components/PartyDropdown/normalizeFlattenParties.ts';
import { QUERY_KEYS } from '../constants/queryKeys.ts';
Expand All @@ -18,6 +18,7 @@ interface UsePartiesOutput {
setSelectedPartyIds: (parties: string[], allOrganizationsSelected: boolean) => void;
currentEndUser: PartyFieldsFragment | undefined;
allOrganizationsSelected: boolean;
selectedProfile: 'company' | 'person';
}

interface PartiesResult {
Expand Down Expand Up @@ -131,13 +132,24 @@ export const useParties = (): UsePartiesOutput => {
}
};

const isCompanyFromParams = useMemo(() => {
const party = searchParams.get('party');
const allParties = searchParams.get('allParties');
return Boolean(party || allParties);
}, [searchParams]);

// biome-ignore lint/correctness/useExhaustiveDependencies: Full control of what triggers this code is needed
useEffect(() => {
if (isSuccess && data?.parties?.length > 0) {
handlePartySelection();
}
}, [isSuccess, data?.parties, location.search]);

const isCompanyProfile =
isCompanyFromParams || allOrganizationsSelected || selectedParties?.[0]?.partyType === 'Organization';

const selectedProfile = (isCompanyProfile ? 'company' : 'person') as 'company' | 'person';

return {
isLoading,
isSuccess,
Expand All @@ -149,5 +161,6 @@ export const useParties = (): UsePartiesOutput => {
currentEndUser: data?.parties.find((party) => party.isCurrentEndUser),
deletedParties: data?.deletedParties ?? [],
allOrganizationsSelected,
selectedProfile,
};
};
9 changes: 3 additions & 6 deletions packages/frontend/src/components/PageLayout/PageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const PageLayoutContent: React.FC<PageLayoutContentProps> = memo(

const Background: React.FC<{ children: React.ReactNode; isCompany: boolean }> = ({ children, isCompany }) => {
const { inSelectionMode } = useSelectedDialogs();

return (
<div
data-testid="pageLayout-background"
Expand All @@ -90,14 +89,12 @@ export const ProtectedPageLayout = () => {
export const PageLayout: React.FC = () => {
const queryClient = useQueryClient();
const { t } = useTranslation();
const { selectedParties, allOrganizationsSelected } = useParties();
const { selectedParties, allOrganizationsSelected, selectedProfile } = useParties();
const [searchParams] = useSearchParams();

useProfile();

const name = allOrganizationsSelected ? t('parties.labels.all_organizations') : selectedParties?.[0]?.name || '';
const isCompany = allOrganizationsSelected || selectedParties?.[0]?.partyType === 'Organization';
const profile = isCompany ? 'company' : 'person';

useUpdateOnLocationChange(() => {
const searchString = getSearchStringFromQueryParams(searchParams);
Expand All @@ -106,10 +103,10 @@ export const PageLayout: React.FC = () => {

return (
<SelectedDialogsContainer>
<Background isCompany={isCompany}>
<Background isCompany={selectedProfile === 'company'}>
<BottomDrawerContainer>
<BetaBanner />
<PageLayoutContent name={name} profile={profile} />
<PageLayoutContent name={name} profile={selectedProfile} />
<Snackbar />
</BottomDrawerContainer>
</Background>
Expand Down

0 comments on commit fe107a6

Please sign in to comment.