/* * Copyright (C) 2025 TomTom Navigation B.V. * Licensed under the Apache License, Version 2.0 */ import "./ui-visibility.css"; /** * Utility to check if the UI should be displayed based on the tool response. * * When show_ui is false, the App should minimize its footprint - showing only * a small indicator that data was received but no interactive map visualization. * This is useful for intermediate operations (e.g., geocoding as part of routing). * * @param apiResponse - The parsed API response from the tool * @returns true if UI should be displayed, false otherwise */ export function shouldShowUI(apiResponse: unknown): boolean { const response = apiResponse as Record & { _meta?: Record }; // Default to true if _meta or show_ui is not present if (!response?._meta) return true; if (typeof response._meta.show_ui !== "boolean") return true; return response._meta.show_ui as boolean; } /** * Hides the map container and shows a compact status indicator. * Call this when show_ui is false. */ export function hideMapUI(): void { // Add class to collapse the widget height document.documentElement.classList.add("ui-hidden"); const mapContainer = document.getElementById("sdk-map"); if (mapContainer) { mapContainer.classList.remove("visible"); mapContainer.style.display = "none"; } // Create compact status indicator if it doesn't exist let indicator = document.getElementById("ui-hidden-indicator"); if (!indicator) { indicator = document.createElement("div"); indicator.id = "ui-hidden-indicator"; indicator.innerHTML = `
Data processed
`; document.body.appendChild(indicator); } indicator.style.display = "block"; } /** * Shows a user-friendly error indicator when the tool call fails. * The actual error details are already visible to the agent in the tool response * for retry decisions — this is purely for the user's UI. */ export function showErrorUI(): void { // Collapse the widget like hideMapUI document.documentElement.classList.add("ui-hidden"); const mapContainer = document.getElementById("sdk-map"); if (mapContainer) { mapContainer.classList.remove("visible"); mapContainer.style.display = "none"; } // Hide the data-processed indicator if visible const hiddenIndicator = document.getElementById("ui-hidden-indicator"); if (hiddenIndicator) { hiddenIndicator.style.display = "none"; } // Create error indicator if it doesn't exist let indicator = document.getElementById("ui-error-indicator"); if (!indicator) { indicator = document.createElement("div"); indicator.id = "ui-error-indicator"; indicator.innerHTML = `
Something went wrong
`; document.body.appendChild(indicator); } indicator.style.display = "block"; } /** * Shows the map container and hides the minimal indicator. * Call this when show_ui is true (default behavior). */ export function showMapUI(): void { // Remove compact mode class document.documentElement.classList.remove("ui-hidden"); const mapContainer = document.getElementById("sdk-map"); if (mapContainer) { mapContainer.style.display = "block"; // Use requestAnimationFrame to ensure display:block is applied before adding visible class requestAnimationFrame(() => { mapContainer.classList.add("visible"); }); } const indicator = document.getElementById("ui-hidden-indicator"); if (indicator) { indicator.style.display = "none"; } }