import { Icon } from "@getpaseo/plugin/client/react-native"; import { ExternalLink } from "@getpaseo/plugin/client/ui"; import React, { useEffect, useMemo, useState } from "react"; import { Image as RNImage, Pressable, StyleSheet, Text, View } from "react-native"; import { fitView, project, tileZoom, toScreen, visibleTiles, zoomAround } from "../../shared/logic/mapMath"; import { hotspotPoints, imageRef } from "../../shared/logic/references-model"; import { asRecord, finiteNumber, list, optionalText, text, type Props } from "../../shared/logic/values"; import { useGeocodePlace, useImageSearch, useMapTileLoader } from "../loaders"; import { ErrorLine } from "../node-renderer"; import { useUiRuntime, type ImageUrl } from "../runtime"; import { LoadedImageView } from "./content"; import { ImageLightbox } from "./image-lightbox"; export function ImageSearch({ props }: { props: Props }) { const { theme } = useUiRuntime(); const query = text(props.query); const alt = optionalText(props.alt) ?? query; const aspect = optionalText(props.aspect) ?? "wide"; const found = useImageSearch(query); const height = aspect === "tall" ? 260 : aspect === "square" ? 220 : 180; if (!query.trim()) return ; if (found === null) { return ( Searching photo for “{query}”… ); } if (!found.ok) { return ( {found.error} ); } return ( {found.image.title} {found.image.creator || found.image.license ? ( · {[found.image.creator, found.image.license].filter(Boolean).join(" · ")} ) : null} ); } function GalleryLightbox({ item, onClose, }: { item: { src?: string; query?: string; caption: string | null }; onClose(): void; }) { const { loadImage } = useUiRuntime(); const found = useImageSearch(item.query ?? ""); const [loaded, setLoaded] = useState(null); useEffect(() => { if (item.query || !item.src || !loadImage) return; let live = true; void loadImage(item.src).then((res) => { if (live) setLoaded(res); }); return () => { live = false; }; }, [item.query, item.src, loadImage]); const result = item.query ? (found === null ? null : found.ok ? { ok: true as const, url: found.image.url } : found) : loaded; const failed = result !== null && !result.ok; // The tile already shows why the image failed, so a failed lookup just closes. useEffect(() => { if (failed) onClose(); }, [failed, onClose]); if (result === null || !result.ok) return null; return ; } export function Gallery({ props }: { props: Props }) { const { theme, compact } = useUiRuntime(); const rawItems = list(props.images); const cols = compact ? 1 : Math.max(1, Math.min(4, finiteNumber(props.columns) ?? 2)); const [selected, setSelected] = useState<{ src?: string; query?: string; caption: string | null } | null>( null, ); const items = rawItems.map((item) => { if (typeof item === "string") return { src: item, query: undefined, caption: null }; const rec = asRecord(item); if (rec) { return { src: optionalText(rec.src) ?? undefined, query: optionalText(rec.query) ?? undefined, caption: optionalText(rec.caption), }; } return { src: undefined, query: undefined, caption: null }; }); const rows: (typeof items)[] = []; for (let i = 0; i < items.length; i += cols) rows.push(items.slice(i, i + cols)); return ( {rows.map((row, rIdx) => ( {row.map((item, cIdx) => ( setSelected(item)} style={{ flex: 1, borderRadius: 8, borderWidth: StyleSheet.hairlineWidth, borderColor: theme.colors.border, backgroundColor: theme.colors.surface2, padding: 8, gap: 4, }} > {item.query ? ( ) : item.src ? ( ) : null} ))} ))} {selected ? setSelected(null)} /> : null} ); } interface PlaceSpec { name: string; lat: number | null; lng: number | null; note: string | null; } export function MapView({ props }: { props: Props }) { const { theme } = useUiRuntime(); const geocode = useGeocodePlace(); const loadTile = useMapTileLoader(); const route = props.route === true; const places: PlaceSpec[] = useMemo( () => list(props.places).flatMap((item): PlaceSpec[] => { if (typeof item === "string") return [{ name: item, lat: null, lng: null, note: null }]; const rec = asRecord(item); if (!rec) return []; const name = text(rec.name ?? rec.title); if (!name) return []; return [ { name, lat: finiteNumber(rec.lat), lng: finiteNumber(rec.lng), note: optionalText(rec.note), }, ]; }), [props.places], ); const [resolved, setResolved] = useState< ReadonlyArray<{ name: string; lat: number; lng: number; note: string | null }> >([]); const [geoError, setGeoError] = useState(null); useEffect(() => { let live = true; async function run() { const out: { name: string; lat: number; lng: number; note: string | null }[] = []; let near: { lat: number; lng: number } | null = null; for (const place of places) { if (place.lat !== null && place.lng !== null) { near = { lat: place.lat, lng: place.lng }; out.push({ name: place.name, lat: place.lat, lng: place.lng, note: place.note }); } else { const found = await geocode(place.name, near); if (!live) return; if (found.ok) { near = { lat: found.lat, lng: found.lng }; out.push({ name: place.name, lat: found.lat, lng: found.lng, note: place.note }); } else { setGeoError(found.error); } } } if (live) setResolved(out); } void run(); return () => { live = false; }; }, [geocode, places]); const mapSize = { width: 520, height: 220 }; const worldPoints = useMemo(() => resolved.map((p) => project(p.lat, p.lng)), [resolved]); const fitted = useMemo(() => fitView(worldPoints, mapSize, 36, 15), [worldPoints]); const [zoomDelta, setZoomDelta] = useState(0); const view = useMemo( () => zoomAround(fitted, fitted.zoom + zoomDelta, { left: mapSize.width / 2, top: mapSize.height / 2 }, mapSize), [fitted, zoomDelta], ); const tiles = useMemo(() => visibleTiles(view, mapSize, tileZoom(view)), [view]); const [tileData, setTileData] = useState>(new Map()); useEffect(() => { let live = true; for (const tile of tiles) { const k = `${tile.z}/${tile.x}/${tile.y}`; if (tileData.has(k)) continue; void loadTile(tile.z, tile.x, tile.y).then((res) => { if (live && res.ok) { setTileData((prev) => new Map(prev).set(k, res.dataUrl)); } }); } return () => { live = false; }; }, [loadTile, tiles, tileData]); return ( {resolved.length > 0 ? ( {tiles.map((tile) => { const uri = tileData.get(`${tile.z}/${tile.x}/${tile.y}`); return uri ? ( ) : null; })} {resolved.map((place, idx) => { const pt = toScreen(project(place.lat, place.lng), view, mapSize); return ( {idx + 1} ); })} setZoomDelta((d) => d + 1)} style={{ width: 26, height: 26, borderRadius: 6, backgroundColor: theme.colors.surface0, alignItems: "center", justifyContent: "center", }} > + setZoomDelta((d) => d - 1)} style={{ width: 26, height: 26, borderRadius: 6, backgroundColor: theme.colors.surface0, alignItems: "center", justifyContent: "center", }} > − ) : null} {places.map((p, idx) => ( {idx + 1} {p.name} {route && idx < places.length - 1 ? " →" : ""} {p.note ? ( {p.note} ) : null} ))} {geoError ? ( {geoError} ) : null} ); } export function Hotspots({ props }: { props: Props }) { const { theme } = useUiRuntime(); const img = imageRef(text(props.image)); const { points, problems } = hotspotPoints(list(props.points)); const [active, setActive] = useState(points[0]?.number ?? 1); return ( {img.kind === "load" ? : null} {points.map((pt) => { const isSelected = active === pt.number; return ( setActive(pt.number)} style={{ flexDirection: "row", alignItems: "flex-start", gap: 8, padding: 8, borderRadius: 8, borderWidth: isSelected ? 1.5 : StyleSheet.hairlineWidth, borderColor: isSelected ? theme.colors.foregroundMuted : theme.colors.border, backgroundColor: theme.colors.surface2, }} > {pt.number} {pt.title} {pt.text ? ( {pt.text} ) : null} ); })} {problems.map((p, i) => ( ))} ); }