SampleViewer / gate.html
faradayfuture's picture
Scene Sample + sign-in page: back link and top bar match the other viewers ("← all datasets")
0771c95 verified
Raw History Blame Contribute Delete
6.98 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sample Viewer Access</title>
<style>
:root{color-scheme:dark}*{box-sizing:border-box}
body{margin:0;background:#0b0d12;color:#e6e8ee;font:15px/1.55 -apple-system,"PingFang SC","Helvetica Neue",Arial,sans-serif}
a{color:#7fb2ff}
.wrap{max-width:760px;margin:0 auto;padding:28px 16px 64px}
#top{height:56px;display:flex;align-items:center;gap:16px;padding:0 22px;border-bottom:1px solid #232733}
#top h1{font-size:18px;margin:0;white-space:nowrap}
#top .back{color:#6f92c8;text-decoration:none;font-size:13px;white-space:nowrap}
#top .back:hover{color:#9fc4ff}
#top .sub{color:#9aa1ad;font-size:12px;margin:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
@media (max-width:600px){#top{gap:10px;padding:0 14px}#top .sub{display:none}}
.sub{color:#9aa1ad;font-size:14px;margin:0 0 18px}
.gate{background:#12151c;border:1px solid #232733;border-radius:12px;padding:22px}
.gate p{margin:0 0 12px}
.btn{display:inline-block;background:#1b2740;color:#7fb2ff;border:1px solid #2c3d63;border-radius:8px;padding:9px 16px;font:inherit;cursor:pointer;text-decoration:none;margin:4px 8px 4px 0}
.btn:hover{border-color:#7fb2ff}
.who{font-size:13px;color:#9aa1ad}.who button{background:none;border:0;color:#7fb2ff;cursor:pointer;font:inherit;padding:0}
code{font:13px ui-monospace,Menlo,monospace;background:#1b2030;padding:1px 5px;border-radius:4px}
</style>
</head>
<body>
<div id="top"><a class="back" href="index.html">&larr; all datasets</a><h1 id="title">Data sample viewer</h1><span class="sub">access</span></div>
<div class="wrap">
<p class="sub">These previews are available to approved Hugging Face accounts.</p>
<div class="gate" id="gate"><p>Checking access…</p></div>
</div>
<script type="module">
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@2.11.2/+esm";
const SECTIONS = {
egoSample: { repo: 'faradayfuture/egoSample', name: 'Ego Sample' },
teleopSample: { repo: 'faradayfuture/teleopSample', name: 'Teleop Sample' },
motionSample: { repo: 'faradayfuture/motionSample', name: 'Motion Sample' },
};
const KEYS = ['sv_oauth', 'sceneSample_oauth'];
const SELF = location.origin + location.pathname;
const inFrame = window.top !== window.self;
const gate = document.getElementById('gate');
const qs = new URLSearchParams(location.search);
let to = qs.get('to');
if (to) { if (location.hash && !to.includes('#')) to += location.hash; try { localStorage.setItem('sv_next', to); } catch (e) {} }
else { try { to = localStorage.getItem('sv_next'); } catch (e) {} }
if (!to || !/^\/(egoSample|teleopSample|motionSample)(\/|$)/.test(to.split('#')[0])) to = '/egoSample/index.html';
const sec = to.split('/')[1];
const S = SECTIONS[sec];
const DATASET = 'https://huggingface.co/datasets/' + S.repo;
document.getElementById('title').textContent = S.name + ' viewer';
document.title = S.name + ' · access';
const show = html => { gate.innerHTML = html; };
function session() {
for (const k of KEYS) {
try { const s = JSON.parse(localStorage.getItem(k) || 'null');
if (s && s.accessToken && (!s.accessTokenExpiresAt || new Date(s.accessTokenExpiresAt) > new Date())) return s; } catch (e) {}
}
return null;
}
function save(s) { for (const k of KEYS) { try { localStorage.setItem(k, JSON.stringify(s)); } catch (e) {} } }
function clear() { for (const k of KEYS) { try { localStorage.removeItem(k); } catch (e) {} } }
async function worker() {
if (!('serviceWorker' in navigator)) throw new Error('This browser does not support service workers.');
const reg = await navigator.serviceWorker.register('/sw.js', { scope: '/' });
await navigator.serviceWorker.ready;
if (!navigator.serviceWorker.controller)
await new Promise(r => { navigator.serviceWorker.addEventListener('controllerchange', r, { once: true }); setTimeout(r, 3000); });
return reg;
}
async function sendToken(reg, tokenValue) {
const sw = reg.active || navigator.serviceWorker.controller;
await new Promise(r => {
const on = ev => { if (ev.data && ev.data.type === 'token-ok') { navigator.serviceWorker.removeEventListener('message', on); r(); } };
navigator.serviceWorker.addEventListener('message', on);
sw.postMessage({ type: 'token', token: tokenValue });
setTimeout(r, 2000);
});
}
function who(s) {
const n = (s.userInfo && (s.userInfo.preferred_username || s.userInfo.name)) || 'signed in';
return `<p class="who">Signed in as <b>${n}</b> · <button id="signout">Sign out</button></p>`;
}
function wireSignOut(reg) {
const b = document.getElementById('signout');
if (b) b.onclick = async () => { clear(); if (reg) await sendToken(reg, null); location.href = SELF + '?to=' + encodeURIComponent(to); };
}
async function signIn() {
const vars = (window.huggingface && window.huggingface.variables) || {};
location.href = (await oauthLoginUrl({ scopes: 'openid profile gated-repos', redirectUrl: SELF, clientId: vars.OAUTH_CLIENT_ID })) + '&prompt=consent';
}
function askLogin() {
if (inFrame) {
show(`<p>Sign-in opens in a new tab.</p><a class="btn" href="${SELF}?to=${encodeURIComponent(to)}" target="_blank" rel="noopener">Open in a new tab to sign in ↗</a>`);
return;
}
show(`<p>Sign in with Hugging Face. If your account has not been approved yet, request access on the dataset page first.</p>
<button class="btn" id="signin">Sign in with Hugging Face</button>
<a class="btn" href="${DATASET}" target="_blank" rel="noopener">Request access ↗</a>`);
document.getElementById('signin').onclick = signIn;
}
async function main() {
let s = session();
if (!s) {
try { const r = await oauthHandleRedirectIfPresent(); if (r) { save(r); s = r; history.replaceState(null, '', SELF); } } catch (e) { console.warn(e); }
}
if (!s) return askLogin();
let reg;
try { reg = await worker(); await sendToken(reg, s.accessToken); }
catch (e) { show(`<p>${e.message}</p><p>Please open this page in a current version of Chrome, Edge, Safari or Firefox.</p>`); return; }
const r = await fetch(`https://huggingface.co/datasets/${S.repo}/resolve/main/viewer/${sec}/index.html`, { headers: { Authorization: 'Bearer ' + s.accessToken }, cache: 'no-store' }).catch(() => null);
if (r && r.ok) { try { localStorage.removeItem('sv_next'); } catch (e) {} location.replace(to); return; }
if (r && r.status === 401) { clear(); await sendToken(reg, null); return askLogin(); }
show(who(s) + `<p>Your account does not have access to the ${S.name} yet. Request access on the dataset page; once it is approved, reload this page.</p>
<a class="btn" href="${DATASET}" target="_blank" rel="noopener">Request access ↗</a>
<button class="btn" id="retry">Reload</button>`);
wireSignOut(reg);
document.getElementById('retry').onclick = () => location.reload();
}
main();
</script>
</body>
</html>