const baseUrlInput = document.getElementById('baseUrl');
const apiKeyInput = document.getElementById('apiKey');
const statusEl = document.getElementById('status');
const defaultSection = document.getElementById('defaultTabletSection');
const defaultDeviceSelect = document.getElementById('defaultDevice');
async function load() {
const { baseUrl, apiKey, defaultDeviceId } = await chrome.storage.local.get(['baseUrl', 'apiKey', 'defaultDeviceId']);
if (baseUrl) baseUrlInput.value = baseUrl;
if (apiKey) apiKeyInput.value = apiKey; // deliberately shown back here (options page,
// not the popup) so the value can be corrected without regenerating the key.
if (baseUrl && apiKey) refreshDeviceList(defaultDeviceId);
}
// Shared by Test Connection and the default-tablet picker — one fetch, two
// uses, rather than hitting the API twice for what's the same data.
async function fetchDevices() {
const { baseUrl, apiKey } = await chrome.storage.local.get(['baseUrl', 'apiKey']);
if (!baseUrl || !apiKey) throw new Error('Set both fields and save first.');
const res = await fetch(`${baseUrl}/api/staff/devices`, {
headers: { Authorization: `Bearer ${apiKey}` },
});
const body = await res.json().catch(() => ([]));
if (!res.ok) throw new Error(body.error || `HTTP ${res.status}`);
return body;
}
// Populates the "default tablet for this machine" picker — see project_checkin
// memory's extension notes: the popup pre-selects this but still lets staff
// override it; the injected buttons skip asking entirely and push straight
// to it unless it's busy. Selection auto-saves on change, no separate button
// — it's a small enough setting not to warrant one.
async function refreshDeviceList(preselectId) {
try {
const devices = await fetchDevices();
if (!devices.length) {
defaultSection.style.display = 'none';
return;
}
defaultDeviceSelect.innerHTML = devices.map(d =>
``
).join('');
if (preselectId && devices.some(d => d.device_id === preselectId)) {
defaultDeviceSelect.value = preselectId;
}
defaultSection.style.display = '';
// A