Skip to content

Commit 698a172

Browse files
stephanie-andersonmydeavivianyentran
authored
Add note to sign in in order to fully benefit from code samples (#7318)
--------- Co-authored-by: Francesco Novy <francesco.novy@sentry.io> Co-authored-by: vivianyentran <20403606+vivianyentran@users.noreply.github.com>
1 parent 5472580 commit 698a172

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

src/components/codeContext.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ type ProjectCodeKeywords = {
1616
title: string;
1717
};
1818

19+
type UserCodeKeywords = {
20+
ID: number;
21+
NAME: string;
22+
};
23+
1924
type CodeKeywords = {
2025
PROJECT: ProjectCodeKeywords[];
26+
USER: UserCodeKeywords | undefined;
2127
};
2228

2329
type Dsn = {
@@ -31,13 +37,20 @@ type Dsn = {
3137
type ProjectApiResult = {
3238
dsn: string;
3339
dsnPublic: string;
34-
id: string;
35-
organizationId: string;
40+
id: number;
41+
organizationId: number;
3642
organizationSlug: string;
3743
projectSlug: string;
3844
slug: string;
3945
};
4046

47+
type UserApiResult = {
48+
avatarUrl: string;
49+
id: number;
50+
isAuthenticated: boolean;
51+
name: string;
52+
};
53+
4154
// only fetch them once
4255
let cachedCodeKeywords = null;
4356

@@ -60,6 +73,7 @@ const DEFAULTS: CodeKeywords = {
6073
title: `example-org / example-project`,
6174
},
6275
],
76+
USER: undefined,
6377
};
6478

6579
type CodeContextType = {
@@ -99,8 +113,8 @@ const formatApiUrl = ({scheme, host}: Dsn) => {
99113
/**
100114
* Fetch project details from sentry
101115
*/
102-
export async function fetchCodeKeywords() {
103-
let json: {projects: ProjectApiResult[]} | null = null;
116+
export async function fetchCodeKeywords(): Promise<CodeKeywords> {
117+
let json: {projects: ProjectApiResult[]; user: UserApiResult} | null = null;
104118

105119
const url =
106120
process.env.NODE_ENV === 'development'
@@ -125,7 +139,7 @@ export async function fetchCodeKeywords() {
125139
return makeDefaults();
126140
}
127141

128-
const {projects} = json;
142+
const {projects, user} = json;
129143

130144
if (projects?.length === 0) {
131145
return makeDefaults();
@@ -150,6 +164,12 @@ export async function fetchCodeKeywords() {
150164
title: `${project.organizationSlug} / ${project.projectSlug}`,
151165
};
152166
}),
167+
USER: user.isAuthenticated
168+
? {
169+
ID: user.id,
170+
NAME: user.name,
171+
}
172+
: undefined,
153173
};
154174
}
155175

src/components/markdown.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {PlatformSection} from './platformSection';
2727
import {RelayMetrics} from './relayMetrics';
2828
import {PiiFields} from './relayPiifields';
2929
import {SandboxLink, SandboxOnly} from './sandboxLink';
30+
import {SignInNote} from './signInNote';
3031
import {SmartLink} from './smartLink';
3132
import {VimeoEmbed, YouTubeEmbed} from './video';
3233

@@ -59,6 +60,7 @@ const mdxComponents = {
5960
YouTubeEmbed,
6061
SandboxLink,
6162
SandboxOnly,
63+
SignInNote,
6264
};
6365

6466
export function Markdown({value}) {

src/components/signInNote.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, {useContext} from 'react';
2+
import {useLocation} from '@reach/router';
3+
import {graphql, StaticQuery} from 'gatsby';
4+
5+
import {CodeContext} from './codeContext';
6+
import {ExternalLink} from './externalLink';
7+
import {Note} from './note';
8+
9+
const siteMetaQuery = graphql`
10+
query SignInNoteQuery {
11+
site {
12+
siteMetadata {
13+
siteUrl
14+
}
15+
}
16+
}
17+
`;
18+
19+
export function SignInNote(): JSX.Element {
20+
const location = useLocation();
21+
22+
const {codeKeywords} = useContext(CodeContext);
23+
24+
const user = codeKeywords.USER;
25+
26+
// This means the user is signed in
27+
if (user) {
28+
return null;
29+
}
30+
31+
return (
32+
<StaticQuery
33+
query={siteMetaQuery}
34+
render={data => {
35+
const url = data.site.siteMetadata.siteUrl + location.pathname;
36+
return (
37+
<Note>
38+
The following code sample will let you choose your personal config from the
39+
dropdown, once you're{' '}
40+
<ExternalLink
41+
href={`https://sentry.io/auth/login/?next=${url}`}
42+
target="_blank"
43+
>
44+
logged in
45+
</ExternalLink>
46+
.
47+
</Note>
48+
);
49+
}}
50+
/>
51+
);
52+
}

src/platform-includes/getting-started-install/javascript.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
In order to get started using the Sentry JavaScript SDK, add the following code to the top of your application, before all other scripts:
22

3+
<SignInNote />
4+
35
```html
46
<script
57
src="https://js.sentry-cdn.com/___PUBLIC_KEY___.min.js"

0 commit comments

Comments
 (0)