Skip to content

Commit 581aa45

Browse files
committed
chore: address code review
1 parent 3460e43 commit 581aa45

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

static/app/constants/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export const CUSTOMER_DOMAIN =
2121
? undefined
2222
: window?.__initialData?.customerDomain?.subdomain;
2323

24+
// Constant used for tracking referrer in session storage rather than
25+
// ?referrer=foo get parameter:
26+
export const CUSTOM_REFERRER_KEY = 'customReferrer';
27+
2428
// This is considered the "default" route/view that users should be taken
2529
// to when the application does not have any further context
2630
//

static/app/views/nav/orgDropdown.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ import {Button} from 'sentry/components/core/button';
99
import {DropdownMenu, type MenuItemProps} from 'sentry/components/dropdownMenu';
1010
import OrganizationBadge from 'sentry/components/idBadge/organizationBadge';
1111
import QuestionTooltip from 'sentry/components/questionTooltip';
12+
import {CUSTOM_REFERRER_KEY} from 'sentry/constants';
1213
import {IconAdd} from 'sentry/icons';
1314
import {t, tn} from 'sentry/locale';
1415
import ConfigStore from 'sentry/stores/configStore';
1516
import OrganizationsStore from 'sentry/stores/organizationsStore';
1617
import {useLegacyStore} from 'sentry/stores/useLegacyStore';
1718
import type {Organization} from 'sentry/types/organization';
1819
import {isDemoModeActive} from 'sentry/utils/demoMode';
19-
import localStorage from 'sentry/utils/localStorage';
2020
import {localizeDomain, resolveRoute} from 'sentry/utils/resolveRoute';
2121
import {useNavigate} from 'sentry/utils/useNavigate';
2222
import useOrganization from 'sentry/utils/useOrganization';
2323
import useProjects from 'sentry/utils/useProjects';
24+
import {useSessionStorage} from 'sentry/utils/useSessionStorage';
2425
import {useNavContext} from 'sentry/views/nav/context';
2526
import {NavLayout} from 'sentry/views/nav/types';
2627
import {makeProjectsPathname} from 'sentry/views/projects/pathname';
2728

29+
const ORG_DROPDOWN_REFERRER = 'org-dropdown';
30+
2831
function createOrganizationMenuItem(): MenuItemProps {
2932
const configFeatures = ConfigStore.get('features');
3033
const sentryUrl = localizeDomain(ConfigStore.get('links').sentryUrl);
@@ -64,6 +67,7 @@ export function OrgDropdown({
6467
const config = useLegacyStore(ConfigStore);
6568
const organization = useOrganization();
6669
const navigate = useNavigate();
70+
const [, setReferrer] = useSessionStorage<string | null>(CUSTOM_REFERRER_KEY, null);
6771

6872
// It's possible we do not have an org in context (e.g. RouteNotFound)
6973
// Otherwise, we should have the full org
@@ -148,7 +152,7 @@ export function OrgDropdown({
148152
key: 'projects',
149153
label: t('Projects'),
150154
onAction: () => {
151-
localStorage.setItem('customReferrer', 'org-dropdown');
155+
setReferrer(ORG_DROPDOWN_REFERRER);
152156
navigate(makeProjectsPathname({path: '/', organization}));
153157
},
154158
hidden: hideOrgLinks,

static/gsApp/utils/rawTrackAnalyticsEvent.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {OrganizationFixture} from 'sentry-fixture/organization';
33
import {SubscriptionFixture} from 'getsentry-test/fixtures/subscription';
44
import {setWindowLocation} from 'sentry-test/utils';
55

6+
import {CUSTOM_REFERRER_KEY} from 'sentry/constants';
67
import ConfigStore from 'sentry/stores/configStore';
78
import {uniqueId} from 'sentry/utils/guid';
8-
import localStorageWrapper from 'sentry/utils/localStorage';
99
import sessionStorage from 'sentry/utils/sessionStorage';
1010

1111
import rawTrackAnalyticsEvent from 'getsentry/utils/rawTrackAnalyticsEvent';
@@ -187,7 +187,7 @@ describe('rawTrackAnalyticsEvent', () => {
187187
});
188188

189189
it('sets custom_referrer if found in local storage', () => {
190-
localStorageWrapper.setItem('customReferrer', 'batman');
190+
sessionStorage.setItem(CUSTOM_REFERRER_KEY, 'batman');
191191
setWindowLocation('http:/localhost');
192192
rawTrackAnalyticsEvent({
193193
eventKey: 'test_event',
@@ -207,7 +207,7 @@ describe('rawTrackAnalyticsEvent', () => {
207207
{time: undefined}
208208
);
209209
setWindowLocation('http:/localhost/');
210-
expect(localStorageWrapper.getItem('customReferrer')).toBeNull();
210+
expect(sessionStorage.getItem(CUSTOM_REFERRER_KEY)).toBeNull();
211211
});
212212

213213
it('start analytics session', () => {

static/gsApp/utils/rawTrackAnalyticsEvent.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as qs from 'query-string';
22

3+
import {CUSTOM_REFERRER_KEY} from 'sentry/constants';
34
import ConfigStore from 'sentry/stores/configStore';
45
import type {Hooks} from 'sentry/types/hooks';
56
import type {Organization} from 'sentry/types/organization';
67
import type {User} from 'sentry/types/user';
78
import getDaysSinceDate from 'sentry/utils/getDaysSinceDate';
89
import {uniqueId} from 'sentry/utils/guid';
9-
import localStorage from 'sentry/utils/localStorage';
1010
import sessionStorage from 'sentry/utils/sessionStorage';
1111

1212
import type {Subscription} from 'getsentry/types';
@@ -71,20 +71,21 @@ const hasAnalyticsDebug = () => localStorage.getItem('DEBUG_ANALYTICS_GETSENTRY'
7171

7272
const getCustomReferrer = () => {
7373
try {
74-
// pull the referrer from local storage.
75-
if (
76-
localStorage.getItem('customReferrer') &&
77-
typeof localStorage.getItem('customReferrer') === 'string'
78-
) {
79-
const referrer = localStorage.getItem('customReferrer');
80-
localStorage.removeItem('customReferrer');
81-
return referrer;
82-
}
8374
// pull the referrer from the query parameter of the page
8475
const {referrer} = qs.parse(window.location.search) || {};
8576
if (referrer && typeof referrer === 'string') {
8677
return referrer;
8778
}
79+
80+
// pull the referrer from session storage.
81+
if (
82+
sessionStorage.getItem(CUSTOM_REFERRER_KEY) &&
83+
typeof sessionStorage.getItem(CUSTOM_REFERRER_KEY) === 'string'
84+
) {
85+
const sessionReferrer = sessionStorage.getItem(CUSTOM_REFERRER_KEY);
86+
sessionStorage.removeItem(CUSTOM_REFERRER_KEY);
87+
return sessionReferrer;
88+
}
8889
} catch {
8990
// ignore if this fails to parse
9091
// this can happen if we have an invalid query string

0 commit comments

Comments
 (0)