Skip to content

Commit 2ac70e9

Browse files
committed
Adjust X/Twitter donation success share formatting
1 parent 5faf1c2 commit 2ac70e9

File tree

1 file changed

+73
-46
lines changed

1 file changed

+73
-46
lines changed

src/features/donation/components/DonationSuccess.tsx

+73-46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMemo } from "react";
22

33
import { Check } from "lucide-react";
44
import Link from "next/link";
5+
import { isError } from "remeda";
56

67
import { BLOCKCHAIN_EXPLORER_TX_ENDPOINT_URL } from "@/common/_config";
78
import { type PotId, indexer } from "@/common/api/indexer";
@@ -25,7 +26,7 @@ import {
2526
} from "@/common/ui/components";
2627
import TwitterSvg from "@/common/ui/svg/twitter";
2728
import { useWalletUserSession } from "@/common/wallet";
28-
import { AccountProfileLink } from "@/entities/_shared/account";
29+
import { AccountProfileLink, useAccountSocialProfile } from "@/entities/_shared/account";
2930
import { TokenTotalValue, useToken } from "@/entities/_shared/token";
3031
import { rootPathnames } from "@/pathnames";
3132

@@ -45,7 +46,7 @@ export const DonationSuccess = ({ form, transactionHash, closeModal }: DonationS
4546
const viewer = useWalletUserSession();
4647
const { finalOutcome } = useDonationState();
4748
const isResultLoading = finalOutcome === undefined;
48-
const [potId] = form.watch(["potAccountId"]);
49+
const [potId, recipientAccountIdFormValue] = form.watch(["potAccountId", "recipientAccountId"]);
4950

5051
const { data: pot } = indexer.usePot({
5152
enabled: potId !== undefined,
@@ -54,24 +55,32 @@ export const DonationSuccess = ({ form, transactionHash, closeModal }: DonationS
5455

5556
const recipientAccountId = useMemo(
5657
() =>
57-
"recipient_id" in (finalOutcome ?? {})
58+
("recipient_id" in (finalOutcome ?? {})
5859
? (finalOutcome as DirectDonation).recipient_id
59-
: ((finalOutcome as PotDonation).project_id ?? undefined),
60+
: (finalOutcome as PotDonation).project_id) ?? recipientAccountIdFormValue,
6061

61-
[finalOutcome],
62+
[finalOutcome, recipientAccountIdFormValue],
6263
);
6364

64-
const { data: recipient, error: recipientDataError } = indexer.useAccount({
65+
const {
66+
isLoading: isRecipientSocialProfileLoading,
67+
profile: recipientSocialProfile,
68+
error: recipientProfileLoadingError,
69+
} = useAccountSocialProfile({
6570
enabled: recipientAccountId !== undefined,
6671
accountId: recipientAccountId ?? "noop",
6772
});
6873

69-
const tokenId =
70-
"ft_id" in (finalOutcome ?? {}) ? (finalOutcome as DirectDonation).ft_id : NATIVE_TOKEN_ID;
74+
const tokenId = useMemo(
75+
() =>
76+
"ft_id" in (finalOutcome ?? {}) ? (finalOutcome as DirectDonation).ft_id : NATIVE_TOKEN_ID,
7177

72-
const { data: token } = useToken({ tokenId });
78+
[finalOutcome],
79+
);
7380

74-
const isLoading = isResultLoading || recipient === undefined || token === undefined;
81+
const { isLoading: isTokenLoading, data: token } = useToken({ tokenId });
82+
83+
const isLoading = isResultLoading || isRecipientSocialProfileLoading || isTokenLoading;
7584

7685
const totalAmountFloat = indivisibleUnitsToFloat(
7786
finalOutcome?.total_amount ?? "0",
@@ -97,41 +106,56 @@ export const DonationSuccess = ({ form, transactionHash, closeModal }: DonationS
97106
});
98107

99108
const twitterIntent = useMemo(() => {
100-
if (!recipient?.near_social_profile_data) return;
101-
const twitterIntentBase = "https://twitter.com/intent/tweet?text=";
102-
103-
const profile = recipient.near_social_profile_data;
104-
105-
const text = encodeURIComponent(`🎉 Just supported ${
106-
profile.linktree?.twitter
107-
? `${profile.name} (@${profile.linktree.twitter})`
108-
: `${profile.name} (${recipient.id})`
109-
} through @${PLATFORM_TWITTER_ACCOUNT_ID}!
110-
111-
💝 Making an impact by funding public goods that shape our future.
112-
113-
🤝 Join me in supporting amazing projects:\n`);
114-
115-
const url = encodeURIComponent(
116-
`${APP_DEFAULT_PUBLIC_URL}${rootPathnames.PROFILE}/${recipient.id}/donations`,
117-
);
118-
119-
const relation = encodeURIComponent(APP_DEFAULT_PUBLIC_URL);
120-
121-
return (
122-
twitterIntentBase +
123-
text +
124-
`&url=${url}` +
125-
`&related=${relation}` +
126-
`&hashtags=${DEFAULT_SHARE_HASHTAGS.join(",")}`
127-
);
128-
}, [recipient?.id, recipient?.near_social_profile_data]);
129-
130-
return recipientDataError !== undefined ? (
109+
if (recipientAccountId !== undefined) {
110+
const twitterIntentBase = "https://twitter.com/intent/tweet?text=";
111+
112+
const text = encodeURIComponent(
113+
`🎉 Just supported ${
114+
recipientSocialProfile?.linktree?.twitter
115+
? `${recipientSocialProfile?.name ?? recipientAccountId} (@${
116+
recipientSocialProfile.linktree.twitter
117+
})`
118+
: `${
119+
recipientSocialProfile?.name !== undefined &&
120+
recipientSocialProfile?.name?.length > 1
121+
? `${recipientSocialProfile.name} (${recipientAccountId})`
122+
: recipientAccountId
123+
}`
124+
} through @${PLATFORM_TWITTER_ACCOUNT_ID}!\n\n` +
125+
"💝 Making an impact by funding public goods that shape our future." +
126+
`\n\n🤝 Join me in supporting amazing projects:\n`,
127+
);
128+
129+
// viewer.isSignedIn
130+
// ? `?referrerAccountId=${viewer.accountId}`
131+
// : "",
132+
133+
const url = encodeURIComponent(
134+
`${APP_DEFAULT_PUBLIC_URL}${rootPathnames.PROFILE}/${recipientAccountId}/donations`,
135+
);
136+
137+
const relation = encodeURIComponent(APP_DEFAULT_PUBLIC_URL);
138+
139+
return (
140+
twitterIntentBase +
141+
text +
142+
`&url=${url}` +
143+
`&related=${relation}` +
144+
`&hashtags=${DEFAULT_SHARE_HASHTAGS.join(",")}`
145+
);
146+
} else return undefined;
147+
}, [recipientAccountId, recipientSocialProfile]);
148+
149+
return recipientProfileLoadingError !== undefined ||
150+
(!isResultLoading && recipientAccountId === undefined) ? (
131151
<ModalErrorBody
132152
heading="Donation"
133153
title="Unable to load recipient data!"
134-
message={recipientDataError?.message}
154+
message={
155+
isError(recipientProfileLoadingError)
156+
? recipientProfileLoadingError.message
157+
: recipientProfileLoadingError
158+
}
135159
/>
136160
) : (
137161
<DialogDescription className="items-center gap-8 p-10">
@@ -181,14 +205,17 @@ export const DonationSuccess = ({ form, transactionHash, closeModal }: DonationS
181205
<TokenTotalValue amountBigString={finalOutcome.total_amount} {...{ tokenId }} />
182206
)}
183207

184-
{isLoading ? (
208+
{isLoading || recipientAccountId === undefined ? (
185209
<Skeleton className="w-49 h-5" />
186210
) : (
187211
<p className="m-0 flex flex-col">
188212
<div className="flex gap-1">
189213
<span className="prose">{"has been donated to"}</span>
190214

191-
<AccountProfileLink accountId={recipient.id} classNames={{ name: "font-600" }} />
215+
<AccountProfileLink
216+
accountId={recipientAccountId}
217+
classNames={{ name: "font-600" }}
218+
/>
192219
</div>
193220

194221
{pot?.name && (
@@ -197,11 +224,11 @@ export const DonationSuccess = ({ form, transactionHash, closeModal }: DonationS
197224
</p>
198225
)}
199226

200-
{isLoading ? (
227+
{isLoading || recipientAccountId === undefined ? (
201228
<Skeleton className="w-23.5 h-5" />
202229
) : (
203230
<Link
204-
href={`${rootPathnames.PROFILE}/${recipient.id}/funding-raised`}
231+
href={`${rootPathnames.PROFILE}/${recipientAccountId}/funding-raised`}
205232
onClick={closeModal}
206233
className="font-500 text-red-600"
207234
>

0 commit comments

Comments
 (0)