Skip to content

Commit 0432c86

Browse files
committed
fix(map-server): fix localStorage persistence and always show home button
- Change storage key prefix to 'map:' and use 'default' fallback if no toolId - Always show home button after init (flies to default USA view if no initial bbox) - Add flyTo animation (1.5s) for home button
1 parent 8e915b6 commit 0432c86

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

examples/map-server/src/mcp-app.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,13 @@ interface PersistedCameraState {
9494

9595
/**
9696
* Get localStorage key for persisting view state
97-
* Uses toolInfo.id (tool invocation ID) - localStorage is scoped per conversation per server,
98-
* so each tool call remembers its own view state within the conversation.
97+
* Uses toolInfo.id (tool invocation ID) if available, otherwise falls back to "default".
98+
* localStorage is scoped per origin, so each tool call can remember its view state.
9999
*/
100-
function getViewStorageKey(): string | null {
100+
function getViewStorageKey(): string {
101101
const context = app.getHostContext();
102-
const toolId = context?.toolInfo?.id;
103-
if (!toolId) return null;
104-
return `cesium-view:${toolId}`;
102+
const toolId = context?.toolInfo?.id || "default";
103+
return `map:${toolId}`;
105104
}
106105

107106
/**
@@ -142,11 +141,6 @@ function schedulePersistViewState(cesiumViewer: any): void {
142141
*/
143142
function persistViewState(cesiumViewer: any): void {
144143
const key = getViewStorageKey();
145-
if (!key) {
146-
log.info("No storage key available, skipping view persistence");
147-
return;
148-
}
149-
150144
const state = getCameraState(cesiumViewer);
151145
if (!state) return;
152146

@@ -168,7 +162,6 @@ function persistViewState(cesiumViewer: any): void {
168162
*/
169163
function loadPersistedViewState(): PersistedCameraState | null {
170164
const key = getViewStorageKey();
171-
if (!key) return null;
172165

173166
try {
174167
const stored = localStorage.getItem(key);
@@ -184,6 +177,7 @@ function loadPersistedViewState(): PersistedCameraState | null {
184177
log.warn("Invalid persisted view state, ignoring");
185178
return null;
186179
}
180+
log.info("Loaded persisted view state from", key);
187181
return state;
188182
} catch (e) {
189183
log.warn("Failed to load persisted view state:", e);
@@ -818,6 +812,14 @@ const app = new App(
818812
{ autoResize: false },
819813
);
820814

815+
// Default bounding box (USA) for home button fallback
816+
const DEFAULT_BOUNDING_BOX: BoundingBox = {
817+
west: -130,
818+
south: 20,
819+
east: -60,
820+
north: 55,
821+
};
822+
821823
/**
822824
* Show home button and set up click handler
823825
*/
@@ -827,10 +829,10 @@ function setupHomeButton(cesiumViewer: any): void {
827829

828830
btn.style.display = "flex";
829831
btn.addEventListener("click", () => {
830-
if (initialBoundingBox && cesiumViewer) {
831-
log.info("Flying to initial view:", initialBoundingBox);
832-
flyToBoundingBox(cesiumViewer, initialBoundingBox);
833-
}
832+
// Use initial bbox from tool input, or default to USA view
833+
const targetBbox = initialBoundingBox || DEFAULT_BOUNDING_BOX;
834+
log.info("Flying to home view:", targetBbox);
835+
flyToBoundingBox(cesiumViewer, targetBbox);
834836
});
835837
}
836838

@@ -1017,10 +1019,9 @@ app.ontoolinput = async (params) => {
10171019
// Mark that we received explicit tool input (overrides persisted state)
10181020
hasReceivedToolInput = true;
10191021

1020-
// Store initial bbox for home button
1022+
// Store initial bbox for home button (first tool input becomes the "home" view)
10211023
if (!initialBoundingBox) {
10221024
initialBoundingBox = bbox;
1023-
setupHomeButton(viewer);
10241025
}
10251026

10261027
log.info("Positioning camera to bbox:", bbox);
@@ -1154,6 +1155,9 @@ async function init() {
11541155
fullscreenBtn.addEventListener("click", toggleFullscreen);
11551156
}
11561157

1158+
// Set up home button
1159+
setupHomeButton(viewer);
1160+
11571161
// Set up keyboard shortcuts for fullscreen (Escape to exit, Ctrl/Cmd+Enter to toggle)
11581162
document.addEventListener("keydown", handleFullscreenKeyboard);
11591163
} catch (error) {

0 commit comments

Comments
 (0)