forked from getsentry/sentry-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.js
160 lines (139 loc) · 4.43 KB
/
User.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const ORIGIN = process.env.JEKYLL_GETSENTRY_ORIGIN || 'https://sentry.io';
import { escape } from './Helpers';
import { logPageview } from './Page';
export default class User {
constructor() {
this.namespace = 'docsUserDataV1';
this.init = this.init.bind(this);
this.update = this.update.bind(this);
this.setUserData = this.setUserData.bind(this);
this.getUserData = this.getUserData.bind(this);
}
update() {
$(document).trigger('user.didUpdate', [this.getUserData()]);
}
setUserData(newData = {}) {
let userData = {};
try {
userData = JSON.parse(localStorage.getItem(this.namespace)) || {};
} catch (error) {
// Don't worry about errors if localStorage is corrupt, we'll just discard
// the object and start fresh.
}
const { projects: tmpProjects, preferred: tmpPref, user } = {
...userData,
...newData
};
const projects =
tmpProjects && tmpProjects.length ? tmpProjects : [defaultProject];
// Default to the first project
let preferred = projects[0];
if (tmpPref) {
// If a preference is set, check it still exists, otherwise still use
// the first project as the default.
preferred = projects.find(({ id }) => id === tmpPref.id) || preferred;
}
const payload = { projects, preferred, user: user || defaultUser };
localStorage.setItem(this.namespace, JSON.stringify(payload));
return payload;
}
getUserData() {
let data = null;
try {
data = JSON.parse(localStorage.getItem(this.namespace));
} catch (error) {
// If the parse failed, we'll just purge the data
}
const { preferred, projects, user } = data || this.setUserData();
return {
preferred: constructDSNObject(preferred),
projects: projects.map(constructDSNObject),
user
};
}
setPreference(newID) {
const { projects } = this.getUserData();
const preferred = projects.find(({ id }) => id === newID);
this.setUserData({ preferred });
this.update();
}
init() {
const cached = JSON.parse(localStorage.getItem(this.namespace));
if (cached) this.update(cached);
return $.ajax({
type: 'GET',
url: ORIGIN + '/docs/api/user/',
crossDomain: true,
xhrFields: {
withCredentials: true
}
})
.then(({ projects, user }) => {
window.ra.identify(user.id);
window.amplitude.getInstance().setUserId(user.id);
const { isAuthenticated } = user;
$('[data-hide-when-logged-in]').toggleClass('d-none', isAuthenticated);
this.setUserData({ projects, user });
this.update();
})
.catch(error => {
$('[data-hide-when-logged-in]').toggleClass('d-none', false);
this.update();
})
.then(() => {
logPageview();
});
}
}
const formatDsn = function(
{ publicKey, secretKey, scheme, host, pathSection },
opts = { public: false }
) {
const auth = opts.public ? publicKey : `${publicKey}:${secretKey}`;
return `${scheme}${auth}@${host}${pathSection}`;
};
const formatMinidumpURL = function(dsn) {
const { scheme, host, pathSection, publicKey } = dsn;
return `${scheme}${host}/api${pathSection}/minidump?sentry_key=${publicKey}`;
};
const formatAPIURL = function(dsn) {
return `${dsn.scheme}${dsn.host}/api`;
};
export const defaultUser = {
isAuthenticated: false
};
export const defaultProject = {
projectName: 'your-project',
secretKey: '<secret>',
publicKey: '<key>',
dsnPublic: 'https://<key>@sentry.io/<project>',
id: -1,
dsn: 'https://<key>:<secret>@sentry.io/<project>',
organizationSlug: 'your-org',
projectSlug: 'your-project'
};
export const constructDSNObject = function(project = {}) {
project = { ...defaultProject, ...project };
// Transform the dsn into useful values
const match = project.dsn.match(/^(.*?\/\/)(.*?):(.*?)@(.*?)(\/.*?)$/);
const dsn = {
scheme: escape(match[1]),
publicKey: escape(match[2]),
secretKey: `${escape(match[3])}`,
host: escape(match[4]),
pathSection: escape(match[5])
};
return {
id: project.id,
group: escape(project.organizationSlug),
PROJECT_NAME: escape(project.projectSlug),
PROJECT_ID: project.id.toString(),
ORG_NAME: escape(project.organizationSlug),
DSN: formatDsn(dsn),
PUBLIC_DSN: formatDsn(dsn, { public: true }),
PUBLIC_KEY: dsn.publicKey,
SECRET_KEY: dsn.secretKey,
API_URL: formatAPIURL(dsn),
MINIDUMP_URL: formatMinidumpURL(dsn)
};
};