Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mfi-v2-xnft): full referral link url #275

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions apps/marginfi-v2-ui/src/pages/points.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useMemo, FC, useEffect, useState } from "react";
import React, { useMemo, FC, useEffect, useState, useCallback } from "react";
import Link from "next/link";
import { Button } from "@mui/material";
import FileCopyIcon from "@mui/icons-material/FileCopy";
import CheckIcon from "@mui/icons-material/Check";
import { useRouter } from "next/router";
import { getFavoriteDomain } from "@bonfida/spl-name-service";
import { Connection, PublicKey } from "@solana/web3.js";
Expand Down Expand Up @@ -34,12 +35,15 @@ const Points: FC = () => {

const currentUserId = useMemo(() => domain ?? currentFirebaseUser?.uid, [currentFirebaseUser, domain]);
const referralCode = useMemo(() => routerQuery.referralCode as string | undefined, [routerQuery.referralCode]);
const [isReferralCopied, setIsReferralCopied] = useState(false);

useEffect(() => {
if (connection && walletAddress) {
resolveDomain(connection, new PublicKey(walletAddress));
const handleReferralCopy = useCallback(() => {
if (userPointsData.referralLink) {
navigator.clipboard.writeText(`https://www.mfi.gg/refer/${userPointsData.referralLink}`);
setIsReferralCopied(true);
setTimeout(() => setIsReferralCopied(false), 2000);
}
}, [connection, walletAddress]);
}, [userPointsData.referralLink]);

const resolveDomain = async (connection: Connection, user: PublicKey) => {
try {
Expand All @@ -50,6 +54,12 @@ const Points: FC = () => {
}
};

useEffect(() => {
if (connection && walletAddress) {
resolveDomain(connection, new PublicKey(walletAddress));
}
}, [connection, walletAddress]);

useEffect(() => {
fetchLeaderboardData(connection).then(setLeaderboardData); // TODO: cache leaderboard and avoid call
}, [connection, connected, walletAddress]); // Dependency array to re-fetch when these variables change
Expand Down Expand Up @@ -98,18 +108,16 @@ const Points: FC = () => {
color: "black",
zIndex: 10,
}}
onClick={() => {
if (userPointsData.referralLink) {
navigator.clipboard.writeText(userPointsData.referralLink);
}
}}
onClick={handleReferralCopy}
>
{`${
userPointsData.isCustomReferralLink
? userPointsData.referralLink?.replace("https://", "")
: "Copy referral link"
}`}
<FileCopyIcon />
{isReferralCopied
? "Link copied"
: `${
userPointsData.isCustomReferralLink
? userPointsData.referralLink?.replace("https://", "")
: "Copy referral link"
}`}
{isReferralCopied ? <CheckIcon /> : <FileCopyIcon />}
</Button>
)}
</div>
Expand Down