<script src="https://browser.sentry-cdn.com/5.4.1/bundle.js" crossorigin="anonymous"></script>
<!-- TODO: Replace with real tracing integration once it's fixed -->
<script src="/static/tracing.js" crossorigin="anonymous"></script>

<script>



Sentry.init({
    dsn: "{{ sentry_dsn }}",
    integrations: [
        new Sentry.Integrations.Tracing({ tracingOrigins: ['']})
    ],
    debug: true
});

window.setTimeout(function() {
    const scope = Sentry.getCurrentHub().getScope();
    // TODO: Wait for Daniel's traceparent API
    scope.setSpan(scope.getSpan().constructor.fromTraceparent(
        "00-{{ traceparent['sentry-trace'].strip("-") }}-00"
    ));
});

async function compute() {
    const res = await fetch(
        "/compute/" +
        document.getElementsByName('b64str')[0].value
    );

    const token = await res.text();
    wait(token);

    return false;
}

async function wait(token) {
    const res = await fetch("/wait/" + token);
    const line = await res.text();
    document.getElementById('output').innerHTML += line;
    document.getElementById('output').innerHTML += '<br>';

    if(line == "NONE") {
        window.setTimeout(function() { wait(token) }, 500);
    }
}

</script>


<p>Decode your base64 string as a service (that calls another service)</p>

<input value='aGVsbG8gd29ybGQK' name=b64str> A base64 string<br>
<input type=button value=submit onclick="compute()">

<p>Output:</p>
<pre id=output />
