fix: correct syntax error and improve chat UI initialization
- Removed stray `o` character after `pendingContextChange` declaration to fix a syntax error in `chat.js`. - Added safety check before calling `focus()` on the message input element. - Implemented `preloadChatCSS()` to preload chat styles and prevent flash‑of‑unstyled‑content. - Added GSAP fade‑in animation when switching sections for smoother visual transition. - Ensured input field is auto‑focused after a section loads. - Changed the default initial section from `drive` to `chat` to align with new default behavior.
This commit is contained in:
parent
e38554ea51
commit
df36448f14
2 changed files with 34 additions and 6 deletions
|
|
@ -2,7 +2,7 @@ function chatApp() {
|
||||||
|
|
||||||
// Core state variables (shared via closure)
|
// Core state variables (shared via closure)
|
||||||
let ws = null,
|
let ws = null,
|
||||||
pendingContextChange = null,
|
pendingContextChange = null,o
|
||||||
currentSessionId = null,
|
currentSessionId = null,
|
||||||
currentUserId = null,
|
currentUserId = null,
|
||||||
currentBotId = "default_bot",
|
currentBotId = "default_bot",
|
||||||
|
|
@ -127,7 +127,9 @@ function chatApp() {
|
||||||
this.applyTheme();
|
this.applyTheme();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
messageInputEl.focus();
|
if (messageInputEl) {
|
||||||
|
messageInputEl.focus();
|
||||||
|
}
|
||||||
|
|
||||||
// UI event listeners
|
// UI event listeners
|
||||||
document.addEventListener('click', (e) => {
|
document.addEventListener('click', (e) => {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,19 @@ function getBasePath() {
|
||||||
// with the Actix static file configuration.
|
// with the Actix static file configuration.
|
||||||
return '/';
|
return '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Preload chat CSS to avoid flash on first load
|
||||||
|
function preloadChatCSS() {
|
||||||
|
const chatCssPath = getBasePath() + 'chat/chat.css';
|
||||||
|
const existing = document.querySelector(`link[href="${chatCssPath}"]`);
|
||||||
|
if (!existing) {
|
||||||
|
const link = document.createElement('link');
|
||||||
|
link.rel = 'stylesheet';
|
||||||
|
link.href = chatCssPath;
|
||||||
|
document.head.appendChild(link);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function loadSectionHTML(path) {
|
async function loadSectionHTML(path) {
|
||||||
const fullPath = getBasePath() + path;
|
const fullPath = getBasePath() + path;
|
||||||
const response = await fetch(fullPath);
|
const response = await fetch(fullPath);
|
||||||
|
|
@ -28,6 +41,11 @@ async function switchSection(section) {
|
||||||
// Resolve CSS path relative to the base directory.
|
// Resolve CSS path relative to the base directory.
|
||||||
const cssPath = getBasePath() + htmlPath.replace('.html', '.css');
|
const cssPath = getBasePath() + htmlPath.replace('.html', '.css');
|
||||||
|
|
||||||
|
// Preload chat CSS if the target is chat
|
||||||
|
if (section === 'chat') {
|
||||||
|
preloadChatCSS();
|
||||||
|
}
|
||||||
|
|
||||||
// Remove any existing section CSS
|
// Remove any existing section CSS
|
||||||
document.querySelectorAll('link[data-section-css]').forEach(link => link.remove());
|
document.querySelectorAll('link[data-section-css]').forEach(link => link.remove());
|
||||||
|
|
||||||
|
|
@ -85,8 +103,12 @@ async function switchSection(section) {
|
||||||
container.appendChild(wrapper);
|
container.appendChild(wrapper);
|
||||||
sectionCache[section] = wrapper;
|
sectionCache[section] = wrapper;
|
||||||
|
|
||||||
// Ensure the new section is visible
|
// Ensure the new section is visible with a fast GSAP fade-in
|
||||||
wrapper.style.display = 'block';
|
gsap.fromTo(
|
||||||
|
wrapper,
|
||||||
|
{ opacity: 0 },
|
||||||
|
{ opacity: 1, duration: 0.15, ease: 'power2.out' }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then load JS after HTML is inserted (skip if already loaded)
|
// Then load JS after HTML is inserted (skip if already loaded)
|
||||||
|
|
@ -101,6 +123,10 @@ async function switchSection(section) {
|
||||||
}
|
}
|
||||||
window.history.pushState({}, '', `#${section}`);
|
window.history.pushState({}, '', `#${section}`);
|
||||||
Alpine.initTree(mainContent);
|
Alpine.initTree(mainContent);
|
||||||
|
const inputEl = document.getElementById('messageInput');
|
||||||
|
if (inputEl) {
|
||||||
|
inputEl.focus();
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error loading section:', err);
|
console.error('Error loading section:', err);
|
||||||
mainContent.innerHTML = `<div class="error">Failed to load ${section} section</div>`;
|
mainContent.innerHTML = `<div class="error">Failed to load ${section} section</div>`;
|
||||||
|
|
@ -126,8 +152,8 @@ function getInitialSection() {
|
||||||
section = match[1].toLowerCase();
|
section = match[1].toLowerCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Default to drive if nothing matches
|
// Default to chat if nothing matches
|
||||||
return section || 'drive';
|
return section || 'chat';
|
||||||
}
|
}
|
||||||
window.addEventListener('DOMContentLoaded', () => {
|
window.addEventListener('DOMContentLoaded', () => {
|
||||||
switchSection(getInitialSection());
|
switchSection(getInitialSection());
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue