{"version":3,"file":"userSelection-BifVfRyx.js","sources":["../../src/scripts/ui/userSelection.ts"],"sourcesContent":["import { api } from '../api'\r\nimport { $el } from '../ui'\r\nimport { createSpinner } from './spinner'\r\nimport './userSelection.css'\r\n\r\ninterface SelectedUser {\r\n username: string\r\n userId: string\r\n created: boolean\r\n}\r\n\r\nexport class UserSelectionScreen {\r\n async show(users, user): Promise {\r\n const userSelection = document.getElementById('comfy-user-selection')\r\n userSelection.style.display = ''\r\n return new Promise((resolve) => {\r\n const input = userSelection.getElementsByTagName('input')[0]\r\n const select = userSelection.getElementsByTagName('select')[0]\r\n const inputSection = input.closest('section')\r\n const selectSection = select.closest('section')\r\n const form = userSelection.getElementsByTagName('form')[0]\r\n const error = userSelection.getElementsByClassName('comfy-user-error')[0]\r\n const button = userSelection.getElementsByClassName(\r\n 'comfy-user-button-next'\r\n )[0]\r\n\r\n let inputActive = null\r\n input.addEventListener('focus', () => {\r\n inputSection.classList.add('selected')\r\n selectSection.classList.remove('selected')\r\n inputActive = true\r\n })\r\n select.addEventListener('focus', () => {\r\n inputSection.classList.remove('selected')\r\n selectSection.classList.add('selected')\r\n inputActive = false\r\n select.style.color = ''\r\n })\r\n select.addEventListener('blur', () => {\r\n if (!select.value) {\r\n select.style.color = 'var(--descrip-text)'\r\n }\r\n })\r\n\r\n form.addEventListener('submit', async (e) => {\r\n e.preventDefault()\r\n if (inputActive == null) {\r\n error.textContent =\r\n 'Please enter a username or select an existing user.'\r\n } else if (inputActive) {\r\n const username = input.value.trim()\r\n if (!username) {\r\n error.textContent = 'Please enter a username.'\r\n return\r\n }\r\n\r\n // Create new user\r\n // Property 'readonly' does not exist on type 'HTMLSelectElement'.ts(2339)\r\n // Property 'readonly' does not exist on type 'HTMLInputElement'. Did you mean 'readOnly'?ts(2551)\r\n input.disabled =\r\n select.disabled =\r\n // @ts-expect-error\r\n input.readonly =\r\n // @ts-expect-error\r\n select.readonly =\r\n true\r\n const spinner = createSpinner()\r\n button.prepend(spinner)\r\n try {\r\n const resp = await api.createUser(username)\r\n if (resp.status >= 300) {\r\n let message =\r\n 'Error creating user: ' + resp.status + ' ' + resp.statusText\r\n try {\r\n const res = await resp.json()\r\n if (res.error) {\r\n message = res.error\r\n }\r\n } catch (error) {}\r\n throw new Error(message)\r\n }\r\n\r\n resolve({ username, userId: await resp.json(), created: true })\r\n } catch (err) {\r\n spinner.remove()\r\n error.textContent =\r\n err.message ??\r\n err.statusText ??\r\n err ??\r\n 'An unknown error occurred.'\r\n // Property 'readonly' does not exist on type 'HTMLSelectElement'.ts(2339)\r\n // Property 'readonly' does not exist on type 'HTMLInputElement'. Did you mean 'readOnly'?ts(2551)\r\n input.disabled =\r\n select.disabled =\r\n // @ts-expect-error\r\n input.readonly =\r\n // @ts-expect-error\r\n select.readonly =\r\n false\r\n return\r\n }\r\n } else if (!select.value) {\r\n error.textContent = 'Please select an existing user.'\r\n return\r\n } else {\r\n resolve({\r\n username: users[select.value],\r\n userId: select.value,\r\n created: false\r\n })\r\n }\r\n })\r\n\r\n if (user) {\r\n const name = localStorage['Comfy.userName']\r\n if (name) {\r\n input.value = name\r\n }\r\n }\r\n if (input.value) {\r\n // Focus the input, do this separately as sometimes browsers like to fill in the value\r\n input.focus()\r\n }\r\n\r\n const userIds = Object.keys(users ?? {})\r\n if (userIds.length) {\r\n for (const u of userIds) {\r\n $el('option', { textContent: users[u], value: u, parent: select })\r\n }\r\n select.style.color = 'var(--descrip-text)'\r\n\r\n if (select.value) {\r\n // Focus the select, do this separately as sometimes browsers like to fill in the value\r\n select.focus()\r\n }\r\n } else {\r\n userSelection.classList.add('no-users')\r\n input.focus()\r\n }\r\n }).then((r: SelectedUser) => {\r\n userSelection.remove()\r\n return r\r\n })\r\n }\r\n}\r\n"],"names":["error"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAWO,MAAM,oBAAoB;AAAA,EACzB,KAAK,OAAO,MAA6B;AAAA;AACvC,YAAA,gBAAgB,SAAS,eAAe,sBAAsB;AACpE,oBAAc,M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