From cee76edac9784ae082d5fbbd0b34554a7665f5b9 Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Wed, 6 Mar 2024 01:09:37 +0700 Subject: [PATCH] Use ref from url if user not logged in --- src/components/referral/ReferralUrlChanger.tsx | 4 ++-- src/components/utils/index.tsx | 16 ---------------- src/utils/url.ts | 7 +++++++ 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/components/referral/ReferralUrlChanger.tsx b/src/components/referral/ReferralUrlChanger.tsx index 568ba5506..5ceed8a56 100644 --- a/src/components/referral/ReferralUrlChanger.tsx +++ b/src/components/referral/ReferralUrlChanger.tsx @@ -1,13 +1,13 @@ import { useRouter } from 'next/router' import { useEffect } from 'react' import { useSelectProfileSpace } from 'src/rtk/app/hooks' -import { getCurrentSearchParams, getCurrentUrlWithoutQuery } from 'src/utils/url' +import { getCurrentSearchParams, getCurrentUrlWithoutQuery, getUrlQuery } from 'src/utils/url' import { useMyAddress } from '../auth/MyAccountsContext' export function useReferralId() { const myAddress = useMyAddress() const profileSpace = useSelectProfileSpace(myAddress) - return profileSpace?.spaceId ?? '' + return profileSpace?.spaceId || getUrlQuery('ref') } export function ReferralUrlChanger() { diff --git a/src/components/utils/index.tsx b/src/components/utils/index.tsx index 1deb72151..43c56802e 100644 --- a/src/components/utils/index.tsx +++ b/src/components/utils/index.tsx @@ -8,7 +8,6 @@ import dayjs from 'dayjs' import { CID } from 'ipfs-http-client' import isbot from 'isbot' import NextError from 'next/error' -import queryString from 'query-string' import React, { useEffect, useMemo } from 'react' import { IconBaseProps } from 'react-icons' import config from 'src/config' @@ -34,21 +33,6 @@ export const offchainApi = { ...offchain } export const ZERO = new BN(0) export const ONE = new BN(1) -// Substrate/Polkadot API utils -// -------------------------------------- - -// Parse URLs -// -------------------------------------- - -export function getUrlParam( - location: Location, - paramName: string, - deflt?: string, -): string | undefined { - const params = queryString.parse(location.search) - return params[paramName] ? (params[paramName] as string) : deflt -} - // Next.js utils // -------------------------------------- diff --git a/src/utils/url.ts b/src/utils/url.ts index 1b05288b8..a93b61151 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -28,3 +28,10 @@ export function getCurrentUrlOrigin() { if (typeof window === 'undefined') return '' return window.location.origin } + +export function getUrlQuery(queryName: string) { + if (isServerSide()) return '' + const query = window.location.search + const searchParams = new URLSearchParams(query) + return searchParams.get(queryName) ?? '' +}