Fix navigation tracking and ChatPanel window width initialization

Navigation tracking fixes:
- Filter out chrome-extension:// URLs to avoid triggering on extension internal pages
- Fixes "Could not establish connection" errors when sidepanel URL updates
- Remove unused currentTabUrl/currentTabIndex tracking variables

ChatPanel layout fixes:
- Fix windowWidth initialization (was 0 due to too-early evaluation)
- Set windowWidth in connectedCallback after DOM connection
- Add requestAnimationFrame to ensure width is measured after layout
- Add debug logging for troubleshooting layout issues
- Now properly respects BREAKPOINT (800px) for mobile vs desktop layout
This commit is contained in:
Mario Zechner 2025-10-06 18:26:01 +02:00
parent 4f11cc15db
commit 7e79c05407
2 changed files with 20 additions and 14 deletions

View file

@ -71,10 +71,6 @@ let agent: Agent;
let chatPanel: ChatPanel; let chatPanel: ChatPanel;
let agentUnsubscribe: (() => void) | undefined; let agentUnsubscribe: (() => void) | undefined;
// Track current active tab for real-time navigation updates
let currentTabUrl: string | undefined;
let currentTabIndex: number | undefined;
// ============================================================================ // ============================================================================
// HELPERS // HELPERS
// ============================================================================ // ============================================================================
@ -302,7 +298,8 @@ const renderApp = () => {
// Listen for tab updates and insert navigation messages immediately // Listen for tab updates and insert navigation messages immediately
chrome.tabs.onUpdated.addListener((_tabId, changeInfo, tab) => { chrome.tabs.onUpdated.addListener((_tabId, changeInfo, tab) => {
// Only care about URL changes on the active tab // Only care about URL changes on the active tab
if (changeInfo.url && tab.active && tab.url && agent) { // Ignore chrome-extension:// URLs (extension internal pages)
if (changeInfo.url && tab.active && tab.url && agent && !tab.url.startsWith("chrome-extension://")) {
const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index); const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index);
agent.appendMessage(navMessage); agent.appendMessage(navMessage);
} }
@ -311,7 +308,8 @@ chrome.tabs.onUpdated.addListener((_tabId, changeInfo, tab) => {
// Listen for tab activation (user switches tabs) // Listen for tab activation (user switches tabs)
chrome.tabs.onActivated.addListener(async (activeInfo) => { chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await chrome.tabs.get(activeInfo.tabId); const tab = await chrome.tabs.get(activeInfo.tabId);
if (tab.url && agent) { // Ignore chrome-extension:// URLs (extension internal pages)
if (tab.url && agent && !tab.url.startsWith("chrome-extension://")) {
const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index); const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index);
agent.appendMessage(navMessage); agent.appendMessage(navMessage);
} }
@ -345,13 +343,6 @@ async function initApp() {
}; };
chatPanel.additionalTools = [browserJavaScriptTool]; chatPanel.additionalTools = [browserJavaScriptTool];
// Initialize current tab state
const [currentTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (currentTab?.url) {
currentTabUrl = currentTab.url;
currentTabIndex = currentTab.index;
}
// Check for session in URL // Check for session in URL
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
let sessionIdFromUrl = urlParams.get("session"); let sessionIdFromUrl = urlParams.get("session");

View file

@ -19,7 +19,7 @@ export class ChatPanel extends LitElement {
@state() private hasArtifacts = false; @state() private hasArtifacts = false;
@state() private artifactCount = 0; @state() private artifactCount = 0;
@state() private showArtifactsPanel = false; @state() private showArtifactsPanel = false;
@state() private windowWidth = window.innerWidth; @state() private windowWidth = 0;
@property({ attribute: false }) sandboxUrlProvider?: () => string; @property({ attribute: false }) sandboxUrlProvider?: () => string;
@property({ attribute: false }) onApiKeyRequired?: (provider: string) => Promise<boolean>; @property({ attribute: false }) onApiKeyRequired?: (provider: string) => Promise<boolean>;
@property({ attribute: false }) onBeforeSend?: () => void | Promise<void>; @property({ attribute: false }) onBeforeSend?: () => void | Promise<void>;
@ -36,11 +36,17 @@ export class ChatPanel extends LitElement {
override connectedCallback() { override connectedCallback() {
super.connectedCallback(); super.connectedCallback();
this.windowWidth = window.innerWidth; // Set initial width after connection
window.addEventListener("resize", this.resizeHandler); window.addEventListener("resize", this.resizeHandler);
this.style.display = "flex"; this.style.display = "flex";
this.style.flexDirection = "column"; this.style.flexDirection = "column";
this.style.height = "100%"; this.style.height = "100%";
this.style.minHeight = "0"; this.style.minHeight = "0";
// Update width after initial render
requestAnimationFrame(() => {
this.windowWidth = window.innerWidth;
this.requestUpdate();
});
} }
override disconnectedCallback() { override disconnectedCallback() {
@ -146,6 +152,15 @@ export class ChatPanel extends LitElement {
const isMobile = this.windowWidth < BREAKPOINT; const isMobile = this.windowWidth < BREAKPOINT;
console.log("[ChatPanel Debug]", {
windowWidth: this.windowWidth,
breakpoint: BREAKPOINT,
isMobile,
showArtifactsPanel: this.showArtifactsPanel,
hasArtifacts: this.hasArtifacts,
artifactCount: this.artifactCount,
});
// Set panel props // Set panel props
if (this.artifactsPanel) { if (this.artifactsPanel) {
this.artifactsPanel.collapsed = !this.showArtifactsPanel; this.artifactsPanel.collapsed = !this.showArtifactsPanel;