1 import { useMemo, useState } from 'react';
3 import { c, msgid } from 'ttag';
5 import { Button } from '@proton/atoms';
6 import Form from '@proton/components/components/form/Form';
7 import ModalTwo from '@proton/components/components/modalTwo/Modal';
8 import ModalTwoContent from '@proton/components/components/modalTwo/ModalContent';
9 import ModalTwoFooter from '@proton/components/components/modalTwo/ModalFooter';
10 import ModalTwoHeader from '@proton/components/components/modalTwo/ModalHeader';
11 import type { ModalStateProps } from '@proton/components/components/modalTwo/useModalState';
13 import type { CountryOptions } from '../../../helpers/countries';
14 import { GatewayCountrySelection } from './GatewayCountrySelection';
15 import type { GatewayDto } from './GatewayDto';
16 import type { GatewayUser } from './GatewayUser';
17 import { getInitialModel } from './helpers';
18 import { useAddedQuantities } from './useAddedQuantities';
19 import { useSpecificCountryCount } from './useSpecificCountryCount';
21 interface Props extends ModalStateProps {
22 countries: readonly string[];
23 deletedInCountries: Record<string, number>;
26 users: readonly GatewayUser[];
27 countryOptions: CountryOptions;
28 singleServer?: boolean;
29 showCancelButton?: boolean;
30 onSubmitDone: (quantities: Record<string, number>) => void;
34 const GatewayAddServersModal = ({
44 showCancelButton = false,
47 const [model, setModel] = useState(getInitialModel(countries));
48 const remainingCount = useMemo(() => ownedCount - usedCount, [ownedCount, usedCount]);
49 const numberOfAddedServers = useAddedQuantities(model);
50 const totalCountExceeded = useMemo(
51 () => numberOfAddedServers > remainingCount,
52 [numberOfAddedServers, remainingCount]
54 const specificCountryCount = useSpecificCountryCount(model, remainingCount, deletedInCountries);
55 const needUpsell = useMemo(
56 () => totalCountExceeded || specificCountryCount > 0,
57 [totalCountExceeded, specificCountryCount]
60 const changeModel = (diff: Partial<GatewayDto>) => setModel((model) => ({ ...model, ...diff }));
62 const handleSubmit = async () => {
63 onSubmitDone(model.quantities || {});
68 <ModalTwo size="large" as={Form} onSubmit={handleSubmit} {...rest}>
69 <ModalTwoHeader title={c('Title').t`Add servers`} />
71 <GatewayCountrySelection
72 singleServer={singleServer}
74 ownedCount={ownedCount}
76 addedCount={numberOfAddedServers}
77 needUpsell={needUpsell}
78 specificCountryCount={specificCountryCount}
79 countryOptions={countryOptions}
85 changeModel={changeModel}
90 <Button color="weak" onClick={rest.onClose}>
91 {c('Action').t`Cancel`}
96 {!needUpsell && numberOfAddedServers >= 1 ? (
97 <Button color="norm" type="submit">
98 {c('Feature').ngettext(
99 msgid`Add (${numberOfAddedServers})`,
100 `Add (${numberOfAddedServers})`,
105 <Button color="norm" type="submit" disabled>
106 {c('Feature').t`Add`}
114 export default GatewayAddServersModal;