@joewinke/jatui 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,341 @@
1
+ <script lang="ts">
2
+ /**
3
+ * MapView — multi-marker Google Maps component.
4
+ *
5
+ * Generic map for dispatching / field-service use cases. Renders job pins
6
+ * (colored by status) and agent pins (colored by availability status) using
7
+ * Google Maps Advanced Markers.
8
+ *
9
+ * API key is passed explicitly — jatui has no $env dependency.
10
+ *
11
+ * Usage:
12
+ *
13
+ * <MapView
14
+ * apiKey={PUBLIC_GOOGLE_MAPS_API_KEY}
15
+ * jobs={jobs}
16
+ * agents={technicians}
17
+ * onJobClick={(job) => openDetail(job)}
18
+ * />
19
+ *
20
+ * Ported from flush DispatchMap (622L) — steelbridge fork capture (jst-e0d1u.4).
21
+ */
22
+ import { onMount, onDestroy, mount, unmount } from 'svelte';
23
+ import { loadGoogleMapsAPI, isGoogleMapsLoaded } from '../utils/googleMapsLoader';
24
+ import UserAvatar from './UserAvatar.svelte';
25
+ import type { MapJob, MapAgent } from '../types/maps';
26
+
27
+ export type { MapJob, MapAgent };
28
+
29
+ interface Props {
30
+ apiKey: string;
31
+ jobs?: MapJob[];
32
+ agents?: MapAgent[];
33
+ /** Default map center — lat/lng. Defaults to West Palm Beach. */
34
+ defaultCenter?: { lat: number; lng: number };
35
+ showLegend?: boolean;
36
+ onJobClick?: (job: MapJob) => void;
37
+ onAgentClick?: (agent: MapAgent) => void;
38
+ }
39
+
40
+ let {
41
+ apiKey,
42
+ jobs = [],
43
+ agents = [],
44
+ defaultCenter = { lat: 26.3683, lng: -80.1289 },
45
+ showLegend = true,
46
+ onJobClick,
47
+ onAgentClick
48
+ }: Props = $props();
49
+
50
+ // Job status → hex color (hardcoded by design — not themed, maps sit outside DaisyUI theming)
51
+ const STATUS_COLORS: Record<string, string> = {
52
+ pending: '#f59e0b',
53
+ scheduled: '#3b82f6',
54
+ in_progress: '#10b981',
55
+ completed: '#6b7280',
56
+ cancelled: '#ef4444'
57
+ };
58
+
59
+ let mapContainer: HTMLElement;
60
+ let map: google.maps.Map;
61
+ let jobMarkers: any[] = [];
62
+ let agentMarkers: any[] = [];
63
+ let AdvancedMarkerElement: any;
64
+
65
+ onMount(async () => {
66
+ if (!apiKey) {
67
+ console.warn('MapView: No Google Maps API key provided');
68
+ return;
69
+ }
70
+ try {
71
+ await loadGoogleMapsAPI({ apiKey, libraries: ['geometry', 'marker'] });
72
+ if (window.google?.maps?.marker) {
73
+ AdvancedMarkerElement = window.google.maps.marker.AdvancedMarkerElement;
74
+ }
75
+ if (isGoogleMapsLoaded()) initializeMap();
76
+ } catch (e) {
77
+ console.warn('MapView: Failed to load Google Maps API', e);
78
+ }
79
+ });
80
+
81
+ onDestroy(() => {
82
+ clearAllMarkers();
83
+ });
84
+
85
+ async function geocodeAddress(address: string): Promise<{ lat: number; lng: number } | null> {
86
+ if (!apiKey || !address) return null;
87
+ try {
88
+ const res = await fetch(
89
+ `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`
90
+ );
91
+ const data = await res.json();
92
+ if (data.status === 'OK' && data.results.length > 0) {
93
+ const loc = data.results[0].geometry.location;
94
+ return { lat: loc.lat, lng: loc.lng };
95
+ }
96
+ } catch {
97
+ // silently skip
98
+ }
99
+ return null;
100
+ }
101
+
102
+ function initializeMap() {
103
+ map = new google.maps.Map(mapContainer, {
104
+ zoom: 10,
105
+ center: defaultCenter,
106
+ mapTypeId: google.maps.MapTypeId.ROADMAP,
107
+ mapId: 'jatui-map-view',
108
+ styles: [{ featureType: 'poi', elementType: 'labels', stylers: [{ visibility: 'off' }] }]
109
+ });
110
+ updateMarkers();
111
+ fitMapToMarkers();
112
+ }
113
+
114
+ function updateMarkers() {
115
+ clearAllMarkers();
116
+ createJobMarkers();
117
+ createAgentMarkers();
118
+ }
119
+
120
+ function clearAllMarkers() {
121
+ jobMarkers.forEach((m) => (m.map = null));
122
+ agentMarkers.forEach((m) => {
123
+ m.map = null;
124
+ if (m._component) unmount(m._component);
125
+ });
126
+ jobMarkers = [];
127
+ agentMarkers = [];
128
+ }
129
+
130
+ function createJobMarkers() {
131
+ jobs.forEach((job) => {
132
+ if (!job.location?.latitude || !job.location?.longitude) {
133
+ // Try geocoding if we have an address but no coords
134
+ if (job.location?.address) {
135
+ geocodeAddress(job.location.address).then((coords) => {
136
+ if (coords) placeJobMarker(job, coords);
137
+ });
138
+ }
139
+ return;
140
+ }
141
+ placeJobMarker(job, { lat: job.location.latitude, lng: job.location.longitude });
142
+ });
143
+ }
144
+
145
+ function placeJobMarker(job: MapJob, position: { lat: number; lng: number }) {
146
+ if (!AdvancedMarkerElement) return;
147
+
148
+ const pin = document.createElement('div');
149
+ pin.style.cssText = `width:24px;height:24px;border-radius:50%;background:${STATUS_COLORS[job.status] ?? STATUS_COLORS.pending};border:2px solid #fff;box-shadow:0 2px 4px rgba(0,0,0,.3)`;
150
+
151
+ const marker = new AdvancedMarkerElement({
152
+ position,
153
+ map,
154
+ title: `${job.customer?.name ?? 'Unknown'} — ${job.type ?? ''}`,
155
+ content: pin
156
+ });
157
+
158
+ const infoWindow = new google.maps.InfoWindow({ content: buildJobInfo(job) });
159
+
160
+ marker.addListener('click', () => {
161
+ infoWindow.open(map, marker);
162
+ onJobClick?.(job);
163
+ });
164
+
165
+ jobMarkers.push(marker);
166
+ fitMapToMarkers();
167
+ }
168
+
169
+ function createAgentMarkers() {
170
+ agents.forEach((agent) => {
171
+ let position: { lat: number; lng: number } | null = null;
172
+
173
+ // Priority 1: fresh GPS (< 5 min)
174
+ if (agent.location?.latitude && agent.location?.longitude) {
175
+ const ageMins = (agent.location.age_seconds ?? 0) / 60;
176
+ if (ageMins < 5) position = { lat: agent.location.latitude, lng: agent.location.longitude };
177
+ }
178
+
179
+ // Priority 2: active dispatch job location
180
+ if (!position && agent.dispatches) {
181
+ const active = agent.dispatches.find(
182
+ (d) => d.status === 'in_progress' || d.status === 'en_route'
183
+ );
184
+ if (active) {
185
+ const assignedJob = jobs.find((j) => j.id === active.job_id);
186
+ if (assignedJob?.location?.latitude && assignedJob?.location?.longitude) {
187
+ position = { lat: assignedJob.location.latitude, lng: assignedJob.location.longitude };
188
+ }
189
+ }
190
+ }
191
+
192
+ if (!position) return;
193
+
194
+ const container = document.createElement('div');
195
+ container.style.cssText = 'display:flex;flex-direction:column;align-items:center;cursor:pointer';
196
+
197
+ const avatarEl = document.createElement('div');
198
+ container.appendChild(avatarEl);
199
+
200
+ const label = document.createElement('div');
201
+ label.style.cssText =
202
+ 'font-size:10px;font-weight:600;white-space:nowrap;margin-top:2px;color:#fff;text-shadow:0 1px 2px rgba(0,0,0,.8)';
203
+ label.textContent = agent.name.split(' ')[0];
204
+ container.appendChild(label);
205
+
206
+ // Mount UserAvatar into the DOM node
207
+ const avatarComponent = mount(UserAvatar, {
208
+ target: avatarEl,
209
+ props: {
210
+ name: agent.name,
211
+ email: agent.email,
212
+ avatarUrl: agent.avatarUrl,
213
+ size: 'sm',
214
+ tooltip: false
215
+ }
216
+ });
217
+
218
+ const marker = new AdvancedMarkerElement({
219
+ position,
220
+ map,
221
+ title: agent.name,
222
+ content: container
223
+ });
224
+
225
+ const infoWindow = new google.maps.InfoWindow({ content: buildAgentInfo(agent) });
226
+
227
+ marker.addListener('click', () => {
228
+ infoWindow.open(map, marker);
229
+ onAgentClick?.(agent);
230
+ });
231
+
232
+ marker._component = avatarComponent;
233
+ agentMarkers.push(marker);
234
+ });
235
+ }
236
+
237
+ function buildJobInfo(job: MapJob): string {
238
+ const statusBadge = `<span class="badge badge-sm" style="background:${STATUS_COLORS[job.status] ?? '#888'};color:#fff">${job.status.replace(/_/g, ' ')}</span>`;
239
+ const priority =
240
+ job.priority === 'urgent'
241
+ ? `<span class="badge badge-error badge-sm">urgent</span>`
242
+ : '';
243
+ return `<div style="padding:8px;min-width:180px;font-family:sans-serif;font-size:13px">
244
+ <div style="display:flex;align-items:center;gap:6px;margin-bottom:6px;font-weight:600">
245
+ ${job.customer?.name ?? 'Unknown Customer'} ${statusBadge}
246
+ </div>
247
+ ${job.type ? `<div><b>Service:</b> ${job.type.replace(/_/g, ' ')}</div>` : ''}
248
+ ${job.customer?.phone ? `<div><b>Phone:</b> ${job.customer.phone}</div>` : ''}
249
+ ${job.location?.address ? `<div><b>Address:</b> ${job.location.address}</div>` : ''}
250
+ ${job.scheduled_at ? `<div><b>Scheduled:</b> ${new Date(job.scheduled_at).toLocaleString()}</div>` : ''}
251
+ ${priority}
252
+ ${job.description ? `<div style="margin-top:4px"><b>Notes:</b> ${job.description}</div>` : ''}
253
+ </div>`;
254
+ }
255
+
256
+ function buildAgentInfo(agent: MapAgent): string {
257
+ const gpsAge = agent.location?.age_seconds ?? 0;
258
+ const gpsMins = Math.floor(gpsAge / 60);
259
+ const hasGps = !!(agent.location?.latitude && agent.location?.longitude);
260
+ return `<div style="padding:8px;min-width:180px;font-family:sans-serif;font-size:13px">
261
+ <div style="font-weight:600;margin-bottom:6px">${agent.name}</div>
262
+ ${agent.status ? `<div><b>Status:</b> ${agent.status}</div>` : ''}
263
+ ${agent.capacity ? `<div><b>Capacity:</b> ${agent.capacity}</div>` : ''}
264
+ ${hasGps ? `<div><b>GPS:</b> <span style="color:${gpsMins < 5 ? '#16a34a' : '#d97706'}">${gpsMins < 1 ? 'Live' : `${gpsMins}m ago`}</span></div>` : ''}
265
+ ${agent.specializations?.length ? `<div><b>Specializations:</b> ${agent.specializations.join(', ')}</div>` : ''}
266
+ ${agent.vehicle?.name ? `<div><b>Vehicle:</b> ${agent.vehicle.name}</div>` : ''}
267
+ </div>`;
268
+ }
269
+
270
+ function fitMapToMarkers() {
271
+ if (!map) return;
272
+ const all = [...jobMarkers, ...agentMarkers];
273
+ if (all.length === 0) return;
274
+
275
+ const bounds = new google.maps.LatLngBounds();
276
+ all.forEach((m) => m.position && bounds.extend(m.position));
277
+ map.fitBounds(bounds);
278
+
279
+ const listener = google.maps.event.addListener(map, 'idle', () => {
280
+ if ((map.getZoom() ?? 0) > 15) map.setZoom(15);
281
+ google.maps.event.removeListener(listener);
282
+ });
283
+ }
284
+
285
+ // Reactive: re-render markers when jobs/agents change
286
+ $effect(() => {
287
+ if (map) {
288
+ updateMarkers();
289
+ fitMapToMarkers();
290
+ }
291
+ });
292
+ </script>
293
+
294
+ <div class="h-full flex flex-col">
295
+ <div class="map-container flex-1 w-full rounded-lg border border-base-300 overflow-hidden relative">
296
+ <div bind:this={mapContainer} class="w-full h-full"></div>
297
+ </div>
298
+
299
+ {#if showLegend}
300
+ <div class="p-3 bg-base-100 border-t border-base-300 flex-shrink-0">
301
+ <div class="flex flex-wrap items-center gap-x-6 gap-y-2 text-sm">
302
+ <span class="font-semibold">Legend</span>
303
+
304
+ <div class="flex items-center gap-2">
305
+ <span class="font-medium text-xs">Job status</span>
306
+ <div class="tooltip" data-tip="Pending">
307
+ <div class="w-3.5 h-3.5 rounded-full" style="background:#f59e0b"></div>
308
+ </div>
309
+ <div class="tooltip" data-tip="Scheduled">
310
+ <div class="w-3.5 h-3.5 rounded-full" style="background:#3b82f6"></div>
311
+ </div>
312
+ <div class="tooltip" data-tip="In Progress">
313
+ <div class="w-3.5 h-3.5 rounded-full" style="background:#10b981"></div>
314
+ </div>
315
+ <div class="tooltip" data-tip="Completed">
316
+ <div class="w-3.5 h-3.5 rounded-full" style="background:#6b7280"></div>
317
+ </div>
318
+ <div class="tooltip" data-tip="Cancelled">
319
+ <div class="w-3.5 h-3.5 rounded-full" style="background:#ef4444"></div>
320
+ </div>
321
+ </div>
322
+
323
+ <div class="flex items-center gap-2">
324
+ <span class="font-medium text-xs">Agents</span>
325
+ <div class="tooltip" data-tip="Available">
326
+ <div class="w-7 h-7 rounded-full bg-neutral ring-2 ring-offset-1 ring-offset-base-100 ring-success flex items-center justify-center text-neutral-content text-[10px] font-bold">A</div>
327
+ </div>
328
+ <div class="tooltip" data-tip="En Route">
329
+ <div class="w-7 h-7 rounded-full bg-neutral ring-2 ring-offset-1 ring-offset-base-100 ring-warning flex items-center justify-center text-neutral-content text-[10px] font-bold">E</div>
330
+ </div>
331
+ <div class="tooltip" data-tip="On Job">
332
+ <div class="w-7 h-7 rounded-full bg-neutral ring-2 ring-offset-1 ring-offset-base-100 ring-primary flex items-center justify-center text-neutral-content text-[10px] font-bold">W</div>
333
+ </div>
334
+ <div class="tooltip" data-tip="Offline">
335
+ <div class="w-7 h-7 rounded-full bg-neutral ring-2 ring-offset-1 ring-offset-base-100 ring-base-300 flex items-center justify-center text-neutral-content text-[10px] font-bold">O</div>
336
+ </div>
337
+ </div>
338
+ </div>
339
+ </div>
340
+ {/if}
341
+ </div>