diff --git a/browse/static/js/catalyzex.js b/browse/static/js/catalyzex.js index fa47bc2c3..1889ded13 100644 --- a/browse/static/js/catalyzex.js +++ b/browse/static/js/catalyzex.js @@ -4,6 +4,17 @@ const paperUrl = window.location.href.split('?')[0]; const $output = $("#catalyzex-output"); + const url = new URL(location.href); + let cxToken = url.searchParams.get('cx_token'); + + if(cxToken) { + localStorage.setItem('@cx/token', cxToken) + url.searchParams.delete('cx_token') + window.history.replaceState({}, document.title, url.href); + } else { + cxToken = localStorage.getItem('@cx/token') + } + if ($output.html() != "") { // Toggled off $output.html(""); @@ -19,14 +30,27 @@ } const fetchCatalyzeXCode = async () => { - const cxApiUrl = `https://www.catalyzex.com/api/code?src=arxiv&paper_arxiv_id=${arxivId}`; + const cxApiUrl = new URL("https://www.catalyzex.com/api/code") + const queryParams = { + src: 'arxiv', + paper_arxiv_id: arxivId, + paper_url: paperUrl, + paper_title: paperTitle + } - let result = {}; + Object.entries(queryParams).forEach(([key, val]) => { + cxApiUrl.searchParams.set(key, val); + }) try { - result = await $.ajax({ url: cxApiUrl, timeout: 2000, dataType: "json" }); + result = await $.ajax({ + url: cxApiUrl, + timeout: 2000, + dataType: "json", + headers: cxToken ? { 'Authorization': `Bearer ${cxToken}`} : undefined + }); } catch (error) { - result = {}; + result = error?.responseJSON || {}; } return result; @@ -34,8 +58,7 @@ $output.html(''); - const { count: implementations, cx_url: cxImplementationsUrl } = await fetchCatalyzeXCode() - + const { count: implementations, cx_url: cxImplementationsUrl, is_alert_active: isAlertActive, authed_user_id: authedUserId } = await fetchCatalyzeXCode() $output.append("
No code found for this paper just yet.
`) } - $output.append(`If you have code to share with the arXiv community, please ${submitItHereLink} to benefit all researchers & engineers.
`) + $output.append(window.DOMPurify.sanitize(`If you have code to share with the arXiv community, please ${submitItHereLink} to benefit all researchers & engineers.
`)) + if(isAlertActive) { + $output.append(window.DOMPurify.sanitize(` +You've set up an alert for this paper, and we'll notify you once new code becomes available. Manage all your alerts here. 🚀 + `)) + } else { + const createAlertUrl = new URL("https://www.catalyzex.com/alerts/code/create"); + const queryParams = { + paper_arxiv_id: arxivId, + paper_url: paperUrl, + paper_title: paperTitle, + redirect_url: paperUrl + } + Object.entries(queryParams).forEach(([key, val]) => { + createAlertUrl.searchParams.set(key, val); + }) + $output.append(window.DOMPurify.sanitize(`
Create an alert to get notified when new code is available for this paper.
`)) + } })(); \ No newline at end of file