# Issue #298: Fix async response handling in dashboard

## Objective

Replace setTimeout hacks with proper async response handling for federated queries in the dashboard.

## Current State

- Dashboard uses `setTimeout(resolve, 1000)` to wait for query responses (lines 59, 83)
- Race conditions possible
- Unreliable data loading
- Poor UX with arbitrary delays

## Approach

Implement polling mechanism to check query message status:

1. Send query and get message ID
2. Poll for message status until DELIVERED or FAILED
3. Extract response data when available
4. Handle timeouts gracefully

## Implementation Plan

- [x] Create scratchpad
- [x] Add polling utility function
- [x] Update dashboard to use polling instead of setTimeout
- [x] Add proper loading states (via polling)
- [x] Add error handling (polling throws on failure)
- [x] Add timeout handling (30s timeout)
- [ ] Test with actual federation connection (requires live system)

## Changes Made

### apps/web/src/lib/api/federation-queries.ts

Added `pollForQueryResponse()` function:

- Polls every 500ms for message status
- Timeout after 30 seconds
- Returns message when DELIVERED
- Throws error when FAILED or timeout reached
- Configurable poll interval and timeout

### apps/web/src/app/(authenticated)/federation/dashboard/page.tsx

Replaced setTimeout hacks with polling:

- Line 57-59: Removed setTimeout, added pollForQueryResponse
- Line 83: Removed setTimeout, added pollForQueryResponse
- Updated query strings to match new format ("get tasks", "get events")
- Improved response data parsing to handle new response format
- Error handling via try/catch (polling throws on failure)

## Testing Notes

- Polling function handles PENDING → DELIVERED transition
- Polling function handles PENDING → FAILED transition
- Timeout prevents infinite waiting
- Errors are properly propagated to dashboard error handling

## Changes to Make

### apps/web/src/lib/api/federation-queries.ts

Add polling function:

- `pollForQueryResponse(messageId: string): Promise<QueryResponse>`
- Poll every 500ms, timeout after 30 seconds
- Check message status until DELIVERED or FAILED

### apps/web/src/app/(authenticated)/federation/dashboard/page.tsx

- Remove setTimeout hacks
- Use polling function to wait for responses
- Add proper loading states per connection
- Add better error messages
- Add timeout handling

## Notes

- Message status: PENDING → DELIVERED or FAILED
- Poll interval: 500ms
- Timeout: 30 seconds
- Need to handle both successful and failed queries
