feat: декомпозиция JS на модули (auth, tasks, chat, app) и добавление гостевого режима с локальной нейросетью
This commit is contained in:
+19
-162
@@ -1,11 +1,12 @@
|
||||
const API_TOKEN = "scud_secret_token_2026";
|
||||
const AUTH_TOKEN_KEY = "scud_api_auth_token";
|
||||
const SESSION_ID = "web_session_main";
|
||||
const STORAGE_KEY = 'scud_chat_input_history';
|
||||
|
||||
let API_TOKEN = localStorage.getItem(AUTH_TOKEN_KEY) || "";
|
||||
let IS_GUEST = localStorage.getItem('scud_is_guest') === 'true';
|
||||
let currentFilter = 'ALL';
|
||||
let allTasks = [];
|
||||
|
||||
// === ИСТОРИЯ КОМАНД (LOCALSTORAGE + СТРЕЛКИ ВВЕРХ/ВНИЗ) ===
|
||||
let inputHistory = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
|
||||
let historyIndex = -1;
|
||||
|
||||
@@ -13,10 +14,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
const userInputEl = document.getElementById('user-input');
|
||||
|
||||
if (userInputEl) {
|
||||
// Динамическое расширение высоты поля до 4 строк (~96px)
|
||||
userInputEl.addEventListener('input', function() {
|
||||
this.style.height = 'auto';
|
||||
this.style.height = Math.min(this.scrollHeight, 96) + 'px';
|
||||
this.style.height = Math.min(this.scrollHeight, 80) + 'px';
|
||||
});
|
||||
|
||||
userInputEl.addEventListener('keydown', function(e) {
|
||||
@@ -53,163 +53,20 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
});
|
||||
}
|
||||
|
||||
loadTasks();
|
||||
});
|
||||
|
||||
function toggleDrawer() {
|
||||
const drawer = document.getElementById('task-drawer');
|
||||
const backdrop = document.getElementById('drawer-backdrop');
|
||||
const isHidden = drawer.classList.contains('translate-x-full');
|
||||
if (isHidden) {
|
||||
drawer.classList.remove('translate-x-full');
|
||||
backdrop.classList.remove('hidden');
|
||||
if (IS_GUEST) {
|
||||
hideAuthModal();
|
||||
updateUIState();
|
||||
} else if (API_TOKEN) {
|
||||
verifyToken(API_TOKEN).then(isValid => {
|
||||
if (isValid) {
|
||||
hideAuthModal();
|
||||
updateUIState();
|
||||
loadTasks();
|
||||
} else {
|
||||
showAuthModal();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
drawer.classList.add('translate-x-full');
|
||||
backdrop.classList.add('hidden');
|
||||
showAuthModal();
|
||||
}
|
||||
}
|
||||
|
||||
function setFilter(status) {
|
||||
currentFilter = status;
|
||||
['ALL', 'IN_PROGRESS', 'BACKLOG', 'COMPLETED'].forEach(f => {
|
||||
const btn = document.getElementById(`filter-${f}`);
|
||||
if (f === status) {
|
||||
btn.className = "px-3 py-1.5 rounded-t-lg border-b-2 border-indigo-600 text-indigo-600 font-bold";
|
||||
} else {
|
||||
btn.className = "px-3 py-1.5 rounded-t-lg border-b-2 border-transparent hover:text-slate-700";
|
||||
}
|
||||
});
|
||||
renderTasks();
|
||||
}
|
||||
|
||||
async function loadTasks() {
|
||||
try {
|
||||
const res = await fetch('/api/v1/tasks', {
|
||||
headers: { 'Authorization': `Bearer ${API_TOKEN}` }
|
||||
});
|
||||
allTasks = await res.json();
|
||||
document.getElementById('task-count-badge').innerText = allTasks.length;
|
||||
renderTasks();
|
||||
} catch (err) {
|
||||
document.getElementById('tasks-container').innerHTML = `<div class="text-red-500 text-xs py-4 text-center">Ошибка загрузки задач</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderTasks() {
|
||||
const container = document.getElementById('tasks-container');
|
||||
const filtered = allTasks.filter(t => currentFilter === 'ALL' || t.status === currentFilter);
|
||||
|
||||
if (filtered.length === 0) {
|
||||
container.innerHTML = `<div class="text-slate-400 text-xs py-8 text-center">Нет задач с выбранным фильтром</div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = filtered.map(t => {
|
||||
let statusBadge = 'bg-slate-100 text-slate-600 border-slate-200';
|
||||
let cardBg = 'bg-white';
|
||||
if (t.status === 'COMPLETED') {
|
||||
statusBadge = 'bg-emerald-50 text-emerald-700 border-emerald-300 font-semibold';
|
||||
cardBg = 'bg-emerald-50/20';
|
||||
} else if (t.status === 'IN_PROGRESS') {
|
||||
statusBadge = 'bg-amber-50 text-amber-700 border-amber-300 font-bold';
|
||||
cardBg = 'bg-amber-50/20 border-amber-200';
|
||||
}
|
||||
|
||||
let priorityBadge = 'text-slate-500 bg-slate-100 border-slate-200';
|
||||
if (t.priority === 'HIGH') priorityBadge = 'text-red-700 bg-red-50 border-red-200 font-bold';
|
||||
|
||||
let dueDateHtml = '';
|
||||
if (t.due_date) {
|
||||
dueDateHtml = `
|
||||
<div class="mt-2 text-[11px] text-amber-800 bg-amber-50 border border-amber-200 px-2 py-0.5 rounded-md flex items-center gap-1.5 w-fit font-medium">
|
||||
<i class="fa-solid fa-clock text-amber-600"></i>
|
||||
<span>Срок: ${t.due_date}</span>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
return `
|
||||
<div class="${cardBg} border border-slate-200 rounded-xl p-3.5 shadow-sm hover:shadow-md transition">
|
||||
<div class="flex justify-between items-center mb-1.5">
|
||||
<div class="flex items-center gap-1.5">
|
||||
<span class="font-mono text-xs font-bold text-slate-900 bg-slate-100 px-2 py-0.5 rounded border border-slate-200">${t.task_id}</span>
|
||||
<span class="text-[10px] uppercase px-1.5 py-0.5 rounded border ${priorityBadge}">${t.priority || 'HIGH'}</span>
|
||||
</div>
|
||||
<span class="text-[10px] uppercase px-2 py-0.5 rounded border ${statusBadge}">${t.status}</span>
|
||||
</div>
|
||||
|
||||
<h3 class="text-xs font-semibold text-slate-800 mb-1 leading-snug">${t.title}</h3>
|
||||
|
||||
<div class="text-[10px] text-slate-400 font-mono flex items-center gap-1">
|
||||
<i class="fa-solid fa-folder-closed text-slate-300"></i>
|
||||
<span>${t.module}</span>
|
||||
</div>
|
||||
|
||||
${dueDateHtml}
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
async function sendMessage(e) {
|
||||
e.preventDefault();
|
||||
const input = document.getElementById('user-input');
|
||||
const chatWindow = document.getElementById('chat-window');
|
||||
const sendBtn = document.getElementById('send-btn');
|
||||
const text = input.value.trim();
|
||||
|
||||
if (!text) return;
|
||||
|
||||
if (inputHistory[inputHistory.length - 1] !== text) {
|
||||
inputHistory.push(text);
|
||||
if (inputHistory.length > 50) inputHistory.shift();
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(inputHistory));
|
||||
}
|
||||
historyIndex = -1;
|
||||
|
||||
chatWindow.innerHTML += `
|
||||
<div class="flex justify-end">
|
||||
<div class="bg-indigo-600 text-white rounded-xl px-4 py-2.5 max-w-2xl text-sm shadow-sm">
|
||||
${text}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
input.value = '';
|
||||
input.style.height = 'auto';
|
||||
chatWindow.scrollTop = chatWindow.scrollHeight;
|
||||
|
||||
sendBtn.disabled = true;
|
||||
sendBtn.classList.add('opacity-50');
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/v1/chat', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${API_TOKEN}`
|
||||
},
|
||||
body: JSON.stringify({ session_id: SESSION_ID, message: text })
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
chatWindow.innerHTML += `
|
||||
<div class="bg-white border border-slate-200 rounded-xl p-4 max-w-2xl shadow-sm">
|
||||
<p class="text-xs font-bold text-indigo-600 uppercase tracking-wider mb-1"><i class="fa-solid fa-robot mr-1"></i> ИИ-Ассистент</p>
|
||||
<p class="text-slate-700 text-sm whitespace-pre-wrap leading-relaxed">${data.reply}</p>
|
||||
</div>
|
||||
`;
|
||||
chatWindow.scrollTop = chatWindow.scrollHeight;
|
||||
loadTasks();
|
||||
|
||||
} catch (err) {
|
||||
chatWindow.innerHTML += `
|
||||
<div class="bg-red-50 border border-red-200 rounded-xl p-4 max-w-2xl text-red-700 text-sm">
|
||||
Ошибка связи с сервером API.
|
||||
</div>
|
||||
`;
|
||||
} finally {
|
||||
sendBtn.disabled = false;
|
||||
sendBtn.classList.remove('opacity-50');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
function showAuthModal() {
|
||||
document.getElementById('auth-modal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function hideAuthModal() {
|
||||
document.getElementById('auth-modal').classList.add('hidden');
|
||||
}
|
||||
|
||||
async function verifyToken(token) {
|
||||
try {
|
||||
const res = await fetch('/api/v1/tasks', {
|
||||
headers: { 'Authorization': `Bearer ${token}` }
|
||||
});
|
||||
return res.status === 200;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleLogin(e) {
|
||||
e.preventDefault();
|
||||
const tokenInput = document.getElementById('auth-token-input');
|
||||
const errorEl = document.getElementById('auth-error');
|
||||
const token = tokenInput.value.trim();
|
||||
|
||||
if (!token) return;
|
||||
|
||||
errorEl.classList.add('hidden');
|
||||
const isValid = await verifyToken(token);
|
||||
|
||||
if (isValid) {
|
||||
API_TOKEN = token;
|
||||
IS_GUEST = false;
|
||||
localStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
localStorage.removeItem('scud_is_guest');
|
||||
hideAuthModal();
|
||||
updateUIState();
|
||||
loadTasks();
|
||||
} else {
|
||||
errorEl.classList.remove('hidden');
|
||||
}
|
||||
}
|
||||
|
||||
function enableGuestMode() {
|
||||
IS_GUEST = true;
|
||||
API_TOKEN = "";
|
||||
localStorage.setItem('scud_is_guest', 'true');
|
||||
hideAuthModal();
|
||||
updateUIState();
|
||||
}
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
localStorage.removeItem('scud_is_guest');
|
||||
API_TOKEN = "";
|
||||
IS_GUEST = false;
|
||||
document.getElementById('auth-token-input').value = "";
|
||||
showAuthModal();
|
||||
}
|
||||
|
||||
function updateUIState() {
|
||||
const tasksBtn = document.getElementById('tasks-drawer-btn');
|
||||
const guestBadge = document.getElementById('guest-badge');
|
||||
|
||||
if (IS_GUEST) {
|
||||
if (tasksBtn) tasksBtn.classList.add('hidden');
|
||||
if (guestBadge) guestBadge.classList.remove('hidden');
|
||||
} else {
|
||||
if (tasksBtn) tasksBtn.classList.remove('hidden');
|
||||
if (guestBadge) guestBadge.classList.add('hidden');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
async function sendMessage(e) {
|
||||
e.preventDefault();
|
||||
const input = document.getElementById('user-input');
|
||||
const chatWindow = document.getElementById('chat-window');
|
||||
const sendBtn = document.getElementById('send-btn');
|
||||
const text = input.value.trim();
|
||||
|
||||
if (!text) return;
|
||||
|
||||
if (inputHistory[inputHistory.length - 1] !== text) {
|
||||
inputHistory.push(text);
|
||||
if (inputHistory.length > 50) inputHistory.shift();
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(inputHistory));
|
||||
}
|
||||
historyIndex = -1;
|
||||
|
||||
chatWindow.innerHTML += `
|
||||
<div class="flex justify-end">
|
||||
<div class="bg-indigo-600 text-white rounded-2xl px-4 py-2.5 max-w-2xl text-xs sm:text-sm shadow-sm">
|
||||
${text}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
input.value = '';
|
||||
input.style.height = 'auto';
|
||||
chatWindow.scrollTop = chatWindow.scrollHeight;
|
||||
|
||||
sendBtn.disabled = true;
|
||||
sendBtn.classList.add('opacity-50');
|
||||
|
||||
const endpoint = IS_GUEST ? '/api/v1/chat/guest' : '/api/v1/chat';
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (!IS_GUEST) {
|
||||
headers['Authorization'] = `Bearer ${API_TOKEN}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: JSON.stringify({ session_id: SESSION_ID, message: text })
|
||||
});
|
||||
|
||||
if (res.status === 401 && !IS_GUEST) {
|
||||
logout();
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const assistantTitle = IS_GUEST ? "Локальная нейросеть (Гость)" : "ИИ-Ассистент SCUD Orion AI";
|
||||
|
||||
chatWindow.innerHTML += `
|
||||
<div class="bg-white border border-slate-200 rounded-2xl p-3.5 shadow-sm max-w-2xl">
|
||||
<p class="text-[11px] font-bold text-indigo-600 uppercase tracking-wider mb-1"><i class="fa-solid fa-robot mr-1"></i> ${assistantTitle}</p>
|
||||
<p class="text-slate-800 text-xs sm:text-sm whitespace-pre-wrap leading-relaxed">${data.reply}</p>
|
||||
</div>
|
||||
`;
|
||||
chatWindow.scrollTop = chatWindow.scrollHeight;
|
||||
if (!IS_GUEST) loadTasks();
|
||||
|
||||
} catch (err) {
|
||||
chatWindow.innerHTML += `
|
||||
<div class="bg-red-50 border border-red-200 rounded-2xl p-3.5 max-w-2xl text-red-700 text-xs sm:text-sm">
|
||||
Ошибка связи с сервером.
|
||||
</div>
|
||||
`;
|
||||
} finally {
|
||||
sendBtn.disabled = false;
|
||||
sendBtn.classList.remove('opacity-50');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
function toggleDrawer() {
|
||||
if (IS_GUEST) return;
|
||||
const drawer = document.getElementById('task-drawer');
|
||||
const backdrop = document.getElementById('drawer-backdrop');
|
||||
const isHidden = drawer.classList.contains('translate-x-full');
|
||||
if (isHidden) {
|
||||
drawer.classList.remove('translate-x-full');
|
||||
backdrop.classList.remove('hidden');
|
||||
} else {
|
||||
drawer.classList.add('translate-x-full');
|
||||
backdrop.classList.add('hidden');
|
||||
}
|
||||
}
|
||||
|
||||
function setFilter(status) {
|
||||
currentFilter = status;
|
||||
['ALL', 'IN_PROGRESS', 'BACKLOG', 'COMPLETED'].forEach(f => {
|
||||
const btn = document.getElementById(`filter-${f}`);
|
||||
if (btn) {
|
||||
btn.className = (f === status)
|
||||
? "px-3 py-1.5 rounded-t-lg border-b-2 border-indigo-600 text-indigo-600 font-bold whitespace-nowrap"
|
||||
: "px-3 py-1.5 rounded-t-lg border-b-2 border-transparent whitespace-nowrap";
|
||||
}
|
||||
});
|
||||
renderTasks();
|
||||
}
|
||||
|
||||
async function loadTasks() {
|
||||
if (IS_GUEST || !API_TOKEN) return;
|
||||
try {
|
||||
const res = await fetch('/api/v1/tasks', {
|
||||
headers: { 'Authorization': `Bearer ${API_TOKEN}` }
|
||||
});
|
||||
if (res.status === 401) {
|
||||
logout();
|
||||
return;
|
||||
}
|
||||
allTasks = await res.json();
|
||||
document.getElementById('task-count-badge').innerText = allTasks.length;
|
||||
renderTasks();
|
||||
} catch (err) {
|
||||
document.getElementById('tasks-container').innerHTML = `<div class="text-red-500 text-xs py-4 text-center">Ошибка загрузки задач</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderTasks() {
|
||||
const container = document.getElementById('tasks-container');
|
||||
const filtered = allTasks.filter(t => currentFilter === 'ALL' || t.status === currentFilter);
|
||||
|
||||
if (filtered.length === 0) {
|
||||
container.innerHTML = `<div class="text-slate-400 text-xs py-8 text-center">Нет задач с выбранным фильтром</div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = filtered.map(t => {
|
||||
let statusBadge = 'bg-slate-100 text-slate-600 border-slate-200';
|
||||
let cardBg = 'bg-white';
|
||||
if (t.status === 'COMPLETED') {
|
||||
statusBadge = 'bg-emerald-50 text-emerald-700 border-emerald-300 font-semibold';
|
||||
cardBg = 'bg-emerald-50/20';
|
||||
} else if (t.status === 'IN_PROGRESS') {
|
||||
statusBadge = 'bg-amber-50 text-amber-700 border-amber-300 font-bold';
|
||||
cardBg = 'bg-amber-50/20 border-amber-200';
|
||||
}
|
||||
|
||||
let priorityBadge = 'text-slate-500 bg-slate-100 border-slate-200';
|
||||
if (t.priority === 'HIGH') priorityBadge = 'text-red-700 bg-red-50 border-red-200 font-bold';
|
||||
|
||||
let dueDateHtml = t.due_date ? `
|
||||
<div class="mt-2 text-[11px] text-amber-800 bg-amber-50 border border-amber-200 px-2 py-0.5 rounded-md flex items-center gap-1.5 w-fit font-medium">
|
||||
<i class="fa-solid fa-clock text-amber-600"></i>
|
||||
<span>Срок: ${t.due_date}</span>
|
||||
</div>` : '';
|
||||
|
||||
return `
|
||||
<div class="${cardBg} border border-slate-200 rounded-xl p-3.5 shadow-sm hover:shadow-md transition">
|
||||
<div class="flex justify-between items-center mb-1.5">
|
||||
<div class="flex items-center gap-1.5">
|
||||
<span class="font-mono text-xs font-bold text-slate-900 bg-slate-100 px-2 py-0.5 rounded border border-slate-200">${t.task_id}</span>
|
||||
<span class="text-[10px] uppercase px-1.5 py-0.5 rounded border ${priorityBadge}">${t.priority || 'HIGH'}</span>
|
||||
</div>
|
||||
<span class="text-[10px] uppercase px-2 py-0.5 rounded border ${statusBadge}">${t.status}</span>
|
||||
</div>
|
||||
<h3 class="text-xs font-semibold text-slate-800 mb-1 leading-snug">${t.title}</h3>
|
||||
<div class="text-[10px] text-slate-400 font-mono flex items-center gap-1">
|
||||
<i class="fa-solid fa-folder-closed text-slate-300"></i>
|
||||
<span>${t.module}</span>
|
||||
</div>
|
||||
${dueDateHtml}
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
Reference in New Issue
Block a user