Merge branch 'pass-lifetime-fixes' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / vpn / gateways / useSpecificCountryCount.ts
blob796046f85bf45e5d3183e85f6e4ac5414adf1370
1 import { useMemo } from 'react';
3 const getMaxForCountry = (country: string, remainingCount: number, deletedInCountries: Record<string, number>) =>
4     remainingCount -
5     Object.keys(deletedInCountries).reduce(
6         (count, otherCountry) => count + (country === otherCountry ? 0 : deletedInCountries[country]),
7         0
8     );
10 export const useSpecificCountryCount = (
11     model: { quantities?: Record<string, number> },
12     remainingCount: number,
13     deletedInCountries: Record<string, number>
14 ) =>
15     useMemo(
16         () =>
17             Object.keys(model.quantities || {}).reduce(
18                 (total, country) =>
19                     total +
20                     Math.max(
21                         0,
22                         (model.quantities?.[country] || 0) -
23                             getMaxForCountry(country, remainingCount, deletedInCountries)
24                     ),
25                 0
26             ),
27         [model, remainingCount, deletedInCountries]
28     );