Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 13 additions & 5 deletions src/components/trade/header/user-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PlusCircleIcon,
SignOutIcon,
SpinnerGapIcon,
TrophyIcon,
WalletIcon,
} from "@phosphor-icons/react";
import { useLogin, useLogout, usePrivy } from "@privy-io/react-auth";
Expand All @@ -22,6 +23,7 @@ import {
import { useCopyToClipboard } from "@/hooks/ui/use-copy-to-clipboard";
import { useAutoRegisterReferral } from "@/hooks/use-referral";
import { shortenAddress } from "@/lib/format";
import { usePointsModalActions } from "@/stores/use-global-modal-store";

function CopyAddressMenuItem({ address }: { address: string }) {
const { copied, copy } = useCopyToClipboard();
Expand Down Expand Up @@ -50,22 +52,22 @@ export function UserMenu() {
const { authenticated, ready } = usePrivy();
const { login } = useLogin();
const { logout } = useLogout();
const { disconnect } = useDisconnect();
const { mutate: disconnect } = useDisconnect();
const { data: ensName } = useEnsName({ address });
const [mounted, setMounted] = useState(false);

useAutoRegisterReferral();
const { open: openPointsModal } = usePointsModalActions();

useEffect(() => {
setMounted(true);
}, []);

function handleLogout() {
async function handleLogout() {
if (authenticated) {
logout();
} else {
disconnect();
await logout();
}
disconnect();
}

if (!mounted || !ready || isConnecting) {
Expand Down Expand Up @@ -104,6 +106,12 @@ export function UserMenu() {
<Trans>Add funds</Trans>
</span>
</DropdownMenuItem>
<DropdownMenuItem className="flex items-center gap-2" onClick={openPointsModal}>
<TrophyIcon className="size-3.5 text-text-600" />
<span>
<Trans>Referrals</Trans>
</span>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive" className="flex items-center gap-2" onClick={handleLogout}>
<SignOutIcon className="size-3.5" />
Expand Down
14 changes: 8 additions & 6 deletions src/hooks/use-referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ export function useReferralCapture() {

export function useAutoRegisterReferral() {
const { address, isConnected } = useConnection();
const { user } = usePrivy();
const registeredRef = useRef(false);
const { user, authenticated } = usePrivy();
const registeredAddressRef = useRef<string | null>(null);

useEffect(() => {
async function autoRegister() {
if (!isConnected || !address || registeredRef.current) return;
if (!isConnected || !authenticated || !address) return;
if (authenticated && !user?.id) return;
if (registeredAddressRef.current === address) return;

registeredRef.current = true;
registeredAddressRef.current = address;
Comment on lines +37 to +45
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation only tracks the wallet address to prevent redundant registration calls. However, if a user switches their Privy account while using the same wallet address, the registration check will be skipped. Additionally, it's safer to check for the presence of API_URL before attempting to fetch. Using a composite key of address and user ID in the ref provides better tracking.

Suggested change
const registeredAddressRef = useRef<string | null>(null);
useEffect(() => {
async function autoRegister() {
if (!isConnected || !address || registeredRef.current) return;
if (!isConnected || !authenticated || !address) return;
if (authenticated && !user?.id) return;
if (registeredAddressRef.current === address) return;
registeredRef.current = true;
registeredAddressRef.current = address;
const registeredAddressRef = useRef<string | null>(null);
useEffect(() => {
async function autoRegister() {
if (!isConnected || !authenticated || !address || !user?.id || !API_URL) return;
const key = address + "-" + user.id;
if (registeredAddressRef.current === key) return;
registeredAddressRef.current = key;


try {
const res = await fetch(`${API_URL}/user_points?user_address=${address}`);
Expand All @@ -67,12 +69,12 @@ export function useAutoRegisterReferral() {
}
} catch (error) {
console.error("Auto-registration failed:", error);
registeredRef.current = false;
registeredAddressRef.current = null;
}
}

autoRegister();
}, [isConnected, address, user?.id]);
}, [isConnected, authenticated, address, user?.id]);
}

export { getStoredReferral };
4 changes: 4 additions & 0 deletions src/locales/ar/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,10 @@ msgstr ""
msgid "Referral Code (optional)"
msgstr ""

#: src/components/trade/header/user-menu.tsx
msgid "Referrals"
msgstr ""

#: src/components/pages/builder-page.tsx
#~ msgid "Register Another"
#~ msgstr ""
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,10 @@ msgstr "Refer friends"
msgid "Referral Code (optional)"
msgstr "Referral Code (optional)"

#: src/components/trade/header/user-menu.tsx
msgid "Referrals"
msgstr "Referrals"

#: src/components/pages/builder-page.tsx
#~ msgid "Register Another"
#~ msgstr "Register Another"
Expand Down
4 changes: 4 additions & 0 deletions src/locales/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,10 @@ msgstr ""
msgid "Referral Code (optional)"
msgstr ""

#: src/components/trade/header/user-menu.tsx
msgid "Referrals"
msgstr ""

#: src/components/pages/builder-page.tsx
#~ msgid "Register Another"
#~ msgstr ""
Expand Down
4 changes: 4 additions & 0 deletions src/locales/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,10 @@ msgstr ""
msgid "Referral Code (optional)"
msgstr ""

#: src/components/trade/header/user-menu.tsx
msgid "Referrals"
msgstr ""

#: src/components/pages/builder-page.tsx
#~ msgid "Register Another"
#~ msgstr ""
Expand Down
4 changes: 4 additions & 0 deletions src/locales/hi/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,10 @@ msgstr ""
msgid "Referral Code (optional)"
msgstr ""

#: src/components/trade/header/user-menu.tsx
msgid "Referrals"
msgstr ""

#: src/components/pages/builder-page.tsx
#~ msgid "Register Another"
#~ msgstr ""
Expand Down
4 changes: 4 additions & 0 deletions src/locales/zh/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,10 @@ msgstr ""
msgid "Referral Code (optional)"
msgstr ""

#: src/components/trade/header/user-menu.tsx
msgid "Referrals"
msgstr ""

#: src/components/pages/builder-page.tsx
#~ msgid "Register Another"
#~ msgstr ""
Expand Down
Loading