2025-06-14 03:38:09 +10:00
|
|
|
// Copyright 2025 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2025-07-15 09:52:56 -07:00
|
|
|
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';
|
|
|
|
|
2025-07-21 14:31:09 -05:00
|
|
|
import { getIntl, getTheme } 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';
|
2025-07-17 04:33:41 +10:00
|
|
|
import type { SettingsPage } from '../../types/Nav';
|
2025-06-27 13:48:50 -07:00
|
|
|
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';
|
2025-07-15 09:52:56 -07:00
|
|
|
import { getDonationHumanAmounts } from '../../util/subscriptionConfiguration';
|
|
|
|
import { drop } from '../../util/drop';
|
|
|
|
import type { OneTimeDonationHumanAmounts } from '../../types/Donations';
|
2025-07-21 14:31:09 -05:00
|
|
|
import { getPreferredBadgeSelector } from '../selectors/badges';
|
2025-06-14 03:38:09 +10:00
|
|
|
|
|
|
|
export const SmartPreferencesDonations = memo(
|
2025-06-27 13:48:50 -07:00
|
|
|
function SmartPreferencesDonations({
|
|
|
|
contentsRef,
|
|
|
|
page,
|
|
|
|
setPage,
|
|
|
|
}: {
|
2025-06-14 03:38:09 +10:00
|
|
|
contentsRef: MutableRefObject<HTMLDivElement | null>;
|
2025-07-17 04:33:41 +10:00
|
|
|
page: SettingsPage;
|
|
|
|
setPage: (page: SettingsPage) => void;
|
2025-06-14 03:38:09 +10:00
|
|
|
}) {
|
2025-07-15 09:52:56 -07:00
|
|
|
const [validCurrencies, setValidCurrencies] = useState<
|
|
|
|
ReadonlyArray<string>
|
|
|
|
>([]);
|
|
|
|
const [donationAmountsConfig, setDonationAmountsConfig] =
|
|
|
|
useState<OneTimeDonationHumanAmounts>();
|
|
|
|
|
2025-07-21 14:31:09 -05:00
|
|
|
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
|
2025-06-27 13:48:50 -07:00
|
|
|
const isStaging = isStagingServer();
|
2025-06-14 03:38:09 +10:00
|
|
|
const i18n = useSelector(getIntl);
|
2025-07-21 14:31:09 -05:00
|
|
|
const theme = useSelector(getTheme);
|
2025-07-07 18:53:46 -05:00
|
|
|
|
2025-07-22 10:12:01 -05:00
|
|
|
const donationsState = useSelector((state: StateType) => state.donations);
|
|
|
|
const { clearWorkflow, submitDonation, updateLastError } =
|
|
|
|
useDonationsActions();
|
2025-06-14 03:38:09 +10:00
|
|
|
|
2025-07-21 14:31:09 -05:00
|
|
|
const { badges, color, firstName, profileAvatarUrl } = useSelector(getMe);
|
|
|
|
const badge = getPreferredBadge(badges);
|
|
|
|
|
2025-07-07 18:53:46 -05:00
|
|
|
const { showToast } = useToastActions();
|
|
|
|
const donationReceipts = useSelector(
|
|
|
|
(state: StateType) => state.donations.receipts
|
|
|
|
);
|
2025-07-21 14:31:09 -05:00
|
|
|
|
2025-07-07 18:53:46 -05:00
|
|
|
const { saveAttachmentToDisk } = window.Signal.Migrations;
|
|
|
|
|
2025-07-15 09:52:56 -07:00
|
|
|
// 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());
|
|
|
|
}, []);
|
|
|
|
|
2025-06-27 13:48:50 -07:00
|
|
|
return (
|
|
|
|
<PreferencesDonations
|
|
|
|
i18n={i18n}
|
2025-07-21 14:31:09 -05:00
|
|
|
badge={badge}
|
2025-07-07 18:53:46 -05:00
|
|
|
color={color}
|
|
|
|
firstName={firstName}
|
|
|
|
profileAvatarUrl={profileAvatarUrl}
|
|
|
|
donationReceipts={donationReceipts}
|
|
|
|
saveAttachmentToDisk={saveAttachmentToDisk}
|
|
|
|
generateDonationReceiptBlob={generateDonationReceiptBlob}
|
2025-07-15 09:52:56 -07:00
|
|
|
donationAmountsConfig={donationAmountsConfig}
|
|
|
|
validCurrencies={validCurrencies}
|
2025-07-07 18:53:46 -05:00
|
|
|
showToast={showToast}
|
|
|
|
contentsRef={contentsRef}
|
2025-06-27 13:48:50 -07:00
|
|
|
isStaging={isStaging}
|
|
|
|
page={page}
|
2025-07-22 10:12:01 -05:00
|
|
|
lastError={donationsState.lastError}
|
|
|
|
workflow={donationsState.currentWorkflow}
|
2025-06-27 13:48:50 -07:00
|
|
|
clearWorkflow={clearWorkflow}
|
2025-07-22 10:12:01 -05:00
|
|
|
updateLastError={updateLastError}
|
2025-06-27 13:48:50 -07:00
|
|
|
submitDonation={submitDonation}
|
|
|
|
setPage={setPage}
|
2025-07-21 14:31:09 -05:00
|
|
|
theme={theme}
|
2025-06-27 13:48:50 -07:00
|
|
|
/>
|
|
|
|
);
|
2025-06-14 03:38:09 +10:00
|
|
|
}
|
|
|
|
);
|