Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import { uniq } from 'lodash-es'; import { memo, useMemo, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import { useLocation, useParams } from 'react-router-dom'; import { match } from 'ts-pattern'; import { type SetRequired } from 'type-fest'; import { Stack } from '@allshares/studio-design-system'; import { PageContainer } from '@amalia/core/layout/components'; import { CalculationType, PaymentCategory, type CalculationRequest, type UserStatementListFilters, } from '@amalia/core/types'; import { useShallowObjectMemo, useStateInStorage } from '@amalia/ext/react/hooks'; import { canViewStatements } from '@amalia/kernel/auth/shared'; import { useAbilityContext } from '@amalia/kernel/auth/state'; import { usePaymentsOfPeriod } from '@amalia/payout-calculation/payments/state'; import { useCalculation, type MultiPayoutCategory } from '@amalia/payout-calculation/statements/components'; import { useUserStatementFacets } from '@amalia/payout-calculation/statements/state'; import { PlanIsHiddenQueryChoices, type Plan } from '@amalia/payout-definition/plans/types'; import { usePeriod, usePlan } from '@amalia/payout-definition/state'; import { useCompanyDefaultDate, useCurrentCompany } from '@amalia/tenants/companies/state'; import { StatementListViewOptionsEnum } from './header/filters/FiltersContainer.types'; import { StatementsListHeader } from './header/StatementsListHeader'; import { useShouldRedirectToEmployeeStatement } from './hooks/use-should-redirect-to-employee-statement'; import { StatementsListCards } from './list/grid/StatementsListCards'; import { StatementsListTable } from './list/table/StatementsListTable'; import { StatementsListWorkflow } from './list/workflow/StatementListWorkflow'; import { StatementListViewContext, type StatementListViewContextInterface } from './StatementsListView.context'; const useStatementsListCalculationType = () => { const { pathname } = useLocation(); return pathname.includes('forecasts') ? CalculationType.FORECAST : CalculationType.STATEMENT; }; const useStatementsListCurrentPeriod = (planIds?: Plan['id'][]) => { const { startDate } = useParams<Record<string, string>>(); const { data: company } = useCurrentCompany(); const { data: plan } = usePlan(planIds?.length === 1 ? planIds[0] : undefined); const frequency = plan?.frequency ?? company.statementFrequency; const companyDefaultDate = useCompanyDefaultDate({ periodFrequency: frequency }); const { data: period } = usePeriod(startDate ?? companyDefaultDate, frequency); return { period, frequency }; }; export const StatementsListView = memo(function StatementsListView() { const ability = useAbilityContext(); const calculationType = useStatementsListCalculationType(); const [paymentCategory, setPaymentCategory] = useState<MultiPayoutCategory>(PaymentCategory.achievement); // Source of truth for the statement list breadcrumbs filter. const [statementListFilters, setStatementListFilters] = useStateInStorage<UserStatementListFilters>( ['statements', 'list', calculationType, 'filters'], { teamIds: undefined, planIds: undefined, userIds: undefined, planHiddenStatus: PlanIsHiddenQueryChoices.LIVE, statementStatus: undefined, }, // Only save for current navigation session. { storage: sessionStorage }, ); // Source of truth for fulltext search. const [searchValue, setSearchValue] = useState<string>(''); const { period, frequency } = useStatementsListCurrentPeriod(statementListFilters.planIds); // TODO: Properly handle statementStatus in calculation creation. // The previous version was doing something super shady, we should implement it properly on the backend. const calculationRequest = useMemo( (): SetRequired<CalculationRequest, 'periodId'> | null => period?.id ? { periodId: period.id, planIds: statementListFilters.planIds, teamIds: statementListFilters.teamIds, userIds: statementListFilters.userIds, type: calculationType, } : null, [period, statementListFilters, calculationType], ); const compute = useCalculation(calculationRequest); const [selectedView, setSelectedView] = useStateInStorage<StatementListViewOptionsEnum>( ['statements', 'list', 'view'], StatementListViewOptionsEnum.GRID, ); const { data: statementsListFacets, isPending: isFacetsPending } = useUserStatementFacets({ filters: statementListFilters, periodId: period?.id, calculationType, }); const { data: paymentsOfPeriod, isPending: isPaymentsLoading } = usePaymentsOfPeriod({ currentPeriodId: period?.id, filters: statementListFilters, }); const shouldRedirectToEmployeeStatement = useShouldRedirectToEmployeeStatement( period, statementsListFacets, isFacetsPending, calculationType, ); const validUserStatementsCount = useMemo( () => uniq( statementsListFacets?.users.flatMap((u) => u.statementsOptions?.filter((s) => !s.error).map((s) => s.statementId), ) ?? [], ).length, [statementsListFacets], ); const totalUserStatementsCount = useMemo( () => uniq(statementsListFacets?.users.flatMap((u) => u.statementsOptions?.map((s) => s.statementId)) ?? []).length, [statementsListFacets], ); const hasFiltersApplied = !!( statementListFilters.planIds?.length || statementListFilters.teamIds?.length || searchValue || statementListFilters.statementStatus || statementListFilters.userIds?.length || statementListFilters.planHiddenStatus !== PlanIsHiddenQueryChoices.BOTH ); const value = useShallowObjectMemo<StatementListViewContextInterface>({ statementListFilters, setStatementListFilters, searchValue, setSearchValue, paymentCategory, setPaymentCategory, paymentsOfPeriod, isPaymentsLoading, statementsListFacets, validUserStatementsCount, totalUserStatementsCount, hasFiltersApplied, // Consumers only render when period is ready. period, frequency, calculationType, ...compute, }); return ( <PageContainer> {period && !isFacetsPending && !shouldRedirectToEmployeeStatement ? ( <StatementListViewContext.Provider value={value}> {canViewStatements(ability) ? ( <Stack gap={32}> <StatementsListHeader selectedView={selectedView} setSelectedView={setSelectedView} /> {!!(totalUserStatementsCount || hasFiltersApplied) && match(selectedView) .with(StatementListViewOptionsEnum.GRID, () => <StatementsListCards />) .with(StatementListViewOptionsEnum.LIST, () => <StatementsListTable />) .with(StatementListViewOptionsEnum.WORKFLOW, () => <StatementsListWorkflow />) .exhaustive()} </Stack> ) : ( <FormattedMessage defaultMessage="You don't have the authorization to view statements" /> )} </StatementListViewContext.Provider> ) : null} </PageContainer> ); }); |