diff --git a/src/components/Modal/ModalConfirmConnect/ModalConfirmConnect.tsx b/src/components/Modal/ModalConfirmConnect/ModalConfirmConnect.tsx index 19facda..ee4446b 100644 --- a/src/components/Modal/ModalConfirmConnect/ModalConfirmConnect.tsx +++ b/src/components/Modal/ModalConfirmConnect/ModalConfirmConnect.tsx @@ -1,7 +1,7 @@ import { useModalSearchParams } from '@/hooks/useModalSearchParams' import { Modal } from '@/shared/Modal/Modal' import { MODAL_PARAMS_KEYS } from '@/types/modal' -import { askNotificationPermission, call, getAppIconTitle, getDomain, getShortenNpub } from '@/utils/helpers/helpers' +import { askNotificationPermission, call, getAppIconTitle, getDomain, getReferrerAppUrl, getShortenNpub } from '@/utils/helpers/helpers' import { Avatar, Box, Stack, Typography } from '@mui/material' import { useNavigate, useParams, useSearchParams } from 'react-router-dom' import { useAppSelector } from '@/store/hooks/redux' @@ -37,15 +37,7 @@ export const ModalConfirmConnect = () => { const triggerApp = apps.find((app) => app.appNpub === appNpub) const { name, url = '', icon = '' } = triggerApp || {} - let appUrl = url || searchParams.get('appUrl') || '' - console.log('referrer', window.document.referrer, appUrl) - if (!appUrl && window.document.referrer) { - try { - const u = new URL(window.document.referrer) - appUrl = u.origin - } catch {} - } - + const appUrl = url || searchParams.get('appUrl') || getReferrerAppUrl(); const appDomain = getDomain(appUrl) const appName = name || appDomain || getShortenNpub(appNpub) const appAvatarTitle = getAppIconTitle(name || appDomain, appNpub) diff --git a/src/pages/CreatePage/Create.Page.tsx b/src/pages/CreatePage/Create.Page.tsx index 08b7010..e0f75db 100644 --- a/src/pages/CreatePage/Create.Page.tsx +++ b/src/pages/CreatePage/Create.Page.tsx @@ -8,6 +8,7 @@ import { ModalConfirmConnect } from '@/components/Modal/ModalConfirmConnect/Moda import { useModalSearchParams } from '@/hooks/useModalSearchParams' import { MODAL_PARAMS_KEYS } from '@/types/modal' import { useState } from 'react' +import { getReferrerAppUrl } from '@/utils/helpers/helpers' const CreatePage = () => { const notify = useEnqueueSnackbar() @@ -32,13 +33,7 @@ const CreatePage = () => { try { const key: any = await swicCall('generateKey', name) - let appUrl = '' - if (window.document.referrer) { - try { - const u = new URL(window.document.referrer) - appUrl = u.origin - } catch {} - } + const appUrl = getReferrerAppUrl(); console.log('Created', key.npub, 'app', appUrl) setCreated(true) diff --git a/src/utils/helpers/helpers.ts b/src/utils/helpers/helpers.ts index 72fed38..0c31a5a 100644 --- a/src/utils/helpers/helpers.ts +++ b/src/utils/helpers/helpers.ts @@ -1,5 +1,5 @@ import { nip19 } from 'nostr-tools' -import { ACTIONS, ACTION_TYPE, NIP46_RELAYS } from '../consts' +import { ACTIONS, ACTION_TYPE, DOMAIN, NIP46_RELAYS } from '../consts' import { DbPending, DbPerm } from '@/modules/db' import { MetaEvent } from '@/types/meta-event' @@ -105,6 +105,17 @@ export const getDomain = (url: string) => { } } +export const getReferrerAppUrl = () => { + console.log('referrer', window.document.referrer) + if (!window.document.referrer) return '' + try { + const u = new URL(window.document.referrer.toLocaleLowerCase()) + if (u.hostname != DOMAIN && !u.hostname.endsWith("."+DOMAIN)) + return u.origin + } catch {} + return '' +} + export const getAppIconTitle = (name: string | undefined, appNpub: string) => { return name ? name[0].toLocaleUpperCase() : appNpub.substring(4, 7) }