import { CLOSE_ICON } from './icons.js';
import { logoLockup } from '../logo.js';
import { escapeHtml } from '../util.js';
import { renderCornerRail } from '../cornerRail.js';
import { attachTooltips } from '../tooltip.js';
import { renderTopRail } from '../topRail.js';
import { getUser, onAuthChange, register, login, logout, ApiError } from '../auth.js';
import { api, apiUrl } from '../api.js';
// onShowAccount is accepted but intentionally not supplied by app.js for this
// screen: the top rail's identity control is already pointing here, so it has
// nothing to navigate to. Declared so the shared rail call below resolves.
export function renderAccountScreen(root, { onBack, onShowAccount, onShowStats, onShowPlaceholder, onShowLeaderboard, onShowFriends, onShowMultiplayer, onShowStore }) {
let mode = 'login'; // 'login' | 'register'
let stopped = false;
// null = checking, true/false once the now-playing probe resolves --
// there's no dedicated "am I connected" endpoint, but now-playing
// itself 404s specifically when there's no stored Spotify connection,
// so it doubles as the status check.
let spotifyConnected = null;
let spotifyCheckedForUserId = null;
function checkSpotifyStatus(userId) {
spotifyCheckedForUserId = userId;
spotifyConnected = null;
api.get('/api/spotify/now-playing')
.then(() => { spotifyConnected = true; rerender(); })
.catch(err => { spotifyConnected = !(err instanceof ApiError && err.status === 404); rerender(); });
}
function spotifyStatusMarkup() {
if (spotifyConnected === null) return `
Checking...
`;
if (spotifyConnected) return `
Connected
`;
return `Connect Spotify`;
}
function formMarkup(user) {
if (user) {
return `
Signed in as ${escapeHtml(user.username)}
Spotify
${spotifyStatusMarkup()}
`;
}
return `
`;
}
function render() {
const user = getUser();
root.innerHTML = `