Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
chore: Remove DSN loader from develop docs
Browse files Browse the repository at this point in the history
The DSN loader has never worked in develop docs as develop.sentry.dev is
not in the allowlist for CORS domains.

DSN substitution is infrequently used in develop docs and not having it
is simpler than fixing and maintaining it.
  • Loading branch information
markstory committed Nov 14, 2023
1 parent 4aa7421 commit 83ee0dc
Showing 1 changed file with 3 additions and 117 deletions.
120 changes: 3 additions & 117 deletions src/components/codeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useEffect} from 'react';
import React, {useState} from 'react';

type ProjectCodeKeywords = {
DSN: string;
Expand All @@ -20,27 +20,6 @@ type CodeKeywords = {
PROJECT: ProjectCodeKeywords[];
};

type Dsn = {
scheme: string;
publicKey: string;
secretKey?: string;
host: string;
pathname: string;
};

type ProjectApiResult = {
dsn: string;
dsnPublic: string;
id: string;
slug: string;
organizationId: string;
organizationSlug: string;
projectSlug: string;
};

// only fetch them once
let cachedCodeKeywords = null;

const DEFAULTS: CodeKeywords = {
PROJECT: [
{
Expand Down Expand Up @@ -69,104 +48,11 @@ type CodeContextType = {

const CodeContext = React.createContext<CodeContextType | null>(null);

const parseDsn = function (dsn: string): Dsn {
const match = dsn.match(/^(.*?\/\/)(.*?):(.*?)@(.*?)(\/.*?)$/);

return {
scheme: match[1],
publicKey: escape(match[2]),
secretKey: escape(match[3]),
host: escape(match[4]),
pathname: escape(match[5]),
};
};

const formatMinidumpURL = ({scheme, host, pathname, publicKey}: Dsn) => {
return `${scheme}${host}/api${pathname}/minidump/?sentry_key=${publicKey}`;
};

const formatUnrealEngineURL = ({scheme, host, pathname, publicKey}: Dsn) => {
return `${scheme}${host}/api${pathname}/unreal/${publicKey}/`;
};

const formatApiUrl = ({scheme, host}: Dsn) => {
const apiHost = host.indexOf('.ingest.') >= 0 ? host.split('.ingest.')[1] : host;

return `${scheme}${apiHost}/api`;
};

export function fetchCodeKeywords() {
return new Promise(resolve => {
function transformResults(projects: ProjectApiResult[]) {
if (projects.length === 0) {
console.warn('Unable to fetch codeContext - using defaults.');
resolve(DEFAULTS);
} else {
resolve({
PROJECT: projects.map(project => {
const parsedDsn = parseDsn(project.dsn);
return {
DSN: project.dsn,
PUBLIC_DSN: project.dsnPublic,
PUBLIC_KEY: parsedDsn.publicKey,
SECRET_KEY: parsedDsn.secretKey,
API_URL: formatApiUrl(parsedDsn),
PROJECT_ID: project.id,
PROJECT_SLUG: project.projectSlug,
ORG_ID: project.organizationId,
ORG_SLUG: project.organizationSlug,
ORG_INGEST_DOMAIN: `o${project.organizationId}.ingest.sentry.io`,
MINIDUMP_URL: formatMinidumpURL(parsedDsn),
UNREAL_URL: formatUnrealEngineURL(parsedDsn),
title: `${project.organizationSlug} / ${project.projectSlug}`,
};
}),
});
}
}

const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://sentry.io/docs/api/user/');
xhr.withCredentials = true;
xhr.responseType = 'json';
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 0) {
transformResults([]);
} else {
const {
projects,
}: {
projects: ProjectApiResult[];
} = xhr.response;
transformResults(projects);
}
}
};
xhr.send(null);
});
}

export default CodeContext;

export function useCodeContextState(fetcher = fetchCodeKeywords) {
let [codeKeywords, setCodeKeywords] = useState(DEFAULTS);
if (codeKeywords === null && cachedCodeKeywords !== null) {
setCodeKeywords(cachedCodeKeywords);
codeKeywords = cachedCodeKeywords;
}

useEffect(() => {
if (cachedCodeKeywords === null) {
fetcher().then((config: CodeKeywords) => {
cachedCodeKeywords = config;
setCodeKeywords(config);
});
}
});

export function useCodeContextState() {
return {
codeKeywords,
codeKeywords: DEFAULTS,
sharedCodeSelection: useState(null),
sharedKeywordSelection: useState({}),
};
Expand Down

0 comments on commit 83ee0dc

Please sign in to comment.