-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathauth-cta.js
More file actions
74 lines (68 loc) · 2.43 KB
/
Copy pathauth-cta.js
File metadata and controls
74 lines (68 loc) · 2.43 KB
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
/*
* The navbar CTA in docs.json is a static "Sign In" button. The docs are
* served same-origin under openrouter.ai, so Clerk's JS-readable
* `__client_uat` cookie (a sign-in timestamp, `0` or absent when signed
* out, possibly suffixed with a publishable-key hash) tells us whether
* the reader is signed in.
*
* Signed out: keep "Sign In" but carry the current docs page as Clerk's
* `redirect_url`, so signing in returns the reader to where they were.
* Signed in: repoint the CTA to "Home", which — like the signed-in "Home"
* link in the web app's own navbar — lands on the workspace list rather
* than the marketing homepage.
*
* Mintlify's navbar is React-rendered, so a MutationObserver reapplies
* the target whenever client-side navigation re-renders the button or
* changes the page the sign-in should return to, and the cookie is
* re-read when the tab regains focus so a sign-in or sign-out in another
* tab is reflected without a reload.
*/
const SIGN_IN_HREF = 'https://openrouter.ai/sign-in';
// Mirrors navbar.primary.label in docs.json.
const SIGN_IN_LABEL = 'Sign In';
const HOME_HREF = 'https://openrouter.ai/workspaces';
const HOME_LABEL = 'Home';
function isSignedIn() {
return document.cookie.split('; ').some((cookie) => {
const [name, value] = cookie.split('=');
return name?.startsWith('__client_uat') && value !== undefined && value !== '' && value !== '0';
});
}
function signInHref() {
const url = new URL(SIGN_IN_HREF);
url.searchParams.set('redirect_url', window.location.href);
return url.href;
}
function setLabel(link, text) {
const label = [...link.querySelectorAll('*')].findLast(
(el) => !(el instanceof SVGElement) && el.childElementCount === 0,
);
(label ?? link).textContent = text;
}
function applyCta() {
const cta = document.querySelector('#topbar-cta-button');
if (!cta) {
return;
}
const link = cta.matches('a') ? cta : cta.querySelector('a');
if (!link) {
return;
}
const signedIn = isSignedIn();
const href = signedIn ? HOME_HREF : signInHref();
if (link.getAttribute('href') === href) {
return;
}
link.setAttribute('href', href);
setLabel(link, signedIn ? HOME_LABEL : SIGN_IN_LABEL);
}
function initAuthCta() {
applyCta();
new MutationObserver(applyCta).observe(document.body, {
childList: true,
subtree: true,
});
window.addEventListener('focus', applyCta);
document.addEventListener('visibilitychange', applyCta);
}
initAuthCta();