const ImportUI = { _currentBrowsePath: '/home', _selectedPath: '', _importing: false, async load() { const container = document.getElementById('import-container'); if (!container) return; let repos = []; try { repos = await API.repos.list(); } catch {} container.innerHTML = `

Importar Projeto

Selecione um diretório do servidor para importar ao Gitea. Os arquivos serão copiados respeitando o .gitignore, sem alterar o projeto original.

Letras minúsculas, números e hífens. Será criado no Gitea e clonado em /home/projetos/

Repositórios no Gitea

${repos.length}
${repos.length === 0 ? '

Nenhum repositório encontrado

' : ''}
${repos.map(r => ImportUI._renderRepoCard(r)).join('')}
`; Utils.refreshIcons(container); ImportUI._bindEvents(); }, _renderRepoCard(repo) { const domain = 'nitro-cloud.duckdns.org'; const repoUrl = `https://git.${domain}/${repo.full_name || repo.name}`; const updated = repo.updated_at ? new Date(repo.updated_at).toLocaleDateString('pt-BR') : ''; const size = repo.size ? ImportUI._formatSize(repo.size * 1024) : ''; return `
${Utils.escapeHtml(repo.name)}
${repo.description ? `

${Utils.escapeHtml(repo.description)}

` : ''}
${updated ? ` ${updated}` : ''} ${size ? ` ${size}` : ''} ${repo.default_branch ? ` ${Utils.escapeHtml(repo.default_branch)}` : ''}
`; }, _formatSize(bytes) { if (!bytes) return ''; const units = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return (bytes / Math.pow(1024, i)).toFixed(i > 0 ? 1 : 0) + ' ' + units[i]; }, _bindEvents() { const browseBtn = document.getElementById('import-browse-btn'); const submitBtn = document.getElementById('import-submit-btn'); const pathInput = document.getElementById('import-path'); if (browseBtn) { browseBtn.addEventListener('click', () => { const browser = document.getElementById('import-browser'); if (!browser) return; const isVisible = !browser.hidden; browser.hidden = isVisible; if (!isVisible) { const currentVal = pathInput?.value.trim(); ImportUI._browseTo(currentVal || '/home'); } }); } if (pathInput) { pathInput.addEventListener('change', () => { const val = pathInput.value.trim(); if (val) { ImportUI._autoFillRepoName(val); } }); pathInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); const val = pathInput.value.trim(); if (val) { ImportUI._autoFillRepoName(val); const browser = document.getElementById('import-browser'); if (browser && !browser.hidden) { ImportUI._browseTo(val); } } } }); } if (submitBtn) { submitBtn.addEventListener('click', () => ImportUI._doImport()); } }, _autoFillRepoName(path) { const nameInput = document.getElementById('import-repo-name'); if (!nameInput || nameInput.value.trim()) return; const folderName = path.split('/').filter(Boolean).pop() || ''; nameInput.value = folderName.toLowerCase().replace(/[^a-z0-9-]/g, '-'); }, async _browseTo(path) { try { const data = await API.projects.browse(path); ImportUI._currentBrowsePath = data.currentPath; ImportUI._renderBrowser(data); } catch (err) { Toast.error(`Erro ao navegar: ${err.message}`); } }, _renderBrowser(data) { const breadcrumbEl = document.getElementById('import-browser-breadcrumb'); const listEl = document.getElementById('import-browser-list'); if (!breadcrumbEl || !listEl) return; const parts = data.currentPath.split('/').filter(Boolean); let breadcrumb = ` /`; let accumulated = ''; for (const part of parts) { accumulated += '/' + part; breadcrumb += ` / ${Utils.escapeHtml(part)}`; } breadcrumbEl.innerHTML = breadcrumb; const dirs = data.directories || []; if (dirs.length === 0) { listEl.innerHTML = '
Nenhum subdiretório encontrado
'; } else { listEl.innerHTML = dirs.map(d => `
${Utils.escapeHtml(d.name)}
`).join(''); } Utils.refreshIcons(breadcrumbEl); Utils.refreshIcons(listEl); breadcrumbEl.querySelectorAll('.import-browse-link').forEach(link => { link.addEventListener('click', (e) => { e.preventDefault(); ImportUI._browseTo(link.dataset.browsePath); }); }); listEl.querySelectorAll('.import-browse-link').forEach(link => { link.addEventListener('click', (e) => { e.preventDefault(); ImportUI._browseTo(link.dataset.browsePath); }); }); listEl.querySelectorAll('.import-select-btn').forEach(btn => { btn.addEventListener('click', () => { const selectedPath = btn.dataset.selectPath; const selectedName = btn.dataset.selectName; const pathInput = document.getElementById('import-path'); const nameInput = document.getElementById('import-repo-name'); if (pathInput) pathInput.value = selectedPath; if (nameInput && !nameInput.value.trim()) { nameInput.value = selectedName.toLowerCase().replace(/[^a-z0-9-]/g, '-'); } document.getElementById('import-browser').hidden = true; ImportUI._selectedPath = selectedPath; }); }); }, async _doImport() { if (ImportUI._importing) return; const pathInput = document.getElementById('import-path'); const nameInput = document.getElementById('import-repo-name'); const submitBtn = document.getElementById('import-submit-btn'); const sourcePath = pathInput?.value.trim(); const repoName = nameInput?.value.trim().toLowerCase().replace(/[^a-z0-9-]/g, '-'); if (!sourcePath) { Toast.warning('Informe o caminho do projeto'); return; } if (!repoName) { Toast.warning('Informe o nome do repositório'); return; } ImportUI._importing = true; if (submitBtn) { submitBtn.disabled = true; submitBtn.innerHTML = ' Importando...'; Utils.refreshIcons(submitBtn); } try { Toast.info('Importando projeto... isso pode levar alguns segundos'); const result = await API.projects.import(sourcePath, repoName); Toast.success('Projeto importado com sucesso!'); const modal = document.getElementById('execution-detail-modal-overlay'); const title = document.getElementById('execution-detail-title'); const content = document.getElementById('execution-detail-content'); if (modal && title && content) { title.textContent = 'Projeto Importado'; content.innerHTML = `
Repositório: ${Utils.escapeHtml(result.repoUrl)}
Diretório: ${Utils.escapeHtml(result.projectDir)}
Status: ${Utils.escapeHtml(result.status)}
${result.message ? `
${Utils.escapeHtml(result.message)}
` : ''}
Passos:
`; Modal.open('execution-detail-modal-overlay'); } if (pathInput) pathInput.value = ''; if (nameInput) nameInput.value = ''; App._reposCache = null; await ImportUI.load(); } catch (err) { Toast.error(`Erro ao importar: ${err.message}`); } finally { ImportUI._importing = false; if (submitBtn) { submitBtn.disabled = false; submitBtn.innerHTML = ' Importar para o Gitea'; Utils.refreshIcons(submitBtn); } } }, }; window.ImportUI = ImportUI;