signal-desktop/ts/state/smart/PreferencesDonations.tsx

94 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-06-14 03:38:09 +10:00
// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { memo, useEffect, useState } from 'react';
2025-06-14 03:38:09 +10:00
import { useSelector } from 'react-redux';
import type { MutableRefObject } from 'react';
import { getIntl } from '../selectors/user';
2025-07-07 18:53:46 -05:00
import { getMe } from '../selectors/conversations';
2025-06-14 03:38:09 +10:00
import { PreferencesDonations } from '../../components/PreferencesDonations';
import type { Page } from '../../components/Preferences';
import { useDonationsActions } from '../ducks/donations';
import type { StateType } from '../reducer';
import { isStagingServer } from '../../util/isStagingServer';
2025-07-07 18:53:46 -05:00
import { generateDonationReceiptBlob } from '../../util/generateDonationReceipt';
import { useToastActions } from '../ducks/toast';
import { getDonationHumanAmounts } from '../../util/subscriptionConfiguration';
import { drop } from '../../util/drop';
import type { OneTimeDonationHumanAmounts } from '../../types/Donations';
2025-06-14 03:38:09 +10:00
export const SmartPreferencesDonations = memo(
function SmartPreferencesDonations({
contentsRef,
page,
setPage,
}: {
2025-06-14 03:38:09 +10:00
contentsRef: MutableRefObject<HTMLDivElement | null>;
page: Page;
setPage: (page: Page) => void;
2025-06-14 03:38:09 +10:00
}) {
const [validCurrencies, setValidCurrencies] = useState<
ReadonlyArray<string>
>([]);
const [donationAmountsConfig, setDonationAmountsConfig] =
useState<OneTimeDonationHumanAmounts>();
const isStaging = isStagingServer();
2025-06-14 03:38:09 +10:00
const i18n = useSelector(getIntl);
2025-07-07 18:53:46 -05:00
const workflow = useSelector(
(state: StateType) => state.donations.currentWorkflow
);
const { clearWorkflow, submitDonation } = useDonationsActions();
2025-06-14 03:38:09 +10:00
2025-07-07 18:53:46 -05:00
const {
avatars: userAvatarData = [],
color,
firstName,
profileAvatarUrl,
} = useSelector(getMe);
const { showToast } = useToastActions();
const donationReceipts = useSelector(
(state: StateType) => state.donations.receipts
);
const { saveAttachmentToDisk } = window.Signal.Migrations;
// Eagerly load donation config from API when entering Donations Home so the
// Amount picker loads instantly
useEffect(() => {
async function loadDonationAmounts() {
const amounts = await getDonationHumanAmounts();
setDonationAmountsConfig(amounts);
const currencies = Object.keys(amounts);
setValidCurrencies(currencies);
}
drop(loadDonationAmounts());
}, []);
return (
<PreferencesDonations
i18n={i18n}
2025-07-07 18:53:46 -05:00
userAvatarData={userAvatarData}
color={color}
firstName={firstName}
profileAvatarUrl={profileAvatarUrl}
donationReceipts={donationReceipts}
saveAttachmentToDisk={saveAttachmentToDisk}
generateDonationReceiptBlob={generateDonationReceiptBlob}
donationAmountsConfig={donationAmountsConfig}
validCurrencies={validCurrencies}
2025-07-07 18:53:46 -05:00
showToast={showToast}
contentsRef={contentsRef}
isStaging={isStaging}
page={page}
workflow={workflow}
clearWorkflow={clearWorkflow}
submitDonation={submitDonation}
setPage={setPage}
/>
);
2025-06-14 03:38:09 +10:00
}
);