import { api } from '../api.js';
import { showToast } from '../components/toast.js';
import { t, tn } from '../i18n.js';
import { esc } from '../utils.js';
const API = (url, opts = {}) => fetch('/api' + url, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${localStorage.getItem('token')}`, ...opts.headers }, ...opts }).then(r => r.json());
export async function render(container) {
const hash = window.location.hash;
if (hash.startsWith('#/team/')) {
const id = hash.split('#/team/')[1];
return renderTeamDetail(container, id);
}
return renderList(container);
}
async function renderList(container) {
container.innerHTML = `
${t('team.title')} ?
${t('team.subtitle')}
`;
document.getElementById('newTeamBtn').onclick = async () => {
const name = prompt(t('team.prompt_name'));
if (!name) return;
const team = await API('/teams', { method: 'POST', body: JSON.stringify({ name }) });
window.location.hash = `#/team/${team.id}`;
};
try {
const teams = await API('/teams');
const list = document.getElementById('teamsList');
// Teams is switched off server-side while it is redesigned: every endpoint answers 503 with
// an explanation. API() resolves the BODY whatever the status, so an object arrives where an
// array was expected — and `!teams.length` then rendered "No teams yet" over a feature that
// is not there, next to a New Team button that could only ever fail. Say what is actually
// happening, and take away the button that leads nowhere.
if (!Array.isArray(teams)) {
document.getElementById('newTeamBtn')?.remove();
list.innerHTML = `