diff --git a/src/components/codeContext.tsx b/src/components/codeContext.tsx index f5bee08b20..faea92d4c1 100644 --- a/src/components/codeContext.tsx +++ b/src/components/codeContext.tsx @@ -1,4 +1,4 @@ -import React, {useState, useEffect} from 'react'; +import React, {useState} from 'react'; type ProjectCodeKeywords = { DSN: string; @@ -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: [ { @@ -69,104 +48,11 @@ type CodeContextType = { const CodeContext = React.createContext(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({}), };